cards.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. parentId: {
  18. type: String,
  19. optional: true,
  20. defaultValue: '',
  21. },
  22. listId: {
  23. type: String,
  24. },
  25. swimlaneId: {
  26. type: String,
  27. },
  28. // The system could work without this `boardId` information (we could deduce
  29. // the board identifier from the card), but it would make the system more
  30. // difficult to manage and less efficient.
  31. boardId: {
  32. type: String,
  33. },
  34. coverId: {
  35. type: String,
  36. optional: true,
  37. },
  38. createdAt: {
  39. type: Date,
  40. autoValue() { // eslint-disable-line consistent-return
  41. if (this.isInsert) {
  42. return new Date();
  43. } else {
  44. this.unset();
  45. }
  46. },
  47. },
  48. customFields: {
  49. type: [Object],
  50. optional: true,
  51. },
  52. 'customFields.$': {
  53. type: new SimpleSchema({
  54. _id: {
  55. type: String,
  56. },
  57. value: {
  58. type: Match.OneOf(String, Number, Boolean, Date),
  59. optional: true,
  60. },
  61. }),
  62. },
  63. dateLastActivity: {
  64. type: Date,
  65. autoValue() {
  66. return new Date();
  67. },
  68. },
  69. description: {
  70. type: String,
  71. optional: true,
  72. },
  73. requestedBy: {
  74. type: String,
  75. optional: true,
  76. },
  77. assignedBy: {
  78. type: String,
  79. optional: true,
  80. },
  81. labelIds: {
  82. type: [String],
  83. optional: true,
  84. },
  85. members: {
  86. type: [String],
  87. optional: true,
  88. },
  89. receivedAt: {
  90. type: Date,
  91. optional: true,
  92. },
  93. startAt: {
  94. type: Date,
  95. optional: true,
  96. },
  97. dueAt: {
  98. type: Date,
  99. optional: true,
  100. },
  101. endAt: {
  102. type: Date,
  103. optional: true,
  104. },
  105. spentTime: {
  106. type: Number,
  107. decimal: true,
  108. optional: true,
  109. },
  110. isOvertime: {
  111. type: Boolean,
  112. defaultValue: false,
  113. optional: true,
  114. },
  115. // XXX Should probably be called `authorId`. Is it even needed since we have
  116. // the `members` field?
  117. userId: {
  118. type: String,
  119. autoValue() { // eslint-disable-line consistent-return
  120. if (this.isInsert && !this.isSet) {
  121. return this.userId;
  122. }
  123. },
  124. },
  125. sort: {
  126. type: Number,
  127. decimal: true,
  128. },
  129. subtaskSort: {
  130. type: Number,
  131. decimal: true,
  132. defaultValue: -1,
  133. optional: true,
  134. },
  135. }));
  136. Cards.allow({
  137. insert(userId, doc) {
  138. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  139. },
  140. update(userId, doc) {
  141. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  142. },
  143. remove(userId, doc) {
  144. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  145. },
  146. fetch: ['boardId'],
  147. });
  148. Cards.helpers({
  149. list() {
  150. return Lists.findOne(this.listId);
  151. },
  152. board() {
  153. return Boards.findOne(this.boardId);
  154. },
  155. labels() {
  156. const boardLabels = this.board().labels;
  157. const cardLabels = _.filter(boardLabels, (label) => {
  158. return _.contains(this.labelIds, label._id);
  159. });
  160. return cardLabels;
  161. },
  162. hasLabel(labelId) {
  163. return _.contains(this.labelIds, labelId);
  164. },
  165. user() {
  166. return Users.findOne(this.userId);
  167. },
  168. isAssigned(memberId) {
  169. return _.contains(this.members, memberId);
  170. },
  171. activities() {
  172. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  173. },
  174. comments() {
  175. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  176. },
  177. attachments() {
  178. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  179. },
  180. cover() {
  181. const cover = Attachments.findOne(this.coverId);
  182. // if we return a cover before it is fully stored, we will get errors when we try to display it
  183. // todo XXX we could return a default "upload pending" image in the meantime?
  184. return cover && cover.url() && cover;
  185. },
  186. checklists() {
  187. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  188. },
  189. checklistItemCount() {
  190. const checklists = this.checklists().fetch();
  191. return checklists.map((checklist) => {
  192. return checklist.itemCount();
  193. }).reduce((prev, next) => {
  194. return prev + next;
  195. }, 0);
  196. },
  197. checklistFinishedCount() {
  198. const checklists = this.checklists().fetch();
  199. return checklists.map((checklist) => {
  200. return checklist.finishedCount();
  201. }).reduce((prev, next) => {
  202. return prev + next;
  203. }, 0);
  204. },
  205. checklistFinished() {
  206. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  207. },
  208. hasChecklist() {
  209. return this.checklistItemCount() !== 0;
  210. },
  211. subtasks() {
  212. return Cards.find({
  213. parentId: this._id,
  214. archived: false,
  215. }, {sort: { sort: 1 } });
  216. },
  217. allSubtasks() {
  218. return Cards.find({
  219. parentId: this._id,
  220. archived: false,
  221. }, {sort: { sort: 1 } });
  222. },
  223. subtasksCount() {
  224. return Cards.find({
  225. parentId: this._id,
  226. archived: false,
  227. }).count();
  228. },
  229. subtasksFinishedCount() {
  230. return Cards.find({
  231. parentId: this._id,
  232. archived: true}).count();
  233. },
  234. subtasksFinished() {
  235. const finishCount = this.subtasksFinishedCount();
  236. return finishCount > 0 && this.subtasksCount() === finishCount;
  237. },
  238. allowsSubtasks() {
  239. return this.subtasksCount() !== 0;
  240. },
  241. customFieldIndex(customFieldId) {
  242. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  243. },
  244. // customFields with definitions
  245. customFieldsWD() {
  246. // get all definitions
  247. const definitions = CustomFields.find({
  248. boardId: this.boardId,
  249. }).fetch();
  250. // match right definition to each field
  251. if (!this.customFields) return [];
  252. return this.customFields.map((customField) => {
  253. const definition = definitions.find((definition) => {
  254. return definition._id === customField._id;
  255. });
  256. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  257. let trueValue = customField.value;
  258. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0)
  259. {
  260. for (let i = 0; i < definition.settings.dropdownItems.length; i++)
  261. {
  262. if (definition.settings.dropdownItems[i]._id === customField.value)
  263. {
  264. trueValue = definition.settings.dropdownItems[i].name;
  265. }
  266. }
  267. }
  268. return {
  269. _id: customField._id,
  270. value: customField.value,
  271. trueValue,
  272. definition,
  273. };
  274. });
  275. },
  276. absoluteUrl() {
  277. const board = this.board();
  278. return FlowRouter.url('card', {
  279. boardId: board._id,
  280. slug: board.slug,
  281. cardId: this._id,
  282. });
  283. },
  284. canBeRestored() {
  285. const list = Lists.findOne({_id: this.listId});
  286. if(!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()){
  287. return false;
  288. }
  289. return true;
  290. },
  291. parentCard() {
  292. if (this.parentId === '') {
  293. return null;
  294. }
  295. return Cards.findOne(this.parentId);
  296. },
  297. parentCardName() {
  298. let result = '';
  299. if (this.parentId !== '') {
  300. const card = Cards.findOne(this.parentId);
  301. if (card) {
  302. result = card.title;
  303. }
  304. }
  305. return result;
  306. },
  307. parentListId() {
  308. const result = [];
  309. let crtParentId = this.parentId;
  310. while (crtParentId !== '') {
  311. const crt = Cards.findOne(crtParentId);
  312. if ((crt === null) || (crt === undefined)) {
  313. // maybe it has been deleted
  314. break;
  315. }
  316. if (crtParentId in result) {
  317. // circular reference
  318. break;
  319. }
  320. result.unshift(crtParentId);
  321. crtParentId = crt.parentId;
  322. }
  323. return result;
  324. },
  325. parentList() {
  326. const resultId = [];
  327. const result = [];
  328. let crtParentId = this.parentId;
  329. while (crtParentId !== '') {
  330. const crt = Cards.findOne(crtParentId);
  331. if ((crt === null) || (crt === undefined)) {
  332. // maybe it has been deleted
  333. break;
  334. }
  335. if (crtParentId in resultId) {
  336. // circular reference
  337. break;
  338. }
  339. resultId.unshift(crtParentId);
  340. result.unshift(crt);
  341. crtParentId = crt.parentId;
  342. }
  343. return result;
  344. },
  345. parentString(sep) {
  346. return this.parentList().map(function(elem){
  347. return elem.title;
  348. }).join(sep);
  349. },
  350. isTopLevel() {
  351. return this.parentId === '';
  352. },
  353. });
  354. Cards.mutations({
  355. applyToChildren(funct) {
  356. Cards.find({ parentId: this._id }).forEach((card) => {
  357. funct(card);
  358. });
  359. },
  360. archive() {
  361. this.applyToChildren((card) => { return card.archive(); });
  362. return {$set: {archived: true}};
  363. },
  364. restore() {
  365. this.applyToChildren((card) => { return card.restore(); });
  366. return {$set: {archived: false}};
  367. },
  368. setTitle(title) {
  369. return {$set: {title}};
  370. },
  371. setDescription(description) {
  372. return {$set: {description}};
  373. },
  374. setRequestedBy(requestedBy) {
  375. return {$set: {requestedBy}};
  376. },
  377. setAssignedBy(assignedBy) {
  378. return {$set: {assignedBy}};
  379. },
  380. move(swimlaneId, listId, sortIndex) {
  381. const list = Lists.findOne(listId);
  382. const mutatedFields = {
  383. swimlaneId,
  384. listId,
  385. boardId: list.boardId,
  386. sort: sortIndex,
  387. };
  388. return {$set: mutatedFields};
  389. },
  390. addLabel(labelId) {
  391. return {$addToSet: {labelIds: labelId}};
  392. },
  393. removeLabel(labelId) {
  394. return {$pull: {labelIds: labelId}};
  395. },
  396. toggleLabel(labelId) {
  397. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  398. return this.removeLabel(labelId);
  399. } else {
  400. return this.addLabel(labelId);
  401. }
  402. },
  403. assignMember(memberId) {
  404. return {$addToSet: {members: memberId}};
  405. },
  406. unassignMember(memberId) {
  407. return {$pull: {members: memberId}};
  408. },
  409. toggleMember(memberId) {
  410. if (this.members && this.members.indexOf(memberId) > -1) {
  411. return this.unassignMember(memberId);
  412. } else {
  413. return this.assignMember(memberId);
  414. }
  415. },
  416. assignCustomField(customFieldId) {
  417. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  418. },
  419. unassignCustomField(customFieldId) {
  420. return {$pull: {customFields: {_id: customFieldId}}};
  421. },
  422. toggleCustomField(customFieldId) {
  423. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  424. return this.unassignCustomField(customFieldId);
  425. } else {
  426. return this.assignCustomField(customFieldId);
  427. }
  428. },
  429. setCustomField(customFieldId, value) {
  430. // todo
  431. const index = this.customFieldIndex(customFieldId);
  432. if (index > -1) {
  433. const update = {$set: {}};
  434. update.$set[`customFields.${index}.value`] = value;
  435. return update;
  436. }
  437. // TODO
  438. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  439. return null;
  440. },
  441. setCover(coverId) {
  442. return {$set: {coverId}};
  443. },
  444. unsetCover() {
  445. return {$unset: {coverId: ''}};
  446. },
  447. setReceived(receivedAt) {
  448. return {$set: {receivedAt}};
  449. },
  450. unsetReceived() {
  451. return {$unset: {receivedAt: ''}};
  452. },
  453. setStart(startAt) {
  454. return {$set: {startAt}};
  455. },
  456. unsetStart() {
  457. return {$unset: {startAt: ''}};
  458. },
  459. setDue(dueAt) {
  460. return {$set: {dueAt}};
  461. },
  462. unsetDue() {
  463. return {$unset: {dueAt: ''}};
  464. },
  465. setEnd(endAt) {
  466. return {$set: {endAt}};
  467. },
  468. unsetEnd() {
  469. return {$unset: {endAt: ''}};
  470. },
  471. setOvertime(isOvertime) {
  472. return {$set: {isOvertime}};
  473. },
  474. setSpentTime(spentTime) {
  475. return {$set: {spentTime}};
  476. },
  477. unsetSpentTime() {
  478. return {$unset: {spentTime: '', isOvertime: false}};
  479. },
  480. setParentId(parentId) {
  481. return {$set: {parentId}};
  482. },
  483. });
  484. //FUNCTIONS FOR creation of Activities
  485. function cardMove(userId, doc, fieldNames, oldListId) {
  486. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  487. Activities.insert({
  488. userId,
  489. oldListId,
  490. activityType: 'moveCard',
  491. listId: doc.listId,
  492. boardId: doc.boardId,
  493. cardId: doc._id,
  494. });
  495. }
  496. }
  497. function cardState(userId, doc, fieldNames) {
  498. if (_.contains(fieldNames, 'archived')) {
  499. if (doc.archived) {
  500. Activities.insert({
  501. userId,
  502. activityType: 'archivedCard',
  503. boardId: doc.boardId,
  504. listId: doc.listId,
  505. cardId: doc._id,
  506. });
  507. } else {
  508. Activities.insert({
  509. userId,
  510. activityType: 'restoredCard',
  511. boardId: doc.boardId,
  512. listId: doc.listId,
  513. cardId: doc._id,
  514. });
  515. }
  516. }
  517. }
  518. function cardMembers(userId, doc, fieldNames, modifier) {
  519. if (!_.contains(fieldNames, 'members'))
  520. return;
  521. let memberId;
  522. // Say hello to the new member
  523. if (modifier.$addToSet && modifier.$addToSet.members) {
  524. memberId = modifier.$addToSet.members;
  525. if (!_.contains(doc.members, memberId)) {
  526. Activities.insert({
  527. userId,
  528. memberId,
  529. activityType: 'joinMember',
  530. boardId: doc.boardId,
  531. cardId: doc._id,
  532. });
  533. }
  534. }
  535. // Say goodbye to the former member
  536. if (modifier.$pull && modifier.$pull.members) {
  537. memberId = modifier.$pull.members;
  538. // Check that the former member is member of the card
  539. if (_.contains(doc.members, memberId)) {
  540. Activities.insert({
  541. userId,
  542. memberId,
  543. activityType: 'unjoinMember',
  544. boardId: doc.boardId,
  545. cardId: doc._id,
  546. });
  547. }
  548. }
  549. }
  550. function cardCreation(userId, doc) {
  551. Activities.insert({
  552. userId,
  553. activityType: 'createCard',
  554. boardId: doc.boardId,
  555. listId: doc.listId,
  556. cardId: doc._id,
  557. });
  558. }
  559. function cardRemover(userId, doc) {
  560. Activities.remove({
  561. cardId: doc._id,
  562. });
  563. Checklists.remove({
  564. cardId: doc._id,
  565. });
  566. Subtasks.remove({
  567. cardId: doc._id,
  568. });
  569. CardComments.remove({
  570. cardId: doc._id,
  571. });
  572. Attachments.remove({
  573. cardId: doc._id,
  574. });
  575. }
  576. if (Meteor.isServer) {
  577. // Cards are often fetched within a board, so we create an index to make these
  578. // queries more efficient.
  579. Meteor.startup(() => {
  580. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  581. });
  582. Cards.after.insert((userId, doc) => {
  583. cardCreation(userId, doc);
  584. });
  585. // New activity for card (un)archivage
  586. Cards.after.update((userId, doc, fieldNames) => {
  587. cardState(userId, doc, fieldNames);
  588. });
  589. //New activity for card moves
  590. Cards.after.update(function (userId, doc, fieldNames) {
  591. const oldListId = this.previous.listId;
  592. cardMove(userId, doc, fieldNames, oldListId);
  593. });
  594. // Add a new activity if we add or remove a member to the card
  595. Cards.before.update((userId, doc, fieldNames, modifier) => {
  596. cardMembers(userId, doc, fieldNames, modifier);
  597. });
  598. // Remove all activities associated with a card if we remove the card
  599. // Remove also card_comments / checklists / attachments
  600. Cards.after.remove((userId, doc) => {
  601. cardRemover(userId, doc);
  602. });
  603. }
  604. //LISTS REST API
  605. if (Meteor.isServer) {
  606. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  607. const paramBoardId = req.params.boardId;
  608. const paramListId = req.params.listId;
  609. Authentication.checkBoardAccess(req.userId, paramBoardId);
  610. JsonRoutes.sendResult(res, {
  611. code: 200,
  612. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  613. return {
  614. _id: doc._id,
  615. title: doc.title,
  616. description: doc.description,
  617. };
  618. }),
  619. });
  620. });
  621. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  622. const paramBoardId = req.params.boardId;
  623. const paramListId = req.params.listId;
  624. const paramCardId = req.params.cardId;
  625. Authentication.checkBoardAccess(req.userId, paramBoardId);
  626. JsonRoutes.sendResult(res, {
  627. code: 200,
  628. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  629. });
  630. });
  631. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  632. Authentication.checkUserId(req.userId);
  633. const paramBoardId = req.params.boardId;
  634. const paramListId = req.params.listId;
  635. const check = Users.findOne({_id: req.body.authorId});
  636. const members = req.body.members || [req.body.authorId];
  637. if (typeof check !== 'undefined') {
  638. const id = Cards.direct.insert({
  639. title: req.body.title,
  640. boardId: paramBoardId,
  641. listId: paramListId,
  642. description: req.body.description,
  643. userId: req.body.authorId,
  644. swimlaneId: req.body.swimlaneId,
  645. sort: 0,
  646. members,
  647. });
  648. JsonRoutes.sendResult(res, {
  649. code: 200,
  650. data: {
  651. _id: id,
  652. },
  653. });
  654. const card = Cards.findOne({_id:id});
  655. cardCreation(req.body.authorId, card);
  656. } else {
  657. JsonRoutes.sendResult(res, {
  658. code: 401,
  659. });
  660. }
  661. });
  662. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  663. Authentication.checkUserId(req.userId);
  664. const paramBoardId = req.params.boardId;
  665. const paramCardId = req.params.cardId;
  666. const paramListId = req.params.listId;
  667. if (req.body.hasOwnProperty('title')) {
  668. const newTitle = req.body.title;
  669. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  670. {$set: {title: newTitle}});
  671. }
  672. if (req.body.hasOwnProperty('listId')) {
  673. const newParamListId = req.body.listId;
  674. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  675. {$set: {listId: newParamListId}});
  676. const card = Cards.findOne({_id: paramCardId} );
  677. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  678. }
  679. if (req.body.hasOwnProperty('description')) {
  680. const newDescription = req.body.description;
  681. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  682. {$set: {description: newDescription}});
  683. }
  684. if (req.body.hasOwnProperty('labelIds')) {
  685. const newlabelIds = req.body.labelIds;
  686. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  687. {$set: {labelIds: newlabelIds}});
  688. }
  689. JsonRoutes.sendResult(res, {
  690. code: 200,
  691. data: {
  692. _id: paramCardId,
  693. },
  694. });
  695. });
  696. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  697. Authentication.checkUserId(req.userId);
  698. const paramBoardId = req.params.boardId;
  699. const paramListId = req.params.listId;
  700. const paramCardId = req.params.cardId;
  701. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  702. const card = Cards.find({_id: paramCardId} );
  703. cardRemover(req.body.authorId, card);
  704. JsonRoutes.sendResult(res, {
  705. code: 200,
  706. data: {
  707. _id: paramCardId,
  708. },
  709. });
  710. });
  711. }