cards.js 93 KB

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