cards.js 95 KB

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