cards.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. type: {
  136. type: String,
  137. },
  138. importedId: {
  139. type: String,
  140. optional: true,
  141. },
  142. }));
  143. Cards.allow({
  144. insert(userId, doc) {
  145. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  146. },
  147. update(userId, doc) {
  148. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  149. },
  150. remove(userId, doc) {
  151. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  152. },
  153. fetch: ['boardId'],
  154. });
  155. Cards.helpers({
  156. list() {
  157. return Lists.findOne(this.listId);
  158. },
  159. board() {
  160. return Boards.findOne(this.boardId);
  161. },
  162. labels() {
  163. const boardLabels = this.board().labels;
  164. const cardLabels = _.filter(boardLabels, (label) => {
  165. return _.contains(this.labelIds, label._id);
  166. });
  167. return cardLabels;
  168. },
  169. hasLabel(labelId) {
  170. return _.contains(this.labelIds, labelId);
  171. },
  172. user() {
  173. return Users.findOne(this.userId);
  174. },
  175. isAssigned(memberId) {
  176. return _.contains(this.getMembers(), memberId);
  177. },
  178. activities() {
  179. if (this.isImportedCard()) {
  180. return Activities.find({cardId: this.importedId}, {sort: {createdAt: -1}});
  181. } else if (this.isImportedBoard()) {
  182. return Activities.find({boardId: this.importedId}, {sort: {createdAt: -1}});
  183. } else {
  184. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  185. }
  186. },
  187. comments() {
  188. if (this.isImportedCard()) {
  189. return CardComments.find({cardId: this.importedId}, {sort: {createdAt: -1}});
  190. } else {
  191. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  192. }
  193. },
  194. attachments() {
  195. if (this.isImportedCard()) {
  196. return Attachments.find({cardId: this.importedId}, {sort: {uploadedAt: -1}});
  197. } else {
  198. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  199. }
  200. },
  201. cover() {
  202. const cover = Attachments.findOne(this.coverId);
  203. // if we return a cover before it is fully stored, we will get errors when we try to display it
  204. // todo XXX we could return a default "upload pending" image in the meantime?
  205. return cover && cover.url() && cover;
  206. },
  207. checklists() {
  208. if (this.isImportedCard()) {
  209. return Checklists.find({cardId: this.importedId}, {sort: { sort: 1 } });
  210. } else {
  211. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  212. }
  213. },
  214. checklistItemCount() {
  215. const checklists = this.checklists().fetch();
  216. return checklists.map((checklist) => {
  217. return checklist.itemCount();
  218. }).reduce((prev, next) => {
  219. return prev + next;
  220. }, 0);
  221. },
  222. checklistFinishedCount() {
  223. const checklists = this.checklists().fetch();
  224. return checklists.map((checklist) => {
  225. return checklist.finishedCount();
  226. }).reduce((prev, next) => {
  227. return prev + next;
  228. }, 0);
  229. },
  230. checklistFinished() {
  231. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  232. },
  233. hasChecklist() {
  234. return this.checklistItemCount() !== 0;
  235. },
  236. subtasks() {
  237. return Cards.find({
  238. parentId: this._id,
  239. archived: false,
  240. }, {sort: { sort: 1 } });
  241. },
  242. allSubtasks() {
  243. return Cards.find({
  244. parentId: this._id,
  245. archived: false,
  246. }, {sort: { sort: 1 } });
  247. },
  248. subtasksCount() {
  249. return Cards.find({
  250. parentId: this._id,
  251. archived: false,
  252. }).count();
  253. },
  254. subtasksFinishedCount() {
  255. return Cards.find({
  256. parentId: this._id,
  257. archived: true}).count();
  258. },
  259. subtasksFinished() {
  260. const finishCount = this.subtasksFinishedCount();
  261. return finishCount > 0 && this.subtasksCount() === finishCount;
  262. },
  263. allowsSubtasks() {
  264. return this.subtasksCount() !== 0;
  265. },
  266. customFieldIndex(customFieldId) {
  267. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  268. },
  269. // customFields with definitions
  270. customFieldsWD() {
  271. // get all definitions
  272. const definitions = CustomFields.find({
  273. boardId: this.boardId,
  274. }).fetch();
  275. // match right definition to each field
  276. if (!this.customFields) return [];
  277. return this.customFields.map((customField) => {
  278. const definition = definitions.find((definition) => {
  279. return definition._id === customField._id;
  280. });
  281. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  282. let trueValue = customField.value;
  283. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0)
  284. {
  285. for (let i = 0; i < definition.settings.dropdownItems.length; i++)
  286. {
  287. if (definition.settings.dropdownItems[i]._id === customField.value)
  288. {
  289. trueValue = definition.settings.dropdownItems[i].name;
  290. }
  291. }
  292. }
  293. return {
  294. _id: customField._id,
  295. value: customField.value,
  296. trueValue,
  297. definition,
  298. };
  299. });
  300. },
  301. absoluteUrl() {
  302. const board = this.board();
  303. return FlowRouter.url('card', {
  304. boardId: board._id,
  305. slug: board.slug,
  306. cardId: this._id,
  307. });
  308. },
  309. canBeRestored() {
  310. const list = Lists.findOne({_id: this.listId});
  311. if(!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()){
  312. return false;
  313. }
  314. return true;
  315. },
  316. parentCard() {
  317. if (this.parentId === '') {
  318. return null;
  319. }
  320. return Cards.findOne(this.parentId);
  321. },
  322. parentCardName() {
  323. let result = '';
  324. if (this.parentId !== '') {
  325. const card = Cards.findOne(this.parentId);
  326. if (card) {
  327. result = card.title;
  328. }
  329. }
  330. return result;
  331. },
  332. parentListId() {
  333. const result = [];
  334. let crtParentId = this.parentId;
  335. while (crtParentId !== '') {
  336. const crt = Cards.findOne(crtParentId);
  337. if ((crt === null) || (crt === undefined)) {
  338. // maybe it has been deleted
  339. break;
  340. }
  341. if (crtParentId in result) {
  342. // circular reference
  343. break;
  344. }
  345. result.unshift(crtParentId);
  346. crtParentId = crt.parentId;
  347. }
  348. return result;
  349. },
  350. parentList() {
  351. const resultId = [];
  352. const result = [];
  353. let crtParentId = this.parentId;
  354. while (crtParentId !== '') {
  355. const crt = Cards.findOne(crtParentId);
  356. if ((crt === null) || (crt === undefined)) {
  357. // maybe it has been deleted
  358. break;
  359. }
  360. if (crtParentId in resultId) {
  361. // circular reference
  362. break;
  363. }
  364. resultId.unshift(crtParentId);
  365. result.unshift(crt);
  366. crtParentId = crt.parentId;
  367. }
  368. return result;
  369. },
  370. parentString(sep) {
  371. return this.parentList().map(function(elem){
  372. return elem.title;
  373. }).join(sep);
  374. },
  375. isTopLevel() {
  376. return this.parentId === '';
  377. },
  378. isImportedCard() {
  379. return this.type === 'cardType-importedCard';
  380. },
  381. isImportedBoard() {
  382. return this.type === 'cardType-importedBoard';
  383. },
  384. isImported() {
  385. return this.isImportedCard() || this.isImportedBoard();
  386. },
  387. setDescription(description) {
  388. if (this.isImportedCard()) {
  389. return Cards.update({_id: this.importedId}, {$set: {description}});
  390. } else if (this.isImportedBoard()) {
  391. return Boards.update({_id: this.importedId}, {$set: {description}});
  392. } else {
  393. return Cards.update(
  394. {_id: this._id},
  395. {$set: {description}}
  396. );
  397. }
  398. },
  399. getDescription() {
  400. if (this.isImportedCard()) {
  401. const card = Cards.findOne({_id: this.importedId});
  402. if (card && card.description)
  403. return card.description;
  404. else
  405. return null;
  406. } else if (this.isImportedBoard()) {
  407. const board = Boards.findOne({_id: this.importedId});
  408. if (board && board.description)
  409. return board.description;
  410. else
  411. return null;
  412. } else if (this.description) {
  413. return this.description;
  414. } else {
  415. return null;
  416. }
  417. },
  418. getMembers() {
  419. if (this.isImportedCard()) {
  420. const card = Cards.findOne({_id: this.importedId});
  421. return card.members;
  422. } else if (this.isImportedBoard()) {
  423. const board = Boards.findOne({_id: this.importedId});
  424. return board.activeMembers().map((member) => {
  425. return member.userId;
  426. });
  427. } else {
  428. return this.members;
  429. }
  430. },
  431. assignMember(memberId) {
  432. if (this.isImportedCard()) {
  433. return Cards.update(
  434. { _id: this.importedId },
  435. { $addToSet: { members: memberId }}
  436. );
  437. } else if (this.isImportedBoard()) {
  438. const board = Boards.findOne({_id: this.importedId});
  439. return board.addMember(memberId);
  440. } else {
  441. return Cards.update(
  442. { _id: this._id },
  443. { $addToSet: { members: memberId}}
  444. );
  445. }
  446. },
  447. unassignMember(memberId) {
  448. if (this.isImportedCard()) {
  449. return Cards.update(
  450. { _id: this.importedId },
  451. { $pull: { members: memberId }}
  452. );
  453. } else if (this.isImportedBoard()) {
  454. const board = Boards.findOne({_id: this.importedId});
  455. return board.removeMember(memberId);
  456. } else {
  457. return Cards.update(
  458. { _id: this._id },
  459. { $pull: { members: memberId}}
  460. );
  461. }
  462. },
  463. toggleMember(memberId) {
  464. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  465. return this.unassignMember(memberId);
  466. } else {
  467. return this.assignMember(memberId);
  468. }
  469. },
  470. getReceived() {
  471. if (this.isImportedCard()) {
  472. const card = Cards.findOne({_id: this.importedId});
  473. return card.receivedAt;
  474. } else {
  475. return this.receivedAt;
  476. }
  477. },
  478. setReceived(receivedAt) {
  479. if (this.isImportedCard()) {
  480. return Cards.update(
  481. {_id: this.importedId},
  482. {$set: {receivedAt}}
  483. );
  484. } else {
  485. return Cards.update(
  486. {_id: this._id},
  487. {$set: {receivedAt}}
  488. );
  489. }
  490. },
  491. getStart() {
  492. if (this.isImportedCard()) {
  493. const card = Cards.findOne({_id: this.importedId});
  494. return card.startAt;
  495. } else if (this.isImportedBoard()) {
  496. const board = Boards.findOne({_id: this.importedId});
  497. return board.startAt;
  498. } else {
  499. return this.startAt;
  500. }
  501. },
  502. setStart(startAt) {
  503. if (this.isImportedCard()) {
  504. return Cards.update(
  505. { _id: this.importedId },
  506. {$set: {startAt}}
  507. );
  508. } else if (this.isImportedBoard()) {
  509. return Boards.update(
  510. {_id: this.importedId},
  511. {$set: {startAt}}
  512. );
  513. } else {
  514. return Cards.update(
  515. {_id: this._id},
  516. {$set: {startAt}}
  517. );
  518. }
  519. },
  520. getDue() {
  521. if (this.isImportedCard()) {
  522. const card = Cards.findOne({_id: this.importedId});
  523. return card.dueAt;
  524. } else if (this.isImportedBoard()) {
  525. const board = Boards.findOne({_id: this.importedId});
  526. return board.dueAt;
  527. } else {
  528. return this.dueAt;
  529. }
  530. },
  531. setDue(dueAt) {
  532. if (this.isImportedCard()) {
  533. return Cards.update(
  534. { _id: this.importedId },
  535. {$set: {dueAt}}
  536. );
  537. } else if (this.isImportedBoard()) {
  538. return Boards.update(
  539. {_id: this.importedId},
  540. {$set: {dueAt}}
  541. );
  542. } else {
  543. return Cards.update(
  544. {_id: this._id},
  545. {$set: {dueAt}}
  546. );
  547. }
  548. },
  549. getEnd() {
  550. if (this.isImportedCard()) {
  551. const card = Cards.findOne({_id: this.importedId});
  552. return card.endAt;
  553. } else if (this.isImportedBoard()) {
  554. const board = Boards.findOne({_id: this.importedId});
  555. return board.endAt;
  556. } else {
  557. return this.endAt;
  558. }
  559. },
  560. setEnd(endAt) {
  561. if (this.isImportedCard()) {
  562. return Cards.update(
  563. { _id: this.importedId },
  564. {$set: {endAt}}
  565. );
  566. } else if (this.isImportedBoard()) {
  567. return Boards.update(
  568. {_id: this.importedId},
  569. {$set: {endAt}}
  570. );
  571. } else {
  572. return Cards.update(
  573. {_id: this._id},
  574. {$set: {endAt}}
  575. );
  576. }
  577. },
  578. getIsOvertime() {
  579. if (this.isImportedCard()) {
  580. const card = Cards.findOne({ _id: this.importedId });
  581. return card.isOvertime;
  582. } else if (this.isImportedBoard()) {
  583. const board = Boards.findOne({ _id: this.importedId});
  584. return board.isOvertime;
  585. } else {
  586. return this.isOvertime;
  587. }
  588. },
  589. setIsOvertime(isOvertime) {
  590. if (this.isImportedCard()) {
  591. return Cards.update(
  592. { _id: this.importedId },
  593. {$set: {isOvertime}}
  594. );
  595. } else if (this.isImportedBoard()) {
  596. return Boards.update(
  597. {_id: this.importedId},
  598. {$set: {isOvertime}}
  599. );
  600. } else {
  601. return Cards.update(
  602. {_id: this._id},
  603. {$set: {isOvertime}}
  604. );
  605. }
  606. },
  607. getSpentTime() {
  608. if (this.isImportedCard()) {
  609. const card = Cards.findOne({ _id: this.importedId });
  610. return card.spentTime;
  611. } else if (this.isImportedBoard()) {
  612. const board = Boards.findOne({ _id: this.importedId});
  613. return board.spentTime;
  614. } else {
  615. return this.spentTime;
  616. }
  617. },
  618. setSpentTime(spentTime) {
  619. if (this.isImportedCard()) {
  620. return Cards.update(
  621. { _id: this.importedId },
  622. {$set: {spentTime}}
  623. );
  624. } else if (this.isImportedBoard()) {
  625. return Boards.update(
  626. {_id: this.importedId},
  627. {$set: {spentTime}}
  628. );
  629. } else {
  630. return Cards.update(
  631. {_id: this._id},
  632. {$set: {spentTime}}
  633. );
  634. }
  635. },
  636. getTitle() {
  637. if (this.isImportedCard()) {
  638. const card = Cards.findOne({ _id: this.importedId });
  639. return card.title;
  640. } else if (this.isImportedBoard()) {
  641. const board = Boards.findOne({ _id: this.importedId});
  642. return board.title;
  643. } else {
  644. return this.title;
  645. }
  646. },
  647. setTitle(title) {
  648. if (this.isImportedCard()) {
  649. return Cards.update(
  650. { _id: this.importedId },
  651. {$set: {title}}
  652. );
  653. } else if (this.isImportedBoard()) {
  654. return Boards.update(
  655. {_id: this.importedId},
  656. {$set: {title}}
  657. );
  658. } else {
  659. return Cards.update(
  660. {_id: this._id},
  661. {$set: {title}}
  662. );
  663. }
  664. },
  665. getArchived() {
  666. if (this.isImportedCard()) {
  667. const card = Cards.findOne({ _id: this.importedId });
  668. return card.archived;
  669. } else if (this.isImportedBoard()) {
  670. const board = Boards.findOne({ _id: this.importedId});
  671. return board.archived;
  672. } else {
  673. return this.archived;
  674. }
  675. },
  676. });
  677. Cards.mutations({
  678. applyToChildren(funct) {
  679. Cards.find({ parentId: this._id }).forEach((card) => {
  680. funct(card);
  681. });
  682. },
  683. archive() {
  684. this.applyToChildren((card) => { return card.archive(); });
  685. return {$set: {archived: true}};
  686. },
  687. restore() {
  688. this.applyToChildren((card) => { return card.restore(); });
  689. return {$set: {archived: false}};
  690. },
  691. move(swimlaneId, listId, sortIndex) {
  692. const list = Lists.findOne(listId);
  693. const mutatedFields = {
  694. swimlaneId,
  695. listId,
  696. boardId: list.boardId,
  697. sort: sortIndex,
  698. };
  699. return {$set: mutatedFields};
  700. },
  701. addLabel(labelId) {
  702. return {$addToSet: {labelIds: labelId}};
  703. },
  704. removeLabel(labelId) {
  705. return {$pull: {labelIds: labelId}};
  706. },
  707. toggleLabel(labelId) {
  708. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  709. return this.removeLabel(labelId);
  710. } else {
  711. return this.addLabel(labelId);
  712. }
  713. },
  714. assignCustomField(customFieldId) {
  715. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  716. },
  717. unassignCustomField(customFieldId) {
  718. return {$pull: {customFields: {_id: customFieldId}}};
  719. },
  720. toggleCustomField(customFieldId) {
  721. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  722. return this.unassignCustomField(customFieldId);
  723. } else {
  724. return this.assignCustomField(customFieldId);
  725. }
  726. },
  727. setCustomField(customFieldId, value) {
  728. // todo
  729. const index = this.customFieldIndex(customFieldId);
  730. if (index > -1) {
  731. const update = {$set: {}};
  732. update.$set[`customFields.${index}.value`] = value;
  733. return update;
  734. }
  735. // TODO
  736. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  737. return null;
  738. },
  739. setCover(coverId) {
  740. return {$set: {coverId}};
  741. },
  742. unsetCover() {
  743. return {$unset: {coverId: ''}};
  744. },
  745. setParentId(parentId) {
  746. return {$set: {parentId}};
  747. },
  748. });
  749. //FUNCTIONS FOR creation of Activities
  750. function cardMove(userId, doc, fieldNames, oldListId) {
  751. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  752. Activities.insert({
  753. userId,
  754. oldListId,
  755. activityType: 'moveCard',
  756. listId: doc.listId,
  757. boardId: doc.boardId,
  758. cardId: doc._id,
  759. });
  760. }
  761. }
  762. function cardState(userId, doc, fieldNames) {
  763. if (_.contains(fieldNames, 'archived')) {
  764. if (doc.archived) {
  765. Activities.insert({
  766. userId,
  767. activityType: 'archivedCard',
  768. boardId: doc.boardId,
  769. listId: doc.listId,
  770. cardId: doc._id,
  771. });
  772. } else {
  773. Activities.insert({
  774. userId,
  775. activityType: 'restoredCard',
  776. boardId: doc.boardId,
  777. listId: doc.listId,
  778. cardId: doc._id,
  779. });
  780. }
  781. }
  782. }
  783. function cardMembers(userId, doc, fieldNames, modifier) {
  784. if (!_.contains(fieldNames, 'members'))
  785. return;
  786. let memberId;
  787. // Say hello to the new member
  788. if (modifier.$addToSet && modifier.$addToSet.members) {
  789. memberId = modifier.$addToSet.members;
  790. if (!_.contains(doc.members, memberId)) {
  791. Activities.insert({
  792. userId,
  793. memberId,
  794. activityType: 'joinMember',
  795. boardId: doc.boardId,
  796. cardId: doc._id,
  797. });
  798. }
  799. }
  800. // Say goodbye to the former member
  801. if (modifier.$pull && modifier.$pull.members) {
  802. memberId = modifier.$pull.members;
  803. // Check that the former member is member of the card
  804. if (_.contains(doc.members, memberId)) {
  805. Activities.insert({
  806. userId,
  807. memberId,
  808. activityType: 'unjoinMember',
  809. boardId: doc.boardId,
  810. cardId: doc._id,
  811. });
  812. }
  813. }
  814. }
  815. function cardCreation(userId, doc) {
  816. Activities.insert({
  817. userId,
  818. activityType: 'createCard',
  819. boardId: doc.boardId,
  820. listId: doc.listId,
  821. cardId: doc._id,
  822. });
  823. }
  824. function cardRemover(userId, doc) {
  825. Activities.remove({
  826. cardId: doc._id,
  827. });
  828. Checklists.remove({
  829. cardId: doc._id,
  830. });
  831. Subtasks.remove({
  832. cardId: doc._id,
  833. });
  834. CardComments.remove({
  835. cardId: doc._id,
  836. });
  837. Attachments.remove({
  838. cardId: doc._id,
  839. });
  840. }
  841. if (Meteor.isServer) {
  842. // Cards are often fetched within a board, so we create an index to make these
  843. // queries more efficient.
  844. Meteor.startup(() => {
  845. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  846. });
  847. Cards.after.insert((userId, doc) => {
  848. cardCreation(userId, doc);
  849. });
  850. // New activity for card (un)archivage
  851. Cards.after.update((userId, doc, fieldNames) => {
  852. cardState(userId, doc, fieldNames);
  853. });
  854. //New activity for card moves
  855. Cards.after.update(function (userId, doc, fieldNames) {
  856. const oldListId = this.previous.listId;
  857. cardMove(userId, doc, fieldNames, oldListId);
  858. });
  859. // Add a new activity if we add or remove a member to the card
  860. Cards.before.update((userId, doc, fieldNames, modifier) => {
  861. cardMembers(userId, doc, fieldNames, modifier);
  862. });
  863. // Remove all activities associated with a card if we remove the card
  864. // Remove also card_comments / checklists / attachments
  865. Cards.after.remove((userId, doc) => {
  866. cardRemover(userId, doc);
  867. });
  868. }
  869. //LISTS REST API
  870. if (Meteor.isServer) {
  871. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  872. const paramBoardId = req.params.boardId;
  873. const paramListId = req.params.listId;
  874. Authentication.checkBoardAccess(req.userId, paramBoardId);
  875. JsonRoutes.sendResult(res, {
  876. code: 200,
  877. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  878. return {
  879. _id: doc._id,
  880. title: doc.title,
  881. description: doc.description,
  882. };
  883. }),
  884. });
  885. });
  886. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  887. const paramBoardId = req.params.boardId;
  888. const paramListId = req.params.listId;
  889. const paramCardId = req.params.cardId;
  890. Authentication.checkBoardAccess(req.userId, paramBoardId);
  891. JsonRoutes.sendResult(res, {
  892. code: 200,
  893. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  894. });
  895. });
  896. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  897. Authentication.checkUserId(req.userId);
  898. const paramBoardId = req.params.boardId;
  899. const paramListId = req.params.listId;
  900. const check = Users.findOne({_id: req.body.authorId});
  901. const members = req.body.members || [req.body.authorId];
  902. if (typeof check !== 'undefined') {
  903. const id = Cards.direct.insert({
  904. title: req.body.title,
  905. boardId: paramBoardId,
  906. listId: paramListId,
  907. description: req.body.description,
  908. userId: req.body.authorId,
  909. swimlaneId: req.body.swimlaneId,
  910. sort: 0,
  911. members,
  912. });
  913. JsonRoutes.sendResult(res, {
  914. code: 200,
  915. data: {
  916. _id: id,
  917. },
  918. });
  919. const card = Cards.findOne({_id:id});
  920. cardCreation(req.body.authorId, card);
  921. } else {
  922. JsonRoutes.sendResult(res, {
  923. code: 401,
  924. });
  925. }
  926. });
  927. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  928. Authentication.checkUserId(req.userId);
  929. const paramBoardId = req.params.boardId;
  930. const paramCardId = req.params.cardId;
  931. const paramListId = req.params.listId;
  932. if (req.body.hasOwnProperty('title')) {
  933. const newTitle = req.body.title;
  934. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  935. {$set: {title: newTitle}});
  936. }
  937. if (req.body.hasOwnProperty('listId')) {
  938. const newParamListId = req.body.listId;
  939. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  940. {$set: {listId: newParamListId}});
  941. const card = Cards.findOne({_id: paramCardId} );
  942. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  943. }
  944. if (req.body.hasOwnProperty('description')) {
  945. const newDescription = req.body.description;
  946. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  947. {$set: {description: newDescription}});
  948. }
  949. if (req.body.hasOwnProperty('labelIds')) {
  950. const newlabelIds = req.body.labelIds;
  951. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  952. {$set: {labelIds: newlabelIds}});
  953. }
  954. JsonRoutes.sendResult(res, {
  955. code: 200,
  956. data: {
  957. _id: paramCardId,
  958. },
  959. });
  960. });
  961. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  962. Authentication.checkUserId(req.userId);
  963. const paramBoardId = req.params.boardId;
  964. const paramListId = req.params.listId;
  965. const paramCardId = req.params.cardId;
  966. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  967. const card = Cards.find({_id: paramCardId} );
  968. cardRemover(req.body.authorId, card);
  969. JsonRoutes.sendResult(res, {
  970. code: 200,
  971. data: {
  972. _id: paramCardId,
  973. },
  974. });
  975. });
  976. }