cards.js 13 KB

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