cards.js 97 KB

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