cards.js 60 KB

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