cards.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. Cards = new Mongo.Collection('cards');
  2. // XXX To improve pub/sub performances a card document should include a
  3. // de-normalized number of comments so we don't have to publish the whole list
  4. // of comments just to display the number of them in the board view.
  5. Cards.attachSchema(new SimpleSchema({
  6. title: {
  7. type: String,
  8. },
  9. archived: {
  10. type: Boolean,
  11. autoValue() { // eslint-disable-line consistent-return
  12. if (this.isInsert && !this.isSet) {
  13. return false;
  14. }
  15. },
  16. },
  17. listId: {
  18. type: String,
  19. },
  20. // The system could work without this `boardId` information (we could deduce
  21. // the board identifier from the card), but it would make the system more
  22. // difficult to manage and less efficient.
  23. boardId: {
  24. type: String,
  25. },
  26. coverId: {
  27. type: String,
  28. optional: true,
  29. },
  30. createdAt: {
  31. type: Date,
  32. autoValue() { // eslint-disable-line consistent-return
  33. if (this.isInsert) {
  34. return new Date();
  35. } else {
  36. this.unset();
  37. }
  38. },
  39. },
  40. dateLastActivity: {
  41. type: Date,
  42. autoValue() {
  43. return new Date();
  44. },
  45. },
  46. description: {
  47. type: String,
  48. optional: true,
  49. },
  50. labelIds: {
  51. type: [String],
  52. optional: true,
  53. },
  54. members: {
  55. type: [String],
  56. optional: true,
  57. },
  58. startAt: {
  59. type: Date,
  60. optional: true,
  61. },
  62. dueAt: {
  63. type: Date,
  64. optional: true,
  65. },
  66. spentTime: {
  67. type: Number,
  68. decimal: true,
  69. optional: true,
  70. },
  71. isOvertime: {
  72. type: Boolean,
  73. defaultValue: false,
  74. optional: true,
  75. },
  76. // XXX Should probably be called `authorId`. Is it even needed since we have
  77. // the `members` field?
  78. userId: {
  79. type: String,
  80. autoValue() { // eslint-disable-line consistent-return
  81. if (this.isInsert && !this.isSet) {
  82. return this.userId;
  83. }
  84. },
  85. },
  86. sort: {
  87. type: Number,
  88. decimal: true,
  89. },
  90. }));
  91. Cards.allow({
  92. insert(userId, doc) {
  93. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  94. },
  95. update(userId, doc) {
  96. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  97. },
  98. remove(userId, doc) {
  99. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  100. },
  101. fetch: ['boardId'],
  102. });
  103. Cards.helpers({
  104. list() {
  105. return Lists.findOne(this.listId);
  106. },
  107. board() {
  108. return Boards.findOne(this.boardId);
  109. },
  110. labels() {
  111. const boardLabels = this.board().labels;
  112. const cardLabels = _.filter(boardLabels, (label) => {
  113. return _.contains(this.labelIds, label._id);
  114. });
  115. return cardLabels;
  116. },
  117. hasLabel(labelId) {
  118. return _.contains(this.labelIds, labelId);
  119. },
  120. user() {
  121. return Users.findOne(this.userId);
  122. },
  123. isAssigned(memberId) {
  124. return _.contains(this.members, memberId);
  125. },
  126. activities() {
  127. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  128. },
  129. comments() {
  130. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  131. },
  132. attachments() {
  133. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  134. },
  135. cover() {
  136. const cover = Attachments.findOne(this.coverId);
  137. // if we return a cover before it is fully stored, we will get errors when we try to display it
  138. // todo XXX we could return a default "upload pending" image in the meantime?
  139. return cover && cover.url() && cover;
  140. },
  141. checklists() {
  142. return Checklists.find({cardId: this._id}, {sort: {createdAt: 1}});
  143. },
  144. checklistItemCount() {
  145. const checklists = this.checklists().fetch();
  146. return checklists.map((checklist) => {
  147. return checklist.itemCount();
  148. }).reduce((prev, next) => {
  149. return prev + next;
  150. }, 0);
  151. },
  152. checklistFinishedCount() {
  153. const checklists = this.checklists().fetch();
  154. return checklists.map((checklist) => {
  155. return checklist.finishedCount();
  156. }).reduce((prev, next) => {
  157. return prev + next;
  158. }, 0);
  159. },
  160. checklistFinished() {
  161. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  162. },
  163. hasChecklist() {
  164. return this.checklistItemCount() !== 0;
  165. },
  166. absoluteUrl() {
  167. const board = this.board();
  168. return FlowRouter.url('card', {
  169. boardId: board._id,
  170. slug: board.slug,
  171. cardId: this._id,
  172. });
  173. },
  174. canBeRestored() {
  175. const list = Lists.findOne({_id: this.listId});
  176. if(!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()){
  177. return false;
  178. }
  179. return true;
  180. },
  181. });
  182. Cards.mutations({
  183. archive() {
  184. return {$set: {archived: true}};
  185. },
  186. restore() {
  187. return {$set: {archived: false}};
  188. },
  189. setTitle(title) {
  190. return {$set: {title}};
  191. },
  192. setDescription(description) {
  193. return {$set: {description}};
  194. },
  195. move(listId, sortIndex) {
  196. const mutatedFields = {listId};
  197. if (sortIndex) {
  198. mutatedFields.sort = sortIndex;
  199. }
  200. return {$set: mutatedFields};
  201. },
  202. addLabel(labelId) {
  203. return {$addToSet: {labelIds: labelId}};
  204. },
  205. removeLabel(labelId) {
  206. return {$pull: {labelIds: labelId}};
  207. },
  208. toggleLabel(labelId) {
  209. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  210. return this.removeLabel(labelId);
  211. } else {
  212. return this.addLabel(labelId);
  213. }
  214. },
  215. assignMember(memberId) {
  216. return {$addToSet: {members: memberId}};
  217. },
  218. unassignMember(memberId) {
  219. return {$pull: {members: memberId}};
  220. },
  221. toggleMember(memberId) {
  222. if (this.members && this.members.indexOf(memberId) > -1) {
  223. return this.unassignMember(memberId);
  224. } else {
  225. return this.assignMember(memberId);
  226. }
  227. },
  228. setCover(coverId) {
  229. return {$set: {coverId}};
  230. },
  231. unsetCover() {
  232. return {$unset: {coverId: ''}};
  233. },
  234. setStart(startAt) {
  235. return {$set: {startAt}};
  236. },
  237. unsetStart() {
  238. return {$unset: {startAt: ''}};
  239. },
  240. setDue(dueAt) {
  241. return {$set: {dueAt}};
  242. },
  243. unsetDue() {
  244. return {$unset: {dueAt: ''}};
  245. },
  246. setOvertime(isOvertime) {
  247. return {$set: {isOvertime}};
  248. },
  249. setSpentTime(spentTime) {
  250. return {$set: {spentTime}};
  251. },
  252. unsetSpentTime() {
  253. return {$unset: {spentTime: '', isOvertime: false}};
  254. },
  255. });
  256. //FUNCTIONS FOR creation of Activities
  257. function cardMove(userId, doc, fieldNames, oldListId) {
  258. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  259. Activities.insert({
  260. userId,
  261. oldListId,
  262. activityType: 'moveCard',
  263. listId: doc.listId,
  264. boardId: doc.boardId,
  265. cardId: doc._id,
  266. });
  267. }
  268. }
  269. function cardState(userId, doc, fieldNames) {
  270. if (_.contains(fieldNames, 'archived')) {
  271. if (doc.archived) {
  272. Activities.insert({
  273. userId,
  274. activityType: 'archivedCard',
  275. boardId: doc.boardId,
  276. listId: doc.listId,
  277. cardId: doc._id,
  278. });
  279. } else {
  280. Activities.insert({
  281. userId,
  282. activityType: 'restoredCard',
  283. boardId: doc.boardId,
  284. listId: doc.listId,
  285. cardId: doc._id,
  286. });
  287. }
  288. }
  289. }
  290. function cardMembers(userId, doc, fieldNames, modifier) {
  291. if (!_.contains(fieldNames, 'members'))
  292. return;
  293. let memberId;
  294. // Say hello to the new member
  295. if (modifier.$addToSet && modifier.$addToSet.members) {
  296. memberId = modifier.$addToSet.members;
  297. if (!_.contains(doc.members, memberId)) {
  298. Activities.insert({
  299. userId,
  300. memberId,
  301. activityType: 'joinMember',
  302. boardId: doc.boardId,
  303. cardId: doc._id,
  304. });
  305. }
  306. }
  307. // Say goodbye to the former member
  308. if (modifier.$pull && modifier.$pull.members) {
  309. memberId = modifier.$pull.members;
  310. // Check that the former member is member of the card
  311. if (_.contains(doc.members, memberId)) {
  312. Activities.insert({
  313. userId,
  314. memberId,
  315. activityType: 'unjoinMember',
  316. boardId: doc.boardId,
  317. cardId: doc._id,
  318. });
  319. }
  320. }
  321. }
  322. function cardCreation(userId, doc) {
  323. Activities.insert({
  324. userId,
  325. activityType: 'createCard',
  326. boardId: doc.boardId,
  327. listId: doc.listId,
  328. cardId: doc._id,
  329. });
  330. }
  331. function cardRemover(userId, doc) {
  332. Activities.remove({
  333. cardId: doc._id,
  334. });
  335. Checklists.remove({
  336. cardId: doc._id,
  337. });
  338. CardComments.remove({
  339. cardId: doc._id,
  340. });
  341. Attachments.remove({
  342. cardId: doc._id,
  343. });
  344. }
  345. if (Meteor.isServer) {
  346. // Cards are often fetched within a board, so we create an index to make these
  347. // queries more efficient.
  348. Meteor.startup(() => {
  349. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  350. });
  351. Cards.after.insert((userId, doc) => {
  352. cardCreation(userId, doc);
  353. });
  354. // New activity for card (un)archivage
  355. Cards.after.update((userId, doc, fieldNames) => {
  356. cardState(userId, doc, fieldNames);
  357. });
  358. //New activity for card moves
  359. Cards.after.update(function (userId, doc, fieldNames) {
  360. const oldListId = this.previous.listId;
  361. cardMove(userId, doc, fieldNames, oldListId);
  362. });
  363. // Add a new activity if we add or remove a member to the card
  364. Cards.before.update((userId, doc, fieldNames, modifier) => {
  365. cardMembers(userId, doc, fieldNames, modifier);
  366. });
  367. // Remove all activities associated with a card if we remove the card
  368. // Remove also card_comments / checklists / attachments
  369. Cards.after.remove((userId, doc) => {
  370. cardRemover(userId, doc);
  371. });
  372. }
  373. //LISTS REST API
  374. if (Meteor.isServer) {
  375. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  376. const paramBoardId = req.params.boardId;
  377. const paramListId = req.params.listId;
  378. Authentication.checkBoardAccess(req.userId, paramBoardId);
  379. JsonRoutes.sendResult(res, {
  380. code: 200,
  381. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  382. return {
  383. _id: doc._id,
  384. title: doc.title,
  385. description: doc.description,
  386. };
  387. }),
  388. });
  389. });
  390. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  391. const paramBoardId = req.params.boardId;
  392. const paramListId = req.params.listId;
  393. const paramCardId = req.params.cardId;
  394. Authentication.checkBoardAccess(req.userId, paramBoardId);
  395. JsonRoutes.sendResult(res, {
  396. code: 200,
  397. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  398. });
  399. });
  400. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  401. Authentication.checkUserId(req.userId);
  402. const paramBoardId = req.params.boardId;
  403. const paramListId = req.params.listId;
  404. const check = Users.findOne({_id: req.body.authorId});
  405. if (typeof check !== 'undefined') {
  406. const id = Cards.direct.insert({
  407. title: req.body.title,
  408. boardId: paramBoardId,
  409. listId: paramListId,
  410. description: req.body.description,
  411. userId: req.body.authorId,
  412. sort: 0,
  413. members: [req.body.authorId],
  414. });
  415. JsonRoutes.sendResult(res, {
  416. code: 200,
  417. data: {
  418. _id: id,
  419. },
  420. });
  421. const card = Cards.findOne({_id:id});
  422. cardCreation(req.body.authorId, card);
  423. } else {
  424. JsonRoutes.sendResult(res, {
  425. code: 401,
  426. });
  427. }
  428. });
  429. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  430. Authentication.checkUserId(req.userId);
  431. const paramBoardId = req.params.boardId;
  432. const paramCardId = req.params.cardId;
  433. const paramListId = req.params.listId;
  434. if (req.body.hasOwnProperty('title')) {
  435. const newTitle = req.body.title;
  436. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  437. {$set: {title: newTitle}});
  438. }
  439. if (req.body.hasOwnProperty('listId')) {
  440. const newParamListId = req.body.listId;
  441. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  442. {$set: {listId: newParamListId}});
  443. const card = Cards.findOne({_id: paramCardId} );
  444. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  445. }
  446. if (req.body.hasOwnProperty('description')) {
  447. const newDescription = req.body.description;
  448. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  449. {$set: {description: newDescription}});
  450. }
  451. JsonRoutes.sendResult(res, {
  452. code: 200,
  453. data: {
  454. _id: paramCardId,
  455. },
  456. });
  457. });
  458. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  459. Authentication.checkUserId(req.userId);
  460. const paramBoardId = req.params.boardId;
  461. const paramListId = req.params.listId;
  462. const paramCardId = req.params.cardId;
  463. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  464. const card = Cards.find({_id: paramCardId} );
  465. cardRemover(req.body.authorId, card);
  466. JsonRoutes.sendResult(res, {
  467. code: 200,
  468. data: {
  469. _id: paramCardId,
  470. },
  471. });
  472. });
  473. }