cards.js 13 KB

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