cards.js 99 KB

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