cards.js 13 KB

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