cards.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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 =
  1461. parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER, 10) || 2; // default as 2 days before and after
  1462. if (!(notifydays > 0 && notifydays < 15)) {
  1463. // notifying due is disabled
  1464. return;
  1465. }
  1466. 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
  1467. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  1468. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  1469. const scheduler = (job => () => {
  1470. const now = new Date();
  1471. const hour = 3600 * 1e3;
  1472. if (now.getHours() === itvl) {
  1473. if (typeof job === 'function') {
  1474. job();
  1475. }
  1476. }
  1477. Meteor.setTimeout(scheduler, hour);
  1478. })(() => {
  1479. findDueCards(notifydays);
  1480. });
  1481. scheduler();
  1482. }),
  1483. 500,
  1484. );
  1485. if (Meteor.isServer) {
  1486. // Cards are often fetched within a board, so we create an index to make these
  1487. // queries more efficient.
  1488. Meteor.startup(() => {
  1489. Cards._collection._ensureIndex({ modifiedAt: -1 });
  1490. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  1491. // https://github.com/wekan/wekan/issues/1863
  1492. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1493. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1494. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1495. // To correct it, add Index to parentId:
  1496. Cards._collection._ensureIndex({ parentId: 1 });
  1497. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  1498. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  1499. // Meteor.call("findDueCards",notifydays,notifyitvl);
  1500. Meteor.defer(() => {
  1501. addCronJob();
  1502. });
  1503. });
  1504. Cards.after.insert((userId, doc) => {
  1505. cardCreation(userId, doc);
  1506. });
  1507. // New activity for card (un)archivage
  1508. Cards.after.update((userId, doc, fieldNames) => {
  1509. cardState(userId, doc, fieldNames);
  1510. });
  1511. //New activity for card moves
  1512. Cards.after.update(function(userId, doc, fieldNames) {
  1513. const oldListId = this.previous.listId;
  1514. const oldSwimlaneId = this.previous.swimlaneId;
  1515. const oldBoardId = this.previous.boardId;
  1516. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  1517. });
  1518. // Add a new activity if we add or remove a member to the card
  1519. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1520. cardMembers(userId, doc, fieldNames, modifier);
  1521. updateActivities(doc, fieldNames, modifier);
  1522. });
  1523. // Add a new activity if we add or remove a label to the card
  1524. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1525. cardLabels(userId, doc, fieldNames, modifier);
  1526. });
  1527. // Add a new activity if we edit a custom field
  1528. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1529. cardCustomFields(userId, doc, fieldNames, modifier);
  1530. });
  1531. // Add a new activity if modify time related field like dueAt startAt etc
  1532. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1533. const dla = 'dateLastActivity';
  1534. const fields = fieldNames.filter(name => name !== dla);
  1535. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  1536. const action = fields[0];
  1537. if (fields.length > 0 && _.contains(timingaction, action)) {
  1538. // add activities for user change these attributes
  1539. const value = modifier.$set[action];
  1540. const oldvalue = doc[action] || '';
  1541. const activityType = `a-${action}`;
  1542. const card = Cards.findOne(doc._id);
  1543. const username = Users.findOne(userId).username;
  1544. const activity = {
  1545. userId,
  1546. username,
  1547. activityType,
  1548. boardId: doc.boardId,
  1549. cardId: doc._id,
  1550. cardTitle: doc.title,
  1551. timeKey: action,
  1552. timeValue: value,
  1553. timeOldValue: oldvalue,
  1554. listId: card.listId,
  1555. swimlaneId: card.swimlaneId,
  1556. };
  1557. Activities.insert(activity);
  1558. }
  1559. });
  1560. // Remove all activities associated with a card if we remove the card
  1561. // Remove also card_comments / checklists / attachments
  1562. Cards.before.remove((userId, doc) => {
  1563. cardRemover(userId, doc);
  1564. });
  1565. }
  1566. //SWIMLANES REST API
  1567. if (Meteor.isServer) {
  1568. /**
  1569. * @operation get_swimlane_cards
  1570. * @summary get all cards attached to a swimlane
  1571. *
  1572. * @param {string} boardId the board ID
  1573. * @param {string} swimlaneId the swimlane ID
  1574. * @return_type [{_id: string,
  1575. * title: string,
  1576. * description: string,
  1577. * listId: string}]
  1578. */
  1579. JsonRoutes.add(
  1580. 'GET',
  1581. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  1582. function(req, res) {
  1583. const paramBoardId = req.params.boardId;
  1584. const paramSwimlaneId = req.params.swimlaneId;
  1585. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1586. JsonRoutes.sendResult(res, {
  1587. code: 200,
  1588. data: Cards.find({
  1589. boardId: paramBoardId,
  1590. swimlaneId: paramSwimlaneId,
  1591. archived: false,
  1592. }).map(function(doc) {
  1593. return {
  1594. _id: doc._id,
  1595. title: doc.title,
  1596. description: doc.description,
  1597. listId: doc.listId,
  1598. };
  1599. }),
  1600. });
  1601. },
  1602. );
  1603. }
  1604. //LISTS REST API
  1605. if (Meteor.isServer) {
  1606. /**
  1607. * @operation get_all_cards
  1608. * @summary Get all Cards attached to a List
  1609. *
  1610. * @param {string} boardId the board ID
  1611. * @param {string} listId the list ID
  1612. * @return_type [{_id: string,
  1613. * title: string,
  1614. * description: string}]
  1615. */
  1616. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  1617. req,
  1618. res,
  1619. ) {
  1620. const paramBoardId = req.params.boardId;
  1621. const paramListId = req.params.listId;
  1622. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1623. JsonRoutes.sendResult(res, {
  1624. code: 200,
  1625. data: Cards.find({
  1626. boardId: paramBoardId,
  1627. listId: paramListId,
  1628. archived: false,
  1629. }).map(function(doc) {
  1630. return {
  1631. _id: doc._id,
  1632. title: doc.title,
  1633. description: doc.description,
  1634. };
  1635. }),
  1636. });
  1637. });
  1638. /**
  1639. * @operation get_card
  1640. * @summary Get a Card
  1641. *
  1642. * @param {string} boardId the board ID
  1643. * @param {string} listId the list ID of the card
  1644. * @param {string} cardId the card ID
  1645. * @return_type Cards
  1646. */
  1647. JsonRoutes.add(
  1648. 'GET',
  1649. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  1650. function(req, res) {
  1651. const paramBoardId = req.params.boardId;
  1652. const paramListId = req.params.listId;
  1653. const paramCardId = req.params.cardId;
  1654. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1655. JsonRoutes.sendResult(res, {
  1656. code: 200,
  1657. data: Cards.findOne({
  1658. _id: paramCardId,
  1659. listId: paramListId,
  1660. boardId: paramBoardId,
  1661. archived: false,
  1662. }),
  1663. });
  1664. },
  1665. );
  1666. /**
  1667. * @operation new_card
  1668. * @summary Create a new Card
  1669. *
  1670. * @param {string} boardId the board ID of the new card
  1671. * @param {string} listId the list ID of the new card
  1672. * @param {string} authorID the user ID of the person owning the card
  1673. * @param {string} parentId the parent ID of the new card
  1674. * @param {string} title the title of the new card
  1675. * @param {string} description the description of the new card
  1676. * @param {string} swimlaneId the swimlane ID of the new card
  1677. * @param {string} [members] the member IDs list of the new card
  1678. * @return_type {_id: string}
  1679. */
  1680. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  1681. req,
  1682. res,
  1683. ) {
  1684. Authentication.checkUserId(req.userId);
  1685. const paramBoardId = req.params.boardId;
  1686. const paramListId = req.params.listId;
  1687. const paramParentId = req.params.parentId;
  1688. const currentCards = Cards.find(
  1689. {
  1690. listId: paramListId,
  1691. archived: false,
  1692. },
  1693. { sort: ['sort'] },
  1694. );
  1695. const check = Users.findOne({
  1696. _id: req.body.authorId,
  1697. });
  1698. const members = req.body.members || [req.body.authorId];
  1699. if (typeof check !== 'undefined') {
  1700. const id = Cards.direct.insert({
  1701. title: req.body.title,
  1702. boardId: paramBoardId,
  1703. listId: paramListId,
  1704. parentId: paramParentId,
  1705. description: req.body.description,
  1706. userId: req.body.authorId,
  1707. swimlaneId: req.body.swimlaneId,
  1708. sort: currentCards.count(),
  1709. members,
  1710. });
  1711. JsonRoutes.sendResult(res, {
  1712. code: 200,
  1713. data: {
  1714. _id: id,
  1715. },
  1716. });
  1717. const card = Cards.findOne({
  1718. _id: id,
  1719. });
  1720. cardCreation(req.body.authorId, card);
  1721. } else {
  1722. JsonRoutes.sendResult(res, {
  1723. code: 401,
  1724. });
  1725. }
  1726. });
  1727. /*
  1728. * Note for the JSDoc:
  1729. * 'list' will be interpreted as the path parameter
  1730. * 'listID' will be interpreted as the body parameter
  1731. */
  1732. /**
  1733. * @operation edit_card
  1734. * @summary Edit Fields in a Card
  1735. *
  1736. * @description Edit a card
  1737. *
  1738. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1739. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1740. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1741. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1742. * `indigo`:
  1743. *
  1744. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1745. *
  1746. * Note: setting the color to white has the same effect than removing it.
  1747. *
  1748. * @param {string} boardId the board ID of the card
  1749. * @param {string} list the list ID of the card
  1750. * @param {string} cardId the ID of the card
  1751. * @param {string} [title] the new title of the card
  1752. * @param {string} [listId] the new list ID of the card (move operation)
  1753. * @param {string} [description] the new description of the card
  1754. * @param {string} [authorId] change the owner of the card
  1755. * @param {string} [parentId] change the parent of the card
  1756. * @param {string} [labelIds] the new list of label IDs attached to the card
  1757. * @param {string} [swimlaneId] the new swimlane ID of the card
  1758. * @param {string} [members] the new list of member IDs attached to the card
  1759. * @param {string} [requestedBy] the new requestedBy field of the card
  1760. * @param {string} [assignedBy] the new assignedBy field of the card
  1761. * @param {string} [receivedAt] the new receivedAt field of the card
  1762. * @param {string} [assignBy] the new assignBy field of the card
  1763. * @param {string} [startAt] the new startAt field of the card
  1764. * @param {string} [dueAt] the new dueAt field of the card
  1765. * @param {string} [endAt] the new endAt field of the card
  1766. * @param {string} [spentTime] the new spentTime field of the card
  1767. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1768. * @param {string} [customFields] the new customFields value of the card
  1769. * @param {string} [color] the new color of the card
  1770. * @return_type {_id: string}
  1771. */
  1772. JsonRoutes.add(
  1773. 'PUT',
  1774. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  1775. function(req, res) {
  1776. Authentication.checkUserId(req.userId);
  1777. const paramBoardId = req.params.boardId;
  1778. const paramCardId = req.params.cardId;
  1779. const paramListId = req.params.listId;
  1780. if (req.body.hasOwnProperty('title')) {
  1781. const newTitle = req.body.title;
  1782. Cards.direct.update(
  1783. {
  1784. _id: paramCardId,
  1785. listId: paramListId,
  1786. boardId: paramBoardId,
  1787. archived: false,
  1788. },
  1789. {
  1790. $set: {
  1791. title: newTitle,
  1792. },
  1793. },
  1794. );
  1795. }
  1796. if (req.body.hasOwnProperty('listId')) {
  1797. const newParamListId = req.body.listId;
  1798. Cards.direct.update(
  1799. {
  1800. _id: paramCardId,
  1801. listId: paramListId,
  1802. boardId: paramBoardId,
  1803. archived: false,
  1804. },
  1805. {
  1806. $set: {
  1807. listId: newParamListId,
  1808. },
  1809. },
  1810. );
  1811. const card = Cards.findOne({
  1812. _id: paramCardId,
  1813. });
  1814. cardMove(
  1815. req.body.authorId,
  1816. card,
  1817. {
  1818. fieldName: 'listId',
  1819. },
  1820. paramListId,
  1821. );
  1822. }
  1823. if (req.body.hasOwnProperty('parentId')) {
  1824. const newParentId = req.body.parentId;
  1825. Cards.direct.update(
  1826. {
  1827. _id: paramCardId,
  1828. listId: paramListId,
  1829. boardId: paramBoardId,
  1830. archived: false,
  1831. },
  1832. {
  1833. $set: {
  1834. parentId: newParentId,
  1835. },
  1836. },
  1837. );
  1838. }
  1839. if (req.body.hasOwnProperty('description')) {
  1840. const newDescription = req.body.description;
  1841. Cards.direct.update(
  1842. {
  1843. _id: paramCardId,
  1844. listId: paramListId,
  1845. boardId: paramBoardId,
  1846. archived: false,
  1847. },
  1848. {
  1849. $set: {
  1850. description: newDescription,
  1851. },
  1852. },
  1853. );
  1854. }
  1855. if (req.body.hasOwnProperty('color')) {
  1856. const newColor = req.body.color;
  1857. Cards.direct.update(
  1858. {
  1859. _id: paramCardId,
  1860. listId: paramListId,
  1861. boardId: paramBoardId,
  1862. archived: false,
  1863. },
  1864. { $set: { color: newColor } },
  1865. );
  1866. }
  1867. if (req.body.hasOwnProperty('labelIds')) {
  1868. let newlabelIds = req.body.labelIds;
  1869. if (_.isString(newlabelIds)) {
  1870. if (newlabelIds === '') {
  1871. newlabelIds = null;
  1872. } else {
  1873. newlabelIds = [newlabelIds];
  1874. }
  1875. }
  1876. Cards.direct.update(
  1877. {
  1878. _id: paramCardId,
  1879. listId: paramListId,
  1880. boardId: paramBoardId,
  1881. archived: false,
  1882. },
  1883. {
  1884. $set: {
  1885. labelIds: newlabelIds,
  1886. },
  1887. },
  1888. );
  1889. }
  1890. if (req.body.hasOwnProperty('requestedBy')) {
  1891. const newrequestedBy = req.body.requestedBy;
  1892. Cards.direct.update(
  1893. {
  1894. _id: paramCardId,
  1895. listId: paramListId,
  1896. boardId: paramBoardId,
  1897. archived: false,
  1898. },
  1899. { $set: { requestedBy: newrequestedBy } },
  1900. );
  1901. }
  1902. if (req.body.hasOwnProperty('assignedBy')) {
  1903. const newassignedBy = req.body.assignedBy;
  1904. Cards.direct.update(
  1905. {
  1906. _id: paramCardId,
  1907. listId: paramListId,
  1908. boardId: paramBoardId,
  1909. archived: false,
  1910. },
  1911. { $set: { assignedBy: newassignedBy } },
  1912. );
  1913. }
  1914. if (req.body.hasOwnProperty('receivedAt')) {
  1915. const newreceivedAt = req.body.receivedAt;
  1916. Cards.direct.update(
  1917. {
  1918. _id: paramCardId,
  1919. listId: paramListId,
  1920. boardId: paramBoardId,
  1921. archived: false,
  1922. },
  1923. { $set: { receivedAt: newreceivedAt } },
  1924. );
  1925. }
  1926. if (req.body.hasOwnProperty('startAt')) {
  1927. const newstartAt = req.body.startAt;
  1928. Cards.direct.update(
  1929. {
  1930. _id: paramCardId,
  1931. listId: paramListId,
  1932. boardId: paramBoardId,
  1933. archived: false,
  1934. },
  1935. { $set: { startAt: newstartAt } },
  1936. );
  1937. }
  1938. if (req.body.hasOwnProperty('dueAt')) {
  1939. const newdueAt = req.body.dueAt;
  1940. Cards.direct.update(
  1941. {
  1942. _id: paramCardId,
  1943. listId: paramListId,
  1944. boardId: paramBoardId,
  1945. archived: false,
  1946. },
  1947. { $set: { dueAt: newdueAt } },
  1948. );
  1949. }
  1950. if (req.body.hasOwnProperty('endAt')) {
  1951. const newendAt = req.body.endAt;
  1952. Cards.direct.update(
  1953. {
  1954. _id: paramCardId,
  1955. listId: paramListId,
  1956. boardId: paramBoardId,
  1957. archived: false,
  1958. },
  1959. { $set: { endAt: newendAt } },
  1960. );
  1961. }
  1962. if (req.body.hasOwnProperty('spentTime')) {
  1963. const newspentTime = req.body.spentTime;
  1964. Cards.direct.update(
  1965. {
  1966. _id: paramCardId,
  1967. listId: paramListId,
  1968. boardId: paramBoardId,
  1969. archived: false,
  1970. },
  1971. { $set: { spentTime: newspentTime } },
  1972. );
  1973. }
  1974. if (req.body.hasOwnProperty('isOverTime')) {
  1975. const newisOverTime = req.body.isOverTime;
  1976. Cards.direct.update(
  1977. {
  1978. _id: paramCardId,
  1979. listId: paramListId,
  1980. boardId: paramBoardId,
  1981. archived: false,
  1982. },
  1983. { $set: { isOverTime: newisOverTime } },
  1984. );
  1985. }
  1986. if (req.body.hasOwnProperty('customFields')) {
  1987. const newcustomFields = req.body.customFields;
  1988. Cards.direct.update(
  1989. {
  1990. _id: paramCardId,
  1991. listId: paramListId,
  1992. boardId: paramBoardId,
  1993. archived: false,
  1994. },
  1995. { $set: { customFields: newcustomFields } },
  1996. );
  1997. }
  1998. if (req.body.hasOwnProperty('members')) {
  1999. let newmembers = req.body.members;
  2000. if (_.isString(newmembers)) {
  2001. if (newmembers === '') {
  2002. newmembers = null;
  2003. } else {
  2004. newmembers = [newmembers];
  2005. }
  2006. }
  2007. Cards.direct.update(
  2008. {
  2009. _id: paramCardId,
  2010. listId: paramListId,
  2011. boardId: paramBoardId,
  2012. archived: false,
  2013. },
  2014. { $set: { members: newmembers } },
  2015. );
  2016. }
  2017. if (req.body.hasOwnProperty('swimlaneId')) {
  2018. const newParamSwimlaneId = req.body.swimlaneId;
  2019. Cards.direct.update(
  2020. {
  2021. _id: paramCardId,
  2022. listId: paramListId,
  2023. boardId: paramBoardId,
  2024. archived: false,
  2025. },
  2026. { $set: { swimlaneId: newParamSwimlaneId } },
  2027. );
  2028. }
  2029. JsonRoutes.sendResult(res, {
  2030. code: 200,
  2031. data: {
  2032. _id: paramCardId,
  2033. },
  2034. });
  2035. },
  2036. );
  2037. /**
  2038. * @operation delete_card
  2039. * @summary Delete a card from a board
  2040. *
  2041. * @description This operation **deletes** a card, and therefore the card
  2042. * is not put in the recycle bin.
  2043. *
  2044. * @param {string} boardId the board ID of the card
  2045. * @param {string} list the list ID of the card
  2046. * @param {string} cardId the ID of the card
  2047. * @return_type {_id: string}
  2048. */
  2049. JsonRoutes.add(
  2050. 'DELETE',
  2051. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2052. function(req, res) {
  2053. Authentication.checkUserId(req.userId);
  2054. const paramBoardId = req.params.boardId;
  2055. const paramListId = req.params.listId;
  2056. const paramCardId = req.params.cardId;
  2057. Cards.direct.remove({
  2058. _id: paramCardId,
  2059. listId: paramListId,
  2060. boardId: paramBoardId,
  2061. });
  2062. const card = Cards.find({
  2063. _id: paramCardId,
  2064. });
  2065. cardRemover(req.body.authorId, card);
  2066. JsonRoutes.sendResult(res, {
  2067. code: 200,
  2068. data: {
  2069. _id: paramCardId,
  2070. },
  2071. });
  2072. },
  2073. );
  2074. }
  2075. export default Cards;