cards.js 15 KB

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