cards.js 44 KB

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