cards.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. const definition = definitions.find((definition) => {
  213. return definition._id === customField._id;
  214. });
  215. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  216. let trueValue = customField.value;
  217. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0)
  218. {
  219. for (let i = 0; i < definition.settings.dropdownItems.length; i++)
  220. {
  221. if (definition.settings.dropdownItems[i]._id === customField.value)
  222. {
  223. trueValue = definition.settings.dropdownItems[i].name;
  224. }
  225. }
  226. }
  227. return {
  228. _id: customField._id,
  229. value: customField.value,
  230. trueValue,
  231. definition,
  232. };
  233. });
  234. },
  235. absoluteUrl() {
  236. const board = this.board();
  237. return FlowRouter.url('card', {
  238. boardId: board._id,
  239. slug: board.slug,
  240. cardId: this._id,
  241. });
  242. },
  243. canBeRestored() {
  244. const list = Lists.findOne({_id: this.listId});
  245. if(!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()){
  246. return false;
  247. }
  248. return true;
  249. },
  250. });
  251. Cards.mutations({
  252. archive() {
  253. return {$set: {archived: true}};
  254. },
  255. restore() {
  256. return {$set: {archived: false}};
  257. },
  258. setTitle(title) {
  259. return {$set: {title}};
  260. },
  261. setDescription(description) {
  262. return {$set: {description}};
  263. },
  264. setRequestedBy(requestedBy) {
  265. return {$set: {requestedBy}};
  266. },
  267. setAssignedBy(assignedBy) {
  268. return {$set: {assignedBy}};
  269. },
  270. move(swimlaneId, listId, sortIndex) {
  271. const list = Lists.findOne(listId);
  272. const mutatedFields = {
  273. swimlaneId,
  274. listId,
  275. boardId: list.boardId,
  276. sort: sortIndex,
  277. };
  278. return {$set: mutatedFields};
  279. },
  280. addLabel(labelId) {
  281. return {$addToSet: {labelIds: labelId}};
  282. },
  283. removeLabel(labelId) {
  284. return {$pull: {labelIds: labelId}};
  285. },
  286. toggleLabel(labelId) {
  287. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  288. return this.removeLabel(labelId);
  289. } else {
  290. return this.addLabel(labelId);
  291. }
  292. },
  293. assignMember(memberId) {
  294. return {$addToSet: {members: memberId}};
  295. },
  296. unassignMember(memberId) {
  297. return {$pull: {members: memberId}};
  298. },
  299. toggleMember(memberId) {
  300. if (this.members && this.members.indexOf(memberId) > -1) {
  301. return this.unassignMember(memberId);
  302. } else {
  303. return this.assignMember(memberId);
  304. }
  305. },
  306. assignCustomField(customFieldId) {
  307. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  308. },
  309. unassignCustomField(customFieldId) {
  310. return {$pull: {customFields: {_id: customFieldId}}};
  311. },
  312. toggleCustomField(customFieldId) {
  313. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  314. return this.unassignCustomField(customFieldId);
  315. } else {
  316. return this.assignCustomField(customFieldId);
  317. }
  318. },
  319. setCustomField(customFieldId, value) {
  320. // todo
  321. const index = this.customFieldIndex(customFieldId);
  322. if (index > -1) {
  323. const update = {$set: {}};
  324. update.$set[`customFields.${index}.value`] = value;
  325. return update;
  326. }
  327. // TODO
  328. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  329. return null;
  330. },
  331. setCover(coverId) {
  332. return {$set: {coverId}};
  333. },
  334. unsetCover() {
  335. return {$unset: {coverId: ''}};
  336. },
  337. setReceived(receivedAt) {
  338. return {$set: {receivedAt}};
  339. },
  340. unsetReceived() {
  341. return {$unset: {receivedAt: ''}};
  342. },
  343. setStart(startAt) {
  344. return {$set: {startAt}};
  345. },
  346. unsetStart() {
  347. return {$unset: {startAt: ''}};
  348. },
  349. setDue(dueAt) {
  350. return {$set: {dueAt}};
  351. },
  352. unsetDue() {
  353. return {$unset: {dueAt: ''}};
  354. },
  355. setEnd(endAt) {
  356. return {$set: {endAt}};
  357. },
  358. unsetEnd() {
  359. return {$unset: {endAt: ''}};
  360. },
  361. setOvertime(isOvertime) {
  362. return {$set: {isOvertime}};
  363. },
  364. setSpentTime(spentTime) {
  365. return {$set: {spentTime}};
  366. },
  367. unsetSpentTime() {
  368. return {$unset: {spentTime: '', isOvertime: false}};
  369. },
  370. });
  371. //FUNCTIONS FOR creation of Activities
  372. function cardMove(userId, doc, fieldNames, oldListId) {
  373. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  374. Activities.insert({
  375. userId,
  376. oldListId,
  377. activityType: 'moveCard',
  378. listId: doc.listId,
  379. boardId: doc.boardId,
  380. cardId: doc._id,
  381. });
  382. }
  383. }
  384. function cardState(userId, doc, fieldNames) {
  385. if (_.contains(fieldNames, 'archived')) {
  386. if (doc.archived) {
  387. Activities.insert({
  388. userId,
  389. activityType: 'archivedCard',
  390. boardId: doc.boardId,
  391. listId: doc.listId,
  392. cardId: doc._id,
  393. });
  394. } else {
  395. Activities.insert({
  396. userId,
  397. activityType: 'restoredCard',
  398. boardId: doc.boardId,
  399. listId: doc.listId,
  400. cardId: doc._id,
  401. });
  402. }
  403. }
  404. }
  405. function cardMembers(userId, doc, fieldNames, modifier) {
  406. if (!_.contains(fieldNames, 'members'))
  407. return;
  408. let memberId;
  409. // Say hello to the new member
  410. if (modifier.$addToSet && modifier.$addToSet.members) {
  411. memberId = modifier.$addToSet.members;
  412. if (!_.contains(doc.members, memberId)) {
  413. Activities.insert({
  414. userId,
  415. memberId,
  416. activityType: 'joinMember',
  417. boardId: doc.boardId,
  418. cardId: doc._id,
  419. });
  420. }
  421. }
  422. // Say goodbye to the former member
  423. if (modifier.$pull && modifier.$pull.members) {
  424. memberId = modifier.$pull.members;
  425. // Check that the former member is member of the card
  426. if (_.contains(doc.members, memberId)) {
  427. Activities.insert({
  428. userId,
  429. memberId,
  430. activityType: 'unjoinMember',
  431. boardId: doc.boardId,
  432. cardId: doc._id,
  433. });
  434. }
  435. }
  436. }
  437. function cardCreation(userId, doc) {
  438. Activities.insert({
  439. userId,
  440. activityType: 'createCard',
  441. boardId: doc.boardId,
  442. listId: doc.listId,
  443. cardId: doc._id,
  444. });
  445. }
  446. function cardRemover(userId, doc) {
  447. Activities.remove({
  448. cardId: doc._id,
  449. });
  450. Checklists.remove({
  451. cardId: doc._id,
  452. });
  453. CardComments.remove({
  454. cardId: doc._id,
  455. });
  456. Attachments.remove({
  457. cardId: doc._id,
  458. });
  459. }
  460. if (Meteor.isServer) {
  461. // Cards are often fetched within a board, so we create an index to make these
  462. // queries more efficient.
  463. Meteor.startup(() => {
  464. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  465. });
  466. Cards.after.insert((userId, doc) => {
  467. cardCreation(userId, doc);
  468. });
  469. // New activity for card (un)archivage
  470. Cards.after.update((userId, doc, fieldNames) => {
  471. cardState(userId, doc, fieldNames);
  472. });
  473. //New activity for card moves
  474. Cards.after.update(function (userId, doc, fieldNames) {
  475. const oldListId = this.previous.listId;
  476. cardMove(userId, doc, fieldNames, oldListId);
  477. });
  478. // Add a new activity if we add or remove a member to the card
  479. Cards.before.update((userId, doc, fieldNames, modifier) => {
  480. cardMembers(userId, doc, fieldNames, modifier);
  481. });
  482. // Remove all activities associated with a card if we remove the card
  483. // Remove also card_comments / checklists / attachments
  484. Cards.after.remove((userId, doc) => {
  485. cardRemover(userId, doc);
  486. });
  487. }
  488. //LISTS REST API
  489. if (Meteor.isServer) {
  490. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  491. const paramBoardId = req.params.boardId;
  492. const paramListId = req.params.listId;
  493. Authentication.checkBoardAccess(req.userId, paramBoardId);
  494. JsonRoutes.sendResult(res, {
  495. code: 200,
  496. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  497. return {
  498. _id: doc._id,
  499. title: doc.title,
  500. description: doc.description,
  501. };
  502. }),
  503. });
  504. });
  505. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  506. const paramBoardId = req.params.boardId;
  507. const paramListId = req.params.listId;
  508. const paramCardId = req.params.cardId;
  509. Authentication.checkBoardAccess(req.userId, paramBoardId);
  510. JsonRoutes.sendResult(res, {
  511. code: 200,
  512. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  513. });
  514. });
  515. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  516. Authentication.checkUserId(req.userId);
  517. const paramBoardId = req.params.boardId;
  518. const paramListId = req.params.listId;
  519. const check = Users.findOne({_id: req.body.authorId});
  520. const members = req.body.members || [req.body.authorId];
  521. if (typeof check !== 'undefined') {
  522. const id = Cards.direct.insert({
  523. title: req.body.title,
  524. boardId: paramBoardId,
  525. listId: paramListId,
  526. description: req.body.description,
  527. userId: req.body.authorId,
  528. swimlaneId: req.body.swimlaneId,
  529. sort: 0,
  530. members,
  531. });
  532. JsonRoutes.sendResult(res, {
  533. code: 200,
  534. data: {
  535. _id: id,
  536. },
  537. });
  538. const card = Cards.findOne({_id:id});
  539. cardCreation(req.body.authorId, card);
  540. } else {
  541. JsonRoutes.sendResult(res, {
  542. code: 401,
  543. });
  544. }
  545. });
  546. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  547. Authentication.checkUserId(req.userId);
  548. const paramBoardId = req.params.boardId;
  549. const paramCardId = req.params.cardId;
  550. const paramListId = req.params.listId;
  551. if (req.body.hasOwnProperty('title')) {
  552. const newTitle = req.body.title;
  553. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  554. {$set: {title: newTitle}});
  555. }
  556. if (req.body.hasOwnProperty('listId')) {
  557. const newParamListId = req.body.listId;
  558. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  559. {$set: {listId: newParamListId}});
  560. const card = Cards.findOne({_id: paramCardId} );
  561. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  562. }
  563. if (req.body.hasOwnProperty('description')) {
  564. const newDescription = req.body.description;
  565. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  566. {$set: {description: newDescription}});
  567. }
  568. if (req.body.hasOwnProperty('labelIds')) {
  569. const newlabelIds = req.body.labelIds;
  570. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  571. {$set: {labelIds: newlabelIds}});
  572. }
  573. JsonRoutes.sendResult(res, {
  574. code: 200,
  575. data: {
  576. _id: paramCardId,
  577. },
  578. });
  579. });
  580. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  581. Authentication.checkUserId(req.userId);
  582. const paramBoardId = req.params.boardId;
  583. const paramListId = req.params.listId;
  584. const paramCardId = req.params.cardId;
  585. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  586. const card = Cards.find({_id: paramCardId} );
  587. cardRemover(req.body.authorId, card);
  588. JsonRoutes.sendResult(res, {
  589. code: 200,
  590. data: {
  591. _id: paramCardId,
  592. },
  593. });
  594. });
  595. }