cards.js 95 KB

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