cards.js 62 KB

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