cards.js 93 KB

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