cards.js 54 KB

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