cards.js 106 KB

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