cards.js 101 KB

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