cards.js 102 KB

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