cards.js 63 KB

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