cards.js 27 KB

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