cards.js 42 KB

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