cards.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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) {
  1568. // change list modifiedAt
  1569. const modifiedAt = new Date();
  1570. const boardId = list.boardId;
  1571. Lists.direct.update(
  1572. {
  1573. _id: list._id,
  1574. },
  1575. {
  1576. $set: {
  1577. modifiedAt,
  1578. boardId,
  1579. },
  1580. },
  1581. );
  1582. }
  1583. const username = Users.findOne(userId).username;
  1584. const activity = {
  1585. userId,
  1586. username,
  1587. activityType,
  1588. boardId: doc.boardId,
  1589. cardId: doc._id,
  1590. cardTitle: doc.title,
  1591. timeKey: action,
  1592. timeValue: value,
  1593. timeOldValue: oldvalue,
  1594. listId: card.listId,
  1595. swimlaneId: card.swimlaneId,
  1596. };
  1597. Activities.insert(activity);
  1598. }
  1599. });
  1600. // Remove all activities associated with a card if we remove the card
  1601. // Remove also card_comments / checklists / attachments
  1602. Cards.before.remove((userId, doc) => {
  1603. cardRemover(userId, doc);
  1604. });
  1605. }
  1606. //SWIMLANES REST API
  1607. if (Meteor.isServer) {
  1608. /**
  1609. * @operation get_swimlane_cards
  1610. * @summary get all cards attached to a swimlane
  1611. *
  1612. * @param {string} boardId the board ID
  1613. * @param {string} swimlaneId the swimlane ID
  1614. * @return_type [{_id: string,
  1615. * title: string,
  1616. * description: string,
  1617. * listId: string}]
  1618. */
  1619. JsonRoutes.add(
  1620. 'GET',
  1621. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  1622. function(req, res) {
  1623. const paramBoardId = req.params.boardId;
  1624. const paramSwimlaneId = req.params.swimlaneId;
  1625. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1626. JsonRoutes.sendResult(res, {
  1627. code: 200,
  1628. data: Cards.find({
  1629. boardId: paramBoardId,
  1630. swimlaneId: paramSwimlaneId,
  1631. archived: false,
  1632. }).map(function(doc) {
  1633. return {
  1634. _id: doc._id,
  1635. title: doc.title,
  1636. description: doc.description,
  1637. listId: doc.listId,
  1638. };
  1639. }),
  1640. });
  1641. },
  1642. );
  1643. }
  1644. //LISTS REST API
  1645. if (Meteor.isServer) {
  1646. /**
  1647. * @operation get_all_cards
  1648. * @summary Get all Cards attached to a List
  1649. *
  1650. * @param {string} boardId the board ID
  1651. * @param {string} listId the list ID
  1652. * @return_type [{_id: string,
  1653. * title: string,
  1654. * description: string}]
  1655. */
  1656. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  1657. req,
  1658. res,
  1659. ) {
  1660. const paramBoardId = req.params.boardId;
  1661. const paramListId = req.params.listId;
  1662. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1663. JsonRoutes.sendResult(res, {
  1664. code: 200,
  1665. data: Cards.find({
  1666. boardId: paramBoardId,
  1667. listId: paramListId,
  1668. archived: false,
  1669. }).map(function(doc) {
  1670. return {
  1671. _id: doc._id,
  1672. title: doc.title,
  1673. description: doc.description,
  1674. };
  1675. }),
  1676. });
  1677. });
  1678. /**
  1679. * @operation get_card
  1680. * @summary Get a Card
  1681. *
  1682. * @param {string} boardId the board ID
  1683. * @param {string} listId the list ID of the card
  1684. * @param {string} cardId the card ID
  1685. * @return_type Cards
  1686. */
  1687. JsonRoutes.add(
  1688. 'GET',
  1689. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  1690. function(req, res) {
  1691. const paramBoardId = req.params.boardId;
  1692. const paramListId = req.params.listId;
  1693. const paramCardId = req.params.cardId;
  1694. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1695. JsonRoutes.sendResult(res, {
  1696. code: 200,
  1697. data: Cards.findOne({
  1698. _id: paramCardId,
  1699. listId: paramListId,
  1700. boardId: paramBoardId,
  1701. archived: false,
  1702. }),
  1703. });
  1704. },
  1705. );
  1706. /**
  1707. * @operation new_card
  1708. * @summary Create a new Card
  1709. *
  1710. * @param {string} boardId the board ID of the new card
  1711. * @param {string} listId the list ID of the new card
  1712. * @param {string} authorID the user ID of the person owning the card
  1713. * @param {string} parentId the parent ID of the new card
  1714. * @param {string} title the title of the new card
  1715. * @param {string} description the description of the new card
  1716. * @param {string} swimlaneId the swimlane ID of the new card
  1717. * @param {string} [members] the member IDs list of the new card
  1718. * @return_type {_id: string}
  1719. */
  1720. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  1721. req,
  1722. res,
  1723. ) {
  1724. Authentication.checkUserId(req.userId);
  1725. const paramBoardId = req.params.boardId;
  1726. const paramListId = req.params.listId;
  1727. const paramParentId = req.params.parentId;
  1728. const currentCards = Cards.find(
  1729. {
  1730. listId: paramListId,
  1731. archived: false,
  1732. },
  1733. { sort: ['sort'] },
  1734. );
  1735. const check = Users.findOne({
  1736. _id: req.body.authorId,
  1737. });
  1738. const members = req.body.members || [req.body.authorId];
  1739. if (typeof check !== 'undefined') {
  1740. const id = Cards.direct.insert({
  1741. title: req.body.title,
  1742. boardId: paramBoardId,
  1743. listId: paramListId,
  1744. parentId: paramParentId,
  1745. description: req.body.description,
  1746. userId: req.body.authorId,
  1747. swimlaneId: req.body.swimlaneId,
  1748. sort: currentCards.count(),
  1749. members,
  1750. });
  1751. JsonRoutes.sendResult(res, {
  1752. code: 200,
  1753. data: {
  1754. _id: id,
  1755. },
  1756. });
  1757. const card = Cards.findOne({
  1758. _id: id,
  1759. });
  1760. cardCreation(req.body.authorId, card);
  1761. } else {
  1762. JsonRoutes.sendResult(res, {
  1763. code: 401,
  1764. });
  1765. }
  1766. });
  1767. /*
  1768. * Note for the JSDoc:
  1769. * 'list' will be interpreted as the path parameter
  1770. * 'listID' will be interpreted as the body parameter
  1771. */
  1772. /**
  1773. * @operation edit_card
  1774. * @summary Edit Fields in a Card
  1775. *
  1776. * @description Edit a card
  1777. *
  1778. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1779. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1780. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1781. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1782. * `indigo`:
  1783. *
  1784. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1785. *
  1786. * Note: setting the color to white has the same effect than removing it.
  1787. *
  1788. * @param {string} boardId the board ID of the card
  1789. * @param {string} list the list ID of the card
  1790. * @param {string} cardId the ID of the card
  1791. * @param {string} [title] the new title of the card
  1792. * @param {string} [listId] the new list ID of the card (move operation)
  1793. * @param {string} [description] the new description of the card
  1794. * @param {string} [authorId] change the owner of the card
  1795. * @param {string} [parentId] change the parent of the card
  1796. * @param {string} [labelIds] the new list of label IDs attached to the card
  1797. * @param {string} [swimlaneId] the new swimlane ID of the card
  1798. * @param {string} [members] the new list of member IDs attached to the card
  1799. * @param {string} [requestedBy] the new requestedBy field of the card
  1800. * @param {string} [assignedBy] the new assignedBy field of the card
  1801. * @param {string} [receivedAt] the new receivedAt field of the card
  1802. * @param {string} [assignBy] the new assignBy field of the card
  1803. * @param {string} [startAt] the new startAt field of the card
  1804. * @param {string} [dueAt] the new dueAt field of the card
  1805. * @param {string} [endAt] the new endAt field of the card
  1806. * @param {string} [spentTime] the new spentTime field of the card
  1807. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1808. * @param {string} [customFields] the new customFields value of the card
  1809. * @param {string} [color] the new color of the card
  1810. * @return_type {_id: string}
  1811. */
  1812. JsonRoutes.add(
  1813. 'PUT',
  1814. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  1815. function(req, res) {
  1816. Authentication.checkUserId(req.userId);
  1817. const paramBoardId = req.params.boardId;
  1818. const paramCardId = req.params.cardId;
  1819. const paramListId = req.params.listId;
  1820. if (req.body.hasOwnProperty('title')) {
  1821. const newTitle = req.body.title;
  1822. Cards.direct.update(
  1823. {
  1824. _id: paramCardId,
  1825. listId: paramListId,
  1826. boardId: paramBoardId,
  1827. archived: false,
  1828. },
  1829. {
  1830. $set: {
  1831. title: newTitle,
  1832. },
  1833. },
  1834. );
  1835. }
  1836. if (req.body.hasOwnProperty('listId')) {
  1837. const newParamListId = req.body.listId;
  1838. Cards.direct.update(
  1839. {
  1840. _id: paramCardId,
  1841. listId: paramListId,
  1842. boardId: paramBoardId,
  1843. archived: false,
  1844. },
  1845. {
  1846. $set: {
  1847. listId: newParamListId,
  1848. },
  1849. },
  1850. );
  1851. const card = Cards.findOne({
  1852. _id: paramCardId,
  1853. });
  1854. cardMove(
  1855. req.body.authorId,
  1856. card,
  1857. {
  1858. fieldName: 'listId',
  1859. },
  1860. paramListId,
  1861. );
  1862. }
  1863. if (req.body.hasOwnProperty('parentId')) {
  1864. const newParentId = req.body.parentId;
  1865. Cards.direct.update(
  1866. {
  1867. _id: paramCardId,
  1868. listId: paramListId,
  1869. boardId: paramBoardId,
  1870. archived: false,
  1871. },
  1872. {
  1873. $set: {
  1874. parentId: newParentId,
  1875. },
  1876. },
  1877. );
  1878. }
  1879. if (req.body.hasOwnProperty('description')) {
  1880. const newDescription = req.body.description;
  1881. Cards.direct.update(
  1882. {
  1883. _id: paramCardId,
  1884. listId: paramListId,
  1885. boardId: paramBoardId,
  1886. archived: false,
  1887. },
  1888. {
  1889. $set: {
  1890. description: newDescription,
  1891. },
  1892. },
  1893. );
  1894. }
  1895. if (req.body.hasOwnProperty('color')) {
  1896. const newColor = req.body.color;
  1897. Cards.direct.update(
  1898. {
  1899. _id: paramCardId,
  1900. listId: paramListId,
  1901. boardId: paramBoardId,
  1902. archived: false,
  1903. },
  1904. { $set: { color: newColor } },
  1905. );
  1906. }
  1907. if (req.body.hasOwnProperty('labelIds')) {
  1908. let newlabelIds = req.body.labelIds;
  1909. if (_.isString(newlabelIds)) {
  1910. if (newlabelIds === '') {
  1911. newlabelIds = null;
  1912. } else {
  1913. newlabelIds = [newlabelIds];
  1914. }
  1915. }
  1916. Cards.direct.update(
  1917. {
  1918. _id: paramCardId,
  1919. listId: paramListId,
  1920. boardId: paramBoardId,
  1921. archived: false,
  1922. },
  1923. {
  1924. $set: {
  1925. labelIds: newlabelIds,
  1926. },
  1927. },
  1928. );
  1929. }
  1930. if (req.body.hasOwnProperty('requestedBy')) {
  1931. const newrequestedBy = req.body.requestedBy;
  1932. Cards.direct.update(
  1933. {
  1934. _id: paramCardId,
  1935. listId: paramListId,
  1936. boardId: paramBoardId,
  1937. archived: false,
  1938. },
  1939. { $set: { requestedBy: newrequestedBy } },
  1940. );
  1941. }
  1942. if (req.body.hasOwnProperty('assignedBy')) {
  1943. const newassignedBy = req.body.assignedBy;
  1944. Cards.direct.update(
  1945. {
  1946. _id: paramCardId,
  1947. listId: paramListId,
  1948. boardId: paramBoardId,
  1949. archived: false,
  1950. },
  1951. { $set: { assignedBy: newassignedBy } },
  1952. );
  1953. }
  1954. if (req.body.hasOwnProperty('receivedAt')) {
  1955. const newreceivedAt = req.body.receivedAt;
  1956. Cards.direct.update(
  1957. {
  1958. _id: paramCardId,
  1959. listId: paramListId,
  1960. boardId: paramBoardId,
  1961. archived: false,
  1962. },
  1963. { $set: { receivedAt: newreceivedAt } },
  1964. );
  1965. }
  1966. if (req.body.hasOwnProperty('startAt')) {
  1967. const newstartAt = req.body.startAt;
  1968. Cards.direct.update(
  1969. {
  1970. _id: paramCardId,
  1971. listId: paramListId,
  1972. boardId: paramBoardId,
  1973. archived: false,
  1974. },
  1975. { $set: { startAt: newstartAt } },
  1976. );
  1977. }
  1978. if (req.body.hasOwnProperty('dueAt')) {
  1979. const newdueAt = req.body.dueAt;
  1980. Cards.direct.update(
  1981. {
  1982. _id: paramCardId,
  1983. listId: paramListId,
  1984. boardId: paramBoardId,
  1985. archived: false,
  1986. },
  1987. { $set: { dueAt: newdueAt } },
  1988. );
  1989. }
  1990. if (req.body.hasOwnProperty('endAt')) {
  1991. const newendAt = req.body.endAt;
  1992. Cards.direct.update(
  1993. {
  1994. _id: paramCardId,
  1995. listId: paramListId,
  1996. boardId: paramBoardId,
  1997. archived: false,
  1998. },
  1999. { $set: { endAt: newendAt } },
  2000. );
  2001. }
  2002. if (req.body.hasOwnProperty('spentTime')) {
  2003. const newspentTime = req.body.spentTime;
  2004. Cards.direct.update(
  2005. {
  2006. _id: paramCardId,
  2007. listId: paramListId,
  2008. boardId: paramBoardId,
  2009. archived: false,
  2010. },
  2011. { $set: { spentTime: newspentTime } },
  2012. );
  2013. }
  2014. if (req.body.hasOwnProperty('isOverTime')) {
  2015. const newisOverTime = req.body.isOverTime;
  2016. Cards.direct.update(
  2017. {
  2018. _id: paramCardId,
  2019. listId: paramListId,
  2020. boardId: paramBoardId,
  2021. archived: false,
  2022. },
  2023. { $set: { isOverTime: newisOverTime } },
  2024. );
  2025. }
  2026. if (req.body.hasOwnProperty('customFields')) {
  2027. const newcustomFields = req.body.customFields;
  2028. Cards.direct.update(
  2029. {
  2030. _id: paramCardId,
  2031. listId: paramListId,
  2032. boardId: paramBoardId,
  2033. archived: false,
  2034. },
  2035. { $set: { customFields: newcustomFields } },
  2036. );
  2037. }
  2038. if (req.body.hasOwnProperty('members')) {
  2039. let newmembers = req.body.members;
  2040. if (_.isString(newmembers)) {
  2041. if (newmembers === '') {
  2042. newmembers = null;
  2043. } else {
  2044. newmembers = [newmembers];
  2045. }
  2046. }
  2047. Cards.direct.update(
  2048. {
  2049. _id: paramCardId,
  2050. listId: paramListId,
  2051. boardId: paramBoardId,
  2052. archived: false,
  2053. },
  2054. { $set: { members: newmembers } },
  2055. );
  2056. }
  2057. if (req.body.hasOwnProperty('swimlaneId')) {
  2058. const newParamSwimlaneId = req.body.swimlaneId;
  2059. Cards.direct.update(
  2060. {
  2061. _id: paramCardId,
  2062. listId: paramListId,
  2063. boardId: paramBoardId,
  2064. archived: false,
  2065. },
  2066. { $set: { swimlaneId: newParamSwimlaneId } },
  2067. );
  2068. }
  2069. JsonRoutes.sendResult(res, {
  2070. code: 200,
  2071. data: {
  2072. _id: paramCardId,
  2073. },
  2074. });
  2075. },
  2076. );
  2077. /**
  2078. * @operation delete_card
  2079. * @summary Delete a card from a board
  2080. *
  2081. * @description This operation **deletes** a card, and therefore the card
  2082. * is not put in the recycle bin.
  2083. *
  2084. * @param {string} boardId the board ID of the card
  2085. * @param {string} list the list ID of the card
  2086. * @param {string} cardId the ID of the card
  2087. * @return_type {_id: string}
  2088. */
  2089. JsonRoutes.add(
  2090. 'DELETE',
  2091. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2092. function(req, res) {
  2093. Authentication.checkUserId(req.userId);
  2094. const paramBoardId = req.params.boardId;
  2095. const paramListId = req.params.listId;
  2096. const paramCardId = req.params.cardId;
  2097. Cards.direct.remove({
  2098. _id: paramCardId,
  2099. listId: paramListId,
  2100. boardId: paramBoardId,
  2101. });
  2102. const card = Cards.find({
  2103. _id: paramCardId,
  2104. });
  2105. cardRemover(req.body.authorId, card);
  2106. JsonRoutes.sendResult(res, {
  2107. code: 200,
  2108. data: {
  2109. _id: paramCardId,
  2110. },
  2111. });
  2112. },
  2113. );
  2114. }
  2115. export default Cards;