cards.js 15 KB

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