cards.js 92 KB

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