cards.js 91 KB

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