cards.js 93 KB

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