cards.js 64 KB

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