cards.js 103 KB

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