cards.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. });
  710. Cards.mutations({
  711. applyToChildren(funct) {
  712. Cards.find({ parentId: this._id }).forEach((card) => {
  713. funct(card);
  714. });
  715. },
  716. archive() {
  717. this.applyToChildren((card) => { return card.archive(); });
  718. return {$set: {archived: true}};
  719. },
  720. restore() {
  721. this.applyToChildren((card) => { return card.restore(); });
  722. return {$set: {archived: false}};
  723. },
  724. move(swimlaneId, listId, sortIndex) {
  725. const list = Lists.findOne(listId);
  726. const mutatedFields = {
  727. swimlaneId,
  728. listId,
  729. boardId: list.boardId,
  730. sort: sortIndex,
  731. };
  732. return {$set: mutatedFields};
  733. },
  734. addLabel(labelId) {
  735. return {$addToSet: {labelIds: labelId}};
  736. },
  737. removeLabel(labelId) {
  738. return {$pull: {labelIds: labelId}};
  739. },
  740. toggleLabel(labelId) {
  741. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  742. return this.removeLabel(labelId);
  743. } else {
  744. return this.addLabel(labelId);
  745. }
  746. },
  747. assignCustomField(customFieldId) {
  748. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  749. },
  750. unassignCustomField(customFieldId) {
  751. return {$pull: {customFields: {_id: customFieldId}}};
  752. },
  753. toggleCustomField(customFieldId) {
  754. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  755. return this.unassignCustomField(customFieldId);
  756. } else {
  757. return this.assignCustomField(customFieldId);
  758. }
  759. },
  760. setCustomField(customFieldId, value) {
  761. // todo
  762. const index = this.customFieldIndex(customFieldId);
  763. if (index > -1) {
  764. const update = {$set: {}};
  765. update.$set[`customFields.${index}.value`] = value;
  766. return update;
  767. }
  768. // TODO
  769. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  770. return null;
  771. },
  772. setCover(coverId) {
  773. return {$set: {coverId}};
  774. },
  775. unsetCover() {
  776. return {$unset: {coverId: ''}};
  777. },
  778. setParentId(parentId) {
  779. return {$set: {parentId}};
  780. },
  781. });
  782. //FUNCTIONS FOR creation of Activities
  783. function cardMove(userId, doc, fieldNames, oldListId) {
  784. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  785. Activities.insert({
  786. userId,
  787. oldListId,
  788. activityType: 'moveCard',
  789. listId: doc.listId,
  790. boardId: doc.boardId,
  791. cardId: doc._id,
  792. });
  793. }
  794. }
  795. function cardState(userId, doc, fieldNames) {
  796. if (_.contains(fieldNames, 'archived')) {
  797. if (doc.archived) {
  798. Activities.insert({
  799. userId,
  800. activityType: 'archivedCard',
  801. boardId: doc.boardId,
  802. listId: doc.listId,
  803. cardId: doc._id,
  804. });
  805. } else {
  806. Activities.insert({
  807. userId,
  808. activityType: 'restoredCard',
  809. boardId: doc.boardId,
  810. listId: doc.listId,
  811. cardId: doc._id,
  812. });
  813. }
  814. }
  815. }
  816. function cardMembers(userId, doc, fieldNames, modifier) {
  817. if (!_.contains(fieldNames, 'members'))
  818. return;
  819. let memberId;
  820. // Say hello to the new member
  821. if (modifier.$addToSet && modifier.$addToSet.members) {
  822. memberId = modifier.$addToSet.members;
  823. if (!_.contains(doc.members, memberId)) {
  824. Activities.insert({
  825. userId,
  826. memberId,
  827. activityType: 'joinMember',
  828. boardId: doc.boardId,
  829. cardId: doc._id,
  830. });
  831. }
  832. }
  833. // Say goodbye to the former member
  834. if (modifier.$pull && modifier.$pull.members) {
  835. memberId = modifier.$pull.members;
  836. // Check that the former member is member of the card
  837. if (_.contains(doc.members, memberId)) {
  838. Activities.insert({
  839. userId,
  840. memberId,
  841. activityType: 'unjoinMember',
  842. boardId: doc.boardId,
  843. cardId: doc._id,
  844. });
  845. }
  846. }
  847. }
  848. function cardCreation(userId, doc) {
  849. Activities.insert({
  850. userId,
  851. activityType: 'createCard',
  852. boardId: doc.boardId,
  853. listId: doc.listId,
  854. cardId: doc._id,
  855. });
  856. }
  857. function cardRemover(userId, doc) {
  858. Activities.remove({
  859. cardId: doc._id,
  860. });
  861. Checklists.remove({
  862. cardId: doc._id,
  863. });
  864. Subtasks.remove({
  865. cardId: doc._id,
  866. });
  867. CardComments.remove({
  868. cardId: doc._id,
  869. });
  870. Attachments.remove({
  871. cardId: doc._id,
  872. });
  873. }
  874. if (Meteor.isServer) {
  875. // Cards are often fetched within a board, so we create an index to make these
  876. // queries more efficient.
  877. Meteor.startup(() => {
  878. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  879. });
  880. Cards.after.insert((userId, doc) => {
  881. cardCreation(userId, doc);
  882. });
  883. // New activity for card (un)archivage
  884. Cards.after.update((userId, doc, fieldNames) => {
  885. cardState(userId, doc, fieldNames);
  886. });
  887. //New activity for card moves
  888. Cards.after.update(function (userId, doc, fieldNames) {
  889. const oldListId = this.previous.listId;
  890. cardMove(userId, doc, fieldNames, oldListId);
  891. });
  892. // Add a new activity if we add or remove a member to the card
  893. Cards.before.update((userId, doc, fieldNames, modifier) => {
  894. cardMembers(userId, doc, fieldNames, modifier);
  895. });
  896. // Remove all activities associated with a card if we remove the card
  897. // Remove also card_comments / checklists / attachments
  898. Cards.after.remove((userId, doc) => {
  899. cardRemover(userId, doc);
  900. });
  901. }
  902. //LISTS REST API
  903. if (Meteor.isServer) {
  904. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  905. const paramBoardId = req.params.boardId;
  906. const paramListId = req.params.listId;
  907. Authentication.checkBoardAccess(req.userId, paramBoardId);
  908. JsonRoutes.sendResult(res, {
  909. code: 200,
  910. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  911. return {
  912. _id: doc._id,
  913. title: doc.title,
  914. description: doc.description,
  915. };
  916. }),
  917. });
  918. });
  919. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  920. const paramBoardId = req.params.boardId;
  921. const paramListId = req.params.listId;
  922. const paramCardId = req.params.cardId;
  923. Authentication.checkBoardAccess(req.userId, paramBoardId);
  924. JsonRoutes.sendResult(res, {
  925. code: 200,
  926. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  927. });
  928. });
  929. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  930. Authentication.checkUserId(req.userId);
  931. const paramBoardId = req.params.boardId;
  932. const paramListId = req.params.listId;
  933. const check = Users.findOne({_id: req.body.authorId});
  934. const members = req.body.members || [req.body.authorId];
  935. if (typeof check !== 'undefined') {
  936. const id = Cards.direct.insert({
  937. title: req.body.title,
  938. boardId: paramBoardId,
  939. listId: paramListId,
  940. description: req.body.description,
  941. userId: req.body.authorId,
  942. swimlaneId: req.body.swimlaneId,
  943. sort: 0,
  944. members,
  945. });
  946. JsonRoutes.sendResult(res, {
  947. code: 200,
  948. data: {
  949. _id: id,
  950. },
  951. });
  952. const card = Cards.findOne({_id:id});
  953. cardCreation(req.body.authorId, card);
  954. } else {
  955. JsonRoutes.sendResult(res, {
  956. code: 401,
  957. });
  958. }
  959. });
  960. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  961. Authentication.checkUserId(req.userId);
  962. const paramBoardId = req.params.boardId;
  963. const paramCardId = req.params.cardId;
  964. const paramListId = req.params.listId;
  965. if (req.body.hasOwnProperty('title')) {
  966. const newTitle = req.body.title;
  967. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  968. {$set: {title: newTitle}});
  969. }
  970. if (req.body.hasOwnProperty('listId')) {
  971. const newParamListId = req.body.listId;
  972. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  973. {$set: {listId: newParamListId}});
  974. const card = Cards.findOne({_id: paramCardId} );
  975. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  976. }
  977. if (req.body.hasOwnProperty('description')) {
  978. const newDescription = req.body.description;
  979. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  980. {$set: {description: newDescription}});
  981. }
  982. if (req.body.hasOwnProperty('labelIds')) {
  983. const newlabelIds = req.body.labelIds;
  984. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  985. {$set: {labelIds: newlabelIds}});
  986. }
  987. JsonRoutes.sendResult(res, {
  988. code: 200,
  989. data: {
  990. _id: paramCardId,
  991. },
  992. });
  993. });
  994. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  995. Authentication.checkUserId(req.userId);
  996. const paramBoardId = req.params.boardId;
  997. const paramListId = req.params.listId;
  998. const paramCardId = req.params.cardId;
  999. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  1000. const card = Cards.find({_id: paramCardId} );
  1001. cardRemover(req.body.authorId, card);
  1002. JsonRoutes.sendResult(res, {
  1003. code: 200,
  1004. data: {
  1005. _id: paramCardId,
  1006. },
  1007. });
  1008. });
  1009. }