cards.js 101 KB

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