cards.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. if (this.parentId === '') {
  299. return '';
  300. }
  301. return Cards.findOne(this.parentId).title;
  302. },
  303. parentListId() {
  304. const result = [];
  305. let crtParentId = this.parentId;
  306. while (crtParentId !== '') {
  307. const crt = Cards.findOne(crtParentId);
  308. if ((crt === null) || (crt === undefined)) {
  309. // maybe it has been deleted
  310. break;
  311. }
  312. if (crtParentId in result) {
  313. // circular reference
  314. break;
  315. }
  316. result.unshift(crtParentId);
  317. crtParentId = crt.parentId;
  318. }
  319. return result;
  320. },
  321. parentList() {
  322. const resultId = [];
  323. const result = [];
  324. let crtParentId = this.parentId;
  325. while (crtParentId !== '') {
  326. const crt = Cards.findOne(crtParentId);
  327. if ((crt === null) || (crt === undefined)) {
  328. // maybe it has been deleted
  329. break;
  330. }
  331. if (crtParentId in resultId) {
  332. // circular reference
  333. break;
  334. }
  335. resultId.unshift(crtParentId);
  336. result.unshift(crt);
  337. crtParentId = crt.parentId;
  338. }
  339. return result;
  340. },
  341. parentString(sep) {
  342. return this.parentList().map(function(elem){
  343. return elem.title;
  344. }).join(sep);
  345. },
  346. isTopLevel() {
  347. return this.parentId === '';
  348. },
  349. });
  350. Cards.mutations({
  351. applyToChildren(funct) {
  352. Cards.find({ parentId: this._id }).forEach((card) => {
  353. funct(card);
  354. });
  355. },
  356. archive() {
  357. this.applyToChildren((card) => { return card.archive(); });
  358. return {$set: {archived: true}};
  359. },
  360. restore() {
  361. this.applyToChildren((card) => { return card.restore(); });
  362. return {$set: {archived: false}};
  363. },
  364. setTitle(title) {
  365. return {$set: {title}};
  366. },
  367. setDescription(description) {
  368. return {$set: {description}};
  369. },
  370. setRequestedBy(requestedBy) {
  371. return {$set: {requestedBy}};
  372. },
  373. setAssignedBy(assignedBy) {
  374. return {$set: {assignedBy}};
  375. },
  376. move(swimlaneId, listId, sortIndex) {
  377. const list = Lists.findOne(listId);
  378. const mutatedFields = {
  379. swimlaneId,
  380. listId,
  381. boardId: list.boardId,
  382. sort: sortIndex,
  383. };
  384. return {$set: mutatedFields};
  385. },
  386. addLabel(labelId) {
  387. return {$addToSet: {labelIds: labelId}};
  388. },
  389. removeLabel(labelId) {
  390. return {$pull: {labelIds: labelId}};
  391. },
  392. toggleLabel(labelId) {
  393. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  394. return this.removeLabel(labelId);
  395. } else {
  396. return this.addLabel(labelId);
  397. }
  398. },
  399. assignMember(memberId) {
  400. return {$addToSet: {members: memberId}};
  401. },
  402. unassignMember(memberId) {
  403. return {$pull: {members: memberId}};
  404. },
  405. toggleMember(memberId) {
  406. if (this.members && this.members.indexOf(memberId) > -1) {
  407. return this.unassignMember(memberId);
  408. } else {
  409. return this.assignMember(memberId);
  410. }
  411. },
  412. assignCustomField(customFieldId) {
  413. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  414. },
  415. unassignCustomField(customFieldId) {
  416. return {$pull: {customFields: {_id: customFieldId}}};
  417. },
  418. toggleCustomField(customFieldId) {
  419. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  420. return this.unassignCustomField(customFieldId);
  421. } else {
  422. return this.assignCustomField(customFieldId);
  423. }
  424. },
  425. setCustomField(customFieldId, value) {
  426. // todo
  427. const index = this.customFieldIndex(customFieldId);
  428. if (index > -1) {
  429. const update = {$set: {}};
  430. update.$set[`customFields.${index}.value`] = value;
  431. return update;
  432. }
  433. // TODO
  434. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  435. return null;
  436. },
  437. setCover(coverId) {
  438. return {$set: {coverId}};
  439. },
  440. unsetCover() {
  441. return {$unset: {coverId: ''}};
  442. },
  443. setReceived(receivedAt) {
  444. return {$set: {receivedAt}};
  445. },
  446. unsetReceived() {
  447. return {$unset: {receivedAt: ''}};
  448. },
  449. setStart(startAt) {
  450. return {$set: {startAt}};
  451. },
  452. unsetStart() {
  453. return {$unset: {startAt: ''}};
  454. },
  455. setDue(dueAt) {
  456. return {$set: {dueAt}};
  457. },
  458. unsetDue() {
  459. return {$unset: {dueAt: ''}};
  460. },
  461. setEnd(endAt) {
  462. return {$set: {endAt}};
  463. },
  464. unsetEnd() {
  465. return {$unset: {endAt: ''}};
  466. },
  467. setOvertime(isOvertime) {
  468. return {$set: {isOvertime}};
  469. },
  470. setSpentTime(spentTime) {
  471. return {$set: {spentTime}};
  472. },
  473. unsetSpentTime() {
  474. return {$unset: {spentTime: '', isOvertime: false}};
  475. },
  476. });
  477. //FUNCTIONS FOR creation of Activities
  478. function cardMove(userId, doc, fieldNames, oldListId) {
  479. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  480. Activities.insert({
  481. userId,
  482. oldListId,
  483. activityType: 'moveCard',
  484. listId: doc.listId,
  485. boardId: doc.boardId,
  486. cardId: doc._id,
  487. });
  488. }
  489. }
  490. function cardState(userId, doc, fieldNames) {
  491. if (_.contains(fieldNames, 'archived')) {
  492. if (doc.archived) {
  493. Activities.insert({
  494. userId,
  495. activityType: 'archivedCard',
  496. boardId: doc.boardId,
  497. listId: doc.listId,
  498. cardId: doc._id,
  499. });
  500. } else {
  501. Activities.insert({
  502. userId,
  503. activityType: 'restoredCard',
  504. boardId: doc.boardId,
  505. listId: doc.listId,
  506. cardId: doc._id,
  507. });
  508. }
  509. }
  510. }
  511. function cardMembers(userId, doc, fieldNames, modifier) {
  512. if (!_.contains(fieldNames, 'members'))
  513. return;
  514. let memberId;
  515. // Say hello to the new member
  516. if (modifier.$addToSet && modifier.$addToSet.members) {
  517. memberId = modifier.$addToSet.members;
  518. if (!_.contains(doc.members, memberId)) {
  519. Activities.insert({
  520. userId,
  521. memberId,
  522. activityType: 'joinMember',
  523. boardId: doc.boardId,
  524. cardId: doc._id,
  525. });
  526. }
  527. }
  528. // Say goodbye to the former member
  529. if (modifier.$pull && modifier.$pull.members) {
  530. memberId = modifier.$pull.members;
  531. // Check that the former member is member of the card
  532. if (_.contains(doc.members, memberId)) {
  533. Activities.insert({
  534. userId,
  535. memberId,
  536. activityType: 'unjoinMember',
  537. boardId: doc.boardId,
  538. cardId: doc._id,
  539. });
  540. }
  541. }
  542. }
  543. function cardCreation(userId, doc) {
  544. Activities.insert({
  545. userId,
  546. activityType: 'createCard',
  547. boardId: doc.boardId,
  548. listId: doc.listId,
  549. cardId: doc._id,
  550. });
  551. }
  552. function cardRemover(userId, doc) {
  553. Activities.remove({
  554. cardId: doc._id,
  555. });
  556. Checklists.remove({
  557. cardId: doc._id,
  558. });
  559. Subtasks.remove({
  560. cardId: doc._id,
  561. });
  562. CardComments.remove({
  563. cardId: doc._id,
  564. });
  565. Attachments.remove({
  566. cardId: doc._id,
  567. });
  568. }
  569. if (Meteor.isServer) {
  570. // Cards are often fetched within a board, so we create an index to make these
  571. // queries more efficient.
  572. Meteor.startup(() => {
  573. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  574. });
  575. Cards.after.insert((userId, doc) => {
  576. cardCreation(userId, doc);
  577. });
  578. // New activity for card (un)archivage
  579. Cards.after.update((userId, doc, fieldNames) => {
  580. cardState(userId, doc, fieldNames);
  581. });
  582. //New activity for card moves
  583. Cards.after.update(function (userId, doc, fieldNames) {
  584. const oldListId = this.previous.listId;
  585. cardMove(userId, doc, fieldNames, oldListId);
  586. });
  587. // Add a new activity if we add or remove a member to the card
  588. Cards.before.update((userId, doc, fieldNames, modifier) => {
  589. cardMembers(userId, doc, fieldNames, modifier);
  590. });
  591. // Remove all activities associated with a card if we remove the card
  592. // Remove also card_comments / checklists / attachments
  593. Cards.after.remove((userId, doc) => {
  594. cardRemover(userId, doc);
  595. });
  596. }
  597. //LISTS REST API
  598. if (Meteor.isServer) {
  599. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  600. const paramBoardId = req.params.boardId;
  601. const paramListId = req.params.listId;
  602. Authentication.checkBoardAccess(req.userId, paramBoardId);
  603. JsonRoutes.sendResult(res, {
  604. code: 200,
  605. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  606. return {
  607. _id: doc._id,
  608. title: doc.title,
  609. description: doc.description,
  610. };
  611. }),
  612. });
  613. });
  614. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  615. const paramBoardId = req.params.boardId;
  616. const paramListId = req.params.listId;
  617. const paramCardId = req.params.cardId;
  618. Authentication.checkBoardAccess(req.userId, paramBoardId);
  619. JsonRoutes.sendResult(res, {
  620. code: 200,
  621. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  622. });
  623. });
  624. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  625. Authentication.checkUserId(req.userId);
  626. const paramBoardId = req.params.boardId;
  627. const paramListId = req.params.listId;
  628. const check = Users.findOne({_id: req.body.authorId});
  629. const members = req.body.members || [req.body.authorId];
  630. if (typeof check !== 'undefined') {
  631. const id = Cards.direct.insert({
  632. title: req.body.title,
  633. boardId: paramBoardId,
  634. listId: paramListId,
  635. description: req.body.description,
  636. userId: req.body.authorId,
  637. swimlaneId: req.body.swimlaneId,
  638. sort: 0,
  639. members,
  640. });
  641. JsonRoutes.sendResult(res, {
  642. code: 200,
  643. data: {
  644. _id: id,
  645. },
  646. });
  647. const card = Cards.findOne({_id:id});
  648. cardCreation(req.body.authorId, card);
  649. } else {
  650. JsonRoutes.sendResult(res, {
  651. code: 401,
  652. });
  653. }
  654. });
  655. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  656. Authentication.checkUserId(req.userId);
  657. const paramBoardId = req.params.boardId;
  658. const paramCardId = req.params.cardId;
  659. const paramListId = req.params.listId;
  660. if (req.body.hasOwnProperty('title')) {
  661. const newTitle = req.body.title;
  662. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  663. {$set: {title: newTitle}});
  664. }
  665. if (req.body.hasOwnProperty('listId')) {
  666. const newParamListId = req.body.listId;
  667. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  668. {$set: {listId: newParamListId}});
  669. const card = Cards.findOne({_id: paramCardId} );
  670. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  671. }
  672. if (req.body.hasOwnProperty('description')) {
  673. const newDescription = req.body.description;
  674. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  675. {$set: {description: newDescription}});
  676. }
  677. if (req.body.hasOwnProperty('labelIds')) {
  678. const newlabelIds = req.body.labelIds;
  679. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  680. {$set: {labelIds: newlabelIds}});
  681. }
  682. JsonRoutes.sendResult(res, {
  683. code: 200,
  684. data: {
  685. _id: paramCardId,
  686. },
  687. });
  688. });
  689. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  690. Authentication.checkUserId(req.userId);
  691. const paramBoardId = req.params.boardId;
  692. const paramListId = req.params.listId;
  693. const paramCardId = req.params.cardId;
  694. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  695. const card = Cards.find({_id: paramCardId} );
  696. cardRemover(req.body.authorId, card);
  697. JsonRoutes.sendResult(res, {
  698. code: 200,
  699. data: {
  700. _id: paramCardId,
  701. },
  702. });
  703. });
  704. }