cards.js 17 KB

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