cards.js 65 KB

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