cards.js 69 KB

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