cards.js 99 KB

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