cards.js 121 KB

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