cards.js 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769
  1. import moment from 'moment';
  2. import {
  3. ALLOWED_COLORS,
  4. TYPE_CARD,
  5. TYPE_LINKED_BOARD,
  6. TYPE_LINKED_CARD,
  7. } from '../config/const';
  8. Cards = new Mongo.Collection('cards');
  9. // XXX To improve pub/sub performances a card document should include a
  10. // de-normalized number of comments so we don't have to publish the whole list
  11. // of comments just to display the number of them in the board view.
  12. Cards.attachSchema(
  13. new SimpleSchema({
  14. title: {
  15. /**
  16. * the title of the card
  17. */
  18. type: String,
  19. optional: true,
  20. defaultValue: '',
  21. },
  22. archived: {
  23. /**
  24. * is the card archived
  25. */
  26. type: Boolean,
  27. // eslint-disable-next-line consistent-return
  28. autoValue() {
  29. // eslint-disable-line consistent-return
  30. if (this.isInsert && !this.isSet) {
  31. return false;
  32. }
  33. },
  34. },
  35. archivedAt: {
  36. /**
  37. * latest archiving date
  38. */
  39. type: Date,
  40. optional: true,
  41. },
  42. parentId: {
  43. /**
  44. * ID of the parent card
  45. */
  46. type: String,
  47. optional: true,
  48. defaultValue: '',
  49. },
  50. listId: {
  51. /**
  52. * List ID where the card is
  53. */
  54. type: String,
  55. optional: true,
  56. defaultValue: '',
  57. },
  58. swimlaneId: {
  59. /**
  60. * Swimlane ID where the card is
  61. */
  62. type: String,
  63. },
  64. // The system could work without this `boardId` information (we could deduce
  65. // the board identifier from the card), but it would make the system more
  66. // difficult to manage and less efficient.
  67. boardId: {
  68. /**
  69. * Board ID of the card
  70. */
  71. type: String,
  72. optional: true,
  73. defaultValue: '',
  74. },
  75. coverId: {
  76. /**
  77. * Cover ID of the card
  78. */
  79. type: String,
  80. optional: true,
  81. defaultValue: '',
  82. },
  83. color: {
  84. type: String,
  85. optional: true,
  86. allowedValues: ALLOWED_COLORS,
  87. },
  88. createdAt: {
  89. /**
  90. * creation date
  91. */
  92. type: Date,
  93. // eslint-disable-next-line consistent-return
  94. autoValue() {
  95. if (this.isInsert) {
  96. return new Date();
  97. } else if (this.isUpsert) {
  98. return { $setOnInsert: new Date() };
  99. } else {
  100. this.unset();
  101. }
  102. },
  103. },
  104. modifiedAt: {
  105. type: Date,
  106. denyUpdate: false,
  107. // eslint-disable-next-line consistent-return
  108. autoValue() {
  109. if (this.isInsert || this.isUpsert || this.isUpdate) {
  110. return new Date();
  111. } else {
  112. this.unset();
  113. }
  114. },
  115. },
  116. customFields: {
  117. /**
  118. * list of custom fields
  119. */
  120. type: [Object],
  121. optional: true,
  122. defaultValue: [],
  123. },
  124. 'customFields.$': {
  125. type: new SimpleSchema({
  126. _id: {
  127. /**
  128. * the ID of the related custom field
  129. */
  130. type: String,
  131. optional: true,
  132. defaultValue: '',
  133. },
  134. value: {
  135. /**
  136. * value attached to the custom field
  137. */
  138. type: Match.OneOf(String, Number, Boolean, Date, [String]),
  139. optional: true,
  140. defaultValue: '',
  141. },
  142. 'value.$': {
  143. type: String,
  144. optional: true,
  145. },
  146. }),
  147. },
  148. dateLastActivity: {
  149. /**
  150. * Date of last activity
  151. */
  152. type: Date,
  153. autoValue() {
  154. return new Date();
  155. },
  156. },
  157. description: {
  158. /**
  159. * description of the card
  160. */
  161. type: String,
  162. optional: true,
  163. defaultValue: '',
  164. },
  165. requestedBy: {
  166. /**
  167. * who requested the card (ID of the user)
  168. */
  169. type: String,
  170. optional: true,
  171. defaultValue: '',
  172. },
  173. assignedBy: {
  174. /**
  175. * who assigned the card (ID of the user)
  176. */
  177. type: String,
  178. optional: true,
  179. defaultValue: '',
  180. },
  181. labelIds: {
  182. /**
  183. * list of labels ID the card has
  184. */
  185. type: [String],
  186. optional: true,
  187. defaultValue: [],
  188. },
  189. members: {
  190. /**
  191. * list of members (user IDs)
  192. */
  193. type: [String],
  194. optional: true,
  195. defaultValue: [],
  196. },
  197. assignees: {
  198. /**
  199. * who is assignee of the card (user ID),
  200. * maximum one ID of assignee in array.
  201. */
  202. type: [String],
  203. optional: true,
  204. defaultValue: [],
  205. },
  206. receivedAt: {
  207. /**
  208. * Date the card was received
  209. */
  210. type: Date,
  211. optional: true,
  212. },
  213. startAt: {
  214. /**
  215. * Date the card was started to be worked on
  216. */
  217. type: Date,
  218. optional: true,
  219. },
  220. dueAt: {
  221. /**
  222. * Date the card is due
  223. */
  224. type: Date,
  225. optional: true,
  226. },
  227. endAt: {
  228. /**
  229. * Date the card ended
  230. */
  231. type: Date,
  232. optional: true,
  233. },
  234. spentTime: {
  235. /**
  236. * How much time has been spent on this
  237. */
  238. type: Number,
  239. decimal: true,
  240. optional: true,
  241. defaultValue: 0,
  242. },
  243. isOvertime: {
  244. /**
  245. * is the card over time?
  246. */
  247. type: Boolean,
  248. defaultValue: false,
  249. optional: true,
  250. },
  251. // XXX Should probably be called `authorId`. Is it even needed since we have
  252. // the `members` field?
  253. userId: {
  254. /**
  255. * user ID of the author of the card
  256. */
  257. type: String,
  258. // eslint-disable-next-line consistent-return
  259. autoValue() {
  260. // eslint-disable-line consistent-return
  261. if (this.isInsert && !this.isSet) {
  262. return this.userId;
  263. }
  264. },
  265. },
  266. sort: {
  267. /**
  268. * Sort value
  269. */
  270. type: Number,
  271. decimal: true,
  272. defaultValue: 0,
  273. optional: true,
  274. },
  275. subtaskSort: {
  276. /**
  277. * subtask sort value
  278. */
  279. type: Number,
  280. decimal: true,
  281. defaultValue: -1,
  282. optional: true,
  283. },
  284. type: {
  285. /**
  286. * type of the card
  287. */
  288. type: String,
  289. defaultValue: TYPE_CARD,
  290. // allowedValues: [TYPE_CARD, TYPE_LINKED_CARD, TYPE_LINKED_BOARD, TYPE_TEMPLATE_CARD],
  291. },
  292. linkedId: {
  293. /**
  294. * ID of the linked card
  295. */
  296. type: String,
  297. optional: true,
  298. defaultValue: '',
  299. },
  300. vote: {
  301. /**
  302. * vote object, see below
  303. */
  304. type: Object,
  305. optional: true,
  306. },
  307. 'vote.question': {
  308. type: String,
  309. defaultValue: '',
  310. },
  311. 'vote.positive': {
  312. /**
  313. * list of members (user IDs)
  314. */
  315. type: [String],
  316. optional: true,
  317. defaultValue: [],
  318. },
  319. 'vote.negative': {
  320. /**
  321. * list of members (user IDs)
  322. */
  323. type: [String],
  324. optional: true,
  325. defaultValue: [],
  326. },
  327. 'vote.end': {
  328. type: Date,
  329. optional: true,
  330. defaultValue: null,
  331. },
  332. 'vote.public': {
  333. type: Boolean,
  334. defaultValue: false,
  335. },
  336. 'vote.allowNonBoardMembers': {
  337. type: Boolean,
  338. defaultValue: false,
  339. },
  340. poker: {
  341. /**
  342. * poker object, see below
  343. */
  344. type: Object,
  345. optional: true,
  346. },
  347. 'poker.question': {
  348. type: Boolean,
  349. defaultValue: false,
  350. },
  351. 'poker.one': {
  352. /**
  353. * poker card one
  354. */
  355. type: [String],
  356. optional: true,
  357. defaultValue: [],
  358. },
  359. 'poker.two': {
  360. /**
  361. * poker card two
  362. */
  363. type: [String],
  364. optional: true,
  365. defaultValue: [],
  366. },
  367. 'poker.three': {
  368. /**
  369. * poker card three
  370. */
  371. type: [String],
  372. optional: true,
  373. defaultValue: [],
  374. },
  375. 'poker.five': {
  376. /**
  377. * poker card five
  378. */
  379. type: [String],
  380. optional: true,
  381. defaultValue: [],
  382. },
  383. 'poker.eight': {
  384. /**
  385. * poker card eight
  386. */
  387. type: [String],
  388. optional: true,
  389. defaultValue: [],
  390. },
  391. 'poker.thirteen': {
  392. /**
  393. * poker card thirteen
  394. */
  395. type: [String],
  396. optional: true,
  397. defaultValue: [],
  398. },
  399. 'poker.twenty': {
  400. /**
  401. * poker card twenty
  402. */
  403. type: [String],
  404. optional: true,
  405. defaultValue: [],
  406. },
  407. 'poker.forty': {
  408. /**
  409. * poker card forty
  410. */
  411. type: [String],
  412. optional: true,
  413. defaultValue: [],
  414. },
  415. 'poker.oneHundred': {
  416. /**
  417. * poker card oneHundred
  418. */
  419. type: [String],
  420. optional: true,
  421. defaultValue: [],
  422. },
  423. 'poker.unsure': {
  424. /**
  425. * poker card unsure
  426. */
  427. type: [String],
  428. optional: true,
  429. defaultValue: [],
  430. },
  431. 'poker.end': {
  432. type: Date,
  433. optional: true,
  434. defaultValue: null,
  435. },
  436. 'poker.allowNonBoardMembers': {
  437. type: Boolean,
  438. defaultValue: false,
  439. },
  440. 'poker.estimation': {
  441. /**
  442. * poker estimation value
  443. */
  444. type: Number,
  445. optional: true,
  446. },
  447. targetId_gantt: {
  448. /**
  449. * ID of card which is the child link in gantt view
  450. */
  451. type: [String],
  452. optional: true,
  453. defaultValue: [],
  454. },
  455. linkType_gantt: {
  456. /**
  457. * ID of card which is the parent link in gantt view
  458. */
  459. type: [Number],
  460. decimal: false,
  461. optional: true,
  462. defaultValue: [],
  463. },
  464. linkId_gantt: {
  465. /**
  466. * ID of card which is the parent link in gantt view
  467. */
  468. type: [String],
  469. optional: true,
  470. defaultValue: [],
  471. },
  472. cardNumber: {
  473. /**
  474. * A boardwise sequentially increasing number that is assigned
  475. * to every newly created card
  476. */
  477. type: Number,
  478. decimal: true,
  479. optional: true,
  480. defaultValue: 0,
  481. },
  482. }),
  483. );
  484. Cards.allow({
  485. insert(userId, doc) {
  486. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  487. },
  488. update(userId, doc, fields) {
  489. // Allow board members or logged in users if only vote get's changed
  490. return (
  491. allowIsBoardMember(userId, Boards.findOne(doc.boardId)) ||
  492. (_.isEqual(fields, ['vote', 'modifiedAt', 'dateLastActivity']) &&
  493. !!userId)
  494. );
  495. },
  496. remove(userId, doc) {
  497. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  498. },
  499. fetch: ['boardId'],
  500. });
  501. Cards.helpers({
  502. // Gantt https://github.com/wekan/wekan/issues/2870#issuecomment-857171127
  503. setGanttTargetId(sourceId, targetId, linkType, linkId){
  504. return Cards.update({ _id: sourceId}, {
  505. $push: {
  506. targetId_gantt: targetId,
  507. linkType_gantt : linkType,
  508. linkId_gantt: linkId
  509. }
  510. });
  511. },
  512. removeGanttTargetId(sourceId, targetId, linkType, linkId){
  513. return Cards.update({ _id: sourceId}, {
  514. $pull: {
  515. targetId_gantt: targetId,
  516. linkType_gantt : linkType,
  517. linkId_gantt: linkId
  518. }
  519. });
  520. },
  521. mapCustomFieldsToBoard(boardId) {
  522. // Map custom fields to new board
  523. return this.customFields.map(cf => {
  524. const oldCf = CustomFields.findOne(cf._id);
  525. const newCf = CustomFields.findOne({
  526. boardIds: boardId,
  527. name: oldCf.name,
  528. type: oldCf.type,
  529. });
  530. if (newCf) {
  531. cf._id = newCf._id;
  532. } else if (!_.contains(oldCf.boardIds, boardId)) {
  533. oldCf.addBoard(boardId);
  534. }
  535. return cf;
  536. });
  537. },
  538. copy(boardId, swimlaneId, listId) {
  539. const oldId = this._id;
  540. const oldCard = Cards.findOne(oldId);
  541. // we must only copy the labels and custom fields if the target board
  542. // differs from the source board
  543. if (this.boardId !== boardId) {
  544. const oldBoard = Boards.findOne(this.boardId);
  545. const oldBoardLabels = oldBoard.labels;
  546. // Get old label names
  547. const oldCardLabels = _.pluck(
  548. _.filter(oldBoardLabels, label => {
  549. return _.contains(this.labelIds, label._id);
  550. }),
  551. 'name',
  552. );
  553. const newBoard = Boards.findOne(boardId);
  554. const newBoardLabels = newBoard.labels;
  555. const newCardLabels = _.pluck(
  556. _.filter(newBoardLabels, label => {
  557. return _.contains(oldCardLabels, label.name);
  558. }),
  559. '_id',
  560. );
  561. // now set the new label ids
  562. delete this.labelIds;
  563. this.labelIds = newCardLabels;
  564. this.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  565. }
  566. delete this._id;
  567. this.boardId = boardId;
  568. this.cardNumber = Boards.findOne(boardId).getNextCardNumber();
  569. this.swimlaneId = swimlaneId;
  570. this.listId = listId;
  571. const _id = Cards.insert(this);
  572. // Copy attachments
  573. oldCard.attachments().forEach(att => {
  574. att.cardId = _id;
  575. delete att._id;
  576. return Attachments.insert(att);
  577. });
  578. // copy checklists
  579. Checklists.find({ cardId: oldId }).forEach(ch => {
  580. ch.copy(_id);
  581. });
  582. // copy subtasks
  583. Cards.find({ parentId: oldId }).forEach(subtask => {
  584. subtask.parentId = _id;
  585. subtask._id = null;
  586. Cards.insert(subtask);
  587. });
  588. // copy card comments
  589. CardComments.find({ cardId: oldId }).forEach(cmt => {
  590. cmt.copy(_id);
  591. });
  592. return _id;
  593. },
  594. link(boardId, swimlaneId, listId) {
  595. // TODO is there a better method to create a deepcopy?
  596. linkCard = JSON.parse(JSON.stringify(this));
  597. // TODO is this how it is meant to be?
  598. linkCard.linkedId = linkCard.linkedId || linkCard._id;
  599. linkCard.boardId = boardId;
  600. linkCard.swimlaneId = swimlaneId;
  601. linkCard.listId = listId;
  602. linkCard.type = 'cardType-linkedCard';
  603. delete linkCard._id;
  604. // TODO shall we copy the labels for a linked card?!
  605. delete linkCard.labelIds;
  606. return Cards.insert(linkCard);
  607. },
  608. list() {
  609. return Lists.findOne(this.listId);
  610. },
  611. swimlane() {
  612. return Swimlanes.findOne(this.swimlaneId);
  613. },
  614. board() {
  615. return Boards.findOne(this.boardId);
  616. },
  617. getList() {
  618. const list = this.list();
  619. if (!list) {
  620. return {
  621. _id: this.listId,
  622. title: 'Undefined List',
  623. archived: false,
  624. colorClass: '',
  625. };
  626. }
  627. return list;
  628. },
  629. getSwimlane() {
  630. const swimlane = this.swimlane();
  631. if (!swimlane) {
  632. return {
  633. _id: this.swimlaneId,
  634. title: 'Undefined Swimlane',
  635. archived: false,
  636. colorClass: '',
  637. };
  638. }
  639. return swimlane;
  640. },
  641. getBoard() {
  642. const board = this.board();
  643. if (!board) {
  644. return {
  645. _id: this.boardId,
  646. title: 'Undefined Board',
  647. archived: false,
  648. colorClass: '',
  649. };
  650. }
  651. return board;
  652. },
  653. labels() {
  654. const boardLabels = this.board().labels;
  655. const cardLabels = _.filter(boardLabels, label => {
  656. return _.contains(this.labelIds, label._id);
  657. });
  658. return cardLabels;
  659. },
  660. hasLabel(labelId) {
  661. return _.contains(this.labelIds, labelId);
  662. },
  663. user() {
  664. return Users.findOne(this.userId);
  665. },
  666. isAssigned(memberId) {
  667. return _.contains(this.getMembers(), memberId);
  668. },
  669. isAssignee(assigneeId) {
  670. return _.contains(this.getAssignees(), assigneeId);
  671. },
  672. activities() {
  673. if (this.isLinkedCard()) {
  674. return Activities.find(
  675. { cardId: this.linkedId },
  676. { sort: { createdAt: -1 } },
  677. );
  678. } else if (this.isLinkedBoard()) {
  679. return Activities.find(
  680. { boardId: this.linkedId },
  681. { sort: { createdAt: -1 } },
  682. );
  683. } else {
  684. return Activities.find({ cardId: this._id }, { sort: { createdAt: -1 } });
  685. }
  686. },
  687. comments() {
  688. if (this.isLinkedCard()) {
  689. return CardComments.find(
  690. { cardId: this.linkedId },
  691. { sort: { createdAt: -1 } },
  692. );
  693. } else if (this.isLinkedBoard()) {
  694. return CardComments.find(
  695. { boardId: this.linkedId },
  696. { sort: { createdAt: -1 } },
  697. );
  698. } else {
  699. return CardComments.find(
  700. { cardId: this._id },
  701. { sort: { createdAt: -1 } },
  702. );
  703. }
  704. },
  705. attachments() {
  706. if (this.isLinkedCard()) {
  707. return Attachments.find(
  708. { cardId: this.linkedId },
  709. { sort: { uploadedAt: -1 } },
  710. );
  711. } else {
  712. return Attachments.find(
  713. { cardId: this._id },
  714. { sort: { uploadedAt: -1 } },
  715. );
  716. }
  717. },
  718. cover() {
  719. if (!this.coverId) return false;
  720. const cover = Attachments.findOne(this.coverId);
  721. // if we return a cover before it is fully stored, we will get errors when we try to display it
  722. // todo XXX we could return a default "upload pending" image in the meantime?
  723. return cover && cover.url() && cover;
  724. },
  725. checklists() {
  726. if (this.isLinkedCard()) {
  727. return Checklists.find({ cardId: this.linkedId }, { sort: { sort: 1 } });
  728. } else {
  729. return Checklists.find({ cardId: this._id }, { sort: { sort: 1 } });
  730. }
  731. },
  732. checklistItemCount() {
  733. const checklists = this.checklists().fetch();
  734. return checklists
  735. .map(checklist => {
  736. return checklist.itemCount();
  737. })
  738. .reduce((prev, next) => {
  739. return prev + next;
  740. }, 0);
  741. },
  742. checklistFinishedCount() {
  743. const checklists = this.checklists().fetch();
  744. return checklists
  745. .map(checklist => {
  746. return checklist.finishedCount();
  747. })
  748. .reduce((prev, next) => {
  749. return prev + next;
  750. }, 0);
  751. },
  752. checklistFinished() {
  753. return (
  754. this.hasChecklist() &&
  755. this.checklistItemCount() === this.checklistFinishedCount()
  756. );
  757. },
  758. hasChecklist() {
  759. return this.checklistItemCount() !== 0;
  760. },
  761. subtasks() {
  762. return Cards.find(
  763. {
  764. parentId: this._id,
  765. archived: false,
  766. },
  767. {
  768. sort: {
  769. sort: 1,
  770. },
  771. },
  772. );
  773. },
  774. subtasksFinished() {
  775. return Cards.find({
  776. parentId: this._id,
  777. archived: true,
  778. });
  779. },
  780. allSubtasks() {
  781. return Cards.find({
  782. parentId: this._id,
  783. });
  784. },
  785. subtasksCount() {
  786. const subtasks = this.subtasks();
  787. return subtasks.count();
  788. },
  789. subtasksFinishedCount() {
  790. const subtasksArchived = this.subtasksFinished();
  791. return subtasksArchived.count();
  792. },
  793. allSubtasksCount() {
  794. const allSubtasks = this.allSubtasks();
  795. return allSubtasks.count();
  796. },
  797. allowsSubtasks() {
  798. return this.subtasksCount() !== 0;
  799. },
  800. customFieldIndex(customFieldId) {
  801. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  802. },
  803. // customFields with definitions
  804. customFieldsWD() {
  805. // get all definitions
  806. const definitions = CustomFields.find({
  807. boardIds: { $in: [this.boardId] },
  808. }).fetch();
  809. if (!definitions) {
  810. return {};
  811. }
  812. // match right definition to each field
  813. if (!this.customFields) return [];
  814. const ret = this.customFields.map(customField => {
  815. const definition = definitions.find(definition => {
  816. return definition._id === customField._id;
  817. });
  818. if (!definition) {
  819. return {};
  820. }
  821. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  822. let trueValue = customField.value;
  823. if (
  824. definition.settings.dropdownItems &&
  825. definition.settings.dropdownItems.length > 0
  826. ) {
  827. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  828. if (definition.settings.dropdownItems[i]._id === customField.value) {
  829. trueValue = definition.settings.dropdownItems[i].name;
  830. }
  831. }
  832. }
  833. return {
  834. _id: customField._id,
  835. value: customField.value,
  836. trueValue,
  837. definition,
  838. };
  839. });
  840. // at linked cards custom fields definition is not found
  841. ret.sort(
  842. (a, b) =>
  843. a.definition !== undefined &&
  844. b.definition !== undefined &&
  845. a.definition.name !== undefined &&
  846. b.definition.name !== undefined &&
  847. a.definition.name.localeCompare(b.definition.name),
  848. );
  849. return ret;
  850. },
  851. colorClass() {
  852. if (this.color) return this.color;
  853. return '';
  854. },
  855. absoluteUrl() {
  856. const board = this.board();
  857. return FlowRouter.url('card', {
  858. boardId: board._id,
  859. slug: board.slug,
  860. cardId: this._id,
  861. });
  862. },
  863. originRelativeUrl() {
  864. const board = this.board();
  865. return FlowRouter.path('card', {
  866. boardId: board._id,
  867. slug: board.slug,
  868. cardId: this._id,
  869. });
  870. },
  871. canBeRestored() {
  872. const list = Lists.findOne({
  873. _id: this.listId,
  874. });
  875. if (
  876. !list.getWipLimit('soft') &&
  877. list.getWipLimit('enabled') &&
  878. list.getWipLimit('value') === list.cards().count()
  879. ) {
  880. return false;
  881. }
  882. return true;
  883. },
  884. parentCard() {
  885. if (this.parentId === '') {
  886. return null;
  887. }
  888. return Cards.findOne(this.parentId);
  889. },
  890. parentCardName() {
  891. let result = '';
  892. if (this.parentId !== '') {
  893. const card = Cards.findOne(this.parentId);
  894. if (card) {
  895. result = card.title;
  896. }
  897. }
  898. return result;
  899. },
  900. parentListId() {
  901. const result = [];
  902. let crtParentId = this.parentId;
  903. while (crtParentId !== '') {
  904. const crt = Cards.findOne(crtParentId);
  905. if (crt === null || crt === undefined) {
  906. // maybe it has been deleted
  907. break;
  908. }
  909. if (crtParentId in result) {
  910. // circular reference
  911. break;
  912. }
  913. result.unshift(crtParentId);
  914. crtParentId = crt.parentId;
  915. }
  916. return result;
  917. },
  918. parentList() {
  919. const resultId = [];
  920. const result = [];
  921. let crtParentId = this.parentId;
  922. while (crtParentId !== '') {
  923. const crt = Cards.findOne(crtParentId);
  924. if (crt === null || crt === undefined) {
  925. // maybe it has been deleted
  926. break;
  927. }
  928. if (crtParentId in resultId) {
  929. // circular reference
  930. break;
  931. }
  932. resultId.unshift(crtParentId);
  933. result.unshift(crt);
  934. crtParentId = crt.parentId;
  935. }
  936. return result;
  937. },
  938. parentString(sep) {
  939. return this.parentList()
  940. .map(function(elem) {
  941. return elem.title;
  942. })
  943. .join(sep);
  944. },
  945. isTopLevel() {
  946. return this.parentId === '';
  947. },
  948. isLinkedCard() {
  949. return this.type === 'cardType-linkedCard';
  950. },
  951. isLinkedBoard() {
  952. return this.type === 'cardType-linkedBoard';
  953. },
  954. isLinked() {
  955. return this.isLinkedCard() || this.isLinkedBoard();
  956. },
  957. setDescription(description) {
  958. if (this.isLinkedCard()) {
  959. return Cards.update({ _id: this.linkedId }, { $set: { description } });
  960. } else if (this.isLinkedBoard()) {
  961. return Boards.update({ _id: this.linkedId }, { $set: { description } });
  962. } else {
  963. return Cards.update({ _id: this._id }, { $set: { description } });
  964. }
  965. },
  966. getDescription() {
  967. if (this.isLinkedCard()) {
  968. const card = Cards.findOne({ _id: this.linkedId });
  969. if (card && card.description) return card.description;
  970. else return null;
  971. } else if (this.isLinkedBoard()) {
  972. const board = Boards.findOne({ _id: this.linkedId });
  973. if (board && board.description) return board.description;
  974. else return null;
  975. } else if (this.description) {
  976. return this.description;
  977. } else {
  978. return null;
  979. }
  980. },
  981. getMembers() {
  982. if (this.isLinkedCard()) {
  983. const card = Cards.findOne({ _id: this.linkedId });
  984. if (card === undefined) {
  985. return null;
  986. } else {
  987. return card.members;
  988. }
  989. } else if (this.isLinkedBoard()) {
  990. const board = Boards.findOne({ _id: this.linkedId });
  991. if (board === undefined) {
  992. return null;
  993. } else {
  994. return board.activeMembers().map(member => {
  995. return member.userId;
  996. });
  997. }
  998. } else {
  999. return this.members;
  1000. }
  1001. },
  1002. getAssignees() {
  1003. if (this.isLinkedCard()) {
  1004. const card = Cards.findOne({ _id: this.linkedId });
  1005. if (card === undefined) {
  1006. return null;
  1007. } else {
  1008. return card.assignees;
  1009. }
  1010. } else if (this.isLinkedBoard()) {
  1011. const board = Boards.findOne({ _id: this.linkedId });
  1012. if (board === undefined) {
  1013. return null;
  1014. } else {
  1015. return board.activeMembers().map(assignee => {
  1016. return assignee.userId;
  1017. });
  1018. }
  1019. } else {
  1020. return this.assignees;
  1021. }
  1022. },
  1023. assignMember(memberId) {
  1024. if (this.isLinkedCard()) {
  1025. return Cards.update(
  1026. { _id: this.linkedId },
  1027. { $addToSet: { members: memberId } },
  1028. );
  1029. } else if (this.isLinkedBoard()) {
  1030. const board = Boards.findOne({ _id: this.linkedId });
  1031. return board.addMember(memberId);
  1032. } else {
  1033. return Cards.update(
  1034. { _id: this._id },
  1035. { $addToSet: { members: memberId } },
  1036. );
  1037. }
  1038. },
  1039. assignAssignee(assigneeId) {
  1040. if (this.isLinkedCard()) {
  1041. return Cards.update(
  1042. { _id: this.linkedId },
  1043. { $addToSet: { assignees: assigneeId } },
  1044. );
  1045. } else if (this.isLinkedBoard()) {
  1046. const board = Boards.findOne({ _id: this.linkedId });
  1047. return board.addAssignee(assigneeId);
  1048. } else {
  1049. return Cards.update(
  1050. { _id: this._id },
  1051. { $addToSet: { assignees: assigneeId } },
  1052. );
  1053. }
  1054. },
  1055. unassignMember(memberId) {
  1056. if (this.isLinkedCard()) {
  1057. return Cards.update(
  1058. { _id: this.linkedId },
  1059. { $pull: { members: memberId } },
  1060. );
  1061. } else if (this.isLinkedBoard()) {
  1062. const board = Boards.findOne({ _id: this.linkedId });
  1063. return board.removeMember(memberId);
  1064. } else {
  1065. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  1066. }
  1067. },
  1068. unassignAssignee(assigneeId) {
  1069. if (this.isLinkedCard()) {
  1070. return Cards.update(
  1071. { _id: this.linkedId },
  1072. { $pull: { assignees: assigneeId } },
  1073. );
  1074. } else if (this.isLinkedBoard()) {
  1075. const board = Boards.findOne({ _id: this.linkedId });
  1076. return board.removeAssignee(assigneeId);
  1077. } else {
  1078. return Cards.update(
  1079. { _id: this._id },
  1080. { $pull: { assignees: assigneeId } },
  1081. );
  1082. }
  1083. },
  1084. toggleMember(memberId) {
  1085. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  1086. return this.unassignMember(memberId);
  1087. } else {
  1088. return this.assignMember(memberId);
  1089. }
  1090. },
  1091. toggleAssignee(assigneeId) {
  1092. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  1093. return this.unassignAssignee(assigneeId);
  1094. } else {
  1095. return this.assignAssignee(assigneeId);
  1096. }
  1097. },
  1098. getReceived() {
  1099. if (this.isLinkedCard()) {
  1100. const card = Cards.findOne({ _id: this.linkedId });
  1101. if (card === undefined) {
  1102. return null;
  1103. } else {
  1104. return card.receivedAt;
  1105. }
  1106. } else if (this.isLinkedBoard()) {
  1107. const board = Boards.findOne({ _id: this.linkedId });
  1108. if (board === undefined) {
  1109. return null;
  1110. } else {
  1111. return board.receivedAt;
  1112. }
  1113. } else {
  1114. return this.receivedAt;
  1115. }
  1116. },
  1117. setReceived(receivedAt) {
  1118. if (this.isLinkedCard()) {
  1119. return Cards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  1120. } else if (this.isLinkedBoard()) {
  1121. return Boards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  1122. } else {
  1123. return Cards.update({ _id: this._id }, { $set: { receivedAt } });
  1124. }
  1125. },
  1126. getStart() {
  1127. if (this.isLinkedCard()) {
  1128. const card = Cards.findOne({ _id: this.linkedId });
  1129. if (card === undefined) {
  1130. return null;
  1131. } else {
  1132. return card.startAt;
  1133. }
  1134. } else if (this.isLinkedBoard()) {
  1135. const board = Boards.findOne({ _id: this.linkedId });
  1136. if (board === undefined) {
  1137. return null;
  1138. } else {
  1139. return board.startAt;
  1140. }
  1141. } else {
  1142. return this.startAt;
  1143. }
  1144. },
  1145. setStart(startAt) {
  1146. if (this.isLinkedCard()) {
  1147. return Cards.update({ _id: this.linkedId }, { $set: { startAt } });
  1148. } else if (this.isLinkedBoard()) {
  1149. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  1150. } else {
  1151. return Cards.update({ _id: this._id }, { $set: { startAt } });
  1152. }
  1153. },
  1154. getDue() {
  1155. if (this.isLinkedCard()) {
  1156. const card = Cards.findOne({ _id: this.linkedId });
  1157. if (card === undefined) {
  1158. return null;
  1159. } else {
  1160. return card.dueAt;
  1161. }
  1162. } else if (this.isLinkedBoard()) {
  1163. const board = Boards.findOne({ _id: this.linkedId });
  1164. if (board === undefined) {
  1165. return null;
  1166. } else {
  1167. return board.dueAt;
  1168. }
  1169. } else {
  1170. return this.dueAt;
  1171. }
  1172. },
  1173. setDue(dueAt) {
  1174. if (this.isLinkedCard()) {
  1175. return Cards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1176. } else if (this.isLinkedBoard()) {
  1177. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1178. } else {
  1179. return Cards.update({ _id: this._id }, { $set: { dueAt } });
  1180. }
  1181. },
  1182. getEnd() {
  1183. if (this.isLinkedCard()) {
  1184. const card = Cards.findOne({ _id: this.linkedId });
  1185. if (card === undefined) {
  1186. return null;
  1187. } else {
  1188. return card.endAt;
  1189. }
  1190. } else if (this.isLinkedBoard()) {
  1191. const board = Boards.findOne({ _id: this.linkedId });
  1192. if (board === undefined) {
  1193. return null;
  1194. } else {
  1195. return board.endAt;
  1196. }
  1197. } else {
  1198. return this.endAt;
  1199. }
  1200. },
  1201. setEnd(endAt) {
  1202. if (this.isLinkedCard()) {
  1203. return Cards.update({ _id: this.linkedId }, { $set: { endAt } });
  1204. } else if (this.isLinkedBoard()) {
  1205. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  1206. } else {
  1207. return Cards.update({ _id: this._id }, { $set: { endAt } });
  1208. }
  1209. },
  1210. getIsOvertime() {
  1211. if (this.isLinkedCard()) {
  1212. const card = Cards.findOne({ _id: this.linkedId });
  1213. if (card === undefined) {
  1214. return null;
  1215. } else {
  1216. return card.isOvertime;
  1217. }
  1218. } else if (this.isLinkedBoard()) {
  1219. const board = Boards.findOne({ _id: this.linkedId });
  1220. if (board === undefined) {
  1221. return null;
  1222. } else {
  1223. return board.isOvertime;
  1224. }
  1225. } else {
  1226. return this.isOvertime;
  1227. }
  1228. },
  1229. setIsOvertime(isOvertime) {
  1230. if (this.isLinkedCard()) {
  1231. return Cards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1232. } else if (this.isLinkedBoard()) {
  1233. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1234. } else {
  1235. return Cards.update({ _id: this._id }, { $set: { isOvertime } });
  1236. }
  1237. },
  1238. getSpentTime() {
  1239. if (this.isLinkedCard()) {
  1240. const card = Cards.findOne({ _id: this.linkedId });
  1241. if (card === undefined) {
  1242. return null;
  1243. } else {
  1244. return card.spentTime;
  1245. }
  1246. } else if (this.isLinkedBoard()) {
  1247. const board = Boards.findOne({ _id: this.linkedId });
  1248. if (board === undefined) {
  1249. return null;
  1250. } else {
  1251. return board.spentTime;
  1252. }
  1253. } else {
  1254. return this.spentTime;
  1255. }
  1256. },
  1257. setSpentTime(spentTime) {
  1258. if (this.isLinkedCard()) {
  1259. return Cards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1260. } else if (this.isLinkedBoard()) {
  1261. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1262. } else {
  1263. return Cards.update({ _id: this._id }, { $set: { spentTime } });
  1264. }
  1265. },
  1266. getVoteQuestion() {
  1267. if (this.isLinkedCard()) {
  1268. const card = Cards.findOne({ _id: this.linkedId });
  1269. if (card === undefined) {
  1270. return null;
  1271. } else if (card && card.vote) {
  1272. return card.vote.question;
  1273. } else {
  1274. return null;
  1275. }
  1276. } else if (this.isLinkedBoard()) {
  1277. const board = Boards.findOne({ _id: this.linkedId });
  1278. if (board === undefined) {
  1279. return null;
  1280. } else if (board && board.vote) {
  1281. return board.vote.question;
  1282. } else {
  1283. return null;
  1284. }
  1285. } else if (this.vote) {
  1286. return this.vote.question;
  1287. } else {
  1288. return null;
  1289. }
  1290. },
  1291. getVotePublic() {
  1292. if (this.isLinkedCard()) {
  1293. const card = Cards.findOne({ _id: this.linkedId });
  1294. if (card === undefined) {
  1295. return null;
  1296. } else if (card && card.vote) {
  1297. return card.vote.public;
  1298. } else {
  1299. return null;
  1300. }
  1301. } else if (this.isLinkedBoard()) {
  1302. const board = Boards.findOne({ _id: this.linkedId });
  1303. if (board === undefined) {
  1304. return null;
  1305. } else if (board && board.vote) {
  1306. return board.vote.public;
  1307. } else {
  1308. return null;
  1309. }
  1310. } else if (this.vote) {
  1311. return this.vote.public;
  1312. } else {
  1313. return null;
  1314. }
  1315. },
  1316. getVoteEnd() {
  1317. if (this.isLinkedCard()) {
  1318. const card = Cards.findOne({ _id: this.linkedId });
  1319. if (card === undefined) {
  1320. return null;
  1321. } else if (card && card.vote) {
  1322. return card.vote.end;
  1323. } else {
  1324. return null;
  1325. }
  1326. } else if (this.isLinkedBoard()) {
  1327. const board = Boards.findOne({ _id: this.linkedId });
  1328. if (board === undefined) {
  1329. return null;
  1330. } else if (board && board.vote) {
  1331. return board.vote.end;
  1332. } else {
  1333. return null;
  1334. }
  1335. } else if (this.vote) {
  1336. return this.vote.end;
  1337. } else {
  1338. return null;
  1339. }
  1340. },
  1341. expiredVote() {
  1342. let end = this.getVoteEnd();
  1343. if (end) {
  1344. end = moment(end);
  1345. return end.isBefore(new Date());
  1346. }
  1347. return false;
  1348. },
  1349. voteMemberPositive() {
  1350. if (this.vote && this.vote.positive)
  1351. return Users.find({ _id: { $in: this.vote.positive } });
  1352. return [];
  1353. },
  1354. voteMemberNegative() {
  1355. if (this.vote && this.vote.negative)
  1356. return Users.find({ _id: { $in: this.vote.negative } });
  1357. return [];
  1358. },
  1359. voteState() {
  1360. const userId = Meteor.userId();
  1361. let state;
  1362. if (this.vote) {
  1363. if (this.vote.positive) {
  1364. state = _.contains(this.vote.positive, userId);
  1365. if (state === true) return true;
  1366. }
  1367. if (this.vote.negative) {
  1368. state = _.contains(this.vote.negative, userId);
  1369. if (state === true) return false;
  1370. }
  1371. }
  1372. return null;
  1373. },
  1374. getPokerQuestion() {
  1375. if (this.isLinkedCard()) {
  1376. const card = Cards.findOne({ _id: this.linkedId });
  1377. if (card === undefined) {
  1378. return null;
  1379. } else if (card && card.poker) {
  1380. return card.poker.question;
  1381. } else {
  1382. return null;
  1383. }
  1384. } else if (this.isLinkedBoard()) {
  1385. const board = Boards.findOne({ _id: this.linkedId });
  1386. if (board === undefined) {
  1387. return null;
  1388. } else if (board && board.poker) {
  1389. return board.poker.question;
  1390. } else {
  1391. return null;
  1392. }
  1393. } else if (this.poker) {
  1394. return this.poker.question;
  1395. } else {
  1396. return null;
  1397. }
  1398. },
  1399. getPokerEstimation() {
  1400. if (this.poker) {
  1401. return this.poker.estimation;
  1402. } else {
  1403. return null;
  1404. }
  1405. },
  1406. getPokerEnd() {
  1407. if (this.isLinkedCard()) {
  1408. const card = Cards.findOne({ _id: this.linkedId });
  1409. if (card === undefined) {
  1410. return null;
  1411. } else if (card && card.poker) {
  1412. return card.poker.end;
  1413. } else {
  1414. return null;
  1415. }
  1416. } else if (this.isLinkedBoard()) {
  1417. const board = Boards.findOne({ _id: this.linkedId });
  1418. if (board === undefined) {
  1419. return null;
  1420. } else if (board && board.poker) {
  1421. return board.poker.end;
  1422. } else {
  1423. return null;
  1424. }
  1425. } else if (this.poker) {
  1426. return this.poker.end;
  1427. } else {
  1428. return null;
  1429. }
  1430. },
  1431. expiredPoker() {
  1432. let end = this.getPokerEnd();
  1433. if (end) {
  1434. end = moment(end);
  1435. return end.isBefore(new Date());
  1436. }
  1437. return false;
  1438. },
  1439. pokerMemberOne() {
  1440. if (this.poker && this.poker.one)
  1441. return Users.find({ _id: { $in: this.poker.one } });
  1442. return [];
  1443. },
  1444. pokerMemberTwo() {
  1445. if (this.poker && this.poker.two)
  1446. return Users.find({ _id: { $in: this.poker.two } });
  1447. return [];
  1448. },
  1449. pokerMemberThree() {
  1450. if (this.poker && this.poker.three)
  1451. return Users.find({ _id: { $in: this.poker.three } });
  1452. return [];
  1453. },
  1454. pokerMemberFive() {
  1455. if (this.poker && this.poker.five)
  1456. return Users.find({ _id: { $in: this.poker.five } });
  1457. return [];
  1458. },
  1459. pokerMemberEight() {
  1460. if (this.poker && this.poker.eight)
  1461. return Users.find({ _id: { $in: this.poker.eight } });
  1462. return [];
  1463. },
  1464. pokerMemberThirteen() {
  1465. if (this.poker && this.poker.thirteen)
  1466. return Users.find({ _id: { $in: this.poker.thirteen } });
  1467. return [];
  1468. },
  1469. pokerMemberTwenty() {
  1470. if (this.poker && this.poker.twenty)
  1471. return Users.find({ _id: { $in: this.poker.twenty } });
  1472. return [];
  1473. },
  1474. pokerMemberForty() {
  1475. if (this.poker && this.poker.forty)
  1476. return Users.find({ _id: { $in: this.poker.forty } });
  1477. return [];
  1478. },
  1479. pokerMemberOneHundred() {
  1480. if (this.poker && this.poker.oneHundred)
  1481. return Users.find({ _id: { $in: this.poker.oneHundred } });
  1482. return [];
  1483. },
  1484. pokerMemberUnsure() {
  1485. if (this.poker && this.poker.unsure)
  1486. return Users.find({ _id: { $in: this.poker.unsure } });
  1487. return [];
  1488. },
  1489. pokerState() {
  1490. const userId = Meteor.userId();
  1491. let state;
  1492. if (this.poker) {
  1493. if (this.poker.one) {
  1494. state = _.contains(this.poker.one, userId);
  1495. if (state === true) {
  1496. return 'one';
  1497. }
  1498. }
  1499. if (this.poker.two) {
  1500. state = _.contains(this.poker.two, userId);
  1501. if (state === true) {
  1502. return 'two';
  1503. }
  1504. }
  1505. if (this.poker.three) {
  1506. state = _.contains(this.poker.three, userId);
  1507. if (state === true) {
  1508. return 'three';
  1509. }
  1510. }
  1511. if (this.poker.five) {
  1512. state = _.contains(this.poker.five, userId);
  1513. if (state === true) {
  1514. return 'five';
  1515. }
  1516. }
  1517. if (this.poker.eight) {
  1518. state = _.contains(this.poker.eight, userId);
  1519. if (state === true) {
  1520. return 'eight';
  1521. }
  1522. }
  1523. if (this.poker.thirteen) {
  1524. state = _.contains(this.poker.thirteen, userId);
  1525. if (state === true) {
  1526. return 'thirteen';
  1527. }
  1528. }
  1529. if (this.poker.twenty) {
  1530. state = _.contains(this.poker.twenty, userId);
  1531. if (state === true) {
  1532. return 'twenty';
  1533. }
  1534. }
  1535. if (this.poker.forty) {
  1536. state = _.contains(this.poker.forty, userId);
  1537. if (state === true) {
  1538. return 'forty';
  1539. }
  1540. }
  1541. if (this.poker.oneHundred) {
  1542. state = _.contains(this.poker.oneHundred, userId);
  1543. if (state === true) {
  1544. return 'oneHundred';
  1545. }
  1546. }
  1547. if (this.poker.unsure) {
  1548. state = _.contains(this.poker.unsure, userId);
  1549. if (state === true) {
  1550. return 'unsure';
  1551. }
  1552. }
  1553. }
  1554. return null;
  1555. },
  1556. getId() {
  1557. if (this.isLinked()) {
  1558. return this.linkedId;
  1559. } else {
  1560. return this._id;
  1561. }
  1562. },
  1563. getTitle() {
  1564. if (this.isLinkedCard()) {
  1565. const card = Cards.findOne({ _id: this.linkedId });
  1566. if (card === undefined) {
  1567. return null;
  1568. } else {
  1569. return card.title;
  1570. }
  1571. } else if (this.isLinkedBoard()) {
  1572. const board = Boards.findOne({ _id: this.linkedId });
  1573. if (board === undefined) {
  1574. return null;
  1575. } else {
  1576. return board.title;
  1577. }
  1578. } else if (this.title === undefined) {
  1579. return null;
  1580. } else {
  1581. return this.title;
  1582. }
  1583. },
  1584. getCardNumber() {
  1585. return this.cardNumber;
  1586. },
  1587. getBoardTitle() {
  1588. if (this.isLinkedCard()) {
  1589. const card = Cards.findOne({ _id: this.linkedId });
  1590. if (card === undefined) {
  1591. return null;
  1592. }
  1593. const board = Boards.findOne({ _id: card.boardId });
  1594. if (board === undefined) {
  1595. return null;
  1596. } else {
  1597. return board.title;
  1598. }
  1599. } else if (this.isLinkedBoard()) {
  1600. const board = Boards.findOne({ _id: this.linkedId });
  1601. if (board === undefined) {
  1602. return null;
  1603. } else {
  1604. return board.title;
  1605. }
  1606. } else {
  1607. const board = Boards.findOne({ _id: this.boardId });
  1608. if (board === undefined) {
  1609. return null;
  1610. } else {
  1611. return board.title;
  1612. }
  1613. }
  1614. },
  1615. setTitle(title) {
  1616. if (this.isLinkedCard()) {
  1617. return Cards.update({ _id: this.linkedId }, { $set: { title } });
  1618. } else if (this.isLinkedBoard()) {
  1619. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  1620. } else {
  1621. return Cards.update({ _id: this._id }, { $set: { title } });
  1622. }
  1623. },
  1624. getArchived() {
  1625. if (this.isLinkedCard()) {
  1626. const card = Cards.findOne({ _id: this.linkedId });
  1627. if (card === undefined) {
  1628. return null;
  1629. } else {
  1630. return card.archived;
  1631. }
  1632. } else if (this.isLinkedBoard()) {
  1633. const board = Boards.findOne({ _id: this.linkedId });
  1634. if (board === undefined) {
  1635. return null;
  1636. } else {
  1637. return board.archived;
  1638. }
  1639. } else {
  1640. return this.archived;
  1641. }
  1642. },
  1643. setRequestedBy(requestedBy) {
  1644. if (this.isLinkedCard()) {
  1645. return Cards.update({ _id: this.linkedId }, { $set: { requestedBy } });
  1646. } else {
  1647. return Cards.update({ _id: this._id }, { $set: { requestedBy } });
  1648. }
  1649. },
  1650. getRequestedBy() {
  1651. if (this.isLinkedCard()) {
  1652. const card = Cards.findOne({ _id: this.linkedId });
  1653. if (card === undefined) {
  1654. return null;
  1655. } else {
  1656. return card.requestedBy;
  1657. }
  1658. } else {
  1659. return this.requestedBy;
  1660. }
  1661. },
  1662. setAssignedBy(assignedBy) {
  1663. if (this.isLinkedCard()) {
  1664. return Cards.update({ _id: this.linkedId }, { $set: { assignedBy } });
  1665. } else {
  1666. return Cards.update({ _id: this._id }, { $set: { assignedBy } });
  1667. }
  1668. },
  1669. getAssignedBy() {
  1670. if (this.isLinkedCard()) {
  1671. const card = Cards.findOne({ _id: this.linkedId });
  1672. if (card === undefined) {
  1673. return null;
  1674. } else {
  1675. return card.assignedBy;
  1676. }
  1677. } else {
  1678. return this.assignedBy;
  1679. }
  1680. },
  1681. isTemplateCard() {
  1682. return this.type === 'template-card';
  1683. },
  1684. votePublic() {
  1685. if (this.vote) return this.vote.public;
  1686. return null;
  1687. },
  1688. voteAllowNonBoardMembers() {
  1689. if (this.vote) return this.vote.allowNonBoardMembers;
  1690. return null;
  1691. },
  1692. voteCountNegative() {
  1693. if (this.vote && this.vote.negative) return this.vote.negative.length;
  1694. return null;
  1695. },
  1696. voteCountPositive() {
  1697. if (this.vote && this.vote.positive) return this.vote.positive.length;
  1698. return null;
  1699. },
  1700. voteCount() {
  1701. return this.voteCountPositive() + this.voteCountNegative();
  1702. },
  1703. pokerAllowNonBoardMembers() {
  1704. if (this.poker) return this.poker.allowNonBoardMembers;
  1705. return null;
  1706. },
  1707. pokerCountOne() {
  1708. if (this.poker && this.poker.one) return this.poker.one.length;
  1709. return null;
  1710. },
  1711. pokerCountTwo() {
  1712. if (this.poker && this.poker.two) return this.poker.two.length;
  1713. return null;
  1714. },
  1715. pokerCountThree() {
  1716. if (this.poker && this.poker.three) return this.poker.three.length;
  1717. return null;
  1718. },
  1719. pokerCountFive() {
  1720. if (this.poker && this.poker.five) return this.poker.five.length;
  1721. return null;
  1722. },
  1723. pokerCountEight() {
  1724. if (this.poker && this.poker.eight) return this.poker.eight.length;
  1725. return null;
  1726. },
  1727. pokerCountThirteen() {
  1728. if (this.poker && this.poker.thirteen) return this.poker.thirteen.length;
  1729. return null;
  1730. },
  1731. pokerCountTwenty() {
  1732. if (this.poker && this.poker.twenty) return this.poker.twenty.length;
  1733. return null;
  1734. },
  1735. pokerCountForty() {
  1736. if (this.poker && this.poker.forty) return this.poker.forty.length;
  1737. return null;
  1738. },
  1739. pokerCountOneHundred() {
  1740. if (this.poker && this.poker.oneHundred) return this.poker.oneHundred.length;
  1741. return null;
  1742. },
  1743. pokerCountUnsure() {
  1744. if (this.poker && this.poker.unsure) return this.poker.unsure.length;
  1745. return null;
  1746. },
  1747. pokerCount() {
  1748. return (
  1749. this.pokerCountOne() +
  1750. this.pokerCountTwo() +
  1751. this.pokerCountThree() +
  1752. this.pokerCountFive() +
  1753. this.pokerCountEight() +
  1754. this.pokerCountThirteen() +
  1755. this.pokerCountTwenty() +
  1756. this.pokerCountForty() +
  1757. this.pokerCountOneHundred() +
  1758. this.pokerCountUnsure()
  1759. );
  1760. },
  1761. pokerWinner() {
  1762. const pokerListMaps = [];
  1763. let pokerWinnersListMap = [];
  1764. if (this.expiredPoker()) {
  1765. const one = { count: this.pokerCountOne(), pokerCard: 1 };
  1766. const two = { count: this.pokerCountTwo(), pokerCard: 2 };
  1767. const three = { count: this.pokerCountThree(), pokerCard: 3 };
  1768. const five = { count: this.pokerCountFive(), pokerCard: 5 };
  1769. const eight = { count: this.pokerCountEight(), pokerCard: 8 };
  1770. const thirteen = { count: this.pokerCountThirteen(), pokerCard: 13 };
  1771. const twenty = { count: this.pokerCountTwenty(), pokerCard: 20 };
  1772. const forty = { count: this.pokerCountForty(), pokerCard: 40 };
  1773. const oneHundred = { count: this.pokerCountOneHundred(), pokerCard: 100 };
  1774. const unsure = { count: this.pokerCountUnsure(), pokerCard: 'Unsure' };
  1775. pokerListMaps.push(one);
  1776. pokerListMaps.push(two);
  1777. pokerListMaps.push(three);
  1778. pokerListMaps.push(five);
  1779. pokerListMaps.push(eight);
  1780. pokerListMaps.push(thirteen);
  1781. pokerListMaps.push(twenty);
  1782. pokerListMaps.push(forty);
  1783. pokerListMaps.push(oneHundred);
  1784. pokerListMaps.push(unsure);
  1785. pokerListMaps.sort(function(a, b) {
  1786. return b.count - a.count;
  1787. });
  1788. const max = pokerListMaps[0].count;
  1789. pokerWinnersListMap = pokerListMaps.filter(task => task.count >= max);
  1790. pokerWinnersListMap.sort(function(a, b) {
  1791. return b.pokerCard - a.pokerCard;
  1792. });
  1793. }
  1794. return pokerWinnersListMap[0].pokerCard;
  1795. },
  1796. });
  1797. Cards.mutations({
  1798. applyToChildren(funct) {
  1799. Cards.find({
  1800. parentId: this._id,
  1801. }).forEach(card => {
  1802. funct(card);
  1803. });
  1804. },
  1805. archive() {
  1806. this.applyToChildren(card => {
  1807. return card.archive();
  1808. });
  1809. return {
  1810. $set: {
  1811. archived: true,
  1812. archivedAt: new Date(),
  1813. },
  1814. };
  1815. },
  1816. restore() {
  1817. this.applyToChildren(card => {
  1818. return card.restore();
  1819. });
  1820. return {
  1821. $set: {
  1822. archived: false,
  1823. },
  1824. };
  1825. },
  1826. moveToEndOfList({ listId } = {}) {
  1827. let swimlaneId = this.swimlaneId;
  1828. const boardId = this.boardId;
  1829. let sortIndex = 0;
  1830. // This should never happen, but there was a bug that was fixed in commit
  1831. // ea0239538a68e225c867411a4f3e0d27c158383.
  1832. if (!swimlaneId) {
  1833. const board = Boards.findOne(boardId);
  1834. swimlaneId = board.getDefaultSwimline()._id;
  1835. }
  1836. // Move the minicard to the end of the target list
  1837. let parentElementDom = $(`#swimlane-${this.swimlaneId}`).get(0);
  1838. if (!parentElementDom) parentElementDom = $(':root');
  1839. const lastCardDom = $(parentElementDom)
  1840. .find(`#js-list-${listId} .js-minicard:last`)
  1841. .get(0);
  1842. if (lastCardDom) sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  1843. return this.moveOptionalArgs({
  1844. boardId,
  1845. swimlaneId,
  1846. listId,
  1847. sort: sortIndex,
  1848. });
  1849. },
  1850. moveOptionalArgs({ boardId, swimlaneId, listId, sort } = {}) {
  1851. boardId = boardId || this.boardId;
  1852. swimlaneId = swimlaneId || this.swimlaneId;
  1853. // This should never happen, but there was a bug that was fixed in commit
  1854. // ea0239538a68e225c867411a4f3e0d27c158383.
  1855. if (!swimlaneId) {
  1856. const board = Boards.findOne(boardId);
  1857. swimlaneId = board.getDefaultSwimline()._id;
  1858. }
  1859. listId = listId || this.listId;
  1860. if (sort === undefined || sort === null) sort = this.sort;
  1861. return this.move(boardId, swimlaneId, listId, sort);
  1862. },
  1863. move(boardId, swimlaneId, listId, sort = null) {
  1864. const mutatedFields = {
  1865. boardId,
  1866. swimlaneId,
  1867. listId,
  1868. };
  1869. if (sort !== null) {
  1870. mutatedFields.sort = sort;
  1871. }
  1872. // we must only copy the labels and custom fields if the target board
  1873. // differs from the source board
  1874. if (this.boardId !== boardId) {
  1875. // Get label names
  1876. const oldBoard = Boards.findOne(this.boardId);
  1877. const oldBoardLabels = oldBoard.labels;
  1878. const oldCardLabels = _.pluck(
  1879. _.filter(oldBoardLabels, label => {
  1880. return _.contains(this.labelIds, label._id);
  1881. }),
  1882. 'name',
  1883. );
  1884. const newBoard = Boards.findOne(boardId);
  1885. const newBoardLabels = newBoard.labels;
  1886. const newCardLabelIds = _.pluck(
  1887. _.filter(newBoardLabels, label => {
  1888. return label.name && _.contains(oldCardLabels, label.name);
  1889. }),
  1890. '_id',
  1891. );
  1892. // assign the new card number from the target board
  1893. const newCardNumber = newBoard.getNextCardNumber();
  1894. Object.assign(mutatedFields, {
  1895. labelIds: newCardLabelIds,
  1896. cardNumber: newCardNumber
  1897. });
  1898. mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  1899. }
  1900. Cards.update(this._id, {
  1901. $set: mutatedFields,
  1902. });
  1903. },
  1904. addLabel(labelId) {
  1905. this.labelIds.push(labelId);
  1906. return {
  1907. $addToSet: {
  1908. labelIds: labelId,
  1909. },
  1910. };
  1911. },
  1912. removeLabel(labelId) {
  1913. this.labelIds = _.without(this.labelIds, labelId);
  1914. return {
  1915. $pull: {
  1916. labelIds: labelId,
  1917. },
  1918. };
  1919. },
  1920. toggleLabel(labelId) {
  1921. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1922. return this.removeLabel(labelId);
  1923. } else {
  1924. return this.addLabel(labelId);
  1925. }
  1926. },
  1927. setColor(newColor) {
  1928. if (newColor === 'white') {
  1929. newColor = null;
  1930. }
  1931. return {
  1932. $set: {
  1933. color: newColor,
  1934. },
  1935. };
  1936. },
  1937. assignMember(memberId) {
  1938. return {
  1939. $addToSet: {
  1940. members: memberId,
  1941. },
  1942. };
  1943. },
  1944. assignAssignee(assigneeId) {
  1945. // If there is not any assignee, allow one assignee, not more.
  1946. /*
  1947. if (this.getAssignees().length === 0) {
  1948. return {
  1949. $addToSet: {
  1950. assignees: assigneeId,
  1951. },
  1952. };
  1953. */
  1954. // Allow more that one assignee:
  1955. // https://github.com/wekan/wekan/issues/3302
  1956. return {
  1957. $addToSet: {
  1958. assignees: assigneeId,
  1959. },
  1960. };
  1961. //} else {
  1962. // return false,
  1963. //}
  1964. },
  1965. unassignMember(memberId) {
  1966. return {
  1967. $pull: {
  1968. members: memberId,
  1969. },
  1970. };
  1971. },
  1972. unassignAssignee(assigneeId) {
  1973. return {
  1974. $pull: {
  1975. assignees: assigneeId,
  1976. },
  1977. };
  1978. },
  1979. toggleMember(memberId) {
  1980. if (this.members && this.members.indexOf(memberId) > -1) {
  1981. return this.unassignMember(memberId);
  1982. } else {
  1983. return this.assignMember(memberId);
  1984. }
  1985. },
  1986. toggleAssignee(assigneeId) {
  1987. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1988. return this.unassignAssignee(assigneeId);
  1989. } else {
  1990. return this.assignAssignee(assigneeId);
  1991. }
  1992. },
  1993. assignCustomField(customFieldId) {
  1994. return {
  1995. $addToSet: {
  1996. customFields: {
  1997. _id: customFieldId,
  1998. value: null,
  1999. },
  2000. },
  2001. };
  2002. },
  2003. unassignCustomField(customFieldId) {
  2004. return {
  2005. $pull: {
  2006. customFields: {
  2007. _id: customFieldId,
  2008. },
  2009. },
  2010. };
  2011. },
  2012. toggleCustomField(customFieldId) {
  2013. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  2014. return this.unassignCustomField(customFieldId);
  2015. } else {
  2016. return this.assignCustomField(customFieldId);
  2017. }
  2018. },
  2019. setCustomField(customFieldId, value) {
  2020. // todo
  2021. const index = this.customFieldIndex(customFieldId);
  2022. if (index > -1) {
  2023. const update = {
  2024. $set: {},
  2025. };
  2026. update.$set[`customFields.${index}.value`] = value;
  2027. return update;
  2028. }
  2029. // TODO
  2030. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  2031. return null;
  2032. },
  2033. setCover(coverId) {
  2034. return {
  2035. $set: {
  2036. coverId,
  2037. },
  2038. };
  2039. },
  2040. unsetCover() {
  2041. return {
  2042. $unset: {
  2043. coverId: '',
  2044. },
  2045. };
  2046. },
  2047. //setReceived(receivedAt) {
  2048. // return {
  2049. // $set: {
  2050. // receivedAt,
  2051. // },
  2052. // };
  2053. //},
  2054. unsetReceived() {
  2055. return {
  2056. $unset: {
  2057. receivedAt: '',
  2058. },
  2059. };
  2060. },
  2061. //setStart(startAt) {
  2062. // return {
  2063. // $set: {
  2064. // startAt,
  2065. // },
  2066. // };
  2067. //},
  2068. unsetStart() {
  2069. return {
  2070. $unset: {
  2071. startAt: '',
  2072. },
  2073. };
  2074. },
  2075. //setDue(dueAt) {
  2076. // return {
  2077. // $set: {
  2078. // dueAt,
  2079. // },
  2080. // };
  2081. //},
  2082. unsetDue() {
  2083. return {
  2084. $unset: {
  2085. dueAt: '',
  2086. },
  2087. };
  2088. },
  2089. //setEnd(endAt) {
  2090. // return {
  2091. // $set: {
  2092. // endAt,
  2093. // },
  2094. // };
  2095. //},
  2096. unsetEnd() {
  2097. return {
  2098. $unset: {
  2099. endAt: '',
  2100. },
  2101. };
  2102. },
  2103. setOvertime(isOvertime) {
  2104. return {
  2105. $set: {
  2106. isOvertime,
  2107. },
  2108. };
  2109. },
  2110. setSpentTime(spentTime) {
  2111. return {
  2112. $set: {
  2113. spentTime,
  2114. },
  2115. };
  2116. },
  2117. unsetSpentTime() {
  2118. return {
  2119. $unset: {
  2120. spentTime: '',
  2121. isOvertime: false,
  2122. },
  2123. };
  2124. },
  2125. setParentId(parentId) {
  2126. return {
  2127. $set: {
  2128. parentId,
  2129. },
  2130. };
  2131. },
  2132. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  2133. return {
  2134. $set: {
  2135. vote: {
  2136. question,
  2137. public: publicVote,
  2138. allowNonBoardMembers,
  2139. positive: [],
  2140. negative: [],
  2141. },
  2142. },
  2143. };
  2144. },
  2145. unsetVote() {
  2146. return {
  2147. $unset: {
  2148. vote: '',
  2149. },
  2150. };
  2151. },
  2152. setVoteEnd(end) {
  2153. return {
  2154. $set: { 'vote.end': end },
  2155. };
  2156. },
  2157. unsetVoteEnd() {
  2158. return {
  2159. $unset: { 'vote.end': '' },
  2160. };
  2161. },
  2162. setVote(userId, forIt) {
  2163. switch (forIt) {
  2164. case true:
  2165. // vote for it
  2166. return {
  2167. $pull: {
  2168. 'vote.negative': userId,
  2169. },
  2170. $addToSet: {
  2171. 'vote.positive': userId,
  2172. },
  2173. };
  2174. case false:
  2175. // vote against
  2176. return {
  2177. $pull: {
  2178. 'vote.positive': userId,
  2179. },
  2180. $addToSet: {
  2181. 'vote.negative': userId,
  2182. },
  2183. };
  2184. default:
  2185. // Remove votes
  2186. return {
  2187. $pull: {
  2188. 'vote.positive': userId,
  2189. 'vote.negative': userId,
  2190. },
  2191. };
  2192. }
  2193. },
  2194. setPokerQuestion(question, allowNonBoardMembers) {
  2195. return {
  2196. $set: {
  2197. poker: {
  2198. question,
  2199. allowNonBoardMembers,
  2200. one: [],
  2201. two: [],
  2202. three: [],
  2203. five: [],
  2204. eight: [],
  2205. thirteen: [],
  2206. twenty: [],
  2207. forty: [],
  2208. oneHundred: [],
  2209. unsure: [],
  2210. },
  2211. },
  2212. };
  2213. },
  2214. setPokerEstimation(estimation) {
  2215. return {
  2216. $set: { 'poker.estimation': estimation },
  2217. };
  2218. },
  2219. unsetPokerEstimation() {
  2220. return {
  2221. $unset: { 'poker.estimation': '' },
  2222. };
  2223. },
  2224. unsetPoker() {
  2225. return {
  2226. $unset: {
  2227. poker: '',
  2228. },
  2229. };
  2230. },
  2231. setPokerEnd(end) {
  2232. return {
  2233. $set: { 'poker.end': end },
  2234. };
  2235. },
  2236. unsetPokerEnd() {
  2237. return {
  2238. $unset: { 'poker.end': '' },
  2239. };
  2240. },
  2241. setPoker(userId, state) {
  2242. switch (state) {
  2243. case 'one':
  2244. // poker one
  2245. return {
  2246. $pull: {
  2247. 'poker.two': userId,
  2248. 'poker.three': userId,
  2249. 'poker.five': userId,
  2250. 'poker.eight': userId,
  2251. 'poker.thirteen': userId,
  2252. 'poker.twenty': userId,
  2253. 'poker.forty': userId,
  2254. 'poker.oneHundred': userId,
  2255. 'poker.unsure': userId,
  2256. },
  2257. $addToSet: {
  2258. 'poker.one': userId,
  2259. },
  2260. };
  2261. case 'two':
  2262. // poker two
  2263. return {
  2264. $pull: {
  2265. 'poker.one': userId,
  2266. 'poker.three': userId,
  2267. 'poker.five': userId,
  2268. 'poker.eight': userId,
  2269. 'poker.thirteen': userId,
  2270. 'poker.twenty': userId,
  2271. 'poker.forty': userId,
  2272. 'poker.oneHundred': userId,
  2273. 'poker.unsure': userId,
  2274. },
  2275. $addToSet: {
  2276. 'poker.two': userId,
  2277. },
  2278. };
  2279. case 'three':
  2280. // poker three
  2281. return {
  2282. $pull: {
  2283. 'poker.one': userId,
  2284. 'poker.two': userId,
  2285. 'poker.five': userId,
  2286. 'poker.eight': userId,
  2287. 'poker.thirteen': userId,
  2288. 'poker.twenty': userId,
  2289. 'poker.forty': userId,
  2290. 'poker.oneHundred': userId,
  2291. 'poker.unsure': userId,
  2292. },
  2293. $addToSet: {
  2294. 'poker.three': userId,
  2295. },
  2296. };
  2297. case 'five':
  2298. // poker five
  2299. return {
  2300. $pull: {
  2301. 'poker.one': userId,
  2302. 'poker.two': userId,
  2303. 'poker.three': userId,
  2304. 'poker.eight': userId,
  2305. 'poker.thirteen': userId,
  2306. 'poker.twenty': userId,
  2307. 'poker.forty': userId,
  2308. 'poker.oneHundred': userId,
  2309. 'poker.unsure': userId,
  2310. },
  2311. $addToSet: {
  2312. 'poker.five': userId,
  2313. },
  2314. };
  2315. case 'eight':
  2316. // poker eight
  2317. return {
  2318. $pull: {
  2319. 'poker.one': userId,
  2320. 'poker.two': userId,
  2321. 'poker.three': userId,
  2322. 'poker.five': userId,
  2323. 'poker.thirteen': userId,
  2324. 'poker.twenty': userId,
  2325. 'poker.forty': userId,
  2326. 'poker.oneHundred': userId,
  2327. 'poker.unsure': userId,
  2328. },
  2329. $addToSet: {
  2330. 'poker.eight': userId,
  2331. },
  2332. };
  2333. case 'thirteen':
  2334. // poker thirteen
  2335. return {
  2336. $pull: {
  2337. 'poker.one': userId,
  2338. 'poker.two': userId,
  2339. 'poker.three': userId,
  2340. 'poker.five': userId,
  2341. 'poker.eight': userId,
  2342. 'poker.twenty': userId,
  2343. 'poker.forty': userId,
  2344. 'poker.oneHundred': userId,
  2345. 'poker.unsure': userId,
  2346. },
  2347. $addToSet: {
  2348. 'poker.thirteen': userId,
  2349. },
  2350. };
  2351. case 'twenty':
  2352. // poker twenty
  2353. return {
  2354. $pull: {
  2355. 'poker.one': userId,
  2356. 'poker.two': userId,
  2357. 'poker.three': userId,
  2358. 'poker.five': userId,
  2359. 'poker.eight': userId,
  2360. 'poker.thirteen': userId,
  2361. 'poker.forty': userId,
  2362. 'poker.oneHundred': userId,
  2363. 'poker.unsure': userId,
  2364. },
  2365. $addToSet: {
  2366. 'poker.twenty': userId,
  2367. },
  2368. };
  2369. case 'forty':
  2370. // poker forty
  2371. return {
  2372. $pull: {
  2373. 'poker.one': userId,
  2374. 'poker.two': userId,
  2375. 'poker.three': userId,
  2376. 'poker.five': userId,
  2377. 'poker.eight': userId,
  2378. 'poker.thirteen': userId,
  2379. 'poker.twenty': userId,
  2380. 'poker.oneHundred': userId,
  2381. 'poker.unsure': userId,
  2382. },
  2383. $addToSet: {
  2384. 'poker.forty': userId,
  2385. },
  2386. };
  2387. case 'oneHundred':
  2388. // poker one hundred
  2389. return {
  2390. $pull: {
  2391. 'poker.one': userId,
  2392. 'poker.two': userId,
  2393. 'poker.three': userId,
  2394. 'poker.five': userId,
  2395. 'poker.eight': userId,
  2396. 'poker.thirteen': userId,
  2397. 'poker.twenty': userId,
  2398. 'poker.forty': userId,
  2399. 'poker.unsure': userId,
  2400. },
  2401. $addToSet: {
  2402. 'poker.oneHundred': userId,
  2403. },
  2404. };
  2405. case 'unsure':
  2406. // poker unsure
  2407. return {
  2408. $pull: {
  2409. 'poker.one': userId,
  2410. 'poker.two': userId,
  2411. 'poker.three': userId,
  2412. 'poker.five': userId,
  2413. 'poker.eight': userId,
  2414. 'poker.thirteen': userId,
  2415. 'poker.twenty': userId,
  2416. 'poker.forty': userId,
  2417. 'poker.oneHundred': userId,
  2418. },
  2419. $addToSet: {
  2420. 'poker.unsure': userId,
  2421. },
  2422. };
  2423. default:
  2424. // Remove pokers
  2425. return {
  2426. $pull: {
  2427. 'poker.one': userId,
  2428. 'poker.two': userId,
  2429. 'poker.three': userId,
  2430. 'poker.five': userId,
  2431. 'poker.eight': userId,
  2432. 'poker.thirteen': userId,
  2433. 'poker.twenty': userId,
  2434. 'poker.forty': userId,
  2435. 'poker.oneHundred': userId,
  2436. 'poker.unsure': userId,
  2437. },
  2438. };
  2439. }
  2440. },
  2441. replayPoker() {
  2442. return {
  2443. $set: {
  2444. 'poker.one': [],
  2445. 'poker.two': [],
  2446. 'poker.three': [],
  2447. 'poker.five': [],
  2448. 'poker.eight': [],
  2449. 'poker.thirteen': [],
  2450. 'poker.twelve': [],
  2451. 'poker.forty': [],
  2452. 'poker.oneHundred': [],
  2453. 'poker.unsure': [],
  2454. },
  2455. };
  2456. },
  2457. });
  2458. //FUNCTIONS FOR creation of Activities
  2459. function updateActivities(doc, fieldNames, modifier) {
  2460. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  2461. Activities.find({
  2462. activityType: 'addedLabel',
  2463. cardId: doc._id,
  2464. }).forEach(a => {
  2465. const lidx = doc.labelIds.indexOf(a.labelId);
  2466. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  2467. Activities.update(a._id, {
  2468. $set: {
  2469. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  2470. boardId: modifier.$set.boardId,
  2471. },
  2472. });
  2473. } else {
  2474. Activities.remove(a._id);
  2475. }
  2476. });
  2477. } else if (_.contains(fieldNames, 'boardId')) {
  2478. Activities.remove({
  2479. activityType: 'addedLabel',
  2480. cardId: doc._id,
  2481. });
  2482. }
  2483. }
  2484. function cardMove(
  2485. userId,
  2486. doc,
  2487. fieldNames,
  2488. oldListId,
  2489. oldSwimlaneId,
  2490. oldBoardId,
  2491. ) {
  2492. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  2493. Activities.insert({
  2494. userId,
  2495. activityType: 'moveCardBoard',
  2496. boardName: Boards.findOne(doc.boardId).title,
  2497. boardId: doc.boardId,
  2498. oldBoardId,
  2499. oldBoardName: Boards.findOne(oldBoardId).title,
  2500. cardId: doc._id,
  2501. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2502. swimlaneId: doc.swimlaneId,
  2503. oldSwimlaneId,
  2504. });
  2505. } else if (
  2506. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  2507. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  2508. ) {
  2509. Activities.insert({
  2510. userId,
  2511. oldListId,
  2512. activityType: 'moveCard',
  2513. listName: Lists.findOne(doc.listId).title,
  2514. listId: doc.listId,
  2515. boardId: doc.boardId,
  2516. cardId: doc._id,
  2517. cardTitle: doc.title,
  2518. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2519. swimlaneId: doc.swimlaneId,
  2520. oldSwimlaneId,
  2521. });
  2522. }
  2523. }
  2524. function cardState(userId, doc, fieldNames) {
  2525. if (_.contains(fieldNames, 'archived')) {
  2526. if (doc.archived) {
  2527. Activities.insert({
  2528. userId,
  2529. activityType: 'archivedCard',
  2530. listName: Lists.findOne(doc.listId).title,
  2531. boardId: doc.boardId,
  2532. listId: doc.listId,
  2533. cardId: doc._id,
  2534. swimlaneId: doc.swimlaneId,
  2535. });
  2536. } else {
  2537. Activities.insert({
  2538. userId,
  2539. activityType: 'restoredCard',
  2540. boardId: doc.boardId,
  2541. listName: Lists.findOne(doc.listId).title,
  2542. listId: doc.listId,
  2543. cardId: doc._id,
  2544. swimlaneId: doc.swimlaneId,
  2545. });
  2546. }
  2547. }
  2548. }
  2549. function cardMembers(userId, doc, fieldNames, modifier) {
  2550. if (!_.contains(fieldNames, 'members')) return;
  2551. let memberId;
  2552. // Say hello to the new member
  2553. if (modifier.$addToSet && modifier.$addToSet.members) {
  2554. memberId = modifier.$addToSet.members;
  2555. const username = Users.findOne(memberId).username;
  2556. if (!_.contains(doc.members, memberId)) {
  2557. Activities.insert({
  2558. userId,
  2559. username,
  2560. activityType: 'joinMember',
  2561. boardId: doc.boardId,
  2562. cardId: doc._id,
  2563. memberId,
  2564. listId: doc.listId,
  2565. swimlaneId: doc.swimlaneId,
  2566. });
  2567. }
  2568. }
  2569. // Say goodbye to the former member
  2570. if (modifier.$pull && modifier.$pull.members) {
  2571. memberId = modifier.$pull.members;
  2572. const username = Users.findOne(memberId).username;
  2573. // Check that the former member is member of the card
  2574. if (_.contains(doc.members, memberId)) {
  2575. Activities.insert({
  2576. userId,
  2577. username,
  2578. activityType: 'unjoinMember',
  2579. boardId: doc.boardId,
  2580. cardId: doc._id,
  2581. memberId,
  2582. listId: doc.listId,
  2583. swimlaneId: doc.swimlaneId,
  2584. });
  2585. }
  2586. }
  2587. }
  2588. function cardAssignees(userId, doc, fieldNames, modifier) {
  2589. if (!_.contains(fieldNames, 'assignees')) return;
  2590. let assigneeId;
  2591. // Say hello to the new assignee
  2592. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  2593. assigneeId = modifier.$addToSet.assignees;
  2594. const username = Users.findOne(assigneeId).username;
  2595. if (!_.contains(doc.assignees, assigneeId)) {
  2596. Activities.insert({
  2597. userId,
  2598. username,
  2599. activityType: 'joinAssignee',
  2600. boardId: doc.boardId,
  2601. cardId: doc._id,
  2602. assigneeId,
  2603. listId: doc.listId,
  2604. swimlaneId: doc.swimlaneId,
  2605. });
  2606. }
  2607. }
  2608. // Say goodbye to the former assignee
  2609. if (modifier.$pull && modifier.$pull.assignees) {
  2610. assigneeId = modifier.$pull.assignees;
  2611. const username = Users.findOne(assigneeId).username;
  2612. // Check that the former assignee is assignee of the card
  2613. if (_.contains(doc.assignees, assigneeId)) {
  2614. Activities.insert({
  2615. userId,
  2616. username,
  2617. activityType: 'unjoinAssignee',
  2618. boardId: doc.boardId,
  2619. cardId: doc._id,
  2620. assigneeId,
  2621. listId: doc.listId,
  2622. swimlaneId: doc.swimlaneId,
  2623. });
  2624. }
  2625. }
  2626. }
  2627. function cardLabels(userId, doc, fieldNames, modifier) {
  2628. if (!_.contains(fieldNames, 'labelIds')) return;
  2629. let labelId;
  2630. // Say hello to the new label
  2631. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  2632. labelId = modifier.$addToSet.labelIds;
  2633. if (!_.contains(doc.labelIds, labelId)) {
  2634. const act = {
  2635. userId,
  2636. labelId,
  2637. activityType: 'addedLabel',
  2638. boardId: doc.boardId,
  2639. cardId: doc._id,
  2640. listId: doc.listId,
  2641. swimlaneId: doc.swimlaneId,
  2642. };
  2643. Activities.insert(act);
  2644. }
  2645. }
  2646. // Say goodbye to the label
  2647. if (modifier.$pull && modifier.$pull.labelIds) {
  2648. labelId = modifier.$pull.labelIds;
  2649. // Check that the former member is member of the card
  2650. if (_.contains(doc.labelIds, labelId)) {
  2651. Activities.insert({
  2652. userId,
  2653. labelId,
  2654. activityType: 'removedLabel',
  2655. boardId: doc.boardId,
  2656. cardId: doc._id,
  2657. listId: doc.listId,
  2658. swimlaneId: doc.swimlaneId,
  2659. });
  2660. }
  2661. }
  2662. }
  2663. function cardCustomFields(userId, doc, fieldNames, modifier) {
  2664. if (!_.contains(fieldNames, 'customFields')) return;
  2665. // Say hello to the new customField value
  2666. if (modifier.$set) {
  2667. _.each(modifier.$set, (value, key) => {
  2668. if (key.startsWith('customFields')) {
  2669. const dotNotation = key.split('.');
  2670. // only individual changes are registered
  2671. if (dotNotation.length > 1) {
  2672. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2673. const act = {
  2674. userId,
  2675. customFieldId,
  2676. value,
  2677. activityType: 'setCustomField',
  2678. boardId: doc.boardId,
  2679. cardId: doc._id,
  2680. listId: doc.listId,
  2681. swimlaneId: doc.swimlaneId,
  2682. };
  2683. Activities.insert(act);
  2684. }
  2685. }
  2686. });
  2687. }
  2688. // Say goodbye to the former customField value
  2689. if (modifier.$unset) {
  2690. _.each(modifier.$unset, (value, key) => {
  2691. if (key.startsWith('customFields')) {
  2692. const dotNotation = key.split('.');
  2693. // only individual changes are registered
  2694. if (dotNotation.length > 1) {
  2695. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2696. const act = {
  2697. userId,
  2698. customFieldId,
  2699. activityType: 'unsetCustomField',
  2700. boardId: doc.boardId,
  2701. cardId: doc._id,
  2702. };
  2703. Activities.insert(act);
  2704. }
  2705. }
  2706. });
  2707. }
  2708. }
  2709. function cardCreation(userId, doc) {
  2710. Activities.insert({
  2711. userId,
  2712. activityType: 'createCard',
  2713. boardId: doc.boardId,
  2714. listName: Lists.findOne(doc.listId).title,
  2715. listId: doc.listId,
  2716. cardId: doc._id,
  2717. cardTitle: doc.title,
  2718. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2719. swimlaneId: doc.swimlaneId,
  2720. });
  2721. }
  2722. function cardRemover(userId, doc) {
  2723. ChecklistItems.remove({
  2724. cardId: doc._id,
  2725. });
  2726. Checklists.remove({
  2727. cardId: doc._id,
  2728. });
  2729. Cards.remove({
  2730. parentId: doc._id,
  2731. });
  2732. CardComments.remove({
  2733. cardId: doc._id,
  2734. });
  2735. Attachments.remove({
  2736. cardId: doc._id,
  2737. });
  2738. }
  2739. const findDueCards = days => {
  2740. const seekDue = ($from, $to, activityType) => {
  2741. Cards.find({
  2742. archived: false,
  2743. dueAt: { $gte: $from, $lt: $to },
  2744. }).forEach(card => {
  2745. const username = Users.findOne(card.userId).username;
  2746. const activity = {
  2747. userId: card.userId,
  2748. username,
  2749. activityType,
  2750. boardId: card.boardId,
  2751. cardId: card._id,
  2752. cardTitle: card.title,
  2753. listId: card.listId,
  2754. timeValue: card.dueAt,
  2755. swimlaneId: card.swimlaneId,
  2756. };
  2757. Activities.insert(activity);
  2758. });
  2759. };
  2760. const now = new Date(),
  2761. aday = 3600 * 24 * 1e3,
  2762. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2763. if (!days) return;
  2764. if (!days.map) days = [days];
  2765. days.map(day => {
  2766. let args = [];
  2767. if (day === 0) {
  2768. args = [then(0), then(1), 'duenow'];
  2769. } else if (day > 0) {
  2770. args = [then(1), then(day), 'almostdue'];
  2771. } else {
  2772. args = [then(day), now, 'pastdue'];
  2773. }
  2774. seekDue(...args);
  2775. });
  2776. };
  2777. const addCronJob = _.debounce(
  2778. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2779. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2780. if (!envValue) {
  2781. return;
  2782. }
  2783. const notifydays = envValue
  2784. .split(',')
  2785. .map(value => {
  2786. const iValue = parseInt(value, 10);
  2787. if (!(iValue > 0 && iValue < 15)) {
  2788. // notifying due is disabled
  2789. return false;
  2790. } else {
  2791. return iValue;
  2792. }
  2793. })
  2794. .filter(Boolean);
  2795. 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
  2796. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2797. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2798. const scheduler = (job => () => {
  2799. const now = new Date();
  2800. const hour = 3600 * 1e3;
  2801. if (now.getHours() === itvl) {
  2802. if (typeof job === 'function') {
  2803. job();
  2804. }
  2805. }
  2806. Meteor.setTimeout(scheduler, hour);
  2807. })(() => {
  2808. findDueCards(notifydays);
  2809. });
  2810. scheduler();
  2811. }),
  2812. 500,
  2813. );
  2814. if (Meteor.isServer) {
  2815. // Cards are often fetched within a board, so we create an index to make these
  2816. // queries more efficient.
  2817. Meteor.startup(() => {
  2818. Cards._collection.createIndex({ modifiedAt: -1 });
  2819. Cards._collection.createIndex({ boardId: 1, createdAt: -1 });
  2820. // https://github.com/wekan/wekan/issues/1863
  2821. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2822. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2823. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2824. // To correct it, add Index to parentId:
  2825. Cards._collection.createIndex({ parentId: 1 });
  2826. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2827. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2828. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2829. Meteor.defer(() => {
  2830. addCronJob();
  2831. });
  2832. });
  2833. Cards.after.insert((userId, doc) => {
  2834. cardCreation(userId, doc);
  2835. });
  2836. // New activity for card (un)archivage
  2837. Cards.after.update((userId, doc, fieldNames) => {
  2838. cardState(userId, doc, fieldNames);
  2839. });
  2840. //New activity for card moves
  2841. Cards.after.update(function(userId, doc, fieldNames) {
  2842. const oldListId = this.previous.listId;
  2843. const oldSwimlaneId = this.previous.swimlaneId;
  2844. const oldBoardId = this.previous.boardId;
  2845. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2846. });
  2847. // Add a new activity if we add or remove a member to the card
  2848. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2849. cardMembers(userId, doc, fieldNames, modifier);
  2850. updateActivities(doc, fieldNames, modifier);
  2851. });
  2852. // Add a new activity if we add or remove a assignee to the card
  2853. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2854. cardAssignees(userId, doc, fieldNames, modifier);
  2855. updateActivities(doc, fieldNames, modifier);
  2856. });
  2857. // Add a new activity if we add or remove a label to the card
  2858. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2859. cardLabels(userId, doc, fieldNames, modifier);
  2860. });
  2861. // Add a new activity if we edit a custom field
  2862. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2863. cardCustomFields(userId, doc, fieldNames, modifier);
  2864. });
  2865. // Add a new activity if modify time related field like dueAt startAt etc
  2866. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2867. const dla = 'dateLastActivity';
  2868. const fields = fieldNames.filter(name => name !== dla);
  2869. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2870. const action = fields[0];
  2871. if (fields.length > 0 && _.contains(timingaction, action)) {
  2872. // add activities for user change these attributes
  2873. const value = modifier.$set[action];
  2874. const oldvalue = doc[action] || '';
  2875. const activityType = `a-${action}`;
  2876. const card = Cards.findOne(doc._id);
  2877. const list = card.list();
  2878. if (list) {
  2879. // change list modifiedAt, when user modified the key values in
  2880. // timingaction array, if it's endAt, put the modifiedAt of list
  2881. // back to one year ago for sorting purpose
  2882. const modifiedAt = moment()
  2883. .subtract(1, 'year')
  2884. .toISOString();
  2885. const boardId = list.boardId;
  2886. Lists.direct.update(
  2887. {
  2888. _id: list._id,
  2889. },
  2890. {
  2891. $set: {
  2892. modifiedAt,
  2893. boardId,
  2894. },
  2895. },
  2896. );
  2897. }
  2898. const username = Users.findOne(userId).username;
  2899. const activity = {
  2900. userId,
  2901. username,
  2902. activityType,
  2903. boardId: doc.boardId,
  2904. cardId: doc._id,
  2905. cardTitle: doc.title,
  2906. timeKey: action,
  2907. timeValue: value,
  2908. timeOldValue: oldvalue,
  2909. listId: card.listId,
  2910. swimlaneId: card.swimlaneId,
  2911. };
  2912. Activities.insert(activity);
  2913. }
  2914. });
  2915. // Remove all activities associated with a card if we remove the card
  2916. // Remove also card_comments / checklists / attachments
  2917. Cards.before.remove((userId, doc) => {
  2918. cardRemover(userId, doc);
  2919. });
  2920. }
  2921. //SWIMLANES REST API
  2922. if (Meteor.isServer) {
  2923. /**
  2924. * @operation get_swimlane_cards
  2925. * @summary get all cards attached to a swimlane
  2926. *
  2927. * @param {string} boardId the board ID
  2928. * @param {string} swimlaneId the swimlane ID
  2929. * @return_type [{_id: string,
  2930. * title: string,
  2931. * description: string,
  2932. * listId: string}]
  2933. */
  2934. JsonRoutes.add(
  2935. 'GET',
  2936. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2937. function(req, res) {
  2938. Authentication.checkUserId(req.userId);
  2939. const paramBoardId = req.params.boardId;
  2940. const paramSwimlaneId = req.params.swimlaneId;
  2941. JsonRoutes.sendResult(res, {
  2942. code: 200,
  2943. data: Cards.find({
  2944. boardId: paramBoardId,
  2945. swimlaneId: paramSwimlaneId,
  2946. archived: false,
  2947. }).map(function(doc) {
  2948. return {
  2949. _id: doc._id,
  2950. title: doc.title,
  2951. description: doc.description,
  2952. listId: doc.listId,
  2953. receivedAt: doc.receivedAt,
  2954. startAt: doc.startAt,
  2955. dueAt: doc.dueAt,
  2956. endAt: doc.endAt,
  2957. assignees: doc.assignees,
  2958. };
  2959. }),
  2960. });
  2961. },
  2962. );
  2963. }
  2964. //LISTS REST API
  2965. if (Meteor.isServer) {
  2966. /**
  2967. * @operation get_all_cards
  2968. * @summary Get all Cards attached to a List
  2969. *
  2970. * @param {string} boardId the board ID
  2971. * @param {string} listId the list ID
  2972. * @return_type [{_id: string,
  2973. * title: string,
  2974. * description: string}]
  2975. */
  2976. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2977. req,
  2978. res,
  2979. ) {
  2980. Authentication.checkUserId(req.userId);
  2981. const paramBoardId = req.params.boardId;
  2982. const paramListId = req.params.listId;
  2983. JsonRoutes.sendResult(res, {
  2984. code: 200,
  2985. data: Cards.find({
  2986. boardId: paramBoardId,
  2987. listId: paramListId,
  2988. archived: false,
  2989. }).map(function(doc) {
  2990. return {
  2991. _id: doc._id,
  2992. title: doc.title,
  2993. description: doc.description,
  2994. receivedAt: doc.receivedAt,
  2995. startAt: doc.startAt,
  2996. dueAt: doc.dueAt,
  2997. endAt: doc.endAt,
  2998. assignees: doc.assignees,
  2999. };
  3000. }),
  3001. });
  3002. });
  3003. /**
  3004. * @operation get_card
  3005. * @summary Get a Card
  3006. *
  3007. * @param {string} boardId the board ID
  3008. * @param {string} listId the list ID of the card
  3009. * @param {string} cardId the card ID
  3010. * @return_type Cards
  3011. */
  3012. JsonRoutes.add(
  3013. 'GET',
  3014. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3015. function(req, res) {
  3016. Authentication.checkUserId(req.userId);
  3017. const paramBoardId = req.params.boardId;
  3018. const paramListId = req.params.listId;
  3019. const paramCardId = req.params.cardId;
  3020. JsonRoutes.sendResult(res, {
  3021. code: 200,
  3022. data: Cards.findOne({
  3023. _id: paramCardId,
  3024. listId: paramListId,
  3025. boardId: paramBoardId,
  3026. archived: false,
  3027. }),
  3028. });
  3029. },
  3030. );
  3031. /**
  3032. * @operation new_card
  3033. * @summary Create a new Card
  3034. *
  3035. * @param {string} boardId the board ID of the new card
  3036. * @param {string} listId the list ID of the new card
  3037. * @param {string} authorID the user ID of the person owning the card
  3038. * @param {string} parentId the parent ID of the new card
  3039. * @param {string} title the title of the new card
  3040. * @param {string} description the description of the new card
  3041. * @param {string} swimlaneId the swimlane ID of the new card
  3042. * @param {string} [members] the member IDs list of the new card
  3043. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  3044. * @return_type {_id: string}
  3045. */
  3046. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  3047. req,
  3048. res,
  3049. ) {
  3050. // Check user is logged in
  3051. Authentication.checkLoggedIn(req.userId);
  3052. const paramBoardId = req.params.boardId;
  3053. // Check user has permission to add card to the board
  3054. const board = Boards.findOne({
  3055. _id: paramBoardId,
  3056. });
  3057. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  3058. Authentication.checkAdminOrCondition(req.userId, addPermission);
  3059. const paramListId = req.params.listId;
  3060. const paramParentId = req.params.parentId;
  3061. const nextCardNumber = board.getNextCardNumber();
  3062. const currentCards = Cards.find(
  3063. {
  3064. listId: paramListId,
  3065. archived: false,
  3066. },
  3067. { sort: ['sort'] },
  3068. );
  3069. const check = Users.findOne({
  3070. _id: req.body.authorId,
  3071. });
  3072. const members = req.body.members;
  3073. const assignees = req.body.assignees;
  3074. if (typeof check !== 'undefined') {
  3075. const id = Cards.direct.insert({
  3076. title: req.body.title,
  3077. boardId: paramBoardId,
  3078. listId: paramListId,
  3079. parentId: paramParentId,
  3080. description: req.body.description,
  3081. userId: req.body.authorId,
  3082. swimlaneId: req.body.swimlaneId,
  3083. sort: currentCards.count(),
  3084. cardNumber: nextCardNumber,
  3085. members,
  3086. assignees,
  3087. });
  3088. JsonRoutes.sendResult(res, {
  3089. code: 200,
  3090. data: {
  3091. _id: id,
  3092. },
  3093. });
  3094. const card = Cards.findOne({
  3095. _id: id,
  3096. });
  3097. cardCreation(req.body.authorId, card);
  3098. } else {
  3099. JsonRoutes.sendResult(res, {
  3100. code: 401,
  3101. });
  3102. }
  3103. });
  3104. /*
  3105. * Note for the JSDoc:
  3106. * 'list' will be interpreted as the path parameter
  3107. * 'listID' will be interpreted as the body parameter
  3108. */
  3109. /**
  3110. * @operation edit_card
  3111. * @summary Edit Fields in a Card
  3112. *
  3113. * @description Edit a card
  3114. *
  3115. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  3116. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  3117. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  3118. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  3119. * `indigo`:
  3120. *
  3121. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  3122. *
  3123. * Note: setting the color to white has the same effect than removing it.
  3124. *
  3125. * @param {string} boardId the board ID of the card
  3126. * @param {string} list the list ID of the card
  3127. * @param {string} cardId the ID of the card
  3128. * @param {string} [title] the new title of the card
  3129. * @param {string} [sort] the new sort value of the card
  3130. * @param {string} [listId] the new list ID of the card (move operation)
  3131. * @param {string} [description] the new description of the card
  3132. * @param {string} [authorId] change the owner of the card
  3133. * @param {string} [parentId] change the parent of the card
  3134. * @param {string} [labelIds] the new list of label IDs attached to the card
  3135. * @param {string} [swimlaneId] the new swimlane ID of the card
  3136. * @param {string} [members] the new list of member IDs attached to the card
  3137. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  3138. * @param {string} [requestedBy] the new requestedBy field of the card
  3139. * @param {string} [assignedBy] the new assignedBy field of the card
  3140. * @param {string} [receivedAt] the new receivedAt field of the card
  3141. * @param {string} [assignBy] the new assignBy field of the card
  3142. * @param {string} [startAt] the new startAt field of the card
  3143. * @param {string} [dueAt] the new dueAt field of the card
  3144. * @param {string} [endAt] the new endAt field of the card
  3145. * @param {string} [spentTime] the new spentTime field of the card
  3146. * @param {boolean} [isOverTime] the new isOverTime field of the card
  3147. * @param {string} [customFields] the new customFields value of the card
  3148. * @param {string} [color] the new color of the card
  3149. * @param {Object} [vote] the vote object
  3150. * @param {string} vote.question the vote question
  3151. * @param {boolean} vote.public show who voted what
  3152. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  3153. * @param {Object} [poker] the poker object
  3154. * @param {string} poker.question the vote question
  3155. * @param {boolean} poker.allowNonBoardMembers allow all logged in users to vote?
  3156. * @return_type {_id: string}
  3157. */
  3158. JsonRoutes.add(
  3159. 'PUT',
  3160. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3161. function(req, res) {
  3162. Authentication.checkUserId(req.userId);
  3163. const paramBoardId = req.params.boardId;
  3164. const paramCardId = req.params.cardId;
  3165. const paramListId = req.params.listId;
  3166. if (req.body.hasOwnProperty('title')) {
  3167. const newTitle = req.body.title;
  3168. Cards.direct.update(
  3169. {
  3170. _id: paramCardId,
  3171. listId: paramListId,
  3172. boardId: paramBoardId,
  3173. archived: false,
  3174. },
  3175. {
  3176. $set: {
  3177. title: newTitle,
  3178. },
  3179. },
  3180. );
  3181. }
  3182. if (req.body.hasOwnProperty('sort')) {
  3183. const newSort = req.body.sort;
  3184. Cards.direct.update(
  3185. {
  3186. _id: paramCardId,
  3187. listId: paramListId,
  3188. boardId: paramBoardId,
  3189. archived: false,
  3190. },
  3191. {
  3192. $set: {
  3193. sort: newSort,
  3194. },
  3195. },
  3196. );
  3197. }
  3198. if (req.body.hasOwnProperty('parentId')) {
  3199. const newParentId = req.body.parentId;
  3200. Cards.direct.update(
  3201. {
  3202. _id: paramCardId,
  3203. listId: paramListId,
  3204. boardId: paramBoardId,
  3205. archived: false,
  3206. },
  3207. {
  3208. $set: {
  3209. parentId: newParentId,
  3210. },
  3211. },
  3212. );
  3213. }
  3214. if (req.body.hasOwnProperty('description')) {
  3215. const newDescription = req.body.description;
  3216. Cards.direct.update(
  3217. {
  3218. _id: paramCardId,
  3219. listId: paramListId,
  3220. boardId: paramBoardId,
  3221. archived: false,
  3222. },
  3223. {
  3224. $set: {
  3225. description: newDescription,
  3226. },
  3227. },
  3228. );
  3229. }
  3230. if (req.body.hasOwnProperty('color')) {
  3231. const newColor = req.body.color;
  3232. Cards.direct.update(
  3233. {
  3234. _id: paramCardId,
  3235. listId: paramListId,
  3236. boardId: paramBoardId,
  3237. archived: false,
  3238. },
  3239. { $set: { color: newColor } },
  3240. );
  3241. }
  3242. if (req.body.hasOwnProperty('vote')) {
  3243. const newVote = req.body.vote;
  3244. newVote.positive = [];
  3245. newVote.negative = [];
  3246. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  3247. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  3248. newVote.allowNonBoardMembers = false;
  3249. Cards.direct.update(
  3250. {
  3251. _id: paramCardId,
  3252. listId: paramListId,
  3253. boardId: paramBoardId,
  3254. archived: false,
  3255. },
  3256. { $set: { vote: newVote } },
  3257. );
  3258. }
  3259. if (req.body.hasOwnProperty('poker')) {
  3260. const newPoker = req.body.poker;
  3261. newPoker.one = [];
  3262. newPoker.two = [];
  3263. newPoker.three = [];
  3264. newPoker.five = [];
  3265. newPoker.eight = [];
  3266. newPoker.thirteen = [];
  3267. newPoker.twenty = [];
  3268. newPoker.forty = [];
  3269. newPoker.oneHundred = [];
  3270. newPoker.unsure = [];
  3271. if (!newPoker.hasOwnProperty('allowNonBoardMembers'))
  3272. newPoker.allowNonBoardMembers = false;
  3273. Cards.direct.update(
  3274. {
  3275. _id: paramCardId,
  3276. listId: paramListId,
  3277. boardId: paramBoardId,
  3278. archived: false,
  3279. },
  3280. { $set: { poker: newPoker } },
  3281. );
  3282. }
  3283. if (req.body.hasOwnProperty('labelIds')) {
  3284. let newlabelIds = req.body.labelIds;
  3285. if (_.isString(newlabelIds)) {
  3286. if (newlabelIds === '') {
  3287. newlabelIds = null;
  3288. } else {
  3289. newlabelIds = [newlabelIds];
  3290. }
  3291. }
  3292. Cards.direct.update(
  3293. {
  3294. _id: paramCardId,
  3295. listId: paramListId,
  3296. boardId: paramBoardId,
  3297. archived: false,
  3298. },
  3299. {
  3300. $set: {
  3301. labelIds: newlabelIds,
  3302. },
  3303. },
  3304. );
  3305. }
  3306. if (req.body.hasOwnProperty('requestedBy')) {
  3307. const newrequestedBy = req.body.requestedBy;
  3308. Cards.direct.update(
  3309. {
  3310. _id: paramCardId,
  3311. listId: paramListId,
  3312. boardId: paramBoardId,
  3313. archived: false,
  3314. },
  3315. { $set: { requestedBy: newrequestedBy } },
  3316. );
  3317. }
  3318. if (req.body.hasOwnProperty('assignedBy')) {
  3319. const newassignedBy = req.body.assignedBy;
  3320. Cards.direct.update(
  3321. {
  3322. _id: paramCardId,
  3323. listId: paramListId,
  3324. boardId: paramBoardId,
  3325. archived: false,
  3326. },
  3327. { $set: { assignedBy: newassignedBy } },
  3328. );
  3329. }
  3330. if (req.body.hasOwnProperty('receivedAt')) {
  3331. const newreceivedAt = req.body.receivedAt;
  3332. Cards.direct.update(
  3333. {
  3334. _id: paramCardId,
  3335. listId: paramListId,
  3336. boardId: paramBoardId,
  3337. archived: false,
  3338. },
  3339. { $set: { receivedAt: newreceivedAt } },
  3340. );
  3341. }
  3342. if (req.body.hasOwnProperty('startAt')) {
  3343. const newstartAt = req.body.startAt;
  3344. Cards.direct.update(
  3345. {
  3346. _id: paramCardId,
  3347. listId: paramListId,
  3348. boardId: paramBoardId,
  3349. archived: false,
  3350. },
  3351. { $set: { startAt: newstartAt } },
  3352. );
  3353. }
  3354. if (req.body.hasOwnProperty('dueAt')) {
  3355. const newdueAt = req.body.dueAt;
  3356. Cards.direct.update(
  3357. {
  3358. _id: paramCardId,
  3359. listId: paramListId,
  3360. boardId: paramBoardId,
  3361. archived: false,
  3362. },
  3363. { $set: { dueAt: newdueAt } },
  3364. );
  3365. }
  3366. if (req.body.hasOwnProperty('endAt')) {
  3367. const newendAt = req.body.endAt;
  3368. Cards.direct.update(
  3369. {
  3370. _id: paramCardId,
  3371. listId: paramListId,
  3372. boardId: paramBoardId,
  3373. archived: false,
  3374. },
  3375. { $set: { endAt: newendAt } },
  3376. );
  3377. }
  3378. if (req.body.hasOwnProperty('spentTime')) {
  3379. const newspentTime = req.body.spentTime;
  3380. Cards.direct.update(
  3381. {
  3382. _id: paramCardId,
  3383. listId: paramListId,
  3384. boardId: paramBoardId,
  3385. archived: false,
  3386. },
  3387. { $set: { spentTime: newspentTime } },
  3388. );
  3389. }
  3390. if (req.body.hasOwnProperty('isOverTime')) {
  3391. const newisOverTime = req.body.isOverTime;
  3392. Cards.direct.update(
  3393. {
  3394. _id: paramCardId,
  3395. listId: paramListId,
  3396. boardId: paramBoardId,
  3397. archived: false,
  3398. },
  3399. { $set: { isOverTime: newisOverTime } },
  3400. );
  3401. }
  3402. if (req.body.hasOwnProperty('customFields')) {
  3403. const newcustomFields = req.body.customFields;
  3404. Cards.direct.update(
  3405. {
  3406. _id: paramCardId,
  3407. listId: paramListId,
  3408. boardId: paramBoardId,
  3409. archived: false,
  3410. },
  3411. { $set: { customFields: newcustomFields } },
  3412. );
  3413. }
  3414. if (req.body.hasOwnProperty('members')) {
  3415. let newmembers = req.body.members;
  3416. if (_.isString(newmembers)) {
  3417. if (newmembers === '') {
  3418. newmembers = null;
  3419. } else {
  3420. newmembers = [newmembers];
  3421. }
  3422. }
  3423. Cards.direct.update(
  3424. {
  3425. _id: paramCardId,
  3426. listId: paramListId,
  3427. boardId: paramBoardId,
  3428. archived: false,
  3429. },
  3430. { $set: { members: newmembers } },
  3431. );
  3432. }
  3433. if (req.body.hasOwnProperty('assignees')) {
  3434. let newassignees = req.body.assignees;
  3435. if (_.isString(newassignees)) {
  3436. if (newassignees === '') {
  3437. newassignees = null;
  3438. } else {
  3439. newassignees = [newassignees];
  3440. }
  3441. }
  3442. Cards.direct.update(
  3443. {
  3444. _id: paramCardId,
  3445. listId: paramListId,
  3446. boardId: paramBoardId,
  3447. archived: false,
  3448. },
  3449. { $set: { assignees: newassignees } },
  3450. );
  3451. }
  3452. if (req.body.hasOwnProperty('swimlaneId')) {
  3453. const newParamSwimlaneId = req.body.swimlaneId;
  3454. Cards.direct.update(
  3455. {
  3456. _id: paramCardId,
  3457. listId: paramListId,
  3458. boardId: paramBoardId,
  3459. archived: false,
  3460. },
  3461. { $set: { swimlaneId: newParamSwimlaneId } },
  3462. );
  3463. }
  3464. if (req.body.hasOwnProperty('listId')) {
  3465. const newParamListId = req.body.listId;
  3466. Cards.direct.update(
  3467. {
  3468. _id: paramCardId,
  3469. listId: paramListId,
  3470. boardId: paramBoardId,
  3471. archived: false,
  3472. },
  3473. {
  3474. $set: {
  3475. listId: newParamListId,
  3476. },
  3477. },
  3478. );
  3479. const card = Cards.findOne({
  3480. _id: paramCardId,
  3481. });
  3482. cardMove(
  3483. req.body.authorId,
  3484. card,
  3485. {
  3486. fieldName: 'listId',
  3487. },
  3488. paramListId,
  3489. );
  3490. }
  3491. JsonRoutes.sendResult(res, {
  3492. code: 200,
  3493. data: {
  3494. _id: paramCardId,
  3495. },
  3496. });
  3497. },
  3498. );
  3499. /**
  3500. * @operation delete_card
  3501. * @summary Delete a card from a board
  3502. *
  3503. * @description This operation **deletes** a card, and therefore the card
  3504. * is not put in the recycle bin.
  3505. *
  3506. * @param {string} boardId the board ID of the card
  3507. * @param {string} list the list ID of the card
  3508. * @param {string} cardId the ID of the card
  3509. * @return_type {_id: string}
  3510. */
  3511. JsonRoutes.add(
  3512. 'DELETE',
  3513. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3514. function(req, res) {
  3515. Authentication.checkUserId(req.userId);
  3516. const paramBoardId = req.params.boardId;
  3517. const paramListId = req.params.listId;
  3518. const paramCardId = req.params.cardId;
  3519. const card = Cards.findOne({
  3520. _id: paramCardId,
  3521. });
  3522. Cards.direct.remove({
  3523. _id: paramCardId,
  3524. listId: paramListId,
  3525. boardId: paramBoardId,
  3526. });
  3527. cardRemover(req.body.authorId, card);
  3528. JsonRoutes.sendResult(res, {
  3529. code: 200,
  3530. data: {
  3531. _id: paramCardId,
  3532. },
  3533. });
  3534. },
  3535. );
  3536. /**
  3537. * @operation get_cards_by_custom_field
  3538. * @summary Get all Cards that matchs a value of a specific custom field
  3539. *
  3540. * @param {string} boardId the board ID
  3541. * @param {string} customFieldId the list ID
  3542. * @param {string} customFieldValue the value to look for
  3543. * @return_type [{_id: string,
  3544. * title: string,
  3545. * description: string,
  3546. * listId: string,
  3547. * swinlaneId: string}]
  3548. */
  3549. JsonRoutes.add(
  3550. 'GET',
  3551. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  3552. function(req, res) {
  3553. Authentication.checkUserId(req.userId);
  3554. const paramBoardId = req.params.boardId;
  3555. const paramCustomFieldId = req.params.customFieldId;
  3556. const paramCustomFieldValue = req.params.customFieldValue;
  3557. JsonRoutes.sendResult(res, {
  3558. code: 200,
  3559. data: Cards.find({
  3560. boardId: paramBoardId,
  3561. customFields: {
  3562. $elemMatch: {
  3563. _id: paramCustomFieldId,
  3564. value: paramCustomFieldValue,
  3565. },
  3566. },
  3567. archived: false,
  3568. }).fetch(),
  3569. });
  3570. },
  3571. );
  3572. }
  3573. export default Cards;