cards.js 54 KB

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