cards.js 97 KB

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