cards.js 13 KB

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