cards.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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() && 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 mutatedFields = {listId};
  187. if (sortIndex) {
  188. mutatedFields.sort = sortIndex;
  189. }
  190. return {$set: mutatedFields};
  191. },
  192. addLabel(labelId) {
  193. return {$addToSet: {labelIds: labelId}};
  194. },
  195. removeLabel(labelId) {
  196. return {$pull: {labelIds: labelId}};
  197. },
  198. toggleLabel(labelId) {
  199. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  200. return this.removeLabel(labelId);
  201. } else {
  202. return this.addLabel(labelId);
  203. }
  204. },
  205. assignMember(memberId) {
  206. return {$addToSet: {members: memberId}};
  207. },
  208. unassignMember(memberId) {
  209. return {$pull: {members: memberId}};
  210. },
  211. toggleMember(memberId) {
  212. if (this.members && this.members.indexOf(memberId) > -1) {
  213. return this.unassignMember(memberId);
  214. } else {
  215. return this.assignMember(memberId);
  216. }
  217. },
  218. setCover(coverId) {
  219. return {$set: {coverId}};
  220. },
  221. unsetCover() {
  222. return {$unset: {coverId: ''}};
  223. },
  224. setStart(startAt) {
  225. return {$set: {startAt}};
  226. },
  227. unsetStart() {
  228. return {$unset: {startAt: ''}};
  229. },
  230. setDue(dueAt) {
  231. return {$set: {dueAt}};
  232. },
  233. unsetDue() {
  234. return {$unset: {dueAt: ''}};
  235. },
  236. });
  237. //FUNCTIONS FOR creation of Activities
  238. function cardMove(userId, doc, fieldNames, oldListId) {
  239. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  240. Activities.insert({
  241. userId,
  242. oldListId,
  243. activityType: 'moveCard',
  244. listId: doc.listId,
  245. boardId: doc.boardId,
  246. cardId: doc._id,
  247. });
  248. }
  249. }
  250. function cardState(userId, doc, fieldNames) {
  251. if (_.contains(fieldNames, 'archived')) {
  252. if (doc.archived) {
  253. Activities.insert({
  254. userId,
  255. activityType: 'archivedCard',
  256. boardId: doc.boardId,
  257. listId: doc.listId,
  258. cardId: doc._id,
  259. });
  260. } else {
  261. Activities.insert({
  262. userId,
  263. activityType: 'restoredCard',
  264. boardId: doc.boardId,
  265. listId: doc.listId,
  266. cardId: doc._id,
  267. });
  268. }
  269. }
  270. }
  271. function cardMembers(userId, doc, fieldNames, modifier) {
  272. if (!_.contains(fieldNames, 'members'))
  273. return;
  274. let memberId;
  275. // Say hello to the new member
  276. if (modifier.$addToSet && modifier.$addToSet.members) {
  277. memberId = modifier.$addToSet.members;
  278. if (!_.contains(doc.members, memberId)) {
  279. Activities.insert({
  280. userId,
  281. memberId,
  282. activityType: 'joinMember',
  283. boardId: doc.boardId,
  284. cardId: doc._id,
  285. });
  286. }
  287. }
  288. // Say goodbye to the former member
  289. if (modifier.$pull && modifier.$pull.members) {
  290. memberId = modifier.$pull.members;
  291. // Check that the former member is member of the card
  292. if (_.contains(doc.members, memberId)) {
  293. Activities.insert({
  294. userId,
  295. memberId,
  296. activityType: 'unjoinMember',
  297. boardId: doc.boardId,
  298. cardId: doc._id,
  299. });
  300. }
  301. }
  302. }
  303. function cardCreation(userId, doc) {
  304. Activities.insert({
  305. userId,
  306. activityType: 'createCard',
  307. boardId: doc.boardId,
  308. listId: doc.listId,
  309. cardId: doc._id,
  310. });
  311. }
  312. function cardRemover(userId, doc) {
  313. Activities.remove({
  314. cardId: doc._id,
  315. });
  316. Checklists.remove({
  317. cardId: doc._id,
  318. });
  319. CardComments.remove({
  320. cardId: doc._id,
  321. });
  322. Attachments.remove({
  323. cardId: doc._id,
  324. });
  325. }
  326. if (Meteor.isServer) {
  327. // Cards are often fetched within a board, so we create an index to make these
  328. // queries more efficient.
  329. Meteor.startup(() => {
  330. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  331. });
  332. Cards.after.insert((userId, doc) => {
  333. cardCreation(userId, doc);
  334. });
  335. // New activity for card (un)archivage
  336. Cards.after.update((userId, doc, fieldNames) => {
  337. cardState(userId, doc, fieldNames);
  338. });
  339. //New activity for card moves
  340. Cards.after.update(function (userId, doc, fieldNames) {
  341. const oldListId = this.previous.listId;
  342. cardMove(userId, doc, fieldNames, oldListId);
  343. });
  344. // Add a new activity if we add or remove a member to the card
  345. Cards.before.update((userId, doc, fieldNames, modifier) => {
  346. cardMembers(userId, doc, fieldNames, modifier);
  347. });
  348. // Remove all activities associated with a card if we remove the card
  349. // Remove also card_comments / checklists / attachments
  350. Cards.after.remove((userId, doc) => {
  351. cardRemover(userId, doc);
  352. });
  353. }
  354. //LISTS REST API
  355. if (Meteor.isServer) {
  356. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  357. const paramBoardId = req.params.boardId;
  358. const paramListId = req.params.listId;
  359. Authentication.checkBoardAccess(req.userId, paramBoardId);
  360. JsonRoutes.sendResult(res, {
  361. code: 200,
  362. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  363. return {
  364. _id: doc._id,
  365. title: doc.title,
  366. description: doc.description,
  367. };
  368. }),
  369. });
  370. });
  371. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  372. const paramBoardId = req.params.boardId;
  373. const paramListId = req.params.listId;
  374. const paramCardId = req.params.cardId;
  375. Authentication.checkBoardAccess(req.userId, paramBoardId);
  376. JsonRoutes.sendResult(res, {
  377. code: 200,
  378. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  379. });
  380. });
  381. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res, next) {
  382. Authentication.checkUserId(req.userId);
  383. const paramBoardId = req.params.boardId;
  384. const paramListId = req.params.listId;
  385. const check = Users.findOne({_id: req.body.authorId});
  386. if (typeof check !== 'undefined') {
  387. const id = Cards.direct.insert({
  388. title: req.body.title,
  389. boardId: paramBoardId,
  390. listId: paramListId,
  391. description: req.body.description,
  392. userId: req.body.authorId,
  393. sort: 0,
  394. members: [req.body.authorId],
  395. });
  396. JsonRoutes.sendResult(res, {
  397. code: 200,
  398. data: {
  399. _id: id,
  400. },
  401. });
  402. const card = Cards.findOne({_id:id});
  403. cardCreation(req.body.authorId, card);
  404. } else {
  405. JsonRoutes.sendResult(res, {
  406. code: 401,
  407. });
  408. }
  409. });
  410. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  411. Authentication.checkUserId(req.userId);
  412. const paramBoardId = req.params.boardId;
  413. const paramCardId = req.params.cardId;
  414. const paramListId = req.params.listId;
  415. if (req.body.hasOwnProperty('title')) {
  416. const newTitle = req.body.title;
  417. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  418. {$set: {title: newTitle}});
  419. }
  420. if (req.body.hasOwnProperty('listId')) {
  421. const newParamListId = req.body.listId;
  422. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  423. {$set: {listId: newParamListId}});
  424. const card = Cards.findOne({_id: paramCardId} );
  425. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  426. }
  427. if (req.body.hasOwnProperty('description')) {
  428. const newDescription = req.body.description;
  429. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  430. {$set: {description: newDescription}});
  431. }
  432. JsonRoutes.sendResult(res, {
  433. code: 200,
  434. data: {
  435. _id: paramCardId,
  436. },
  437. });
  438. });
  439. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res, next) {
  440. Authentication.checkUserId(req.userId);
  441. const paramBoardId = req.params.boardId;
  442. const paramListId = req.params.listId;
  443. const paramCardId = req.params.cardId;
  444. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  445. const card = Cards.find({_id: paramCardId} );
  446. cardRemover(req.body.authorId, card);
  447. JsonRoutes.sendResult(res, {
  448. code: 200,
  449. data: {
  450. _id: paramCardId,
  451. },
  452. });
  453. });
  454. }