cards.js 99 KB

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