cards.js 99 KB

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