cards.js 96 KB

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