cards.js 16 KB

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