cards.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  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(
  6. new SimpleSchema({
  7. title: {
  8. /**
  9. * the title of the card
  10. */
  11. type: String,
  12. optional: true,
  13. defaultValue: '',
  14. },
  15. archived: {
  16. /**
  17. * is the card archived
  18. */
  19. type: Boolean,
  20. // eslint-disable-next-line consistent-return
  21. autoValue() {
  22. // eslint-disable-line consistent-return
  23. if (this.isInsert && !this.isSet) {
  24. return false;
  25. }
  26. },
  27. },
  28. parentId: {
  29. /**
  30. * ID of the parent card
  31. */
  32. type: String,
  33. optional: true,
  34. defaultValue: '',
  35. },
  36. listId: {
  37. /**
  38. * List ID where the card is
  39. */
  40. type: String,
  41. optional: true,
  42. defaultValue: '',
  43. },
  44. swimlaneId: {
  45. /**
  46. * Swimlane ID where the card is
  47. */
  48. type: String,
  49. },
  50. // The system could work without this `boardId` information (we could deduce
  51. // the board identifier from the card), but it would make the system more
  52. // difficult to manage and less efficient.
  53. boardId: {
  54. /**
  55. * Board ID of the card
  56. */
  57. type: String,
  58. optional: true,
  59. defaultValue: '',
  60. },
  61. coverId: {
  62. /**
  63. * Cover ID of the card
  64. */
  65. type: String,
  66. optional: true,
  67. defaultValue: '',
  68. },
  69. color: {
  70. type: String,
  71. optional: true,
  72. allowedValues: [
  73. 'white',
  74. 'green',
  75. 'yellow',
  76. 'orange',
  77. 'red',
  78. 'purple',
  79. 'blue',
  80. 'sky',
  81. 'lime',
  82. 'pink',
  83. 'black',
  84. 'silver',
  85. 'peachpuff',
  86. 'crimson',
  87. 'plum',
  88. 'darkgreen',
  89. 'slateblue',
  90. 'magenta',
  91. 'gold',
  92. 'navy',
  93. 'gray',
  94. 'saddlebrown',
  95. 'paleturquoise',
  96. 'mistyrose',
  97. 'indigo',
  98. ],
  99. },
  100. createdAt: {
  101. /**
  102. * creation date
  103. */
  104. type: Date,
  105. // eslint-disable-next-line consistent-return
  106. autoValue() {
  107. if (this.isInsert) {
  108. return new Date();
  109. } else if (this.isUpsert) {
  110. return { $setOnInsert: new Date() };
  111. } else {
  112. this.unset();
  113. }
  114. },
  115. },
  116. modifiedAt: {
  117. type: Date,
  118. denyUpdate: false,
  119. // eslint-disable-next-line consistent-return
  120. autoValue() {
  121. if (this.isInsert || this.isUpsert || this.isUpdate) {
  122. return new Date();
  123. } else {
  124. this.unset();
  125. }
  126. },
  127. },
  128. customFields: {
  129. /**
  130. * list of custom fields
  131. */
  132. type: [Object],
  133. optional: true,
  134. defaultValue: [],
  135. },
  136. 'customFields.$': {
  137. type: new SimpleSchema({
  138. _id: {
  139. /**
  140. * the ID of the related custom field
  141. */
  142. type: String,
  143. optional: true,
  144. defaultValue: '',
  145. },
  146. value: {
  147. /**
  148. * value attached to the custom field
  149. */
  150. type: Match.OneOf(String, Number, Boolean, Date),
  151. optional: true,
  152. defaultValue: '',
  153. },
  154. }),
  155. },
  156. dateLastActivity: {
  157. /**
  158. * Date of last activity
  159. */
  160. type: Date,
  161. autoValue() {
  162. return new Date();
  163. },
  164. },
  165. description: {
  166. /**
  167. * description of the card
  168. */
  169. type: String,
  170. optional: true,
  171. defaultValue: '',
  172. },
  173. requestedBy: {
  174. /**
  175. * who requested the card (ID of the user)
  176. */
  177. type: String,
  178. optional: true,
  179. defaultValue: '',
  180. },
  181. assignedBy: {
  182. /**
  183. * who assigned the card (ID of the user)
  184. */
  185. type: String,
  186. optional: true,
  187. defaultValue: '',
  188. },
  189. labelIds: {
  190. /**
  191. * list of labels ID the card has
  192. */
  193. type: [String],
  194. optional: true,
  195. defaultValue: [],
  196. },
  197. members: {
  198. /**
  199. * list of members (user IDs)
  200. */
  201. type: [String],
  202. optional: true,
  203. defaultValue: [],
  204. },
  205. assignees: {
  206. /**
  207. * who is assignee of the card (user ID),
  208. * maximum one ID of assignee in array.
  209. */
  210. type: [String],
  211. optional: true,
  212. defaultValue: [],
  213. },
  214. receivedAt: {
  215. /**
  216. * Date the card was received
  217. */
  218. type: Date,
  219. optional: true,
  220. },
  221. startAt: {
  222. /**
  223. * Date the card was started to be worked on
  224. */
  225. type: Date,
  226. optional: true,
  227. },
  228. dueAt: {
  229. /**
  230. * Date the card is due
  231. */
  232. type: Date,
  233. optional: true,
  234. },
  235. endAt: {
  236. /**
  237. * Date the card ended
  238. */
  239. type: Date,
  240. optional: true,
  241. },
  242. spentTime: {
  243. /**
  244. * How much time has been spent on this
  245. */
  246. type: Number,
  247. decimal: true,
  248. optional: true,
  249. defaultValue: 0,
  250. },
  251. isOvertime: {
  252. /**
  253. * is the card over time?
  254. */
  255. type: Boolean,
  256. defaultValue: false,
  257. optional: true,
  258. },
  259. // XXX Should probably be called `authorId`. Is it even needed since we have
  260. // the `members` field?
  261. userId: {
  262. /**
  263. * user ID of the author of the card
  264. */
  265. type: String,
  266. // eslint-disable-next-line consistent-return
  267. autoValue() {
  268. // eslint-disable-line consistent-return
  269. if (this.isInsert && !this.isSet) {
  270. return this.userId;
  271. }
  272. },
  273. },
  274. sort: {
  275. /**
  276. * Sort value
  277. */
  278. type: Number,
  279. decimal: true,
  280. defaultValue: '',
  281. },
  282. subtaskSort: {
  283. /**
  284. * subtask sort value
  285. */
  286. type: Number,
  287. decimal: true,
  288. defaultValue: -1,
  289. optional: true,
  290. },
  291. type: {
  292. /**
  293. * type of the card
  294. */
  295. type: String,
  296. defaultValue: 'cardType-card',
  297. },
  298. linkedId: {
  299. /**
  300. * ID of the linked card
  301. */
  302. type: String,
  303. optional: true,
  304. defaultValue: '',
  305. },
  306. }),
  307. );
  308. Cards.allow({
  309. insert(userId, doc) {
  310. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  311. },
  312. update(userId, doc) {
  313. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  314. },
  315. remove(userId, doc) {
  316. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  317. },
  318. fetch: ['boardId'],
  319. });
  320. Cards.helpers({
  321. copy(boardId, swimlaneId, listId) {
  322. const oldBoard = Boards.findOne(this.boardId);
  323. const oldBoardLabels = oldBoard.labels;
  324. // Get old label names
  325. const oldCardLabels = _.pluck(
  326. _.filter(oldBoardLabels, label => {
  327. return _.contains(this.labelIds, label._id);
  328. }),
  329. 'name',
  330. );
  331. const newBoard = Boards.findOne(boardId);
  332. const newBoardLabels = newBoard.labels;
  333. const newCardLabels = _.pluck(
  334. _.filter(newBoardLabels, label => {
  335. return _.contains(oldCardLabels, label.name);
  336. }),
  337. '_id',
  338. );
  339. const oldId = this._id;
  340. const oldCard = Cards.findOne(oldId);
  341. // Copy Custom Fields
  342. if (oldBoard._id !== boardId) {
  343. CustomFields.find({
  344. _id: {
  345. $in: oldCard.customFields.map(cf => {
  346. return cf._id;
  347. }),
  348. },
  349. }).forEach(cf => {
  350. if (!_.contains(cf.boardIds, boardId)) cf.addBoard(boardId);
  351. });
  352. }
  353. delete this._id;
  354. delete this.labelIds;
  355. this.labelIds = newCardLabels;
  356. this.boardId = boardId;
  357. this.swimlaneId = swimlaneId;
  358. this.listId = listId;
  359. const _id = Cards.insert(this);
  360. // Copy attachments
  361. oldCard.attachments().forEach(att => {
  362. att.meta.cardId = _id;
  363. delete att._id;
  364. return Attachments.insert(att);
  365. });
  366. // copy checklists
  367. Checklists.find({ cardId: oldId }).forEach(ch => {
  368. ch.copy(_id);
  369. });
  370. // copy subtasks
  371. Cards.find({ parentId: oldId }).forEach(subtask => {
  372. subtask.parentId = _id;
  373. subtask._id = null;
  374. Cards.insert(subtask);
  375. });
  376. // copy card comments
  377. CardComments.find({ cardId: oldId }).forEach(cmt => {
  378. cmt.copy(_id);
  379. });
  380. return _id;
  381. },
  382. list() {
  383. return Lists.findOne(this.listId);
  384. },
  385. board() {
  386. return Boards.findOne(this.boardId);
  387. },
  388. labels() {
  389. const boardLabels = this.board().labels;
  390. const cardLabels = _.filter(boardLabels, label => {
  391. return _.contains(this.labelIds, label._id);
  392. });
  393. return cardLabels;
  394. },
  395. hasLabel(labelId) {
  396. return _.contains(this.labelIds, labelId);
  397. },
  398. user() {
  399. return Users.findOne(this.userId);
  400. },
  401. isAssigned(memberId) {
  402. return _.contains(this.getMembers(), memberId);
  403. },
  404. isAssignee(assigneeId) {
  405. return _.contains(this.getAssignees(), assigneeId);
  406. },
  407. activities() {
  408. if (this.isLinkedCard()) {
  409. return Activities.find(
  410. { cardId: this.linkedId },
  411. { sort: { createdAt: -1 } },
  412. );
  413. } else if (this.isLinkedBoard()) {
  414. return Activities.find(
  415. { boardId: this.linkedId },
  416. { sort: { createdAt: -1 } },
  417. );
  418. } else {
  419. return Activities.find({ cardId: this._id }, { sort: { createdAt: -1 } });
  420. }
  421. },
  422. comments() {
  423. if (this.isLinkedCard()) {
  424. return CardComments.find(
  425. { cardId: this.linkedId },
  426. { sort: { createdAt: -1 } },
  427. );
  428. } else {
  429. return CardComments.find(
  430. { cardId: this._id },
  431. { sort: { createdAt: -1 } },
  432. );
  433. }
  434. },
  435. attachments() {
  436. if (this.isLinkedCard()) {
  437. return Attachments.find(
  438. { 'meta.cardId': this.linkedId },
  439. { sort: { uploadedAt: -1 } },
  440. );
  441. } else {
  442. let ret = Attachments.find(
  443. { 'meta.cardId': this._id },
  444. { sort: { uploadedAt: -1 } },
  445. );
  446. return ret;
  447. }
  448. },
  449. cover() {
  450. const cover = Attachments.findOne(this.coverId);
  451. // if we return a cover before it is fully stored, we will get errors when we try to display it
  452. // todo XXX we could return a default "upload pending" image in the meantime?
  453. return cover && cover.link();
  454. },
  455. checklists() {
  456. if (this.isLinkedCard()) {
  457. return Checklists.find({ cardId: this.linkedId }, { sort: { sort: 1 } });
  458. } else {
  459. return Checklists.find({ cardId: this._id }, { sort: { sort: 1 } });
  460. }
  461. },
  462. checklistItemCount() {
  463. const checklists = this.checklists().fetch();
  464. return checklists
  465. .map(checklist => {
  466. return checklist.itemCount();
  467. })
  468. .reduce((prev, next) => {
  469. return prev + next;
  470. }, 0);
  471. },
  472. checklistFinishedCount() {
  473. const checklists = this.checklists().fetch();
  474. return checklists
  475. .map(checklist => {
  476. return checklist.finishedCount();
  477. })
  478. .reduce((prev, next) => {
  479. return prev + next;
  480. }, 0);
  481. },
  482. checklistFinished() {
  483. return (
  484. this.hasChecklist() &&
  485. this.checklistItemCount() === this.checklistFinishedCount()
  486. );
  487. },
  488. hasChecklist() {
  489. return this.checklistItemCount() !== 0;
  490. },
  491. subtasks() {
  492. return Cards.find(
  493. {
  494. parentId: this._id,
  495. archived: false,
  496. },
  497. {
  498. sort: {
  499. sort: 1,
  500. },
  501. },
  502. );
  503. },
  504. allSubtasks() {
  505. return Cards.find(
  506. {
  507. parentId: this._id,
  508. archived: false,
  509. },
  510. {
  511. sort: {
  512. sort: 1,
  513. },
  514. },
  515. );
  516. },
  517. subtasksCount() {
  518. return Cards.find({
  519. parentId: this._id,
  520. archived: false,
  521. }).count();
  522. },
  523. subtasksFinishedCount() {
  524. return Cards.find({
  525. parentId: this._id,
  526. archived: true,
  527. }).count();
  528. },
  529. subtasksFinished() {
  530. const finishCount = this.subtasksFinishedCount();
  531. return finishCount > 0 && this.subtasksCount() === finishCount;
  532. },
  533. allowsSubtasks() {
  534. return this.subtasksCount() !== 0;
  535. },
  536. customFieldIndex(customFieldId) {
  537. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  538. },
  539. // customFields with definitions
  540. customFieldsWD() {
  541. // get all definitions
  542. const definitions = CustomFields.find({
  543. boardIds: { $in: [this.boardId] },
  544. }).fetch();
  545. // match right definition to each field
  546. if (!this.customFields) return [];
  547. return this.customFields.map(customField => {
  548. const definition = definitions.find(definition => {
  549. return definition._id === customField._id;
  550. });
  551. if (!definition) {
  552. return {};
  553. }
  554. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  555. let trueValue = customField.value;
  556. if (
  557. definition.settings.dropdownItems &&
  558. definition.settings.dropdownItems.length > 0
  559. ) {
  560. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  561. if (definition.settings.dropdownItems[i]._id === customField.value) {
  562. trueValue = definition.settings.dropdownItems[i].name;
  563. }
  564. }
  565. }
  566. return {
  567. _id: customField._id,
  568. value: customField.value,
  569. trueValue,
  570. definition,
  571. };
  572. });
  573. },
  574. colorClass() {
  575. if (this.color) return this.color;
  576. return '';
  577. },
  578. absoluteUrl() {
  579. const board = this.board();
  580. return FlowRouter.url('card', {
  581. boardId: board._id,
  582. slug: board.slug,
  583. cardId: this._id,
  584. });
  585. },
  586. canBeRestored() {
  587. const list = Lists.findOne({
  588. _id: this.listId,
  589. });
  590. if (
  591. !list.getWipLimit('soft') &&
  592. list.getWipLimit('enabled') &&
  593. list.getWipLimit('value') === list.cards().count()
  594. ) {
  595. return false;
  596. }
  597. return true;
  598. },
  599. parentCard() {
  600. if (this.parentId === '') {
  601. return null;
  602. }
  603. return Cards.findOne(this.parentId);
  604. },
  605. parentCardName() {
  606. let result = '';
  607. if (this.parentId !== '') {
  608. const card = Cards.findOne(this.parentId);
  609. if (card) {
  610. result = card.title;
  611. }
  612. }
  613. return result;
  614. },
  615. parentListId() {
  616. const result = [];
  617. let crtParentId = this.parentId;
  618. while (crtParentId !== '') {
  619. const crt = Cards.findOne(crtParentId);
  620. if (crt === null || crt === undefined) {
  621. // maybe it has been deleted
  622. break;
  623. }
  624. if (crtParentId in result) {
  625. // circular reference
  626. break;
  627. }
  628. result.unshift(crtParentId);
  629. crtParentId = crt.parentId;
  630. }
  631. return result;
  632. },
  633. parentList() {
  634. const resultId = [];
  635. const result = [];
  636. let crtParentId = this.parentId;
  637. while (crtParentId !== '') {
  638. const crt = Cards.findOne(crtParentId);
  639. if (crt === null || crt === undefined) {
  640. // maybe it has been deleted
  641. break;
  642. }
  643. if (crtParentId in resultId) {
  644. // circular reference
  645. break;
  646. }
  647. resultId.unshift(crtParentId);
  648. result.unshift(crt);
  649. crtParentId = crt.parentId;
  650. }
  651. return result;
  652. },
  653. parentString(sep) {
  654. return this.parentList()
  655. .map(function(elem) {
  656. return elem.title;
  657. })
  658. .join(sep);
  659. },
  660. isTopLevel() {
  661. return this.parentId === '';
  662. },
  663. isLinkedCard() {
  664. return this.type === 'cardType-linkedCard';
  665. },
  666. isLinkedBoard() {
  667. return this.type === 'cardType-linkedBoard';
  668. },
  669. isLinked() {
  670. return this.isLinkedCard() || this.isLinkedBoard();
  671. },
  672. setDescription(description) {
  673. if (this.isLinkedCard()) {
  674. return Cards.update({ _id: this.linkedId }, { $set: { description } });
  675. } else if (this.isLinkedBoard()) {
  676. return Boards.update({ _id: this.linkedId }, { $set: { description } });
  677. } else {
  678. return Cards.update({ _id: this._id }, { $set: { description } });
  679. }
  680. },
  681. getDescription() {
  682. if (this.isLinkedCard()) {
  683. const card = Cards.findOne({ _id: this.linkedId });
  684. if (card && card.description) return card.description;
  685. else return null;
  686. } else if (this.isLinkedBoard()) {
  687. const board = Boards.findOne({ _id: this.linkedId });
  688. if (board && board.description) return board.description;
  689. else return null;
  690. } else if (this.description) {
  691. return this.description;
  692. } else {
  693. return null;
  694. }
  695. },
  696. getMembers() {
  697. if (this.isLinkedCard()) {
  698. const card = Cards.findOne({ _id: this.linkedId });
  699. return card.members;
  700. } else if (this.isLinkedBoard()) {
  701. const board = Boards.findOne({ _id: this.linkedId });
  702. return board.activeMembers().map(member => {
  703. return member.userId;
  704. });
  705. } else {
  706. return this.members;
  707. }
  708. },
  709. getAssignees() {
  710. if (this.isLinkedCard()) {
  711. const card = Cards.findOne({ _id: this.linkedId });
  712. return card.assignees;
  713. } else if (this.isLinkedBoard()) {
  714. const board = Boards.findOne({ _id: this.linkedId });
  715. return board.activeMembers().map(assignee => {
  716. return assignee.userId;
  717. });
  718. } else {
  719. return this.assignees;
  720. }
  721. },
  722. assignMember(memberId) {
  723. if (this.isLinkedCard()) {
  724. return Cards.update(
  725. { _id: this.linkedId },
  726. { $addToSet: { members: memberId } },
  727. );
  728. } else if (this.isLinkedBoard()) {
  729. const board = Boards.findOne({ _id: this.linkedId });
  730. return board.addMember(memberId);
  731. } else {
  732. return Cards.update(
  733. { _id: this._id },
  734. { $addToSet: { members: memberId } },
  735. );
  736. }
  737. },
  738. assignAssignee(assigneeId) {
  739. if (this.isLinkedCard()) {
  740. return Cards.update(
  741. { _id: this.linkedId },
  742. { $addToSet: { assignees: assigneeId } },
  743. );
  744. } else if (this.isLinkedBoard()) {
  745. const board = Boards.findOne({ _id: this.linkedId });
  746. return board.addAssignee(assigneeId);
  747. } else {
  748. return Cards.update(
  749. { _id: this._id },
  750. { $addToSet: { assignees: assigneeId } },
  751. );
  752. }
  753. },
  754. unassignMember(memberId) {
  755. if (this.isLinkedCard()) {
  756. return Cards.update(
  757. { _id: this.linkedId },
  758. { $pull: { members: memberId } },
  759. );
  760. } else if (this.isLinkedBoard()) {
  761. const board = Boards.findOne({ _id: this.linkedId });
  762. return board.removeMember(memberId);
  763. } else {
  764. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  765. }
  766. },
  767. unassignAssignee(assigneeId) {
  768. if (this.isLinkedCard()) {
  769. return Cards.update(
  770. { _id: this.linkedId },
  771. { $pull: { assignees: assigneeId } },
  772. );
  773. } else if (this.isLinkedBoard()) {
  774. const board = Boards.findOne({ _id: this.linkedId });
  775. return board.removeAssignee(assigneeId);
  776. } else {
  777. return Cards.update(
  778. { _id: this._id },
  779. { $pull: { assignees: assigneeId } },
  780. );
  781. }
  782. },
  783. toggleMember(memberId) {
  784. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  785. return this.unassignMember(memberId);
  786. } else {
  787. return this.assignMember(memberId);
  788. }
  789. },
  790. toggleAssignee(assigneeId) {
  791. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  792. return this.unassignAssignee(assigneeId);
  793. } else {
  794. return this.assignAssignee(assigneeId);
  795. }
  796. },
  797. getReceived() {
  798. if (this.isLinkedCard()) {
  799. const card = Cards.findOne({ _id: this.linkedId });
  800. return card.receivedAt;
  801. } else {
  802. return this.receivedAt;
  803. }
  804. },
  805. setReceived(receivedAt) {
  806. if (this.isLinkedCard()) {
  807. return Cards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  808. } else {
  809. return Cards.update({ _id: this._id }, { $set: { receivedAt } });
  810. }
  811. },
  812. getStart() {
  813. if (this.isLinkedCard()) {
  814. const card = Cards.findOne({ _id: this.linkedId });
  815. return card.startAt;
  816. } else if (this.isLinkedBoard()) {
  817. const board = Boards.findOne({ _id: this.linkedId });
  818. return board.startAt;
  819. } else {
  820. return this.startAt;
  821. }
  822. },
  823. setStart(startAt) {
  824. if (this.isLinkedCard()) {
  825. return Cards.update({ _id: this.linkedId }, { $set: { startAt } });
  826. } else if (this.isLinkedBoard()) {
  827. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  828. } else {
  829. return Cards.update({ _id: this._id }, { $set: { startAt } });
  830. }
  831. },
  832. getDue() {
  833. if (this.isLinkedCard()) {
  834. const card = Cards.findOne({ _id: this.linkedId });
  835. return card.dueAt;
  836. } else if (this.isLinkedBoard()) {
  837. const board = Boards.findOne({ _id: this.linkedId });
  838. return board.dueAt;
  839. } else {
  840. return this.dueAt;
  841. }
  842. },
  843. setDue(dueAt) {
  844. if (this.isLinkedCard()) {
  845. return Cards.update({ _id: this.linkedId }, { $set: { dueAt } });
  846. } else if (this.isLinkedBoard()) {
  847. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  848. } else {
  849. return Cards.update({ _id: this._id }, { $set: { dueAt } });
  850. }
  851. },
  852. getEnd() {
  853. if (this.isLinkedCard()) {
  854. const card = Cards.findOne({ _id: this.linkedId });
  855. return card.endAt;
  856. } else if (this.isLinkedBoard()) {
  857. const board = Boards.findOne({ _id: this.linkedId });
  858. return board.endAt;
  859. } else {
  860. return this.endAt;
  861. }
  862. },
  863. setEnd(endAt) {
  864. if (this.isLinkedCard()) {
  865. return Cards.update({ _id: this.linkedId }, { $set: { endAt } });
  866. } else if (this.isLinkedBoard()) {
  867. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  868. } else {
  869. return Cards.update({ _id: this._id }, { $set: { endAt } });
  870. }
  871. },
  872. getIsOvertime() {
  873. if (this.isLinkedCard()) {
  874. const card = Cards.findOne({ _id: this.linkedId });
  875. return card.isOvertime;
  876. } else if (this.isLinkedBoard()) {
  877. const board = Boards.findOne({ _id: this.linkedId });
  878. return board.isOvertime;
  879. } else {
  880. return this.isOvertime;
  881. }
  882. },
  883. setIsOvertime(isOvertime) {
  884. if (this.isLinkedCard()) {
  885. return Cards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  886. } else if (this.isLinkedBoard()) {
  887. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  888. } else {
  889. return Cards.update({ _id: this._id }, { $set: { isOvertime } });
  890. }
  891. },
  892. getSpentTime() {
  893. if (this.isLinkedCard()) {
  894. const card = Cards.findOne({ _id: this.linkedId });
  895. return card.spentTime;
  896. } else if (this.isLinkedBoard()) {
  897. const board = Boards.findOne({ _id: this.linkedId });
  898. return board.spentTime;
  899. } else {
  900. return this.spentTime;
  901. }
  902. },
  903. setSpentTime(spentTime) {
  904. if (this.isLinkedCard()) {
  905. return Cards.update({ _id: this.linkedId }, { $set: { spentTime } });
  906. } else if (this.isLinkedBoard()) {
  907. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  908. } else {
  909. return Cards.update({ _id: this._id }, { $set: { spentTime } });
  910. }
  911. },
  912. getId() {
  913. if (this.isLinked()) {
  914. return this.linkedId;
  915. } else {
  916. return this._id;
  917. }
  918. },
  919. getTitle() {
  920. if (this.isLinkedCard()) {
  921. const card = Cards.findOne({ _id: this.linkedId });
  922. return card.title;
  923. } else if (this.isLinkedBoard()) {
  924. const board = Boards.findOne({ _id: this.linkedId });
  925. return board.title;
  926. } else {
  927. return this.title;
  928. }
  929. },
  930. getBoardTitle() {
  931. if (this.isLinkedCard()) {
  932. const card = Cards.findOne({ _id: this.linkedId });
  933. const board = Boards.findOne({ _id: card.boardId });
  934. return board.title;
  935. } else if (this.isLinkedBoard()) {
  936. const board = Boards.findOne({ _id: this.linkedId });
  937. return board.title;
  938. } else {
  939. const board = Boards.findOne({ _id: this.boardId });
  940. return board.title;
  941. }
  942. },
  943. setTitle(title) {
  944. if (this.isLinkedCard()) {
  945. return Cards.update({ _id: this.linkedId }, { $set: { title } });
  946. } else if (this.isLinkedBoard()) {
  947. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  948. } else {
  949. return Cards.update({ _id: this._id }, { $set: { title } });
  950. }
  951. },
  952. getArchived() {
  953. if (this.isLinkedCard()) {
  954. const card = Cards.findOne({ _id: this.linkedId });
  955. return card.archived;
  956. } else if (this.isLinkedBoard()) {
  957. const board = Boards.findOne({ _id: this.linkedId });
  958. return board.archived;
  959. } else {
  960. return this.archived;
  961. }
  962. },
  963. setRequestedBy(requestedBy) {
  964. if (this.isLinkedCard()) {
  965. return Cards.update({ _id: this.linkedId }, { $set: { requestedBy } });
  966. } else {
  967. return Cards.update({ _id: this._id }, { $set: { requestedBy } });
  968. }
  969. },
  970. getRequestedBy() {
  971. if (this.isLinkedCard()) {
  972. const card = Cards.findOne({ _id: this.linkedId });
  973. return card.requestedBy;
  974. } else {
  975. return this.requestedBy;
  976. }
  977. },
  978. setAssignedBy(assignedBy) {
  979. if (this.isLinkedCard()) {
  980. return Cards.update({ _id: this.linkedId }, { $set: { assignedBy } });
  981. } else {
  982. return Cards.update({ _id: this._id }, { $set: { assignedBy } });
  983. }
  984. },
  985. getAssignedBy() {
  986. if (this.isLinkedCard()) {
  987. const card = Cards.findOne({ _id: this.linkedId });
  988. return card.assignedBy;
  989. } else {
  990. return this.assignedBy;
  991. }
  992. },
  993. isTemplateCard() {
  994. return this.type === 'template-card';
  995. },
  996. });
  997. Cards.mutations({
  998. applyToChildren(funct) {
  999. Cards.find({
  1000. parentId: this._id,
  1001. }).forEach(card => {
  1002. funct(card);
  1003. });
  1004. },
  1005. archive() {
  1006. this.applyToChildren(card => {
  1007. return card.archive();
  1008. });
  1009. return {
  1010. $set: {
  1011. archived: true,
  1012. },
  1013. };
  1014. },
  1015. restore() {
  1016. this.applyToChildren(card => {
  1017. return card.restore();
  1018. });
  1019. return {
  1020. $set: {
  1021. archived: false,
  1022. },
  1023. };
  1024. },
  1025. move(boardId, swimlaneId, listId, sort) {
  1026. // Copy Custom Fields
  1027. if (this.boardId !== boardId) {
  1028. CustomFields.find({
  1029. _id: {
  1030. $in: this.customFields.map(cf => {
  1031. return cf._id;
  1032. }),
  1033. },
  1034. }).forEach(cf => {
  1035. if (!_.contains(cf.boardIds, boardId)) cf.addBoard(boardId);
  1036. });
  1037. }
  1038. // Get label names
  1039. const oldBoard = Boards.findOne(this.boardId);
  1040. const oldBoardLabels = oldBoard.labels;
  1041. const oldCardLabels = _.pluck(
  1042. _.filter(oldBoardLabels, label => {
  1043. return _.contains(this.labelIds, label._id);
  1044. }),
  1045. 'name',
  1046. );
  1047. const newBoard = Boards.findOne(boardId);
  1048. const newBoardLabels = newBoard.labels;
  1049. const newCardLabelIds = _.pluck(
  1050. _.filter(newBoardLabels, label => {
  1051. return label.name && _.contains(oldCardLabels, label.name);
  1052. }),
  1053. '_id',
  1054. );
  1055. const mutatedFields = {
  1056. boardId,
  1057. swimlaneId,
  1058. listId,
  1059. sort,
  1060. labelIds: newCardLabelIds,
  1061. };
  1062. Cards.update(this._id, {
  1063. $set: mutatedFields,
  1064. });
  1065. },
  1066. addLabel(labelId) {
  1067. return {
  1068. $addToSet: {
  1069. labelIds: labelId,
  1070. },
  1071. };
  1072. },
  1073. removeLabel(labelId) {
  1074. return {
  1075. $pull: {
  1076. labelIds: labelId,
  1077. },
  1078. };
  1079. },
  1080. toggleLabel(labelId) {
  1081. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1082. return this.removeLabel(labelId);
  1083. } else {
  1084. return this.addLabel(labelId);
  1085. }
  1086. },
  1087. setColor(newColor) {
  1088. if (newColor === 'white') {
  1089. newColor = null;
  1090. }
  1091. return {
  1092. $set: {
  1093. color: newColor,
  1094. },
  1095. };
  1096. },
  1097. assignMember(memberId) {
  1098. return {
  1099. $addToSet: {
  1100. members: memberId,
  1101. },
  1102. };
  1103. },
  1104. assignAssignee(assigneeId) {
  1105. // If there is not any assignee, allow one assignee, not more.
  1106. if (this.getAssignees().length === 0) {
  1107. return {
  1108. $addToSet: {
  1109. assignees: assigneeId,
  1110. },
  1111. };
  1112. } else {
  1113. return false;
  1114. }
  1115. },
  1116. unassignMember(memberId) {
  1117. return {
  1118. $pull: {
  1119. members: memberId,
  1120. },
  1121. };
  1122. },
  1123. unassignAssignee(assigneeId) {
  1124. return {
  1125. $pull: {
  1126. assignees: assigneeId,
  1127. },
  1128. };
  1129. },
  1130. toggleMember(memberId) {
  1131. if (this.members && this.members.indexOf(memberId) > -1) {
  1132. return this.unassignMember(memberId);
  1133. } else {
  1134. return this.assignMember(memberId);
  1135. }
  1136. },
  1137. toggleAssignee(assigneeId) {
  1138. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1139. return this.unassignAssignee(assigneeId);
  1140. } else {
  1141. return this.assignAssignee(assigneeId);
  1142. }
  1143. },
  1144. assignCustomField(customFieldId) {
  1145. return {
  1146. $addToSet: {
  1147. customFields: {
  1148. _id: customFieldId,
  1149. value: null,
  1150. },
  1151. },
  1152. };
  1153. },
  1154. unassignCustomField(customFieldId) {
  1155. return {
  1156. $pull: {
  1157. customFields: {
  1158. _id: customFieldId,
  1159. },
  1160. },
  1161. };
  1162. },
  1163. toggleCustomField(customFieldId) {
  1164. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1165. return this.unassignCustomField(customFieldId);
  1166. } else {
  1167. return this.assignCustomField(customFieldId);
  1168. }
  1169. },
  1170. setCustomField(customFieldId, value) {
  1171. // todo
  1172. const index = this.customFieldIndex(customFieldId);
  1173. if (index > -1) {
  1174. const update = {
  1175. $set: {},
  1176. };
  1177. update.$set[`customFields.${index}.value`] = value;
  1178. return update;
  1179. }
  1180. // TODO
  1181. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1182. return null;
  1183. },
  1184. setCover(coverId) {
  1185. return {
  1186. $set: {
  1187. coverId,
  1188. },
  1189. };
  1190. },
  1191. unsetCover() {
  1192. return {
  1193. $unset: {
  1194. coverId: '',
  1195. },
  1196. };
  1197. },
  1198. setReceived(receivedAt) {
  1199. return {
  1200. $set: {
  1201. receivedAt,
  1202. },
  1203. };
  1204. },
  1205. unsetReceived() {
  1206. return {
  1207. $unset: {
  1208. receivedAt: '',
  1209. },
  1210. };
  1211. },
  1212. setStart(startAt) {
  1213. return {
  1214. $set: {
  1215. startAt,
  1216. },
  1217. };
  1218. },
  1219. unsetStart() {
  1220. return {
  1221. $unset: {
  1222. startAt: '',
  1223. },
  1224. };
  1225. },
  1226. setDue(dueAt) {
  1227. return {
  1228. $set: {
  1229. dueAt,
  1230. },
  1231. };
  1232. },
  1233. unsetDue() {
  1234. return {
  1235. $unset: {
  1236. dueAt: '',
  1237. },
  1238. };
  1239. },
  1240. setEnd(endAt) {
  1241. return {
  1242. $set: {
  1243. endAt,
  1244. },
  1245. };
  1246. },
  1247. unsetEnd() {
  1248. return {
  1249. $unset: {
  1250. endAt: '',
  1251. },
  1252. };
  1253. },
  1254. setOvertime(isOvertime) {
  1255. return {
  1256. $set: {
  1257. isOvertime,
  1258. },
  1259. };
  1260. },
  1261. setSpentTime(spentTime) {
  1262. return {
  1263. $set: {
  1264. spentTime,
  1265. },
  1266. };
  1267. },
  1268. unsetSpentTime() {
  1269. return {
  1270. $unset: {
  1271. spentTime: '',
  1272. isOvertime: false,
  1273. },
  1274. };
  1275. },
  1276. setParentId(parentId) {
  1277. return {
  1278. $set: {
  1279. parentId,
  1280. },
  1281. };
  1282. },
  1283. });
  1284. //FUNCTIONS FOR creation of Activities
  1285. function updateActivities(doc, fieldNames, modifier) {
  1286. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1287. Activities.find({
  1288. activityType: 'addedLabel',
  1289. cardId: doc._id,
  1290. }).forEach(a => {
  1291. const lidx = doc.labelIds.indexOf(a.labelId);
  1292. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1293. Activities.update(a._id, {
  1294. $set: {
  1295. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1296. boardId: modifier.$set.boardId,
  1297. },
  1298. });
  1299. } else {
  1300. Activities.remove(a._id);
  1301. }
  1302. });
  1303. } else if (_.contains(fieldNames, 'boardId')) {
  1304. Activities.remove({
  1305. activityType: 'addedLabel',
  1306. cardId: doc._id,
  1307. });
  1308. }
  1309. }
  1310. function cardMove(
  1311. userId,
  1312. doc,
  1313. fieldNames,
  1314. oldListId,
  1315. oldSwimlaneId,
  1316. oldBoardId,
  1317. ) {
  1318. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1319. Activities.insert({
  1320. userId,
  1321. activityType: 'moveCardBoard',
  1322. boardName: Boards.findOne(doc.boardId).title,
  1323. boardId: doc.boardId,
  1324. oldBoardId,
  1325. oldBoardName: Boards.findOne(oldBoardId).title,
  1326. cardId: doc._id,
  1327. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1328. swimlaneId: doc.swimlaneId,
  1329. oldSwimlaneId,
  1330. });
  1331. } else if (
  1332. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1333. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1334. ) {
  1335. Activities.insert({
  1336. userId,
  1337. oldListId,
  1338. activityType: 'moveCard',
  1339. listName: Lists.findOne(doc.listId).title,
  1340. listId: doc.listId,
  1341. boardId: doc.boardId,
  1342. cardId: doc._id,
  1343. cardTitle: doc.title,
  1344. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1345. swimlaneId: doc.swimlaneId,
  1346. oldSwimlaneId,
  1347. });
  1348. }
  1349. }
  1350. function cardState(userId, doc, fieldNames) {
  1351. if (_.contains(fieldNames, 'archived')) {
  1352. if (doc.archived) {
  1353. Activities.insert({
  1354. userId,
  1355. activityType: 'archivedCard',
  1356. listName: Lists.findOne(doc.listId).title,
  1357. boardId: doc.boardId,
  1358. listId: doc.listId,
  1359. cardId: doc._id,
  1360. swimlaneId: doc.swimlaneId,
  1361. });
  1362. } else {
  1363. Activities.insert({
  1364. userId,
  1365. activityType: 'restoredCard',
  1366. boardId: doc.boardId,
  1367. listName: Lists.findOne(doc.listId).title,
  1368. listId: doc.listId,
  1369. cardId: doc._id,
  1370. swimlaneId: doc.swimlaneId,
  1371. });
  1372. }
  1373. }
  1374. }
  1375. function cardMembers(userId, doc, fieldNames, modifier) {
  1376. if (!_.contains(fieldNames, 'members')) return;
  1377. let memberId;
  1378. // Say hello to the new member
  1379. if (modifier.$addToSet && modifier.$addToSet.members) {
  1380. memberId = modifier.$addToSet.members;
  1381. const username = Users.findOne(memberId).username;
  1382. if (!_.contains(doc.members, memberId)) {
  1383. Activities.insert({
  1384. userId,
  1385. username,
  1386. activityType: 'joinMember',
  1387. boardId: doc.boardId,
  1388. cardId: doc._id,
  1389. memberId,
  1390. listId: doc.listId,
  1391. swimlaneId: doc.swimlaneId,
  1392. });
  1393. }
  1394. }
  1395. // Say goodbye to the former member
  1396. if (modifier.$pull && modifier.$pull.members) {
  1397. memberId = modifier.$pull.members;
  1398. const username = Users.findOne(memberId).username;
  1399. // Check that the former member is member of the card
  1400. if (_.contains(doc.members, memberId)) {
  1401. Activities.insert({
  1402. userId,
  1403. username,
  1404. activityType: 'unjoinMember',
  1405. boardId: doc.boardId,
  1406. cardId: doc._id,
  1407. memberId,
  1408. listId: doc.listId,
  1409. swimlaneId: doc.swimlaneId,
  1410. });
  1411. }
  1412. }
  1413. }
  1414. function cardAssignees(userId, doc, fieldNames, modifier) {
  1415. if (!_.contains(fieldNames, 'assignees')) return;
  1416. let assigneeId;
  1417. // Say hello to the new assignee
  1418. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1419. assigneeId = modifier.$addToSet.assignees;
  1420. const username = Users.findOne(assigneeId).username;
  1421. if (!_.contains(doc.assignees, assigneeId)) {
  1422. Activities.insert({
  1423. userId,
  1424. username,
  1425. activityType: 'joinAssignee',
  1426. boardId: doc.boardId,
  1427. cardId: doc._id,
  1428. assigneeId,
  1429. listId: doc.listId,
  1430. swimlaneId: doc.swimlaneId,
  1431. });
  1432. }
  1433. }
  1434. // Say goodbye to the former assignee
  1435. if (modifier.$pull && modifier.$pull.assignees) {
  1436. assigneeId = modifier.$pull.assignees;
  1437. const username = Users.findOne(assigneeId).username;
  1438. // Check that the former assignee is assignee of the card
  1439. if (_.contains(doc.assignees, assigneeId)) {
  1440. Activities.insert({
  1441. userId,
  1442. username,
  1443. activityType: 'unjoinAssignee',
  1444. boardId: doc.boardId,
  1445. cardId: doc._id,
  1446. assigneeId,
  1447. listId: doc.listId,
  1448. swimlaneId: doc.swimlaneId,
  1449. });
  1450. }
  1451. }
  1452. }
  1453. function cardLabels(userId, doc, fieldNames, modifier) {
  1454. if (!_.contains(fieldNames, 'labelIds')) return;
  1455. let labelId;
  1456. // Say hello to the new label
  1457. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1458. labelId = modifier.$addToSet.labelIds;
  1459. if (!_.contains(doc.labelIds, labelId)) {
  1460. const act = {
  1461. userId,
  1462. labelId,
  1463. activityType: 'addedLabel',
  1464. boardId: doc.boardId,
  1465. cardId: doc._id,
  1466. listId: doc.listId,
  1467. swimlaneId: doc.swimlaneId,
  1468. };
  1469. Activities.insert(act);
  1470. }
  1471. }
  1472. // Say goodbye to the label
  1473. if (modifier.$pull && modifier.$pull.labelIds) {
  1474. labelId = modifier.$pull.labelIds;
  1475. // Check that the former member is member of the card
  1476. if (_.contains(doc.labelIds, labelId)) {
  1477. Activities.insert({
  1478. userId,
  1479. labelId,
  1480. activityType: 'removedLabel',
  1481. boardId: doc.boardId,
  1482. cardId: doc._id,
  1483. listId: doc.listId,
  1484. swimlaneId: doc.swimlaneId,
  1485. });
  1486. }
  1487. }
  1488. }
  1489. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1490. if (!_.contains(fieldNames, 'customFields')) return;
  1491. // Say hello to the new customField value
  1492. if (modifier.$set) {
  1493. _.each(modifier.$set, (value, key) => {
  1494. if (key.startsWith('customFields')) {
  1495. const dotNotation = key.split('.');
  1496. // only individual changes are registered
  1497. if (dotNotation.length > 1) {
  1498. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1499. const act = {
  1500. userId,
  1501. customFieldId,
  1502. value,
  1503. activityType: 'setCustomField',
  1504. boardId: doc.boardId,
  1505. cardId: doc._id,
  1506. };
  1507. Activities.insert(act);
  1508. }
  1509. }
  1510. });
  1511. }
  1512. // Say goodbye to the former customField value
  1513. if (modifier.$unset) {
  1514. _.each(modifier.$unset, (value, key) => {
  1515. if (key.startsWith('customFields')) {
  1516. const dotNotation = key.split('.');
  1517. // only individual changes are registered
  1518. if (dotNotation.length > 1) {
  1519. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1520. const act = {
  1521. userId,
  1522. customFieldId,
  1523. activityType: 'unsetCustomField',
  1524. boardId: doc.boardId,
  1525. cardId: doc._id,
  1526. };
  1527. Activities.insert(act);
  1528. }
  1529. }
  1530. });
  1531. }
  1532. }
  1533. function cardCreation(userId, doc) {
  1534. Activities.insert({
  1535. userId,
  1536. activityType: 'createCard',
  1537. boardId: doc.boardId,
  1538. listName: Lists.findOne(doc.listId).title,
  1539. listId: doc.listId,
  1540. cardId: doc._id,
  1541. cardTitle: doc.title,
  1542. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1543. swimlaneId: doc.swimlaneId,
  1544. });
  1545. }
  1546. function cardRemover(userId, doc) {
  1547. ChecklistItems.remove({
  1548. cardId: doc._id,
  1549. });
  1550. Checklists.remove({
  1551. cardId: doc._id,
  1552. });
  1553. Cards.remove({
  1554. parentId: doc._id,
  1555. });
  1556. CardComments.remove({
  1557. cardId: doc._id,
  1558. });
  1559. Attachments.remove({
  1560. cardId: doc._id,
  1561. });
  1562. }
  1563. const findDueCards = days => {
  1564. const seekDue = ($from, $to, activityType) => {
  1565. Cards.find({
  1566. archived: false,
  1567. dueAt: { $gte: $from, $lt: $to },
  1568. }).forEach(card => {
  1569. const username = Users.findOne(card.userId).username;
  1570. const activity = {
  1571. userId: card.userId,
  1572. username,
  1573. activityType,
  1574. boardId: card.boardId,
  1575. cardId: card._id,
  1576. cardTitle: card.title,
  1577. listId: card.listId,
  1578. timeValue: card.dueAt,
  1579. swimlaneId: card.swimlaneId,
  1580. };
  1581. Activities.insert(activity);
  1582. });
  1583. };
  1584. const now = new Date(),
  1585. aday = 3600 * 24 * 1e3,
  1586. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  1587. if (!days) return;
  1588. if (!days.map) days = [days];
  1589. days.map(day => {
  1590. let args = [];
  1591. if (day === 0) {
  1592. args = [then(0), then(1), 'duenow'];
  1593. } else if (day > 0) {
  1594. args = [then(1), then(day), 'almostdue'];
  1595. } else {
  1596. args = [then(day), now, 'pastdue'];
  1597. }
  1598. seekDue(...args);
  1599. });
  1600. };
  1601. const addCronJob = _.debounce(
  1602. Meteor.bindEnvironment(function findDueCardsDebounced() {
  1603. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  1604. if (!envValue) {
  1605. return;
  1606. }
  1607. const notifydays = envValue
  1608. .split(',')
  1609. .map(value => {
  1610. const iValue = parseInt(value, 10);
  1611. if (!(iValue > 0 && iValue < 15)) {
  1612. // notifying due is disabled
  1613. return false;
  1614. } else {
  1615. return iValue;
  1616. }
  1617. })
  1618. .filter(Boolean);
  1619. const notifyitvl = process.env.NOTIFY_DUE_AT_HOUR_OF_DAY; //passed in the itvl has to be a number standing for the hour of current time
  1620. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  1621. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  1622. const scheduler = (job => () => {
  1623. const now = new Date();
  1624. const hour = 3600 * 1e3;
  1625. if (now.getHours() === itvl) {
  1626. if (typeof job === 'function') {
  1627. job();
  1628. }
  1629. }
  1630. Meteor.setTimeout(scheduler, hour);
  1631. })(() => {
  1632. findDueCards(notifydays);
  1633. });
  1634. scheduler();
  1635. }),
  1636. 500,
  1637. );
  1638. if (Meteor.isServer) {
  1639. // Cards are often fetched within a board, so we create an index to make these
  1640. // queries more efficient.
  1641. Meteor.startup(() => {
  1642. Cards._collection._ensureIndex({ modifiedAt: -1 });
  1643. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  1644. // https://github.com/wekan/wekan/issues/1863
  1645. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1646. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1647. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1648. // To correct it, add Index to parentId:
  1649. Cards._collection._ensureIndex({ parentId: 1 });
  1650. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  1651. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  1652. // Meteor.call("findDueCards",notifydays,notifyitvl);
  1653. Meteor.defer(() => {
  1654. addCronJob();
  1655. });
  1656. });
  1657. Cards.after.insert((userId, doc) => {
  1658. cardCreation(userId, doc);
  1659. });
  1660. // New activity for card (un)archivage
  1661. Cards.after.update((userId, doc, fieldNames) => {
  1662. cardState(userId, doc, fieldNames);
  1663. });
  1664. //New activity for card moves
  1665. Cards.after.update(function(userId, doc, fieldNames) {
  1666. const oldListId = this.previous.listId;
  1667. const oldSwimlaneId = this.previous.swimlaneId;
  1668. const oldBoardId = this.previous.boardId;
  1669. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  1670. });
  1671. // Add a new activity if we add or remove a member to the card
  1672. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1673. cardMembers(userId, doc, fieldNames, modifier);
  1674. updateActivities(doc, fieldNames, modifier);
  1675. });
  1676. // Add a new activity if we add or remove a assignee to the card
  1677. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1678. cardAssignees(userId, doc, fieldNames, modifier);
  1679. updateActivities(doc, fieldNames, modifier);
  1680. });
  1681. // Add a new activity if we add or remove a label to the card
  1682. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1683. cardLabels(userId, doc, fieldNames, modifier);
  1684. });
  1685. // Add a new activity if we edit a custom field
  1686. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1687. cardCustomFields(userId, doc, fieldNames, modifier);
  1688. });
  1689. // Add a new activity if modify time related field like dueAt startAt etc
  1690. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1691. const dla = 'dateLastActivity';
  1692. const fields = fieldNames.filter(name => name !== dla);
  1693. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  1694. const action = fields[0];
  1695. if (fields.length > 0 && _.contains(timingaction, action)) {
  1696. // add activities for user change these attributes
  1697. const value = modifier.$set[action];
  1698. const oldvalue = doc[action] || '';
  1699. const activityType = `a-${action}`;
  1700. const card = Cards.findOne(doc._id);
  1701. const list = card.list();
  1702. if (list) {
  1703. // change list modifiedAt, when user modified the key values in timingaction array, if it's endAt, put the modifiedAt of list back to one year ago for sorting purpose
  1704. const modifiedAt = new Date(
  1705. new Date(value).getTime() -
  1706. (action === 'endAt' ? 365 * 24 * 3600 * 1e3 : 0),
  1707. ); // set it as 1 year before
  1708. const boardId = list.boardId;
  1709. Lists.direct.update(
  1710. {
  1711. _id: list._id,
  1712. },
  1713. {
  1714. $set: {
  1715. modifiedAt,
  1716. boardId,
  1717. },
  1718. },
  1719. );
  1720. }
  1721. const username = Users.findOne(userId).username;
  1722. const activity = {
  1723. userId,
  1724. username,
  1725. activityType,
  1726. boardId: doc.boardId,
  1727. cardId: doc._id,
  1728. cardTitle: doc.title,
  1729. timeKey: action,
  1730. timeValue: value,
  1731. timeOldValue: oldvalue,
  1732. listId: card.listId,
  1733. swimlaneId: card.swimlaneId,
  1734. };
  1735. Activities.insert(activity);
  1736. }
  1737. });
  1738. // Remove all activities associated with a card if we remove the card
  1739. // Remove also card_comments / checklists / attachments
  1740. Cards.before.remove((userId, doc) => {
  1741. cardRemover(userId, doc);
  1742. });
  1743. }
  1744. //SWIMLANES REST API
  1745. if (Meteor.isServer) {
  1746. /**
  1747. * @operation get_swimlane_cards
  1748. * @summary get all cards attached to a swimlane
  1749. *
  1750. * @param {string} boardId the board ID
  1751. * @param {string} swimlaneId the swimlane ID
  1752. * @return_type [{_id: string,
  1753. * title: string,
  1754. * description: string,
  1755. * listId: string}]
  1756. */
  1757. JsonRoutes.add(
  1758. 'GET',
  1759. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  1760. function(req, res) {
  1761. const paramBoardId = req.params.boardId;
  1762. const paramSwimlaneId = req.params.swimlaneId;
  1763. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1764. JsonRoutes.sendResult(res, {
  1765. code: 200,
  1766. data: Cards.find({
  1767. boardId: paramBoardId,
  1768. swimlaneId: paramSwimlaneId,
  1769. archived: false,
  1770. }).map(function(doc) {
  1771. return {
  1772. _id: doc._id,
  1773. title: doc.title,
  1774. description: doc.description,
  1775. listId: doc.listId,
  1776. };
  1777. }),
  1778. });
  1779. },
  1780. );
  1781. }
  1782. //LISTS REST API
  1783. if (Meteor.isServer) {
  1784. /**
  1785. * @operation get_all_cards
  1786. * @summary Get all Cards attached to a List
  1787. *
  1788. * @param {string} boardId the board ID
  1789. * @param {string} listId the list ID
  1790. * @return_type [{_id: string,
  1791. * title: string,
  1792. * description: string}]
  1793. */
  1794. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  1795. req,
  1796. res,
  1797. ) {
  1798. const paramBoardId = req.params.boardId;
  1799. const paramListId = req.params.listId;
  1800. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1801. JsonRoutes.sendResult(res, {
  1802. code: 200,
  1803. data: Cards.find({
  1804. boardId: paramBoardId,
  1805. listId: paramListId,
  1806. archived: false,
  1807. }).map(function(doc) {
  1808. return {
  1809. _id: doc._id,
  1810. title: doc.title,
  1811. description: doc.description,
  1812. };
  1813. }),
  1814. });
  1815. });
  1816. /**
  1817. * @operation get_card
  1818. * @summary Get a Card
  1819. *
  1820. * @param {string} boardId the board ID
  1821. * @param {string} listId the list ID of the card
  1822. * @param {string} cardId the card ID
  1823. * @return_type Cards
  1824. */
  1825. JsonRoutes.add(
  1826. 'GET',
  1827. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  1828. function(req, res) {
  1829. const paramBoardId = req.params.boardId;
  1830. const paramListId = req.params.listId;
  1831. const paramCardId = req.params.cardId;
  1832. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1833. JsonRoutes.sendResult(res, {
  1834. code: 200,
  1835. data: Cards.findOne({
  1836. _id: paramCardId,
  1837. listId: paramListId,
  1838. boardId: paramBoardId,
  1839. archived: false,
  1840. }),
  1841. });
  1842. },
  1843. );
  1844. /**
  1845. * @operation new_card
  1846. * @summary Create a new Card
  1847. *
  1848. * @param {string} boardId the board ID of the new card
  1849. * @param {string} listId the list ID of the new card
  1850. * @param {string} authorID the user ID of the person owning the card
  1851. * @param {string} parentId the parent ID of the new card
  1852. * @param {string} title the title of the new card
  1853. * @param {string} description the description of the new card
  1854. * @param {string} swimlaneId the swimlane ID of the new card
  1855. * @param {string} [members] the member IDs list of the new card
  1856. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  1857. * @return_type {_id: string}
  1858. */
  1859. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  1860. req,
  1861. res,
  1862. ) {
  1863. // Check user is logged in
  1864. Authentication.checkLoggedIn(req.userId);
  1865. const paramBoardId = req.params.boardId;
  1866. // Check user has permission to add card to the board
  1867. const board = Boards.findOne({
  1868. _id: paramBoardId,
  1869. });
  1870. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  1871. Authentication.checkAdminOrCondition(req.userId, addPermission);
  1872. const paramListId = req.params.listId;
  1873. const paramParentId = req.params.parentId;
  1874. const currentCards = Cards.find(
  1875. {
  1876. listId: paramListId,
  1877. archived: false,
  1878. },
  1879. { sort: ['sort'] },
  1880. );
  1881. const check = Users.findOne({
  1882. _id: req.body.authorId,
  1883. });
  1884. const members = req.body.members || [req.body.authorId];
  1885. const assignees = req.body.assignees;
  1886. if (typeof check !== 'undefined') {
  1887. const id = Cards.direct.insert({
  1888. title: req.body.title,
  1889. boardId: paramBoardId,
  1890. listId: paramListId,
  1891. parentId: paramParentId,
  1892. description: req.body.description,
  1893. userId: req.body.authorId,
  1894. swimlaneId: req.body.swimlaneId,
  1895. sort: currentCards.count(),
  1896. members,
  1897. assignees,
  1898. });
  1899. JsonRoutes.sendResult(res, {
  1900. code: 200,
  1901. data: {
  1902. _id: id,
  1903. },
  1904. });
  1905. const card = Cards.findOne({
  1906. _id: id,
  1907. });
  1908. cardCreation(req.body.authorId, card);
  1909. } else {
  1910. JsonRoutes.sendResult(res, {
  1911. code: 401,
  1912. });
  1913. }
  1914. });
  1915. /*
  1916. * Note for the JSDoc:
  1917. * 'list' will be interpreted as the path parameter
  1918. * 'listID' will be interpreted as the body parameter
  1919. */
  1920. /**
  1921. * @operation edit_card
  1922. * @summary Edit Fields in a Card
  1923. *
  1924. * @description Edit a card
  1925. *
  1926. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1927. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1928. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1929. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1930. * `indigo`:
  1931. *
  1932. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1933. *
  1934. * Note: setting the color to white has the same effect than removing it.
  1935. *
  1936. * @param {string} boardId the board ID of the card
  1937. * @param {string} list the list ID of the card
  1938. * @param {string} cardId the ID of the card
  1939. * @param {string} [title] the new title of the card
  1940. * @param {string} [listId] the new list ID of the card (move operation)
  1941. * @param {string} [description] the new description of the card
  1942. * @param {string} [authorId] change the owner of the card
  1943. * @param {string} [parentId] change the parent of the card
  1944. * @param {string} [labelIds] the new list of label IDs attached to the card
  1945. * @param {string} [swimlaneId] the new swimlane ID of the card
  1946. * @param {string} [members] the new list of member IDs attached to the card
  1947. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  1948. * @param {string} [requestedBy] the new requestedBy field of the card
  1949. * @param {string} [assignedBy] the new assignedBy field of the card
  1950. * @param {string} [receivedAt] the new receivedAt field of the card
  1951. * @param {string} [assignBy] the new assignBy field of the card
  1952. * @param {string} [startAt] the new startAt field of the card
  1953. * @param {string} [dueAt] the new dueAt field of the card
  1954. * @param {string} [endAt] the new endAt field of the card
  1955. * @param {string} [spentTime] the new spentTime field of the card
  1956. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1957. * @param {string} [customFields] the new customFields value of the card
  1958. * @param {string} [color] the new color of the card
  1959. * @return_type {_id: string}
  1960. */
  1961. JsonRoutes.add(
  1962. 'PUT',
  1963. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  1964. function(req, res) {
  1965. Authentication.checkUserId(req.userId);
  1966. const paramBoardId = req.params.boardId;
  1967. const paramCardId = req.params.cardId;
  1968. const paramListId = req.params.listId;
  1969. if (req.body.hasOwnProperty('title')) {
  1970. const newTitle = req.body.title;
  1971. Cards.direct.update(
  1972. {
  1973. _id: paramCardId,
  1974. listId: paramListId,
  1975. boardId: paramBoardId,
  1976. archived: false,
  1977. },
  1978. {
  1979. $set: {
  1980. title: newTitle,
  1981. },
  1982. },
  1983. );
  1984. }
  1985. if (req.body.hasOwnProperty('listId')) {
  1986. const newParamListId = req.body.listId;
  1987. Cards.direct.update(
  1988. {
  1989. _id: paramCardId,
  1990. listId: paramListId,
  1991. boardId: paramBoardId,
  1992. archived: false,
  1993. },
  1994. {
  1995. $set: {
  1996. listId: newParamListId,
  1997. },
  1998. },
  1999. );
  2000. const card = Cards.findOne({
  2001. _id: paramCardId,
  2002. });
  2003. cardMove(
  2004. req.body.authorId,
  2005. card,
  2006. {
  2007. fieldName: 'listId',
  2008. },
  2009. paramListId,
  2010. );
  2011. }
  2012. if (req.body.hasOwnProperty('parentId')) {
  2013. const newParentId = req.body.parentId;
  2014. Cards.direct.update(
  2015. {
  2016. _id: paramCardId,
  2017. listId: paramListId,
  2018. boardId: paramBoardId,
  2019. archived: false,
  2020. },
  2021. {
  2022. $set: {
  2023. parentId: newParentId,
  2024. },
  2025. },
  2026. );
  2027. }
  2028. if (req.body.hasOwnProperty('description')) {
  2029. const newDescription = req.body.description;
  2030. Cards.direct.update(
  2031. {
  2032. _id: paramCardId,
  2033. listId: paramListId,
  2034. boardId: paramBoardId,
  2035. archived: false,
  2036. },
  2037. {
  2038. $set: {
  2039. description: newDescription,
  2040. },
  2041. },
  2042. );
  2043. }
  2044. if (req.body.hasOwnProperty('color')) {
  2045. const newColor = req.body.color;
  2046. Cards.direct.update(
  2047. {
  2048. _id: paramCardId,
  2049. listId: paramListId,
  2050. boardId: paramBoardId,
  2051. archived: false,
  2052. },
  2053. { $set: { color: newColor } },
  2054. );
  2055. }
  2056. if (req.body.hasOwnProperty('labelIds')) {
  2057. let newlabelIds = req.body.labelIds;
  2058. if (_.isString(newlabelIds)) {
  2059. if (newlabelIds === '') {
  2060. newlabelIds = null;
  2061. } else {
  2062. newlabelIds = [newlabelIds];
  2063. }
  2064. }
  2065. Cards.direct.update(
  2066. {
  2067. _id: paramCardId,
  2068. listId: paramListId,
  2069. boardId: paramBoardId,
  2070. archived: false,
  2071. },
  2072. {
  2073. $set: {
  2074. labelIds: newlabelIds,
  2075. },
  2076. },
  2077. );
  2078. }
  2079. if (req.body.hasOwnProperty('requestedBy')) {
  2080. const newrequestedBy = req.body.requestedBy;
  2081. Cards.direct.update(
  2082. {
  2083. _id: paramCardId,
  2084. listId: paramListId,
  2085. boardId: paramBoardId,
  2086. archived: false,
  2087. },
  2088. { $set: { requestedBy: newrequestedBy } },
  2089. );
  2090. }
  2091. if (req.body.hasOwnProperty('assignedBy')) {
  2092. const newassignedBy = req.body.assignedBy;
  2093. Cards.direct.update(
  2094. {
  2095. _id: paramCardId,
  2096. listId: paramListId,
  2097. boardId: paramBoardId,
  2098. archived: false,
  2099. },
  2100. { $set: { assignedBy: newassignedBy } },
  2101. );
  2102. }
  2103. if (req.body.hasOwnProperty('receivedAt')) {
  2104. const newreceivedAt = req.body.receivedAt;
  2105. Cards.direct.update(
  2106. {
  2107. _id: paramCardId,
  2108. listId: paramListId,
  2109. boardId: paramBoardId,
  2110. archived: false,
  2111. },
  2112. { $set: { receivedAt: newreceivedAt } },
  2113. );
  2114. }
  2115. if (req.body.hasOwnProperty('startAt')) {
  2116. const newstartAt = req.body.startAt;
  2117. Cards.direct.update(
  2118. {
  2119. _id: paramCardId,
  2120. listId: paramListId,
  2121. boardId: paramBoardId,
  2122. archived: false,
  2123. },
  2124. { $set: { startAt: newstartAt } },
  2125. );
  2126. }
  2127. if (req.body.hasOwnProperty('dueAt')) {
  2128. const newdueAt = req.body.dueAt;
  2129. Cards.direct.update(
  2130. {
  2131. _id: paramCardId,
  2132. listId: paramListId,
  2133. boardId: paramBoardId,
  2134. archived: false,
  2135. },
  2136. { $set: { dueAt: newdueAt } },
  2137. );
  2138. }
  2139. if (req.body.hasOwnProperty('endAt')) {
  2140. const newendAt = req.body.endAt;
  2141. Cards.direct.update(
  2142. {
  2143. _id: paramCardId,
  2144. listId: paramListId,
  2145. boardId: paramBoardId,
  2146. archived: false,
  2147. },
  2148. { $set: { endAt: newendAt } },
  2149. );
  2150. }
  2151. if (req.body.hasOwnProperty('spentTime')) {
  2152. const newspentTime = req.body.spentTime;
  2153. Cards.direct.update(
  2154. {
  2155. _id: paramCardId,
  2156. listId: paramListId,
  2157. boardId: paramBoardId,
  2158. archived: false,
  2159. },
  2160. { $set: { spentTime: newspentTime } },
  2161. );
  2162. }
  2163. if (req.body.hasOwnProperty('isOverTime')) {
  2164. const newisOverTime = req.body.isOverTime;
  2165. Cards.direct.update(
  2166. {
  2167. _id: paramCardId,
  2168. listId: paramListId,
  2169. boardId: paramBoardId,
  2170. archived: false,
  2171. },
  2172. { $set: { isOverTime: newisOverTime } },
  2173. );
  2174. }
  2175. if (req.body.hasOwnProperty('customFields')) {
  2176. const newcustomFields = req.body.customFields;
  2177. Cards.direct.update(
  2178. {
  2179. _id: paramCardId,
  2180. listId: paramListId,
  2181. boardId: paramBoardId,
  2182. archived: false,
  2183. },
  2184. { $set: { customFields: newcustomFields } },
  2185. );
  2186. }
  2187. if (req.body.hasOwnProperty('members')) {
  2188. let newmembers = req.body.members;
  2189. if (_.isString(newmembers)) {
  2190. if (newmembers === '') {
  2191. newmembers = null;
  2192. } else {
  2193. newmembers = [newmembers];
  2194. }
  2195. }
  2196. Cards.direct.update(
  2197. {
  2198. _id: paramCardId,
  2199. listId: paramListId,
  2200. boardId: paramBoardId,
  2201. archived: false,
  2202. },
  2203. { $set: { members: newmembers } },
  2204. );
  2205. }
  2206. if (req.body.hasOwnProperty('assignees')) {
  2207. let newassignees = req.body.assignees;
  2208. if (_.isString(newassignees)) {
  2209. if (newassignees === '') {
  2210. newassignees = null;
  2211. } else {
  2212. newassignees = [newassignees];
  2213. }
  2214. }
  2215. Cards.direct.update(
  2216. {
  2217. _id: paramCardId,
  2218. listId: paramListId,
  2219. boardId: paramBoardId,
  2220. archived: false,
  2221. },
  2222. { $set: { assignees: newassignees } },
  2223. );
  2224. }
  2225. if (req.body.hasOwnProperty('swimlaneId')) {
  2226. const newParamSwimlaneId = req.body.swimlaneId;
  2227. Cards.direct.update(
  2228. {
  2229. _id: paramCardId,
  2230. listId: paramListId,
  2231. boardId: paramBoardId,
  2232. archived: false,
  2233. },
  2234. { $set: { swimlaneId: newParamSwimlaneId } },
  2235. );
  2236. }
  2237. JsonRoutes.sendResult(res, {
  2238. code: 200,
  2239. data: {
  2240. _id: paramCardId,
  2241. },
  2242. });
  2243. },
  2244. );
  2245. /**
  2246. * @operation delete_card
  2247. * @summary Delete a card from a board
  2248. *
  2249. * @description This operation **deletes** a card, and therefore the card
  2250. * is not put in the recycle bin.
  2251. *
  2252. * @param {string} boardId the board ID of the card
  2253. * @param {string} list the list ID of the card
  2254. * @param {string} cardId the ID of the card
  2255. * @return_type {_id: string}
  2256. */
  2257. JsonRoutes.add(
  2258. 'DELETE',
  2259. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2260. function(req, res) {
  2261. Authentication.checkUserId(req.userId);
  2262. const paramBoardId = req.params.boardId;
  2263. const paramListId = req.params.listId;
  2264. const paramCardId = req.params.cardId;
  2265. const card = Cards.findOne({
  2266. _id: paramCardId,
  2267. });
  2268. Cards.direct.remove({
  2269. _id: paramCardId,
  2270. listId: paramListId,
  2271. boardId: paramBoardId,
  2272. });
  2273. cardRemover(req.body.authorId, card);
  2274. JsonRoutes.sendResult(res, {
  2275. code: 200,
  2276. data: {
  2277. _id: paramCardId,
  2278. },
  2279. });
  2280. },
  2281. );
  2282. }
  2283. export default Cards;