2
0

cards.js 60 KB

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