cards.js 55 KB

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