cards.js 100 KB

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