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