cards.js 103 KB

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