cards.js 63 KB

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