cards.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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. /**
  8. * the title of the card
  9. */
  10. type: String,
  11. optional: true,
  12. defaultValue: '',
  13. },
  14. archived: {
  15. /**
  16. * is the card archived
  17. */
  18. type: Boolean,
  19. autoValue() { // eslint-disable-line consistent-return
  20. if (this.isInsert && !this.isSet) {
  21. return false;
  22. }
  23. },
  24. },
  25. parentId: {
  26. /**
  27. * ID of the parent card
  28. */
  29. type: String,
  30. optional: true,
  31. defaultValue: '',
  32. },
  33. listId: {
  34. /**
  35. * List ID where the card is
  36. */
  37. type: String,
  38. optional: true,
  39. defaultValue: '',
  40. },
  41. swimlaneId: {
  42. /**
  43. * Swimlane ID where the card is
  44. */
  45. type: String,
  46. },
  47. // The system could work without this `boardId` information (we could deduce
  48. // the board identifier from the card), but it would make the system more
  49. // difficult to manage and less efficient.
  50. boardId: {
  51. /**
  52. * Board ID of the card
  53. */
  54. type: String,
  55. optional: true,
  56. defaultValue: '',
  57. },
  58. coverId: {
  59. /**
  60. * Cover ID of the card
  61. */
  62. type: String,
  63. optional: true,
  64. defaultValue: '',
  65. },
  66. color: {
  67. type: String,
  68. optional: true,
  69. allowedValues: [
  70. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  71. 'blue', 'sky', 'lime', 'pink', 'black',
  72. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen',
  73. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  74. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  75. ],
  76. },
  77. createdAt: {
  78. /**
  79. * creation date
  80. */
  81. type: Date,
  82. autoValue() { // eslint-disable-line consistent-return
  83. if (this.isInsert) {
  84. return new Date();
  85. } else {
  86. this.unset();
  87. }
  88. },
  89. },
  90. customFields: {
  91. /**
  92. * list of custom fields
  93. */
  94. type: [Object],
  95. optional: true,
  96. defaultValue: [],
  97. },
  98. 'customFields.$': {
  99. type: new SimpleSchema({
  100. _id: {
  101. /**
  102. * the ID of the related custom field
  103. */
  104. type: String,
  105. optional: true,
  106. defaultValue: '',
  107. },
  108. value: {
  109. /**
  110. * value attached to the custom field
  111. */
  112. type: Match.OneOf(String, Number, Boolean, Date),
  113. optional: true,
  114. defaultValue: '',
  115. },
  116. }),
  117. },
  118. dateLastActivity: {
  119. /**
  120. * Date of last activity
  121. */
  122. type: Date,
  123. autoValue() {
  124. return new Date();
  125. },
  126. },
  127. description: {
  128. /**
  129. * description of the card
  130. */
  131. type: String,
  132. optional: true,
  133. defaultValue: '',
  134. },
  135. requestedBy: {
  136. /**
  137. * who requested the card (ID of the user)
  138. */
  139. type: String,
  140. optional: true,
  141. defaultValue: '',
  142. },
  143. assignedBy: {
  144. /**
  145. * who assigned the card (ID of the user)
  146. */
  147. type: String,
  148. optional: true,
  149. defaultValue: '',
  150. },
  151. labelIds: {
  152. /**
  153. * list of labels ID the card has
  154. */
  155. type: [String],
  156. optional: true,
  157. defaultValue: [],
  158. },
  159. members: {
  160. /**
  161. * list of members (user IDs)
  162. */
  163. type: [String],
  164. optional: true,
  165. defaultValue: [],
  166. },
  167. receivedAt: {
  168. /**
  169. * Date the card was received
  170. */
  171. type: Date,
  172. optional: true,
  173. },
  174. startAt: {
  175. /**
  176. * Date the card was started to be worked on
  177. */
  178. type: Date,
  179. optional: true,
  180. },
  181. dueAt: {
  182. /**
  183. * Date the card is due
  184. */
  185. type: Date,
  186. optional: true,
  187. },
  188. endAt: {
  189. /**
  190. * Date the card ended
  191. */
  192. type: Date,
  193. optional: true,
  194. },
  195. spentTime: {
  196. /**
  197. * How much time has been spent on this
  198. */
  199. type: Number,
  200. decimal: true,
  201. optional: true,
  202. defaultValue: 0,
  203. },
  204. isOvertime: {
  205. /**
  206. * is the card over time?
  207. */
  208. type: Boolean,
  209. defaultValue: false,
  210. optional: true,
  211. },
  212. // XXX Should probably be called `authorId`. Is it even needed since we have
  213. // the `members` field?
  214. userId: {
  215. /**
  216. * user ID of the author of the card
  217. */
  218. type: String,
  219. autoValue() { // eslint-disable-line consistent-return
  220. if (this.isInsert && !this.isSet) {
  221. return this.userId;
  222. }
  223. },
  224. },
  225. sort: {
  226. /**
  227. * Sort value
  228. */
  229. type: Number,
  230. decimal: true,
  231. defaultValue: '',
  232. },
  233. subtaskSort: {
  234. /**
  235. * subtask sort value
  236. */
  237. type: Number,
  238. decimal: true,
  239. defaultValue: -1,
  240. optional: true,
  241. },
  242. type: {
  243. /**
  244. * type of the card
  245. */
  246. type: String,
  247. defaultValue: 'cardType-card',
  248. },
  249. linkedId: {
  250. /**
  251. * ID of the linked card
  252. */
  253. type: String,
  254. optional: true,
  255. defaultValue: '',
  256. },
  257. }));
  258. Cards.allow({
  259. insert(userId, doc) {
  260. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  261. },
  262. update(userId, doc) {
  263. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  264. },
  265. remove(userId, doc) {
  266. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  267. },
  268. fetch: ['boardId'],
  269. });
  270. Cards.helpers({
  271. copy() {
  272. const oldId = this._id;
  273. delete this._id;
  274. const _id = Cards.insert(this);
  275. // copy checklists
  276. Checklists.find({cardId: oldId}).forEach((ch) => {
  277. ch.copy(_id);
  278. });
  279. // copy subtasks
  280. Cards.find({parentId: oldId}).forEach((subtask) => {
  281. subtask.parentId = _id;
  282. subtask._id = null;
  283. Cards.insert(subtask);
  284. });
  285. // copy card comments
  286. CardComments.find({cardId: oldId}).forEach((cmt) => {
  287. cmt.copy(_id);
  288. });
  289. return _id;
  290. },
  291. list() {
  292. return Lists.findOne(this.listId);
  293. },
  294. board() {
  295. return Boards.findOne(this.boardId);
  296. },
  297. labels() {
  298. const boardLabels = this.board().labels;
  299. const cardLabels = _.filter(boardLabels, (label) => {
  300. return _.contains(this.labelIds, label._id);
  301. });
  302. return cardLabels;
  303. },
  304. hasLabel(labelId) {
  305. return _.contains(this.labelIds, labelId);
  306. },
  307. user() {
  308. return Users.findOne(this.userId);
  309. },
  310. isAssigned(memberId) {
  311. return _.contains(this.getMembers(), memberId);
  312. },
  313. activities() {
  314. if (this.isLinkedCard()) {
  315. return Activities.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  316. } else if (this.isLinkedBoard()) {
  317. return Activities.find({boardId: this.linkedId}, {sort: {createdAt: -1}});
  318. } else {
  319. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  320. }
  321. },
  322. comments() {
  323. if (this.isLinkedCard()) {
  324. return CardComments.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  325. } else {
  326. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  327. }
  328. },
  329. attachments() {
  330. if (this.isLinkedCard()) {
  331. return Attachments.find({cardId: this.linkedId}, {sort: {uploadedAt: -1}});
  332. } else {
  333. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  334. }
  335. },
  336. cover() {
  337. const cover = Attachments.findOne(this.coverId);
  338. // if we return a cover before it is fully stored, we will get errors when we try to display it
  339. // todo XXX we could return a default "upload pending" image in the meantime?
  340. return cover && cover.url() && cover;
  341. },
  342. checklists() {
  343. if (this.isLinkedCard()) {
  344. return Checklists.find({cardId: this.linkedId}, {sort: { sort: 1 } });
  345. } else {
  346. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  347. }
  348. },
  349. checklistItemCount() {
  350. const checklists = this.checklists().fetch();
  351. return checklists.map((checklist) => {
  352. return checklist.itemCount();
  353. }).reduce((prev, next) => {
  354. return prev + next;
  355. }, 0);
  356. },
  357. checklistFinishedCount() {
  358. const checklists = this.checklists().fetch();
  359. return checklists.map((checklist) => {
  360. return checklist.finishedCount();
  361. }).reduce((prev, next) => {
  362. return prev + next;
  363. }, 0);
  364. },
  365. checklistFinished() {
  366. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  367. },
  368. hasChecklist() {
  369. return this.checklistItemCount() !== 0;
  370. },
  371. subtasks() {
  372. return Cards.find({
  373. parentId: this._id,
  374. archived: false,
  375. }, {
  376. sort: {
  377. sort: 1,
  378. },
  379. });
  380. },
  381. allSubtasks() {
  382. return Cards.find({
  383. parentId: this._id,
  384. archived: false,
  385. }, {
  386. sort: {
  387. sort: 1,
  388. },
  389. });
  390. },
  391. subtasksCount() {
  392. return Cards.find({
  393. parentId: this._id,
  394. archived: false,
  395. }).count();
  396. },
  397. subtasksFinishedCount() {
  398. return Cards.find({
  399. parentId: this._id,
  400. archived: true,
  401. }).count();
  402. },
  403. subtasksFinished() {
  404. const finishCount = this.subtasksFinishedCount();
  405. return finishCount > 0 && this.subtasksCount() === finishCount;
  406. },
  407. allowsSubtasks() {
  408. return this.subtasksCount() !== 0;
  409. },
  410. customFieldIndex(customFieldId) {
  411. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  412. },
  413. // customFields with definitions
  414. customFieldsWD() {
  415. // get all definitions
  416. const definitions = CustomFields.find({
  417. boardId: this.boardId,
  418. }).fetch();
  419. // match right definition to each field
  420. if (!this.customFields) return [];
  421. return this.customFields.map((customField) => {
  422. const definition = definitions.find((definition) => {
  423. return definition._id === customField._id;
  424. });
  425. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  426. let trueValue = customField.value;
  427. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0) {
  428. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  429. if (definition.settings.dropdownItems[i]._id === customField.value) {
  430. trueValue = definition.settings.dropdownItems[i].name;
  431. }
  432. }
  433. }
  434. return {
  435. _id: customField._id,
  436. value: customField.value,
  437. trueValue,
  438. definition,
  439. };
  440. });
  441. },
  442. colorClass() {
  443. if (this.color)
  444. return this.color;
  445. return '';
  446. },
  447. absoluteUrl() {
  448. const board = this.board();
  449. return FlowRouter.url('card', {
  450. boardId: board._id,
  451. slug: board.slug,
  452. cardId: this._id,
  453. });
  454. },
  455. canBeRestored() {
  456. const list = Lists.findOne({
  457. _id: this.listId,
  458. });
  459. if (!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()) {
  460. return false;
  461. }
  462. return true;
  463. },
  464. parentCard() {
  465. if (this.parentId === '') {
  466. return null;
  467. }
  468. return Cards.findOne(this.parentId);
  469. },
  470. parentCardName() {
  471. let result = '';
  472. if (this.parentId !== '') {
  473. const card = Cards.findOne(this.parentId);
  474. if (card) {
  475. result = card.title;
  476. }
  477. }
  478. return result;
  479. },
  480. parentListId() {
  481. const result = [];
  482. let crtParentId = this.parentId;
  483. while (crtParentId !== '') {
  484. const crt = Cards.findOne(crtParentId);
  485. if ((crt === null) || (crt === undefined)) {
  486. // maybe it has been deleted
  487. break;
  488. }
  489. if (crtParentId in result) {
  490. // circular reference
  491. break;
  492. }
  493. result.unshift(crtParentId);
  494. crtParentId = crt.parentId;
  495. }
  496. return result;
  497. },
  498. parentList() {
  499. const resultId = [];
  500. const result = [];
  501. let crtParentId = this.parentId;
  502. while (crtParentId !== '') {
  503. const crt = Cards.findOne(crtParentId);
  504. if ((crt === null) || (crt === undefined)) {
  505. // maybe it has been deleted
  506. break;
  507. }
  508. if (crtParentId in resultId) {
  509. // circular reference
  510. break;
  511. }
  512. resultId.unshift(crtParentId);
  513. result.unshift(crt);
  514. crtParentId = crt.parentId;
  515. }
  516. return result;
  517. },
  518. parentString(sep) {
  519. return this.parentList().map(function(elem) {
  520. return elem.title;
  521. }).join(sep);
  522. },
  523. isTopLevel() {
  524. return this.parentId === '';
  525. },
  526. isLinkedCard() {
  527. return this.type === 'cardType-linkedCard';
  528. },
  529. isLinkedBoard() {
  530. return this.type === 'cardType-linkedBoard';
  531. },
  532. isLinked() {
  533. return this.isLinkedCard() || this.isLinkedBoard();
  534. },
  535. setDescription(description) {
  536. if (this.isLinkedCard()) {
  537. return Cards.update({_id: this.linkedId}, {$set: {description}});
  538. } else if (this.isLinkedBoard()) {
  539. return Boards.update({_id: this.linkedId}, {$set: {description}});
  540. } else {
  541. return Cards.update(
  542. {_id: this._id},
  543. {$set: {description}}
  544. );
  545. }
  546. },
  547. getDescription() {
  548. if (this.isLinkedCard()) {
  549. const card = Cards.findOne({_id: this.linkedId});
  550. if (card && card.description)
  551. return card.description;
  552. else
  553. return null;
  554. } else if (this.isLinkedBoard()) {
  555. const board = Boards.findOne({_id: this.linkedId});
  556. if (board && board.description)
  557. return board.description;
  558. else
  559. return null;
  560. } else if (this.description) {
  561. return this.description;
  562. } else {
  563. return null;
  564. }
  565. },
  566. getMembers() {
  567. if (this.isLinkedCard()) {
  568. const card = Cards.findOne({_id: this.linkedId});
  569. return card.members;
  570. } else if (this.isLinkedBoard()) {
  571. const board = Boards.findOne({_id: this.linkedId});
  572. return board.activeMembers().map((member) => {
  573. return member.userId;
  574. });
  575. } else {
  576. return this.members;
  577. }
  578. },
  579. assignMember(memberId) {
  580. if (this.isLinkedCard()) {
  581. return Cards.update(
  582. { _id: this.linkedId },
  583. { $addToSet: { members: memberId }}
  584. );
  585. } else if (this.isLinkedBoard()) {
  586. const board = Boards.findOne({_id: this.linkedId});
  587. return board.addMember(memberId);
  588. } else {
  589. return Cards.update(
  590. { _id: this._id },
  591. { $addToSet: { members: memberId}}
  592. );
  593. }
  594. },
  595. unassignMember(memberId) {
  596. if (this.isLinkedCard()) {
  597. return Cards.update(
  598. { _id: this.linkedId },
  599. { $pull: { members: memberId }}
  600. );
  601. } else if (this.isLinkedBoard()) {
  602. const board = Boards.findOne({_id: this.linkedId});
  603. return board.removeMember(memberId);
  604. } else {
  605. return Cards.update(
  606. { _id: this._id },
  607. { $pull: { members: memberId}}
  608. );
  609. }
  610. },
  611. toggleMember(memberId) {
  612. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  613. return this.unassignMember(memberId);
  614. } else {
  615. return this.assignMember(memberId);
  616. }
  617. },
  618. getReceived() {
  619. if (this.isLinkedCard()) {
  620. const card = Cards.findOne({_id: this.linkedId});
  621. return card.receivedAt;
  622. } else {
  623. return this.receivedAt;
  624. }
  625. },
  626. setReceived(receivedAt) {
  627. if (this.isLinkedCard()) {
  628. return Cards.update(
  629. {_id: this.linkedId},
  630. {$set: {receivedAt}}
  631. );
  632. } else {
  633. return Cards.update(
  634. {_id: this._id},
  635. {$set: {receivedAt}}
  636. );
  637. }
  638. },
  639. getStart() {
  640. if (this.isLinkedCard()) {
  641. const card = Cards.findOne({_id: this.linkedId});
  642. return card.startAt;
  643. } else if (this.isLinkedBoard()) {
  644. const board = Boards.findOne({_id: this.linkedId});
  645. return board.startAt;
  646. } else {
  647. return this.startAt;
  648. }
  649. },
  650. setStart(startAt) {
  651. if (this.isLinkedCard()) {
  652. return Cards.update(
  653. { _id: this.linkedId },
  654. {$set: {startAt}}
  655. );
  656. } else if (this.isLinkedBoard()) {
  657. return Boards.update(
  658. {_id: this.linkedId},
  659. {$set: {startAt}}
  660. );
  661. } else {
  662. return Cards.update(
  663. {_id: this._id},
  664. {$set: {startAt}}
  665. );
  666. }
  667. },
  668. getDue() {
  669. if (this.isLinkedCard()) {
  670. const card = Cards.findOne({_id: this.linkedId});
  671. return card.dueAt;
  672. } else if (this.isLinkedBoard()) {
  673. const board = Boards.findOne({_id: this.linkedId});
  674. return board.dueAt;
  675. } else {
  676. return this.dueAt;
  677. }
  678. },
  679. setDue(dueAt) {
  680. if (this.isLinkedCard()) {
  681. return Cards.update(
  682. { _id: this.linkedId },
  683. {$set: {dueAt}}
  684. );
  685. } else if (this.isLinkedBoard()) {
  686. return Boards.update(
  687. {_id: this.linkedId},
  688. {$set: {dueAt}}
  689. );
  690. } else {
  691. return Cards.update(
  692. {_id: this._id},
  693. {$set: {dueAt}}
  694. );
  695. }
  696. },
  697. getEnd() {
  698. if (this.isLinkedCard()) {
  699. const card = Cards.findOne({_id: this.linkedId});
  700. return card.endAt;
  701. } else if (this.isLinkedBoard()) {
  702. const board = Boards.findOne({_id: this.linkedId});
  703. return board.endAt;
  704. } else {
  705. return this.endAt;
  706. }
  707. },
  708. setEnd(endAt) {
  709. if (this.isLinkedCard()) {
  710. return Cards.update(
  711. { _id: this.linkedId },
  712. {$set: {endAt}}
  713. );
  714. } else if (this.isLinkedBoard()) {
  715. return Boards.update(
  716. {_id: this.linkedId},
  717. {$set: {endAt}}
  718. );
  719. } else {
  720. return Cards.update(
  721. {_id: this._id},
  722. {$set: {endAt}}
  723. );
  724. }
  725. },
  726. getIsOvertime() {
  727. if (this.isLinkedCard()) {
  728. const card = Cards.findOne({ _id: this.linkedId });
  729. return card.isOvertime;
  730. } else if (this.isLinkedBoard()) {
  731. const board = Boards.findOne({ _id: this.linkedId});
  732. return board.isOvertime;
  733. } else {
  734. return this.isOvertime;
  735. }
  736. },
  737. setIsOvertime(isOvertime) {
  738. if (this.isLinkedCard()) {
  739. return Cards.update(
  740. { _id: this.linkedId },
  741. {$set: {isOvertime}}
  742. );
  743. } else if (this.isLinkedBoard()) {
  744. return Boards.update(
  745. {_id: this.linkedId},
  746. {$set: {isOvertime}}
  747. );
  748. } else {
  749. return Cards.update(
  750. {_id: this._id},
  751. {$set: {isOvertime}}
  752. );
  753. }
  754. },
  755. getSpentTime() {
  756. if (this.isLinkedCard()) {
  757. const card = Cards.findOne({ _id: this.linkedId });
  758. return card.spentTime;
  759. } else if (this.isLinkedBoard()) {
  760. const board = Boards.findOne({ _id: this.linkedId});
  761. return board.spentTime;
  762. } else {
  763. return this.spentTime;
  764. }
  765. },
  766. setSpentTime(spentTime) {
  767. if (this.isLinkedCard()) {
  768. return Cards.update(
  769. { _id: this.linkedId },
  770. {$set: {spentTime}}
  771. );
  772. } else if (this.isLinkedBoard()) {
  773. return Boards.update(
  774. {_id: this.linkedId},
  775. {$set: {spentTime}}
  776. );
  777. } else {
  778. return Cards.update(
  779. {_id: this._id},
  780. {$set: {spentTime}}
  781. );
  782. }
  783. },
  784. getId() {
  785. if (this.isLinked()) {
  786. return this.linkedId;
  787. } else {
  788. return this._id;
  789. }
  790. },
  791. getTitle() {
  792. if (this.isLinkedCard()) {
  793. const card = Cards.findOne({ _id: this.linkedId });
  794. return card.title;
  795. } else if (this.isLinkedBoard()) {
  796. const board = Boards.findOne({ _id: this.linkedId});
  797. return board.title;
  798. } else {
  799. return this.title;
  800. }
  801. },
  802. getBoardTitle() {
  803. if (this.isLinkedCard()) {
  804. const card = Cards.findOne({ _id: this.linkedId });
  805. const board = Boards.findOne({ _id: card.boardId });
  806. return board.title;
  807. } else if (this.isLinkedBoard()) {
  808. const board = Boards.findOne({ _id: this.linkedId});
  809. return board.title;
  810. } else {
  811. const board = Boards.findOne({ _id: this.boardId });
  812. return board.title;
  813. }
  814. },
  815. setTitle(title) {
  816. if (this.isLinkedCard()) {
  817. return Cards.update(
  818. { _id: this.linkedId },
  819. {$set: {title}}
  820. );
  821. } else if (this.isLinkedBoard()) {
  822. return Boards.update(
  823. {_id: this.linkedId},
  824. {$set: {title}}
  825. );
  826. } else {
  827. return Cards.update(
  828. {_id: this._id},
  829. {$set: {title}}
  830. );
  831. }
  832. },
  833. getArchived() {
  834. if (this.isLinkedCard()) {
  835. const card = Cards.findOne({ _id: this.linkedId });
  836. return card.archived;
  837. } else if (this.isLinkedBoard()) {
  838. const board = Boards.findOne({ _id: this.linkedId});
  839. return board.archived;
  840. } else {
  841. return this.archived;
  842. }
  843. },
  844. setRequestedBy(requestedBy) {
  845. if (this.isLinkedCard()) {
  846. return Cards.update(
  847. { _id: this.linkedId },
  848. {$set: {requestedBy}}
  849. );
  850. } else {
  851. return Cards.update(
  852. {_id: this._id},
  853. {$set: {requestedBy}}
  854. );
  855. }
  856. },
  857. getRequestedBy() {
  858. if (this.isLinkedCard()) {
  859. const card = Cards.findOne({ _id: this.linkedId });
  860. return card.requestedBy;
  861. } else {
  862. return this.requestedBy;
  863. }
  864. },
  865. setAssignedBy(assignedBy) {
  866. if (this.isLinkedCard()) {
  867. return Cards.update(
  868. { _id: this.linkedId },
  869. {$set: {assignedBy}}
  870. );
  871. } else {
  872. return Cards.update(
  873. {_id: this._id},
  874. {$set: {assignedBy}}
  875. );
  876. }
  877. },
  878. getAssignedBy() {
  879. if (this.isLinkedCard()) {
  880. const card = Cards.findOne({ _id: this.linkedId });
  881. return card.assignedBy;
  882. } else {
  883. return this.assignedBy;
  884. }
  885. },
  886. isTemplateCard() {
  887. return this.type === 'template-card';
  888. },
  889. });
  890. Cards.mutations({
  891. applyToChildren(funct) {
  892. Cards.find({
  893. parentId: this._id,
  894. }).forEach((card) => {
  895. funct(card);
  896. });
  897. },
  898. archive() {
  899. this.applyToChildren((card) => {
  900. return card.archive();
  901. });
  902. return {
  903. $set: {
  904. archived: true,
  905. },
  906. };
  907. },
  908. restore() {
  909. this.applyToChildren((card) => {
  910. return card.restore();
  911. });
  912. return {
  913. $set: {
  914. archived: false,
  915. },
  916. };
  917. },
  918. setTitle(title) {
  919. return {
  920. $set: {
  921. title,
  922. },
  923. };
  924. },
  925. setDescription(description) {
  926. return {
  927. $set: {
  928. description,
  929. },
  930. };
  931. },
  932. setRequestedBy(requestedBy) {
  933. return {
  934. $set: {
  935. requestedBy,
  936. },
  937. };
  938. },
  939. setAssignedBy(assignedBy) {
  940. return {
  941. $set: {
  942. assignedBy,
  943. },
  944. };
  945. },
  946. move(swimlaneId, listId, sortIndex) {
  947. const list = Lists.findOne(listId);
  948. const mutatedFields = {
  949. swimlaneId,
  950. listId,
  951. boardId: list.boardId,
  952. sort: sortIndex,
  953. };
  954. return {
  955. $set: mutatedFields,
  956. };
  957. },
  958. addLabel(labelId) {
  959. return {
  960. $addToSet: {
  961. labelIds: labelId,
  962. },
  963. };
  964. },
  965. removeLabel(labelId) {
  966. return {
  967. $pull: {
  968. labelIds: labelId,
  969. },
  970. };
  971. },
  972. toggleLabel(labelId) {
  973. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  974. return this.removeLabel(labelId);
  975. } else {
  976. return this.addLabel(labelId);
  977. }
  978. },
  979. setColor(newColor) {
  980. if (newColor === 'white') {
  981. newColor = null;
  982. }
  983. return {
  984. $set: {
  985. color: newColor,
  986. },
  987. };
  988. },
  989. assignMember(memberId) {
  990. return {
  991. $addToSet: {
  992. members: memberId,
  993. },
  994. };
  995. },
  996. unassignMember(memberId) {
  997. return {
  998. $pull: {
  999. members: memberId,
  1000. },
  1001. };
  1002. },
  1003. toggleMember(memberId) {
  1004. if (this.members && this.members.indexOf(memberId) > -1) {
  1005. return this.unassignMember(memberId);
  1006. } else {
  1007. return this.assignMember(memberId);
  1008. }
  1009. },
  1010. assignCustomField(customFieldId) {
  1011. return {
  1012. $addToSet: {
  1013. customFields: {
  1014. _id: customFieldId,
  1015. value: null,
  1016. },
  1017. },
  1018. };
  1019. },
  1020. unassignCustomField(customFieldId) {
  1021. return {
  1022. $pull: {
  1023. customFields: {
  1024. _id: customFieldId,
  1025. },
  1026. },
  1027. };
  1028. },
  1029. toggleCustomField(customFieldId) {
  1030. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1031. return this.unassignCustomField(customFieldId);
  1032. } else {
  1033. return this.assignCustomField(customFieldId);
  1034. }
  1035. },
  1036. setCustomField(customFieldId, value) {
  1037. // todo
  1038. const index = this.customFieldIndex(customFieldId);
  1039. if (index > -1) {
  1040. const update = {
  1041. $set: {},
  1042. };
  1043. update.$set[`customFields.${index}.value`] = value;
  1044. return update;
  1045. }
  1046. // TODO
  1047. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1048. return null;
  1049. },
  1050. setCover(coverId) {
  1051. return {
  1052. $set: {
  1053. coverId,
  1054. },
  1055. };
  1056. },
  1057. unsetCover() {
  1058. return {
  1059. $unset: {
  1060. coverId: '',
  1061. },
  1062. };
  1063. },
  1064. setReceived(receivedAt) {
  1065. return {
  1066. $set: {
  1067. receivedAt,
  1068. },
  1069. };
  1070. },
  1071. unsetReceived() {
  1072. return {
  1073. $unset: {
  1074. receivedAt: '',
  1075. },
  1076. };
  1077. },
  1078. setStart(startAt) {
  1079. return {
  1080. $set: {
  1081. startAt,
  1082. },
  1083. };
  1084. },
  1085. unsetStart() {
  1086. return {
  1087. $unset: {
  1088. startAt: '',
  1089. },
  1090. };
  1091. },
  1092. setDue(dueAt) {
  1093. return {
  1094. $set: {
  1095. dueAt,
  1096. },
  1097. };
  1098. },
  1099. unsetDue() {
  1100. return {
  1101. $unset: {
  1102. dueAt: '',
  1103. },
  1104. };
  1105. },
  1106. setEnd(endAt) {
  1107. return {
  1108. $set: {
  1109. endAt,
  1110. },
  1111. };
  1112. },
  1113. unsetEnd() {
  1114. return {
  1115. $unset: {
  1116. endAt: '',
  1117. },
  1118. };
  1119. },
  1120. setOvertime(isOvertime) {
  1121. return {
  1122. $set: {
  1123. isOvertime,
  1124. },
  1125. };
  1126. },
  1127. setSpentTime(spentTime) {
  1128. return {
  1129. $set: {
  1130. spentTime,
  1131. },
  1132. };
  1133. },
  1134. unsetSpentTime() {
  1135. return {
  1136. $unset: {
  1137. spentTime: '',
  1138. isOvertime: false,
  1139. },
  1140. };
  1141. },
  1142. setParentId(parentId) {
  1143. return {
  1144. $set: {
  1145. parentId,
  1146. },
  1147. };
  1148. },
  1149. });
  1150. //FUNCTIONS FOR creation of Activities
  1151. function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId) {
  1152. if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1153. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
  1154. Activities.insert({
  1155. userId,
  1156. oldListId,
  1157. activityType: 'moveCard',
  1158. listName: Lists.findOne(doc.listId).title,
  1159. listId: doc.listId,
  1160. boardId: doc.boardId,
  1161. cardId: doc._id,
  1162. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1163. swimlaneId: doc.swimlaneId,
  1164. oldSwimlaneId,
  1165. });
  1166. }
  1167. }
  1168. function cardState(userId, doc, fieldNames) {
  1169. if (_.contains(fieldNames, 'archived')) {
  1170. if (doc.archived) {
  1171. Activities.insert({
  1172. userId,
  1173. activityType: 'archivedCard',
  1174. listName: Lists.findOne(doc.listId).title,
  1175. boardId: doc.boardId,
  1176. listId: doc.listId,
  1177. cardId: doc._id,
  1178. });
  1179. } else {
  1180. Activities.insert({
  1181. userId,
  1182. activityType: 'restoredCard',
  1183. boardId: doc.boardId,
  1184. listName: Lists.findOne(doc.listId).title,
  1185. listId: doc.listId,
  1186. cardId: doc._id,
  1187. });
  1188. }
  1189. }
  1190. }
  1191. function cardMembers(userId, doc, fieldNames, modifier) {
  1192. if (!_.contains(fieldNames, 'members'))
  1193. return;
  1194. let memberId;
  1195. // Say hello to the new member
  1196. if (modifier.$addToSet && modifier.$addToSet.members) {
  1197. memberId = modifier.$addToSet.members;
  1198. const username = Users.findOne(memberId).username;
  1199. if (!_.contains(doc.members, memberId)) {
  1200. Activities.insert({
  1201. userId,
  1202. username,
  1203. activityType: 'joinMember',
  1204. boardId: doc.boardId,
  1205. cardId: doc._id,
  1206. });
  1207. }
  1208. }
  1209. // Say goodbye to the former member
  1210. if (modifier.$pull && modifier.$pull.members) {
  1211. memberId = modifier.$pull.members;
  1212. const username = Users.findOne(memberId).username;
  1213. // Check that the former member is member of the card
  1214. if (_.contains(doc.members, memberId)) {
  1215. Activities.insert({
  1216. userId,
  1217. username,
  1218. activityType: 'unjoinMember',
  1219. boardId: doc.boardId,
  1220. cardId: doc._id,
  1221. });
  1222. }
  1223. }
  1224. }
  1225. function cardLabels(userId, doc, fieldNames, modifier) {
  1226. if (!_.contains(fieldNames, 'labelIds'))
  1227. return;
  1228. let labelId;
  1229. // Say hello to the new label
  1230. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1231. labelId = modifier.$addToSet.labelIds;
  1232. if (!_.contains(doc.labelIds, labelId)) {
  1233. const act = {
  1234. userId,
  1235. labelId,
  1236. activityType: 'addedLabel',
  1237. boardId: doc.boardId,
  1238. cardId: doc._id,
  1239. };
  1240. Activities.insert(act);
  1241. }
  1242. }
  1243. // Say goodbye to the label
  1244. if (modifier.$pull && modifier.$pull.labelIds) {
  1245. labelId = modifier.$pull.labelIds;
  1246. // Check that the former member is member of the card
  1247. if (_.contains(doc.labelIds, labelId)) {
  1248. Activities.insert({
  1249. userId,
  1250. labelId,
  1251. activityType: 'removedLabel',
  1252. boardId: doc.boardId,
  1253. cardId: doc._id,
  1254. });
  1255. }
  1256. }
  1257. }
  1258. function cardCreation(userId, doc) {
  1259. Activities.insert({
  1260. userId,
  1261. activityType: 'createCard',
  1262. boardId: doc.boardId,
  1263. listName: Lists.findOne(doc.listId).title,
  1264. listId: doc.listId,
  1265. cardId: doc._id,
  1266. cardTitle:doc.title,
  1267. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1268. swimlaneId: doc.swimlaneId,
  1269. });
  1270. }
  1271. function cardRemover(userId, doc) {
  1272. Activities.remove({
  1273. cardId: doc._id,
  1274. });
  1275. Checklists.remove({
  1276. cardId: doc._id,
  1277. });
  1278. Subtasks.remove({
  1279. cardId: doc._id,
  1280. });
  1281. CardComments.remove({
  1282. cardId: doc._id,
  1283. });
  1284. Attachments.remove({
  1285. cardId: doc._id,
  1286. });
  1287. }
  1288. if (Meteor.isServer) {
  1289. // Cards are often fetched within a board, so we create an index to make these
  1290. // queries more efficient.
  1291. Meteor.startup(() => {
  1292. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  1293. // https://github.com/wekan/wekan/issues/1863
  1294. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1295. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1296. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1297. // To correct it, add Index to parentId:
  1298. Cards._collection._ensureIndex({parentId: 1});
  1299. });
  1300. Cards.after.insert((userId, doc) => {
  1301. cardCreation(userId, doc);
  1302. });
  1303. // New activity for card (un)archivage
  1304. Cards.after.update((userId, doc, fieldNames) => {
  1305. cardState(userId, doc, fieldNames);
  1306. });
  1307. //New activity for card moves
  1308. Cards.after.update(function(userId, doc, fieldNames) {
  1309. const oldListId = this.previous.listId;
  1310. const oldSwimlaneId = this.previous.swimlaneId;
  1311. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId);
  1312. });
  1313. // Add a new activity if we add or remove a member to the card
  1314. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1315. cardMembers(userId, doc, fieldNames, modifier);
  1316. });
  1317. // Add a new activity if we add or remove a label to the card
  1318. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1319. cardLabels(userId, doc, fieldNames, modifier);
  1320. });
  1321. // Remove all activities associated with a card if we remove the card
  1322. // Remove also card_comments / checklists / attachments
  1323. Cards.after.remove((userId, doc) => {
  1324. cardRemover(userId, doc);
  1325. });
  1326. }
  1327. //SWIMLANES REST API
  1328. if (Meteor.isServer) {
  1329. /**
  1330. * @operation get_swimlane_cards
  1331. * @summary get all cards attached to a swimlane
  1332. *
  1333. * @param {string} boardId the board ID
  1334. * @param {string} swimlaneId the swimlane ID
  1335. * @return_type [{_id: string,
  1336. * title: string,
  1337. * description: string,
  1338. * listId: string}]
  1339. */
  1340. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId/cards', function(req, res) {
  1341. const paramBoardId = req.params.boardId;
  1342. const paramSwimlaneId = req.params.swimlaneId;
  1343. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1344. JsonRoutes.sendResult(res, {
  1345. code: 200,
  1346. data: Cards.find({
  1347. boardId: paramBoardId,
  1348. swimlaneId: paramSwimlaneId,
  1349. archived: false,
  1350. }).map(function(doc) {
  1351. return {
  1352. _id: doc._id,
  1353. title: doc.title,
  1354. description: doc.description,
  1355. listId: doc.listId,
  1356. };
  1357. }),
  1358. });
  1359. });
  1360. }
  1361. //LISTS REST API
  1362. if (Meteor.isServer) {
  1363. /**
  1364. * @operation get_all_cards
  1365. * @summary Get all Cards attached to a List
  1366. *
  1367. * @param {string} boardId the board ID
  1368. * @param {string} listId the list ID
  1369. * @return_type [{_id: string,
  1370. * title: string,
  1371. * description: string}]
  1372. */
  1373. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1374. const paramBoardId = req.params.boardId;
  1375. const paramListId = req.params.listId;
  1376. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1377. JsonRoutes.sendResult(res, {
  1378. code: 200,
  1379. data: Cards.find({
  1380. boardId: paramBoardId,
  1381. listId: paramListId,
  1382. archived: false,
  1383. }).map(function(doc) {
  1384. return {
  1385. _id: doc._id,
  1386. title: doc.title,
  1387. description: doc.description,
  1388. };
  1389. }),
  1390. });
  1391. });
  1392. /**
  1393. * @operation get_card
  1394. * @summary Get a Card
  1395. *
  1396. * @param {string} boardId the board ID
  1397. * @param {string} listId the list ID of the card
  1398. * @param {string} cardId the card ID
  1399. * @return_type Cards
  1400. */
  1401. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1402. const paramBoardId = req.params.boardId;
  1403. const paramListId = req.params.listId;
  1404. const paramCardId = req.params.cardId;
  1405. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1406. JsonRoutes.sendResult(res, {
  1407. code: 200,
  1408. data: Cards.findOne({
  1409. _id: paramCardId,
  1410. listId: paramListId,
  1411. boardId: paramBoardId,
  1412. archived: false,
  1413. }),
  1414. });
  1415. });
  1416. /**
  1417. * @operation new_card
  1418. * @summary Create a new Card
  1419. *
  1420. * @param {string} boardId the board ID of the new card
  1421. * @param {string} listId the list ID of the new card
  1422. * @param {string} authorID the user ID of the person owning the card
  1423. * @param {string} title the title of the new card
  1424. * @param {string} description the description of the new card
  1425. * @param {string} swimlaneId the swimlane ID of the new card
  1426. * @param {string} [members] the member IDs list of the new card
  1427. * @return_type {_id: string}
  1428. */
  1429. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1430. Authentication.checkUserId(req.userId);
  1431. const paramBoardId = req.params.boardId;
  1432. const paramListId = req.params.listId;
  1433. const currentCards = Cards.find({
  1434. listId: paramListId,
  1435. archived: false,
  1436. }, { sort: ['sort'] });
  1437. const check = Users.findOne({
  1438. _id: req.body.authorId,
  1439. });
  1440. const members = req.body.members || [req.body.authorId];
  1441. if (typeof check !== 'undefined') {
  1442. const id = Cards.direct.insert({
  1443. title: req.body.title,
  1444. boardId: paramBoardId,
  1445. listId: paramListId,
  1446. description: req.body.description,
  1447. userId: req.body.authorId,
  1448. swimlaneId: req.body.swimlaneId,
  1449. sort: currentCards.count(),
  1450. members,
  1451. });
  1452. JsonRoutes.sendResult(res, {
  1453. code: 200,
  1454. data: {
  1455. _id: id,
  1456. },
  1457. });
  1458. const card = Cards.findOne({
  1459. _id: id,
  1460. });
  1461. cardCreation(req.body.authorId, card);
  1462. } else {
  1463. JsonRoutes.sendResult(res, {
  1464. code: 401,
  1465. });
  1466. }
  1467. });
  1468. /*
  1469. * Note for the JSDoc:
  1470. * 'list' will be interpreted as the path parameter
  1471. * 'listID' will be interpreted as the body parameter
  1472. */
  1473. /**
  1474. * @operation edit_card
  1475. * @summary Edit Fields in a Card
  1476. *
  1477. * @description Edit a card
  1478. *
  1479. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1480. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1481. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1482. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1483. * `indigo`:
  1484. *
  1485. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1486. *
  1487. * Note: setting the color to white has the same effect than removing it.
  1488. *
  1489. * @param {string} boardId the board ID of the card
  1490. * @param {string} list the list ID of the card
  1491. * @param {string} cardId the ID of the card
  1492. * @param {string} [title] the new title of the card
  1493. * @param {string} [listId] the new list ID of the card (move operation)
  1494. * @param {string} [description] the new description of the card
  1495. * @param {string} [authorId] change the owner of the card
  1496. * @param {string} [labelIds] the new list of label IDs attached to the card
  1497. * @param {string} [swimlaneId] the new swimlane ID of the card
  1498. * @param {string} [members] the new list of member IDs attached to the card
  1499. * @param {string} [requestedBy] the new requestedBy field of the card
  1500. * @param {string} [assignedBy] the new assignedBy field of the card
  1501. * @param {string} [receivedAt] the new receivedAt field of the card
  1502. * @param {string} [assignBy] the new assignBy field of the card
  1503. * @param {string} [startAt] the new startAt field of the card
  1504. * @param {string} [dueAt] the new dueAt field of the card
  1505. * @param {string} [endAt] the new endAt field of the card
  1506. * @param {string} [spentTime] the new spentTime field of the card
  1507. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1508. * @param {string} [customFields] the new customFields value of the card
  1509. * @param {string} [color] the new color of the card
  1510. * @return_type {_id: string}
  1511. */
  1512. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1513. Authentication.checkUserId(req.userId);
  1514. const paramBoardId = req.params.boardId;
  1515. const paramCardId = req.params.cardId;
  1516. const paramListId = req.params.listId;
  1517. if (req.body.hasOwnProperty('title')) {
  1518. const newTitle = req.body.title;
  1519. Cards.direct.update({
  1520. _id: paramCardId,
  1521. listId: paramListId,
  1522. boardId: paramBoardId,
  1523. archived: false,
  1524. }, {
  1525. $set: {
  1526. title: newTitle,
  1527. },
  1528. });
  1529. }
  1530. if (req.body.hasOwnProperty('listId')) {
  1531. const newParamListId = req.body.listId;
  1532. Cards.direct.update({
  1533. _id: paramCardId,
  1534. listId: paramListId,
  1535. boardId: paramBoardId,
  1536. archived: false,
  1537. }, {
  1538. $set: {
  1539. listId: newParamListId,
  1540. },
  1541. });
  1542. const card = Cards.findOne({
  1543. _id: paramCardId,
  1544. });
  1545. cardMove(req.body.authorId, card, {
  1546. fieldName: 'listId',
  1547. }, paramListId);
  1548. }
  1549. if (req.body.hasOwnProperty('description')) {
  1550. const newDescription = req.body.description;
  1551. Cards.direct.update({
  1552. _id: paramCardId,
  1553. listId: paramListId,
  1554. boardId: paramBoardId,
  1555. archived: false,
  1556. }, {
  1557. $set: {
  1558. description: newDescription,
  1559. },
  1560. });
  1561. }
  1562. if (req.body.hasOwnProperty('color')) {
  1563. const newColor = req.body.color;
  1564. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1565. {$set: {color: newColor}});
  1566. }
  1567. if (req.body.hasOwnProperty('labelIds')) {
  1568. let newlabelIds = req.body.labelIds;
  1569. if (_.isString(newlabelIds)) {
  1570. if (newlabelIds === '') {
  1571. newlabelIds = null;
  1572. }
  1573. else {
  1574. newlabelIds = [newlabelIds];
  1575. }
  1576. }
  1577. Cards.direct.update({
  1578. _id: paramCardId,
  1579. listId: paramListId,
  1580. boardId: paramBoardId,
  1581. archived: false,
  1582. }, {
  1583. $set: {
  1584. labelIds: newlabelIds,
  1585. },
  1586. });
  1587. }
  1588. if (req.body.hasOwnProperty('requestedBy')) {
  1589. const newrequestedBy = req.body.requestedBy;
  1590. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1591. {$set: {requestedBy: newrequestedBy}});
  1592. }
  1593. if (req.body.hasOwnProperty('assignedBy')) {
  1594. const newassignedBy = req.body.assignedBy;
  1595. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1596. {$set: {assignedBy: newassignedBy}});
  1597. }
  1598. if (req.body.hasOwnProperty('receivedAt')) {
  1599. const newreceivedAt = req.body.receivedAt;
  1600. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1601. {$set: {receivedAt: newreceivedAt}});
  1602. }
  1603. if (req.body.hasOwnProperty('startAt')) {
  1604. const newstartAt = req.body.startAt;
  1605. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1606. {$set: {startAt: newstartAt}});
  1607. }
  1608. if (req.body.hasOwnProperty('dueAt')) {
  1609. const newdueAt = req.body.dueAt;
  1610. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1611. {$set: {dueAt: newdueAt}});
  1612. }
  1613. if (req.body.hasOwnProperty('endAt')) {
  1614. const newendAt = req.body.endAt;
  1615. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1616. {$set: {endAt: newendAt}});
  1617. }
  1618. if (req.body.hasOwnProperty('spentTime')) {
  1619. const newspentTime = req.body.spentTime;
  1620. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1621. {$set: {spentTime: newspentTime}});
  1622. }
  1623. if (req.body.hasOwnProperty('isOverTime')) {
  1624. const newisOverTime = req.body.isOverTime;
  1625. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1626. {$set: {isOverTime: newisOverTime}});
  1627. }
  1628. if (req.body.hasOwnProperty('customFields')) {
  1629. const newcustomFields = req.body.customFields;
  1630. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1631. {$set: {customFields: newcustomFields}});
  1632. }
  1633. if (req.body.hasOwnProperty('members')) {
  1634. let newmembers = req.body.members;
  1635. if (_.isString(newmembers)) {
  1636. if (newmembers === '') {
  1637. newmembers = null;
  1638. }
  1639. else {
  1640. newmembers = [newmembers];
  1641. }
  1642. }
  1643. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1644. {$set: {members: newmembers}});
  1645. }
  1646. if (req.body.hasOwnProperty('swimlaneId')) {
  1647. const newParamSwimlaneId = req.body.swimlaneId;
  1648. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1649. {$set: {swimlaneId: newParamSwimlaneId}});
  1650. }
  1651. JsonRoutes.sendResult(res, {
  1652. code: 200,
  1653. data: {
  1654. _id: paramCardId,
  1655. },
  1656. });
  1657. });
  1658. /**
  1659. * @operation delete_card
  1660. * @summary Delete a card from a board
  1661. *
  1662. * @description This operation **deletes** a card, and therefore the card
  1663. * is not put in the recycle bin.
  1664. *
  1665. * @param {string} boardId the board ID of the card
  1666. * @param {string} list the list ID of the card
  1667. * @param {string} cardId the ID of the card
  1668. * @return_type {_id: string}
  1669. */
  1670. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1671. Authentication.checkUserId(req.userId);
  1672. const paramBoardId = req.params.boardId;
  1673. const paramListId = req.params.listId;
  1674. const paramCardId = req.params.cardId;
  1675. Cards.direct.remove({
  1676. _id: paramCardId,
  1677. listId: paramListId,
  1678. boardId: paramBoardId,
  1679. });
  1680. const card = Cards.find({
  1681. _id: paramCardId,
  1682. });
  1683. cardRemover(req.body.authorId, card);
  1684. JsonRoutes.sendResult(res, {
  1685. code: 200,
  1686. data: {
  1687. _id: paramCardId,
  1688. },
  1689. });
  1690. });
  1691. }