cards.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518
  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. const mutatedFields = {
  1941. boardId,
  1942. swimlaneId,
  1943. listId,
  1944. };
  1945. if (sort !== null) {
  1946. mutatedFields.sort = sort;
  1947. }
  1948. // we must only copy the labels and custom fields if the target board
  1949. // differs from the source board
  1950. if (this.boardId !== boardId) {
  1951. // Get label names
  1952. const oldBoard = ReactiveCache.getBoard(this.boardId);
  1953. const oldBoardLabels = oldBoard.labels;
  1954. const oldCardLabels = _.pluck(
  1955. _.filter(oldBoardLabels, label => {
  1956. return _.contains(this.labelIds, label._id);
  1957. }),
  1958. 'name',
  1959. );
  1960. const newBoard = ReactiveCache.getBoard(boardId);
  1961. const newBoardLabels = newBoard.labels;
  1962. const newCardLabelIds = _.pluck(
  1963. _.filter(newBoardLabels, label => {
  1964. return label.name && _.contains(oldCardLabels, label.name);
  1965. }),
  1966. '_id',
  1967. );
  1968. // assign the new card number from the target board
  1969. const newCardNumber = newBoard.getNextCardNumber();
  1970. Object.assign(mutatedFields, {
  1971. labelIds: newCardLabelIds,
  1972. cardNumber: newCardNumber
  1973. });
  1974. mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  1975. }
  1976. Cards.update(this._id, {
  1977. $set: mutatedFields,
  1978. });
  1979. },
  1980. addLabel(labelId) {
  1981. this.labelIds.push(labelId);
  1982. return {
  1983. $addToSet: {
  1984. labelIds: labelId,
  1985. },
  1986. };
  1987. },
  1988. removeLabel(labelId) {
  1989. this.labelIds = _.without(this.labelIds, labelId);
  1990. return {
  1991. $pull: {
  1992. labelIds: labelId,
  1993. },
  1994. };
  1995. },
  1996. toggleLabel(labelId) {
  1997. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1998. return this.removeLabel(labelId);
  1999. } else {
  2000. return this.addLabel(labelId);
  2001. }
  2002. },
  2003. setColor(newColor) {
  2004. if (newColor === 'white') {
  2005. newColor = null;
  2006. }
  2007. return {
  2008. $set: {
  2009. color: newColor,
  2010. },
  2011. };
  2012. },
  2013. assignMember(memberId) {
  2014. return {
  2015. $addToSet: {
  2016. members: memberId,
  2017. },
  2018. };
  2019. },
  2020. assignAssignee(assigneeId) {
  2021. // If there is not any assignee, allow one assignee, not more.
  2022. /*
  2023. if (this.getAssignees().length === 0) {
  2024. return {
  2025. $addToSet: {
  2026. assignees: assigneeId,
  2027. },
  2028. };
  2029. */
  2030. // Allow more that one assignee:
  2031. // https://github.com/wekan/wekan/issues/3302
  2032. return {
  2033. $addToSet: {
  2034. assignees: assigneeId,
  2035. },
  2036. };
  2037. //} else {
  2038. // return false,
  2039. //}
  2040. },
  2041. unassignMember(memberId) {
  2042. return {
  2043. $pull: {
  2044. members: memberId,
  2045. },
  2046. };
  2047. },
  2048. unassignAssignee(assigneeId) {
  2049. return {
  2050. $pull: {
  2051. assignees: assigneeId,
  2052. },
  2053. };
  2054. },
  2055. toggleMember(memberId) {
  2056. if (this.members && this.members.indexOf(memberId) > -1) {
  2057. return this.unassignMember(memberId);
  2058. } else {
  2059. return this.assignMember(memberId);
  2060. }
  2061. },
  2062. toggleAssignee(assigneeId) {
  2063. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  2064. return this.unassignAssignee(assigneeId);
  2065. } else {
  2066. return this.assignAssignee(assigneeId);
  2067. }
  2068. },
  2069. assignCustomField(customFieldId) {
  2070. return {
  2071. $addToSet: {
  2072. customFields: {
  2073. _id: customFieldId,
  2074. value: null,
  2075. },
  2076. },
  2077. };
  2078. },
  2079. unassignCustomField(customFieldId) {
  2080. return {
  2081. $pull: {
  2082. customFields: {
  2083. _id: customFieldId,
  2084. },
  2085. },
  2086. };
  2087. },
  2088. toggleCustomField(customFieldId) {
  2089. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  2090. return this.unassignCustomField(customFieldId);
  2091. } else {
  2092. return this.assignCustomField(customFieldId);
  2093. }
  2094. },
  2095. toggleShowActivities() {
  2096. return {
  2097. $set: {
  2098. showActivities: !this.showActivities,
  2099. }
  2100. };
  2101. },
  2102. toggleHideFinishedChecklist() {
  2103. return {
  2104. $set: {
  2105. hideFinishedChecklistIfItemsAreHidden: !this.hideFinishedChecklistIfItemsAreHidden,
  2106. }
  2107. };
  2108. },
  2109. setCustomField(customFieldId, value) {
  2110. // todo
  2111. const index = this.customFieldIndex(customFieldId);
  2112. if (index > -1) {
  2113. const update = {
  2114. $set: {},
  2115. };
  2116. update.$set[`customFields.${index}.value`] = value;
  2117. return update;
  2118. }
  2119. // TODO
  2120. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  2121. return null;
  2122. },
  2123. setCover(coverId) {
  2124. return {
  2125. $set: {
  2126. coverId,
  2127. },
  2128. };
  2129. },
  2130. unsetCover() {
  2131. return {
  2132. $unset: {
  2133. coverId: '',
  2134. },
  2135. };
  2136. },
  2137. //setReceived(receivedAt) {
  2138. // return {
  2139. // $set: {
  2140. // receivedAt,
  2141. // },
  2142. // };
  2143. //},
  2144. unsetReceived() {
  2145. return {
  2146. $unset: {
  2147. receivedAt: '',
  2148. },
  2149. };
  2150. },
  2151. //setStart(startAt) {
  2152. // return {
  2153. // $set: {
  2154. // startAt,
  2155. // },
  2156. // };
  2157. //},
  2158. unsetStart() {
  2159. return {
  2160. $unset: {
  2161. startAt: '',
  2162. },
  2163. };
  2164. },
  2165. //setDue(dueAt) {
  2166. // return {
  2167. // $set: {
  2168. // dueAt,
  2169. // },
  2170. // };
  2171. //},
  2172. unsetDue() {
  2173. return {
  2174. $unset: {
  2175. dueAt: '',
  2176. },
  2177. };
  2178. },
  2179. //setEnd(endAt) {
  2180. // return {
  2181. // $set: {
  2182. // endAt,
  2183. // },
  2184. // };
  2185. //},
  2186. unsetEnd() {
  2187. return {
  2188. $unset: {
  2189. endAt: '',
  2190. },
  2191. };
  2192. },
  2193. setOvertime(isOvertime) {
  2194. return {
  2195. $set: {
  2196. isOvertime,
  2197. },
  2198. };
  2199. },
  2200. setSpentTime(spentTime) {
  2201. return {
  2202. $set: {
  2203. spentTime,
  2204. },
  2205. };
  2206. },
  2207. unsetSpentTime() {
  2208. return {
  2209. $unset: {
  2210. spentTime: '',
  2211. isOvertime: false,
  2212. },
  2213. };
  2214. },
  2215. setParentId(parentId) {
  2216. return {
  2217. $set: {
  2218. parentId,
  2219. },
  2220. };
  2221. },
  2222. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  2223. return {
  2224. $set: {
  2225. vote: {
  2226. question,
  2227. public: publicVote,
  2228. allowNonBoardMembers,
  2229. positive: [],
  2230. negative: [],
  2231. },
  2232. },
  2233. };
  2234. },
  2235. unsetVote() {
  2236. return {
  2237. $unset: {
  2238. vote: '',
  2239. },
  2240. };
  2241. },
  2242. setVoteEnd(end) {
  2243. return {
  2244. $set: { 'vote.end': end },
  2245. };
  2246. },
  2247. unsetVoteEnd() {
  2248. return {
  2249. $unset: { 'vote.end': '' },
  2250. };
  2251. },
  2252. setVote(userId, forIt) {
  2253. switch (forIt) {
  2254. case true:
  2255. // vote for it
  2256. return {
  2257. $pull: {
  2258. 'vote.negative': userId,
  2259. },
  2260. $addToSet: {
  2261. 'vote.positive': userId,
  2262. },
  2263. };
  2264. case false:
  2265. // vote against
  2266. return {
  2267. $pull: {
  2268. 'vote.positive': userId,
  2269. },
  2270. $addToSet: {
  2271. 'vote.negative': userId,
  2272. },
  2273. };
  2274. default:
  2275. // Remove votes
  2276. return {
  2277. $pull: {
  2278. 'vote.positive': userId,
  2279. 'vote.negative': userId,
  2280. },
  2281. };
  2282. }
  2283. },
  2284. setPokerQuestion(question, allowNonBoardMembers) {
  2285. return {
  2286. $set: {
  2287. poker: {
  2288. question,
  2289. allowNonBoardMembers,
  2290. one: [],
  2291. two: [],
  2292. three: [],
  2293. five: [],
  2294. eight: [],
  2295. thirteen: [],
  2296. twenty: [],
  2297. forty: [],
  2298. oneHundred: [],
  2299. unsure: [],
  2300. },
  2301. },
  2302. };
  2303. },
  2304. setPokerEstimation(estimation) {
  2305. return {
  2306. $set: { 'poker.estimation': estimation },
  2307. };
  2308. },
  2309. unsetPokerEstimation() {
  2310. return {
  2311. $unset: { 'poker.estimation': '' },
  2312. };
  2313. },
  2314. unsetPoker() {
  2315. return {
  2316. $unset: {
  2317. poker: '',
  2318. },
  2319. };
  2320. },
  2321. setPokerEnd(end) {
  2322. return {
  2323. $set: { 'poker.end': end },
  2324. };
  2325. },
  2326. unsetPokerEnd() {
  2327. return {
  2328. $unset: { 'poker.end': '' },
  2329. };
  2330. },
  2331. setPoker(userId, state) {
  2332. switch (state) {
  2333. case 'one':
  2334. // poker one
  2335. return {
  2336. $pull: {
  2337. 'poker.two': userId,
  2338. 'poker.three': userId,
  2339. 'poker.five': userId,
  2340. 'poker.eight': userId,
  2341. 'poker.thirteen': userId,
  2342. 'poker.twenty': userId,
  2343. 'poker.forty': userId,
  2344. 'poker.oneHundred': userId,
  2345. 'poker.unsure': userId,
  2346. },
  2347. $addToSet: {
  2348. 'poker.one': userId,
  2349. },
  2350. };
  2351. case 'two':
  2352. // poker two
  2353. return {
  2354. $pull: {
  2355. 'poker.one': userId,
  2356. 'poker.three': userId,
  2357. 'poker.five': userId,
  2358. 'poker.eight': userId,
  2359. 'poker.thirteen': userId,
  2360. 'poker.twenty': userId,
  2361. 'poker.forty': userId,
  2362. 'poker.oneHundred': userId,
  2363. 'poker.unsure': userId,
  2364. },
  2365. $addToSet: {
  2366. 'poker.two': userId,
  2367. },
  2368. };
  2369. case 'three':
  2370. // poker three
  2371. return {
  2372. $pull: {
  2373. 'poker.one': userId,
  2374. 'poker.two': userId,
  2375. 'poker.five': userId,
  2376. 'poker.eight': userId,
  2377. 'poker.thirteen': userId,
  2378. 'poker.twenty': userId,
  2379. 'poker.forty': userId,
  2380. 'poker.oneHundred': userId,
  2381. 'poker.unsure': userId,
  2382. },
  2383. $addToSet: {
  2384. 'poker.three': userId,
  2385. },
  2386. };
  2387. case 'five':
  2388. // poker five
  2389. return {
  2390. $pull: {
  2391. 'poker.one': userId,
  2392. 'poker.two': userId,
  2393. 'poker.three': userId,
  2394. 'poker.eight': userId,
  2395. 'poker.thirteen': userId,
  2396. 'poker.twenty': userId,
  2397. 'poker.forty': userId,
  2398. 'poker.oneHundred': userId,
  2399. 'poker.unsure': userId,
  2400. },
  2401. $addToSet: {
  2402. 'poker.five': userId,
  2403. },
  2404. };
  2405. case 'eight':
  2406. // poker eight
  2407. return {
  2408. $pull: {
  2409. 'poker.one': userId,
  2410. 'poker.two': userId,
  2411. 'poker.three': userId,
  2412. 'poker.five': userId,
  2413. 'poker.thirteen': userId,
  2414. 'poker.twenty': userId,
  2415. 'poker.forty': userId,
  2416. 'poker.oneHundred': userId,
  2417. 'poker.unsure': userId,
  2418. },
  2419. $addToSet: {
  2420. 'poker.eight': userId,
  2421. },
  2422. };
  2423. case 'thirteen':
  2424. // poker thirteen
  2425. return {
  2426. $pull: {
  2427. 'poker.one': userId,
  2428. 'poker.two': userId,
  2429. 'poker.three': userId,
  2430. 'poker.five': userId,
  2431. 'poker.eight': userId,
  2432. 'poker.twenty': userId,
  2433. 'poker.forty': userId,
  2434. 'poker.oneHundred': userId,
  2435. 'poker.unsure': userId,
  2436. },
  2437. $addToSet: {
  2438. 'poker.thirteen': userId,
  2439. },
  2440. };
  2441. case 'twenty':
  2442. // poker twenty
  2443. return {
  2444. $pull: {
  2445. 'poker.one': userId,
  2446. 'poker.two': userId,
  2447. 'poker.three': userId,
  2448. 'poker.five': userId,
  2449. 'poker.eight': userId,
  2450. 'poker.thirteen': userId,
  2451. 'poker.forty': userId,
  2452. 'poker.oneHundred': userId,
  2453. 'poker.unsure': userId,
  2454. },
  2455. $addToSet: {
  2456. 'poker.twenty': userId,
  2457. },
  2458. };
  2459. case 'forty':
  2460. // poker forty
  2461. return {
  2462. $pull: {
  2463. 'poker.one': userId,
  2464. 'poker.two': userId,
  2465. 'poker.three': userId,
  2466. 'poker.five': userId,
  2467. 'poker.eight': userId,
  2468. 'poker.thirteen': userId,
  2469. 'poker.twenty': userId,
  2470. 'poker.oneHundred': userId,
  2471. 'poker.unsure': userId,
  2472. },
  2473. $addToSet: {
  2474. 'poker.forty': userId,
  2475. },
  2476. };
  2477. case 'oneHundred':
  2478. // poker one hundred
  2479. return {
  2480. $pull: {
  2481. 'poker.one': userId,
  2482. 'poker.two': userId,
  2483. 'poker.three': userId,
  2484. 'poker.five': userId,
  2485. 'poker.eight': userId,
  2486. 'poker.thirteen': userId,
  2487. 'poker.twenty': userId,
  2488. 'poker.forty': userId,
  2489. 'poker.unsure': userId,
  2490. },
  2491. $addToSet: {
  2492. 'poker.oneHundred': userId,
  2493. },
  2494. };
  2495. case 'unsure':
  2496. // poker unsure
  2497. return {
  2498. $pull: {
  2499. 'poker.one': userId,
  2500. 'poker.two': userId,
  2501. 'poker.three': userId,
  2502. 'poker.five': userId,
  2503. 'poker.eight': userId,
  2504. 'poker.thirteen': userId,
  2505. 'poker.twenty': userId,
  2506. 'poker.forty': userId,
  2507. 'poker.oneHundred': userId,
  2508. },
  2509. $addToSet: {
  2510. 'poker.unsure': userId,
  2511. },
  2512. };
  2513. default:
  2514. // Remove pokers
  2515. return {
  2516. $pull: {
  2517. 'poker.one': userId,
  2518. 'poker.two': userId,
  2519. 'poker.three': userId,
  2520. 'poker.five': userId,
  2521. 'poker.eight': userId,
  2522. 'poker.thirteen': userId,
  2523. 'poker.twenty': userId,
  2524. 'poker.forty': userId,
  2525. 'poker.oneHundred': userId,
  2526. 'poker.unsure': userId,
  2527. },
  2528. };
  2529. }
  2530. },
  2531. replayPoker() {
  2532. return {
  2533. $set: {
  2534. 'poker.one': [],
  2535. 'poker.two': [],
  2536. 'poker.three': [],
  2537. 'poker.five': [],
  2538. 'poker.eight': [],
  2539. 'poker.thirteen': [],
  2540. 'poker.twelve': [],
  2541. 'poker.forty': [],
  2542. 'poker.oneHundred': [],
  2543. 'poker.unsure': [],
  2544. },
  2545. };
  2546. },
  2547. });
  2548. //FUNCTIONS FOR creation of Activities
  2549. function updateActivities(doc, fieldNames, modifier) {
  2550. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  2551. ReactiveCache.getActivities({
  2552. activityType: 'addedLabel',
  2553. cardId: doc._id,
  2554. }).forEach(a => {
  2555. const lidx = doc.labelIds.indexOf(a.labelId);
  2556. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  2557. Activities.update(a._id, {
  2558. $set: {
  2559. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  2560. boardId: modifier.$set.boardId,
  2561. },
  2562. });
  2563. } else {
  2564. Activities.remove(a._id);
  2565. }
  2566. });
  2567. } else if (_.contains(fieldNames, 'boardId')) {
  2568. Activities.remove({
  2569. activityType: 'addedLabel',
  2570. cardId: doc._id,
  2571. });
  2572. }
  2573. }
  2574. function cardMove(
  2575. userId,
  2576. doc,
  2577. fieldNames,
  2578. oldListId,
  2579. oldSwimlaneId,
  2580. oldBoardId,
  2581. ) {
  2582. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  2583. Activities.insert({
  2584. userId,
  2585. activityType: 'moveCardBoard',
  2586. boardName: ReactiveCache.getBoard(doc.boardId).title,
  2587. boardId: doc.boardId,
  2588. oldBoardId,
  2589. oldBoardName: ReactiveCache.getBoard(oldBoardId).title,
  2590. cardId: doc._id,
  2591. swimlaneName: ReactiveCache.getSwimlane(doc.swimlaneId).title,
  2592. swimlaneId: doc.swimlaneId,
  2593. oldSwimlaneId,
  2594. });
  2595. } else if (
  2596. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  2597. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  2598. ) {
  2599. Activities.insert({
  2600. userId,
  2601. oldListId,
  2602. activityType: 'moveCard',
  2603. listName: ReactiveCache.getList(doc.listId).title,
  2604. listId: doc.listId,
  2605. boardId: doc.boardId,
  2606. cardId: doc._id,
  2607. cardTitle: doc.title,
  2608. swimlaneName: ReactiveCache.getSwimlane(doc.swimlaneId).title,
  2609. swimlaneId: doc.swimlaneId,
  2610. oldSwimlaneId,
  2611. });
  2612. }
  2613. }
  2614. function cardState(userId, doc, fieldNames) {
  2615. if (_.contains(fieldNames, 'archived')) {
  2616. if (doc.archived) {
  2617. Activities.insert({
  2618. userId,
  2619. activityType: 'archivedCard',
  2620. listName: ReactiveCache.getList(doc.listId).title,
  2621. boardId: doc.boardId,
  2622. listId: doc.listId,
  2623. cardId: doc._id,
  2624. swimlaneId: doc.swimlaneId,
  2625. });
  2626. } else {
  2627. Activities.insert({
  2628. userId,
  2629. activityType: 'restoredCard',
  2630. boardId: doc.boardId,
  2631. listName: ReactiveCache.getList(doc.listId).title,
  2632. listId: doc.listId,
  2633. cardId: doc._id,
  2634. swimlaneId: doc.swimlaneId,
  2635. });
  2636. }
  2637. }
  2638. }
  2639. function cardMembers(userId, doc, fieldNames, modifier) {
  2640. if (!_.contains(fieldNames, 'members')) return;
  2641. let memberId;
  2642. // Say hello to the new member
  2643. if (modifier.$addToSet && modifier.$addToSet.members) {
  2644. memberId = modifier.$addToSet.members;
  2645. const username = ReactiveCache.getUser(memberId).username;
  2646. if (!_.contains(doc.members, memberId)) {
  2647. Activities.insert({
  2648. userId,
  2649. username,
  2650. activityType: 'joinMember',
  2651. boardId: doc.boardId,
  2652. cardId: doc._id,
  2653. memberId,
  2654. listId: doc.listId,
  2655. swimlaneId: doc.swimlaneId,
  2656. });
  2657. }
  2658. }
  2659. // Say goodbye to the former member
  2660. if (modifier.$pull && modifier.$pull.members) {
  2661. memberId = modifier.$pull.members;
  2662. const username = ReactiveCache.getUser(memberId).username;
  2663. // Check that the former member is member of the card
  2664. if (_.contains(doc.members, memberId)) {
  2665. Activities.insert({
  2666. userId,
  2667. username,
  2668. activityType: 'unjoinMember',
  2669. boardId: doc.boardId,
  2670. cardId: doc._id,
  2671. memberId,
  2672. listId: doc.listId,
  2673. swimlaneId: doc.swimlaneId,
  2674. });
  2675. }
  2676. }
  2677. }
  2678. function cardAssignees(userId, doc, fieldNames, modifier) {
  2679. if (!_.contains(fieldNames, 'assignees')) return;
  2680. let assigneeId;
  2681. // Say hello to the new assignee
  2682. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  2683. assigneeId = modifier.$addToSet.assignees;
  2684. const username = ReactiveCache.getUser(assigneeId).username;
  2685. if (!_.contains(doc.assignees, assigneeId)) {
  2686. Activities.insert({
  2687. userId,
  2688. username,
  2689. activityType: 'joinAssignee',
  2690. boardId: doc.boardId,
  2691. cardId: doc._id,
  2692. assigneeId,
  2693. listId: doc.listId,
  2694. swimlaneId: doc.swimlaneId,
  2695. });
  2696. }
  2697. }
  2698. // Say goodbye to the former assignee
  2699. if (modifier.$pull && modifier.$pull.assignees) {
  2700. assigneeId = modifier.$pull.assignees;
  2701. const username = ReactiveCache.getUser(assigneeId).username;
  2702. // Check that the former assignee is assignee of the card
  2703. if (_.contains(doc.assignees, assigneeId)) {
  2704. Activities.insert({
  2705. userId,
  2706. username,
  2707. activityType: 'unjoinAssignee',
  2708. boardId: doc.boardId,
  2709. cardId: doc._id,
  2710. assigneeId,
  2711. listId: doc.listId,
  2712. swimlaneId: doc.swimlaneId,
  2713. });
  2714. }
  2715. }
  2716. }
  2717. function cardLabels(userId, doc, fieldNames, modifier) {
  2718. if (!_.contains(fieldNames, 'labelIds')) return;
  2719. let labelId;
  2720. // Say hello to the new label
  2721. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  2722. labelId = modifier.$addToSet.labelIds;
  2723. //const label = labels(labelId).name;
  2724. if (!_.contains(doc.labelIds, labelId)) {
  2725. const act = {
  2726. userId,
  2727. labelId,
  2728. activityType: 'addedLabel',
  2729. boardId: doc.boardId,
  2730. cardId: doc._id,
  2731. listId: doc.listId,
  2732. swimlaneId: doc.swimlaneId,
  2733. };
  2734. Activities.insert(act);
  2735. }
  2736. }
  2737. // Say goodbye to the label
  2738. if (modifier.$pull && modifier.$pull.labelIds) {
  2739. labelId = modifier.$pull.labelIds;
  2740. // Check that the former member is member of the card
  2741. if (_.contains(doc.labelIds, labelId)) {
  2742. Activities.insert({
  2743. userId,
  2744. labelId,
  2745. activityType: 'removedLabel',
  2746. boardId: doc.boardId,
  2747. cardId: doc._id,
  2748. listId: doc.listId,
  2749. swimlaneId: doc.swimlaneId,
  2750. });
  2751. }
  2752. }
  2753. }
  2754. function cardCustomFields(userId, doc, fieldNames, modifier) {
  2755. if (!_.contains(fieldNames, 'customFields')) return;
  2756. // Say hello to the new customField value
  2757. if (modifier.$set) {
  2758. _.each(modifier.$set, (value, key) => {
  2759. if (key.startsWith('customFields')) {
  2760. const dotNotation = key.split('.');
  2761. // only individual changes are registered
  2762. if (dotNotation.length > 1) {
  2763. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2764. const act = {
  2765. userId,
  2766. customFieldId,
  2767. value,
  2768. activityType: 'setCustomField',
  2769. boardId: doc.boardId,
  2770. cardId: doc._id,
  2771. listId: doc.listId,
  2772. swimlaneId: doc.swimlaneId,
  2773. };
  2774. Activities.insert(act);
  2775. }
  2776. }
  2777. });
  2778. }
  2779. // Say goodbye to the former customField value
  2780. if (modifier.$unset) {
  2781. _.each(modifier.$unset, (value, key) => {
  2782. if (key.startsWith('customFields')) {
  2783. const dotNotation = key.split('.');
  2784. // only individual changes are registered
  2785. if (dotNotation.length > 1) {
  2786. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2787. const act = {
  2788. userId,
  2789. customFieldId,
  2790. activityType: 'unsetCustomField',
  2791. boardId: doc.boardId,
  2792. cardId: doc._id,
  2793. };
  2794. Activities.insert(act);
  2795. }
  2796. }
  2797. });
  2798. }
  2799. }
  2800. function cardCreation(userId, doc) {
  2801. Activities.insert({
  2802. userId,
  2803. activityType: 'createCard',
  2804. boardId: doc.boardId,
  2805. listName: ReactiveCache.getList(doc.listId).title,
  2806. listId: doc.listId,
  2807. cardId: doc._id,
  2808. cardTitle: doc.title,
  2809. swimlaneName: ReactiveCache.getSwimlane(doc.swimlaneId).title,
  2810. swimlaneId: doc.swimlaneId,
  2811. });
  2812. }
  2813. Meteor.methods({
  2814. createCardWithDueDate: function(boardId, listId, title, dueDate, swimlaneId) {
  2815. check(boardId, String);
  2816. check(listId, String);
  2817. check(title, String);
  2818. check(dueDate, Date);
  2819. check(swimlaneId, String);
  2820. const card = {
  2821. title,
  2822. listId,
  2823. boardId,
  2824. swimlaneId,
  2825. createdAt: new Date(),
  2826. dueAt: dueDate,
  2827. sort: 0,
  2828. usedId: Meteor.userId(),
  2829. };
  2830. const cardId = Cards.insert(card);
  2831. return cardId;
  2832. },
  2833. });
  2834. function cardRemover(userId, doc) {
  2835. ChecklistItems.remove({
  2836. cardId: doc._id,
  2837. });
  2838. Checklists.remove({
  2839. cardId: doc._id,
  2840. });
  2841. Cards.remove({
  2842. parentId: doc._id,
  2843. });
  2844. CardComments.remove({
  2845. cardId: doc._id,
  2846. });
  2847. Attachments.remove({
  2848. cardId: doc._id,
  2849. });
  2850. }
  2851. const findDueCards = days => {
  2852. const seekDue = ($from, $to, activityType) => {
  2853. ReactiveCache.getCards({
  2854. archived: false,
  2855. dueAt: { $gte: $from, $lt: $to },
  2856. }).forEach(card => {
  2857. const username = ReactiveCache.getUser(card.userId).username;
  2858. const activity = {
  2859. userId: card.userId,
  2860. username,
  2861. activityType,
  2862. boardId: card.boardId,
  2863. cardId: card._id,
  2864. cardTitle: card.title,
  2865. listId: card.listId,
  2866. timeValue: card.dueAt,
  2867. swimlaneId: card.swimlaneId,
  2868. };
  2869. Activities.insert(activity);
  2870. });
  2871. };
  2872. const now = new Date(),
  2873. aday = 3600 * 24 * 1e3,
  2874. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2875. if (!days) return;
  2876. if (!days.map) days = [days];
  2877. days.map(day => {
  2878. let args = [];
  2879. if (day === 0) {
  2880. args = [then(0), then(1), 'duenow'];
  2881. } else if (day > 0) {
  2882. args = [then(1), then(day), 'almostdue'];
  2883. } else {
  2884. args = [then(day), now, 'pastdue'];
  2885. }
  2886. seekDue(...args);
  2887. });
  2888. };
  2889. const addCronJob = _.debounce(
  2890. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2891. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2892. if (!envValue) {
  2893. return;
  2894. }
  2895. const notifydays = envValue
  2896. .split(',')
  2897. .map(value => {
  2898. const iValue = parseInt(value, 10);
  2899. if (!(iValue > 0 && iValue < 15)) {
  2900. // notifying due is disabled
  2901. return false;
  2902. } else {
  2903. return iValue;
  2904. }
  2905. })
  2906. .filter(Boolean);
  2907. 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
  2908. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2909. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2910. const scheduler = (job => () => {
  2911. const now = new Date();
  2912. const hour = 3600 * 1e3;
  2913. if (now.getHours() === itvl) {
  2914. if (typeof job === 'function') {
  2915. job();
  2916. }
  2917. }
  2918. Meteor.setTimeout(scheduler, hour);
  2919. })(() => {
  2920. findDueCards(notifydays);
  2921. });
  2922. scheduler();
  2923. }),
  2924. 500,
  2925. );
  2926. if (Meteor.isServer) {
  2927. Meteor.methods({
  2928. // Secure poker voting: only the caller's userId is modified
  2929. 'cards.pokerVote'(cardId, state) {
  2930. check(cardId, String);
  2931. if (state !== undefined && state !== null) check(state, String);
  2932. if (!this.userId) throw new Meteor.Error('not-authorized');
  2933. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  2934. if (!card) throw new Meteor.Error('not-found');
  2935. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  2936. if (!board) throw new Meteor.Error('not-found');
  2937. const isMember = allowIsBoardMember(this.userId, board);
  2938. const allowNBM = !!(card.poker && card.poker.allowNonBoardMembers);
  2939. if (!(isMember || allowNBM /* && board.permission === 'public' */)) {
  2940. throw new Meteor.Error('not-authorized');
  2941. }
  2942. let mod = card.setPoker(this.userId, state);
  2943. if (!mod || typeof mod !== 'object') mod = {};
  2944. mod.$set = Object.assign({}, mod.$set, { modifiedAt: new Date(), dateLastActivity: new Date() });
  2945. return Cards.update({ _id: cardId }, mod);
  2946. },
  2947. // Configure planning poker on a card (members only)
  2948. 'cards.setPokerQuestion'(cardId, question, allowNonBoardMembers) {
  2949. check(cardId, String);
  2950. check(question, Boolean);
  2951. check(allowNonBoardMembers, Boolean);
  2952. if (!this.userId) throw new Meteor.Error('not-authorized');
  2953. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  2954. if (!card) throw new Meteor.Error('not-found');
  2955. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  2956. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  2957. const modifier = {
  2958. $set: {
  2959. poker: {
  2960. question,
  2961. allowNonBoardMembers,
  2962. one: [], two: [], three: [], five: [], eight: [], thirteen: [], twenty: [], forty: [], oneHundred: [], unsure: [],
  2963. },
  2964. modifiedAt: new Date(),
  2965. dateLastActivity: new Date(),
  2966. },
  2967. };
  2968. return Cards.update({ _id: cardId }, modifier);
  2969. },
  2970. 'cards.setPokerEnd'(cardId, end) {
  2971. check(cardId, String);
  2972. check(end, Date);
  2973. if (!this.userId) throw new Meteor.Error('not-authorized');
  2974. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  2975. if (!card) throw new Meteor.Error('not-found');
  2976. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  2977. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  2978. const modifier = {
  2979. $set: { 'poker.end': end, modifiedAt: new Date(), dateLastActivity: new Date() },
  2980. };
  2981. return Cards.update({ _id: cardId }, modifier);
  2982. },
  2983. 'cards.unsetPokerEnd'(cardId) {
  2984. check(cardId, String);
  2985. if (!this.userId) throw new Meteor.Error('not-authorized');
  2986. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  2987. if (!card) throw new Meteor.Error('not-found');
  2988. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  2989. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  2990. const modifier = {
  2991. $unset: { 'poker.end': '' },
  2992. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  2993. };
  2994. return Cards.update({ _id: cardId }, modifier);
  2995. },
  2996. 'cards.unsetPoker'(cardId) {
  2997. check(cardId, String);
  2998. if (!this.userId) throw new Meteor.Error('not-authorized');
  2999. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3000. if (!card) throw new Meteor.Error('not-found');
  3001. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3002. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3003. const modifier = {
  3004. $unset: { poker: '' },
  3005. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3006. };
  3007. return Cards.update({ _id: cardId }, modifier);
  3008. },
  3009. 'cards.setPokerEstimation'(cardId, estimation) {
  3010. check(cardId, String);
  3011. check(estimation, Number);
  3012. if (!this.userId) throw new Meteor.Error('not-authorized');
  3013. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3014. if (!card) throw new Meteor.Error('not-found');
  3015. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3016. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3017. const modifier = {
  3018. $set: { 'poker.estimation': estimation, modifiedAt: new Date(), dateLastActivity: new Date() },
  3019. };
  3020. return Cards.update({ _id: cardId }, modifier);
  3021. },
  3022. 'cards.unsetPokerEstimation'(cardId) {
  3023. check(cardId, String);
  3024. if (!this.userId) throw new Meteor.Error('not-authorized');
  3025. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3026. if (!card) throw new Meteor.Error('not-found');
  3027. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3028. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3029. const modifier = {
  3030. $unset: { 'poker.estimation': '' },
  3031. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3032. };
  3033. return Cards.update({ _id: cardId }, modifier);
  3034. },
  3035. 'cards.replayPoker'(cardId) {
  3036. check(cardId, String);
  3037. if (!this.userId) throw new Meteor.Error('not-authorized');
  3038. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3039. if (!card) throw new Meteor.Error('not-found');
  3040. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3041. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3042. // Reset all poker votes arrays
  3043. const modifier = {
  3044. $set: {
  3045. 'poker.one': [], 'poker.two': [], 'poker.three': [], 'poker.five': [], 'poker.eight': [], 'poker.thirteen': [], 'poker.twenty': [], 'poker.forty': [], 'poker.oneHundred': [], 'poker.unsure': [],
  3046. modifiedAt: new Date(),
  3047. dateLastActivity: new Date(),
  3048. },
  3049. $unset: { 'poker.end': '' },
  3050. };
  3051. return Cards.update({ _id: cardId }, modifier);
  3052. },
  3053. // Configure voting on a card (members only)
  3054. 'cards.setVoteQuestion'(cardId, question, publicVote, allowNonBoardMembers) {
  3055. check(cardId, String);
  3056. check(question, String);
  3057. check(publicVote, Boolean);
  3058. check(allowNonBoardMembers, Boolean);
  3059. if (!this.userId) throw new Meteor.Error('not-authorized');
  3060. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3061. if (!card) throw new Meteor.Error('not-found');
  3062. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3063. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3064. const modifier = {
  3065. $set: {
  3066. vote: {
  3067. question,
  3068. public: publicVote,
  3069. allowNonBoardMembers,
  3070. positive: [],
  3071. negative: [],
  3072. },
  3073. modifiedAt: new Date(),
  3074. dateLastActivity: new Date(),
  3075. },
  3076. };
  3077. return Cards.update({ _id: cardId }, modifier);
  3078. },
  3079. 'cards.setVoteEnd'(cardId, end) {
  3080. check(cardId, String);
  3081. check(end, Date);
  3082. if (!this.userId) throw new Meteor.Error('not-authorized');
  3083. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3084. if (!card) throw new Meteor.Error('not-found');
  3085. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3086. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3087. const modifier = {
  3088. $set: { 'vote.end': end, modifiedAt: new Date(), dateLastActivity: new Date() },
  3089. };
  3090. return Cards.update({ _id: cardId }, modifier);
  3091. },
  3092. 'cards.unsetVoteEnd'(cardId) {
  3093. check(cardId, String);
  3094. if (!this.userId) throw new Meteor.Error('not-authorized');
  3095. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3096. if (!card) throw new Meteor.Error('not-found');
  3097. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3098. if (!allowIsBoardMember(this.userId, board)) throw new Meteor.Error('not-authorized');
  3099. const modifier = {
  3100. $unset: { 'vote.end': '' },
  3101. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3102. };
  3103. return Cards.update({ _id: cardId }, modifier);
  3104. },
  3105. 'cards.unsetVote'(cardId) {
  3106. check(cardId, String);
  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. $unset: { vote: '' },
  3114. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3115. };
  3116. return Cards.update({ _id: cardId }, modifier);
  3117. },
  3118. // Secure voting: only the caller can set/unset their vote; non-members can vote only when allowed
  3119. 'cards.vote'(cardId, forIt) {
  3120. check(cardId, String);
  3121. // forIt may be true (upvote), false (downvote), or null/undefined (clear)
  3122. if (forIt !== undefined && forIt !== null) check(forIt, Boolean);
  3123. if (!this.userId) throw new Meteor.Error('not-authorized');
  3124. const card = ReactiveCache.getCard(cardId) || Cards.findOne(cardId);
  3125. if (!card) throw new Meteor.Error('not-found');
  3126. const board = ReactiveCache.getBoard(card.boardId) || Boards.findOne(card.boardId);
  3127. if (!board) throw new Meteor.Error('not-found');
  3128. const isMember = allowIsBoardMember(this.userId, board);
  3129. const allowNBM = !!(card.vote && card.vote.allowNonBoardMembers);
  3130. if (!(isMember || allowNBM /* && board.permission === 'public' */)) {
  3131. throw new Meteor.Error('not-authorized');
  3132. }
  3133. // Only modify the caller's own userId in vote arrays
  3134. let modifier;
  3135. if (forIt === true) {
  3136. modifier = {
  3137. $pull: { 'vote.negative': this.userId },
  3138. $addToSet: { 'vote.positive': this.userId },
  3139. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3140. };
  3141. } else if (forIt === false) {
  3142. modifier = {
  3143. $pull: { 'vote.positive': this.userId },
  3144. $addToSet: { 'vote.negative': this.userId },
  3145. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3146. };
  3147. } else {
  3148. // Clear vote
  3149. modifier = {
  3150. $pull: { 'vote.positive': this.userId, 'vote.negative': this.userId },
  3151. $set: { modifiedAt: new Date(), dateLastActivity: new Date() },
  3152. };
  3153. }
  3154. return Cards.update({ _id: cardId }, modifier);
  3155. },
  3156. /** copies a card
  3157. * <li> this method is needed on the server because attachments can only be copied on the server (access to file system)
  3158. * @param card id to copy
  3159. * @param boardId copy to this board
  3160. * @param swimlandeId copy to this swimlane id
  3161. * @param listId copy to this list id
  3162. * @param insertAtTop insert the card at the top?
  3163. * @param mergeCardValues this values into the copied card
  3164. * @return the new card id
  3165. */
  3166. copyCard(cardId, boardId, swimlaneId, listId, insertAtTop, mergeCardValues) {
  3167. check(cardId, String);
  3168. check(boardId, String);
  3169. check(swimlaneId, String);
  3170. check(listId, String);
  3171. check(insertAtTop, Boolean);
  3172. check(mergeCardValues, Object);
  3173. const card = ReactiveCache.getCard(cardId);
  3174. Object.assign(card, mergeCardValues);
  3175. const sort = card.getSort(listId, swimlaneId, insertAtTop);
  3176. if (insertAtTop) {
  3177. card.sort = sort - 1;
  3178. } else
  3179. {
  3180. card.sort = sort + 1;
  3181. }
  3182. const ret = card.copy(boardId, swimlaneId, listId);
  3183. return ret;
  3184. },
  3185. });
  3186. // Cards are often fetched within a board, so we create an index to make these
  3187. // queries more efficient.
  3188. Meteor.startup(() => {
  3189. Cards._collection.createIndex({ modifiedAt: -1 });
  3190. Cards._collection.createIndex({ boardId: 1, createdAt: -1 });
  3191. // https://github.com/wekan/wekan/issues/1863
  3192. // Swimlane added a new field in the cards collection of mongodb named parentId.
  3193. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  3194. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  3195. // To correct it, add Index to parentId:
  3196. Cards._collection.createIndex({ parentId: 1 });
  3197. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  3198. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  3199. // Meteor.call("findDueCards",notifydays,notifyitvl);
  3200. Meteor.defer(() => {
  3201. addCronJob();
  3202. });
  3203. });
  3204. Cards.after.insert((userId, doc) => {
  3205. cardCreation(userId, doc);
  3206. // Track original position for new cards
  3207. Meteor.setTimeout(() => {
  3208. const card = Cards.findOne(doc._id);
  3209. if (card) {
  3210. card.trackOriginalPosition();
  3211. }
  3212. }, 100);
  3213. });
  3214. // New activity for card (un)archivage
  3215. Cards.after.update((userId, doc, fieldNames) => {
  3216. cardState(userId, doc, fieldNames);
  3217. });
  3218. //New activity for card moves
  3219. Cards.after.update(function(userId, doc, fieldNames) {
  3220. const oldListId = this.previous.listId;
  3221. const oldSwimlaneId = this.previous.swimlaneId;
  3222. const oldBoardId = this.previous.boardId;
  3223. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  3224. });
  3225. // Add a new activity if we add or remove a member to the card
  3226. Cards.before.update((userId, doc, fieldNames, modifier) => {
  3227. cardMembers(userId, doc, fieldNames, modifier);
  3228. updateActivities(doc, fieldNames, modifier);
  3229. });
  3230. // Add a new activity if we add or remove a assignee to the card
  3231. Cards.before.update((userId, doc, fieldNames, modifier) => {
  3232. cardAssignees(userId, doc, fieldNames, modifier);
  3233. updateActivities(doc, fieldNames, modifier);
  3234. });
  3235. // Add a new activity if we add or remove a label to the card
  3236. Cards.before.update((userId, doc, fieldNames, modifier) => {
  3237. cardLabels(userId, doc, fieldNames, modifier);
  3238. });
  3239. // Add a new activity if we edit a custom field
  3240. Cards.before.update((userId, doc, fieldNames, modifier) => {
  3241. cardCustomFields(userId, doc, fieldNames, modifier);
  3242. });
  3243. // Add a new activity if modify time related field like dueAt startAt etc
  3244. Cards.before.update((userId, doc, fieldNames, modifier) => {
  3245. const dla = 'dateLastActivity';
  3246. const fields = fieldNames.filter(name => name !== dla);
  3247. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  3248. const action = fields[0];
  3249. if (fields.length > 0 && _.contains(timingaction, action)) {
  3250. // add activities for user change these attributes
  3251. const value = modifier.$set[action];
  3252. const oldvalue = doc[action] || '';
  3253. const activityType = `a-${action}`;
  3254. const card = ReactiveCache.getCard(doc._id);
  3255. const list = card.list();
  3256. if (list) {
  3257. // change list modifiedAt, when user modified the key values in
  3258. // timingaction array, if it's endAt, put the modifiedAt of list
  3259. // back to one year ago for sorting purpose
  3260. const modifiedAt = add(now(), -1, 'year').toISOString();
  3261. const boardId = list.boardId;
  3262. Lists.direct.update(
  3263. {
  3264. _id: list._id,
  3265. },
  3266. {
  3267. $set: {
  3268. modifiedAt,
  3269. boardId,
  3270. },
  3271. },
  3272. );
  3273. }
  3274. const username = ReactiveCache.getUser(userId).username;
  3275. const activity = {
  3276. userId,
  3277. username,
  3278. activityType,
  3279. boardId: doc.boardId,
  3280. cardId: doc._id,
  3281. cardTitle: doc.title,
  3282. timeKey: action,
  3283. timeValue: value,
  3284. timeOldValue: oldvalue,
  3285. listId: card.listId,
  3286. swimlaneId: card.swimlaneId,
  3287. };
  3288. Activities.insert(activity);
  3289. }
  3290. });
  3291. // Remove all activities associated with a card if we remove the card
  3292. // Remove also card_comments / checklists / attachments
  3293. Cards.before.remove((userId, doc) => {
  3294. cardRemover(userId, doc);
  3295. });
  3296. }
  3297. //SWIMLANES REST API
  3298. if (Meteor.isServer) {
  3299. /**
  3300. * @operation get_swimlane_cards
  3301. * @summary get all cards attached to a swimlane
  3302. *
  3303. * @param {string} boardId the board ID
  3304. * @param {string} swimlaneId the swimlane ID
  3305. * @return_type [{_id: string,
  3306. * title: string,
  3307. * description: string,
  3308. * listId: string}]
  3309. */
  3310. JsonRoutes.add(
  3311. 'GET',
  3312. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  3313. function(req, res) {
  3314. const paramBoardId = req.params.boardId;
  3315. const paramSwimlaneId = req.params.swimlaneId;
  3316. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3317. JsonRoutes.sendResult(res, {
  3318. code: 200,
  3319. data: ReactiveCache.getCards({
  3320. boardId: paramBoardId,
  3321. swimlaneId: paramSwimlaneId,
  3322. archived: false,
  3323. },
  3324. { sort: ['sort'] }).map(function(doc) {
  3325. return {
  3326. _id: doc._id,
  3327. title: doc.title,
  3328. description: doc.description,
  3329. listId: doc.listId,
  3330. receivedAt: doc.receivedAt,
  3331. startAt: doc.startAt,
  3332. dueAt: doc.dueAt,
  3333. endAt: doc.endAt,
  3334. assignees: doc.assignees,
  3335. sort: doc.sort,
  3336. };
  3337. }),
  3338. });
  3339. },
  3340. );
  3341. }
  3342. //LISTS REST API
  3343. if (Meteor.isServer) {
  3344. /**
  3345. * @operation get_all_cards
  3346. * @summary Get all Cards attached to a List
  3347. *
  3348. * @param {string} boardId the board ID
  3349. * @param {string} listId the list ID
  3350. * @return_type [{_id: string,
  3351. * title: string,
  3352. * description: string}]
  3353. */
  3354. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  3355. req,
  3356. res,
  3357. ) {
  3358. const paramBoardId = req.params.boardId;
  3359. const paramListId = req.params.listId;
  3360. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3361. JsonRoutes.sendResult(res, {
  3362. code: 200,
  3363. data: ReactiveCache.getCards({
  3364. boardId: paramBoardId,
  3365. listId: paramListId,
  3366. archived: false,
  3367. },
  3368. { sort: ['sort'] }).map(function(doc) {
  3369. return {
  3370. _id: doc._id,
  3371. title: doc.title,
  3372. description: doc.description,
  3373. swimlaneId: doc.swimlaneId,
  3374. receivedAt: doc.receivedAt,
  3375. startAt: doc.startAt,
  3376. dueAt: doc.dueAt,
  3377. endAt: doc.endAt,
  3378. assignees: doc.assignees,
  3379. sort: doc.sort,
  3380. };
  3381. }),
  3382. });
  3383. });
  3384. /**
  3385. * @operation get_card_by_id
  3386. * @summary Get a Card by Card ID
  3387. *
  3388. * @param {string} cardId the card ID
  3389. * @return_type Cards
  3390. */
  3391. JsonRoutes.add(
  3392. 'GET',
  3393. '/api/cards/:cardId',
  3394. function(req, res) {
  3395. const paramCardId = req.params.cardId;
  3396. card = ReactiveCache.getCard(paramCardId)
  3397. Authentication.checkBoardAccess(req.userId, card.boardId);
  3398. JsonRoutes.sendResult(res, {
  3399. code: 200,
  3400. data: card,
  3401. });
  3402. },
  3403. );
  3404. /**
  3405. * @operation get_card
  3406. * @summary Get a Card
  3407. *
  3408. * @param {string} boardId the board ID
  3409. * @param {string} listId the list ID of the card
  3410. * @param {string} cardId the card ID
  3411. * @return_type Cards
  3412. */
  3413. JsonRoutes.add(
  3414. 'GET',
  3415. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3416. function(req, res) {
  3417. const paramBoardId = req.params.boardId;
  3418. const paramListId = req.params.listId;
  3419. const paramCardId = req.params.cardId;
  3420. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3421. JsonRoutes.sendResult(res, {
  3422. code: 200,
  3423. data: ReactiveCache.getCard({
  3424. _id: paramCardId,
  3425. listId: paramListId,
  3426. boardId: paramBoardId,
  3427. archived: false,
  3428. }),
  3429. });
  3430. },
  3431. );
  3432. /**
  3433. * @operation new_card
  3434. * @summary Create a new Card
  3435. *
  3436. * @param {string} boardId the board ID of the new card
  3437. * @param {string} listId the list ID of the new card
  3438. * @param {string} authorID the user ID of the person owning the card
  3439. * @param {string} parentId the parent ID of the new card
  3440. * @param {string} title the title of the new card
  3441. * @param {string} description the description of the new card
  3442. * @param {string} swimlaneId the swimlane ID of the new card
  3443. * @param {string} [members] the member IDs list of the new card
  3444. * @param {string} [assignees] the assignee IDs list of the new card
  3445. * @return_type {_id: string}
  3446. */
  3447. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  3448. req,
  3449. res,
  3450. ) {
  3451. // Check user is logged in
  3452. Authentication.checkLoggedIn(req.userId);
  3453. const paramBoardId = req.params.boardId;
  3454. // Check user has permission to add card to the board
  3455. const board = ReactiveCache.getBoard(paramBoardId);
  3456. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  3457. Authentication.checkAdminOrCondition(req.userId, addPermission);
  3458. const paramListId = req.params.listId;
  3459. const paramParentId = req.params.parentId;
  3460. const nextCardNumber = board.getNextCardNumber();
  3461. let customFieldsArr = [];
  3462. _.forEach(
  3463. ReactiveCache.getCustomFields({'boardIds': paramBoardId}),
  3464. function (field) {
  3465. if (field.automaticallyOnCard || field.alwaysOnCard)
  3466. customFieldsArr.push({ _id: field._id, value: null });
  3467. },
  3468. );
  3469. const currentCards = ReactiveCache.getCards(
  3470. {
  3471. listId: paramListId,
  3472. archived: false,
  3473. },
  3474. { sort: ['sort'] },
  3475. );
  3476. const check = ReactiveCache.getUser(req.body.authorId);
  3477. const members = req.body.members;
  3478. const assignees = req.body.assignees;
  3479. if (typeof check !== 'undefined') {
  3480. const id = Cards.direct.insert({
  3481. title: req.body.title,
  3482. boardId: paramBoardId,
  3483. listId: paramListId,
  3484. parentId: paramParentId,
  3485. description: req.body.description,
  3486. userId: req.body.authorId,
  3487. swimlaneId: req.body.swimlaneId,
  3488. sort: currentCards.length,
  3489. cardNumber: nextCardNumber,
  3490. customFields: customFieldsArr,
  3491. members,
  3492. assignees,
  3493. });
  3494. JsonRoutes.sendResult(res, {
  3495. code: 200,
  3496. data: {
  3497. _id: id,
  3498. },
  3499. });
  3500. const card = ReactiveCache.getCard(id);
  3501. cardCreation(req.body.authorId, card);
  3502. } else {
  3503. JsonRoutes.sendResult(res, {
  3504. code: 401,
  3505. });
  3506. }
  3507. });
  3508. /**
  3509. * @operation get_board_cards_count
  3510. * @summary Get a cards count to a board
  3511. *
  3512. * @param {string} boardId the board ID
  3513. * @return_type {board_cards_count: integer}
  3514. */
  3515. JsonRoutes.add('GET', '/api/boards/:boardId/cards_count', function(
  3516. req,
  3517. res,
  3518. ) {
  3519. try {
  3520. const paramBoardId = req.params.boardId;
  3521. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3522. JsonRoutes.sendResult(res, {
  3523. code: 200,
  3524. data: {
  3525. board_cards_count: ReactiveCache.getCards({
  3526. boardId: paramBoardId,
  3527. archived: false,
  3528. }).length,
  3529. }
  3530. });
  3531. } catch (error) {
  3532. JsonRoutes.sendResult(res, {
  3533. code: 200,
  3534. data: error,
  3535. });
  3536. }
  3537. });
  3538. /**
  3539. * @operation get_list_cards_count
  3540. * @summary Get a cards count to a list
  3541. *
  3542. * @param {string} boardId the board ID
  3543. * @param {string} listId the List ID
  3544. * @return_type {list_cards_count: integer}
  3545. */
  3546. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards_count', function(
  3547. req,
  3548. res,
  3549. ) {
  3550. try {
  3551. const paramBoardId = req.params.boardId;
  3552. const paramListId = req.params.listId;
  3553. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3554. JsonRoutes.sendResult(res, {
  3555. code: 200,
  3556. data: {
  3557. list_cards_count: ReactiveCache.getCards({
  3558. boardId: paramBoardId,
  3559. listId: paramListId,
  3560. archived: false,
  3561. }).length,
  3562. }
  3563. });
  3564. } catch (error) {
  3565. JsonRoutes.sendResult(res, {
  3566. code: 200,
  3567. data: error,
  3568. });
  3569. }
  3570. });
  3571. /*
  3572. * Note for the JSDoc:
  3573. * 'list' will be interpreted as the path parameter
  3574. * 'listID' will be interpreted as the body parameter
  3575. */
  3576. /**
  3577. * @operation edit_card
  3578. * @summary Edit Fields in a Card
  3579. *
  3580. * @description Edit a card
  3581. *
  3582. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  3583. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  3584. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  3585. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  3586. * `indigo`:
  3587. *
  3588. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  3589. *
  3590. * Note: setting the color to white has the same effect than removing it.
  3591. *
  3592. * @param {string} boardId the board ID of the card
  3593. * @param {string} list the list ID of the card
  3594. * @param {string} cardId the ID of the card
  3595. * @param {string} [title] the new title of the card
  3596. * @param {string} [sort] the new sort value of the card
  3597. * @param {string} [listId] the new list ID of the card (move operation)
  3598. * @param {string} [description] the new description of the card
  3599. * @param {string} [authorId] change the owner of the card
  3600. * @param {string} [parentId] change the parent of the card
  3601. * @param {string} [labelIds] the new list of label IDs attached to the card
  3602. * @param {string} [swimlaneId] the new swimlane ID of the card
  3603. * @param {string} [members] the new list of member IDs attached to the card
  3604. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  3605. * @param {string} [requestedBy] the new requestedBy field of the card
  3606. * @param {string} [assignedBy] the new assignedBy field of the card
  3607. * @param {string} [receivedAt] the new receivedAt field of the card
  3608. * @param {string} [assignBy] the new assignBy field of the card
  3609. * @param {string} [startAt] the new startAt field of the card
  3610. * @param {string} [dueAt] the new dueAt field of the card
  3611. * @param {string} [endAt] the new endAt field of the card
  3612. * @param {string} [spentTime] the new spentTime field of the card
  3613. * @param {boolean} [isOverTime] the new isOverTime field of the card
  3614. * @param {string} [customFields] the new customFields value of the card
  3615. * @param {string} [color] the new color of the card
  3616. * @param {Object} [vote] the vote object
  3617. * @param {string} vote.question the vote question
  3618. * @param {boolean} vote.public show who voted what
  3619. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  3620. * @param {Object} [poker] the poker object
  3621. * @param {string} poker.question the vote question
  3622. * @param {boolean} poker.allowNonBoardMembers allow all logged in users to vote?
  3623. * @return_type {_id: string}
  3624. */
  3625. JsonRoutes.add(
  3626. 'PUT',
  3627. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3628. function(req, res) {
  3629. const paramBoardId = req.params.boardId;
  3630. const paramCardId = req.params.cardId;
  3631. const paramListId = req.params.listId;
  3632. const newBoardId = req.body.newBoardId;
  3633. const newSwimlaneId = req.body.newSwimlaneId;
  3634. const newListId = req.body.newListId;
  3635. let updated = false;
  3636. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3637. if (req.body.title) {
  3638. // Basic client-side validation - server will handle full sanitization
  3639. const newTitle = req.body.title.length > 1000 ? req.body.title.substring(0, 1000) : req.body.title;
  3640. if (process.env.DEBUG === 'true' && newTitle !== req.body.title) {
  3641. console.warn('Sanitized card title input:', req.body.title, '->', newTitle);
  3642. }
  3643. Cards.direct.update(
  3644. {
  3645. _id: paramCardId,
  3646. listId: paramListId,
  3647. boardId: paramBoardId,
  3648. archived: false,
  3649. },
  3650. {
  3651. $set: {
  3652. title: newTitle,
  3653. },
  3654. },
  3655. );
  3656. updated = true;
  3657. }
  3658. if (req.body.sort) {
  3659. const newSort = req.body.sort;
  3660. Cards.direct.update(
  3661. {
  3662. _id: paramCardId,
  3663. listId: paramListId,
  3664. boardId: paramBoardId,
  3665. archived: false,
  3666. },
  3667. {
  3668. $set: {
  3669. sort: newSort,
  3670. },
  3671. },
  3672. );
  3673. updated = true;
  3674. }
  3675. if (req.body.parentId) {
  3676. const newParentId = req.body.parentId;
  3677. Cards.direct.update(
  3678. {
  3679. _id: paramCardId,
  3680. listId: paramListId,
  3681. boardId: paramBoardId,
  3682. archived: false,
  3683. },
  3684. {
  3685. $set: {
  3686. parentId: newParentId,
  3687. },
  3688. },
  3689. );
  3690. updated = true;
  3691. }
  3692. if (req.body.description) {
  3693. const newDescription = req.body.description;
  3694. Cards.direct.update(
  3695. {
  3696. _id: paramCardId,
  3697. listId: paramListId,
  3698. boardId: paramBoardId,
  3699. archived: false,
  3700. },
  3701. {
  3702. $set: {
  3703. description: newDescription,
  3704. },
  3705. },
  3706. );
  3707. updated = true;
  3708. }
  3709. if (req.body.color) {
  3710. const newColor = req.body.color;
  3711. Cards.direct.update(
  3712. {
  3713. _id: paramCardId,
  3714. listId: paramListId,
  3715. boardId: paramBoardId,
  3716. archived: false,
  3717. },
  3718. { $set: { color: newColor } },
  3719. );
  3720. updated = true;
  3721. }
  3722. if (req.body.vote) {
  3723. const newVote = req.body.vote;
  3724. newVote.positive = [];
  3725. newVote.negative = [];
  3726. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  3727. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  3728. newVote.allowNonBoardMembers = false;
  3729. Cards.direct.update(
  3730. {
  3731. _id: paramCardId,
  3732. listId: paramListId,
  3733. boardId: paramBoardId,
  3734. archived: false,
  3735. },
  3736. { $set: { vote: newVote } },
  3737. );
  3738. updated = true;
  3739. }
  3740. if (req.body.poker) {
  3741. const newPoker = req.body.poker;
  3742. newPoker.one = [];
  3743. newPoker.two = [];
  3744. newPoker.three = [];
  3745. newPoker.five = [];
  3746. newPoker.eight = [];
  3747. newPoker.thirteen = [];
  3748. newPoker.twenty = [];
  3749. newPoker.forty = [];
  3750. newPoker.oneHundred = [];
  3751. newPoker.unsure = [];
  3752. if (!newPoker.hasOwnProperty('allowNonBoardMembers'))
  3753. newPoker.allowNonBoardMembers = false;
  3754. Cards.direct.update(
  3755. {
  3756. _id: paramCardId,
  3757. listId: paramListId,
  3758. boardId: paramBoardId,
  3759. archived: false,
  3760. },
  3761. { $set: { poker: newPoker } },
  3762. );
  3763. updated = true;
  3764. }
  3765. if (req.body.labelIds) {
  3766. let newlabelIds = req.body.labelIds;
  3767. if (_.isString(newlabelIds)) {
  3768. if (newlabelIds === '') {
  3769. newlabelIds = null;
  3770. } else {
  3771. newlabelIds = [newlabelIds];
  3772. }
  3773. }
  3774. Cards.direct.update(
  3775. {
  3776. _id: paramCardId,
  3777. listId: paramListId,
  3778. boardId: paramBoardId,
  3779. archived: false,
  3780. },
  3781. {
  3782. $set: {
  3783. labelIds: newlabelIds,
  3784. },
  3785. },
  3786. );
  3787. updated = true;
  3788. }
  3789. if (req.body.requestedBy) {
  3790. const newrequestedBy = req.body.requestedBy;
  3791. Cards.direct.update(
  3792. {
  3793. _id: paramCardId,
  3794. listId: paramListId,
  3795. boardId: paramBoardId,
  3796. archived: false,
  3797. },
  3798. { $set: { requestedBy: newrequestedBy } },
  3799. );
  3800. updated = true;
  3801. }
  3802. if (req.body.assignedBy) {
  3803. const newassignedBy = req.body.assignedBy;
  3804. Cards.direct.update(
  3805. {
  3806. _id: paramCardId,
  3807. listId: paramListId,
  3808. boardId: paramBoardId,
  3809. archived: false,
  3810. },
  3811. { $set: { assignedBy: newassignedBy } },
  3812. );
  3813. updated = true;
  3814. }
  3815. if (req.body.receivedAt) {
  3816. const newreceivedAt = req.body.receivedAt;
  3817. Cards.direct.update(
  3818. {
  3819. _id: paramCardId,
  3820. listId: paramListId,
  3821. boardId: paramBoardId,
  3822. archived: false,
  3823. },
  3824. { $set: { receivedAt: newreceivedAt } },
  3825. );
  3826. updated = true;
  3827. }
  3828. if (req.body.startAt) {
  3829. const newstartAt = req.body.startAt;
  3830. Cards.direct.update(
  3831. {
  3832. _id: paramCardId,
  3833. listId: paramListId,
  3834. boardId: paramBoardId,
  3835. archived: false,
  3836. },
  3837. { $set: { startAt: newstartAt } },
  3838. );
  3839. updated = true;
  3840. }
  3841. if (req.body.dueAt) {
  3842. const newdueAt = req.body.dueAt;
  3843. Cards.direct.update(
  3844. {
  3845. _id: paramCardId,
  3846. listId: paramListId,
  3847. boardId: paramBoardId,
  3848. archived: false,
  3849. },
  3850. { $set: { dueAt: newdueAt } },
  3851. );
  3852. updated = true;
  3853. }
  3854. if (req.body.endAt) {
  3855. const newendAt = req.body.endAt;
  3856. Cards.direct.update(
  3857. {
  3858. _id: paramCardId,
  3859. listId: paramListId,
  3860. boardId: paramBoardId,
  3861. archived: false,
  3862. },
  3863. { $set: { endAt: newendAt } },
  3864. );
  3865. updated = true;
  3866. }
  3867. if (req.body.spentTime) {
  3868. const newspentTime = req.body.spentTime;
  3869. Cards.direct.update(
  3870. {
  3871. _id: paramCardId,
  3872. listId: paramListId,
  3873. boardId: paramBoardId,
  3874. archived: false,
  3875. },
  3876. { $set: { spentTime: newspentTime } },
  3877. );
  3878. updated = true;
  3879. }
  3880. if (req.body.isOverTime) {
  3881. const newisOverTime = req.body.isOverTime;
  3882. Cards.direct.update(
  3883. {
  3884. _id: paramCardId,
  3885. listId: paramListId,
  3886. boardId: paramBoardId,
  3887. archived: false,
  3888. },
  3889. { $set: { isOverTime: newisOverTime } },
  3890. );
  3891. updated = true;
  3892. }
  3893. if (req.body.customFields) {
  3894. const newcustomFields = req.body.customFields;
  3895. Cards.direct.update(
  3896. {
  3897. _id: paramCardId,
  3898. listId: paramListId,
  3899. boardId: paramBoardId,
  3900. archived: false,
  3901. },
  3902. { $set: { customFields: newcustomFields } },
  3903. );
  3904. updated = true;
  3905. }
  3906. if (req.body.members) {
  3907. let newmembers = req.body.members;
  3908. if (_.isString(newmembers)) {
  3909. if (newmembers === '') {
  3910. newmembers = null;
  3911. } else {
  3912. newmembers = [newmembers];
  3913. }
  3914. }
  3915. Cards.direct.update(
  3916. {
  3917. _id: paramCardId,
  3918. listId: paramListId,
  3919. boardId: paramBoardId,
  3920. archived: false,
  3921. },
  3922. { $set: { members: newmembers } },
  3923. );
  3924. updated = true;
  3925. }
  3926. if (req.body.assignees) {
  3927. let newassignees = req.body.assignees;
  3928. if (_.isString(newassignees)) {
  3929. if (newassignees === '') {
  3930. newassignees = null;
  3931. } else {
  3932. newassignees = [newassignees];
  3933. }
  3934. }
  3935. Cards.direct.update(
  3936. {
  3937. _id: paramCardId,
  3938. listId: paramListId,
  3939. boardId: paramBoardId,
  3940. archived: false,
  3941. },
  3942. { $set: { assignees: newassignees } },
  3943. );
  3944. updated = true;
  3945. }
  3946. if (req.body.swimlaneId) {
  3947. const newParamSwimlaneId = req.body.swimlaneId;
  3948. Cards.direct.update(
  3949. {
  3950. _id: paramCardId,
  3951. listId: paramListId,
  3952. boardId: paramBoardId,
  3953. archived: false,
  3954. },
  3955. { $set: { swimlaneId: newParamSwimlaneId } },
  3956. );
  3957. updated = true;
  3958. }
  3959. if (req.body.listId) {
  3960. const newParamListId = req.body.listId;
  3961. Cards.direct.update(
  3962. {
  3963. _id: paramCardId,
  3964. listId: paramListId,
  3965. boardId: paramBoardId,
  3966. archived: false,
  3967. },
  3968. {
  3969. $set: {
  3970. listId: newParamListId,
  3971. },
  3972. },
  3973. );
  3974. updated = true;
  3975. const card = ReactiveCache.getCard(paramCardId);
  3976. cardMove(
  3977. req.body.authorId,
  3978. card,
  3979. {
  3980. fieldName: 'listId',
  3981. },
  3982. paramListId,
  3983. );
  3984. }
  3985. if (newBoardId && newSwimlaneId && newListId) {
  3986. // Move the card to the new board, swimlane, and list
  3987. Cards.direct.update(
  3988. {
  3989. _id: paramCardId,
  3990. listId: paramListId,
  3991. boardId: paramBoardId,
  3992. archived: false,
  3993. },
  3994. {
  3995. $set: {
  3996. boardId: newBoardId,
  3997. swimlaneId: newSwimlaneId,
  3998. listId: newListId,
  3999. },
  4000. },
  4001. );
  4002. updated = true;
  4003. const card = ReactiveCache.getCard(paramCardId);
  4004. cardMove(
  4005. req.userId,
  4006. card,
  4007. ['boardId', 'swimlaneId', 'listId'],
  4008. newListId,
  4009. newSwimlaneId,
  4010. newBoardId,
  4011. );
  4012. }
  4013. if (req.body.archive) {
  4014. function isTrue(data) {
  4015. return String(data).toLowerCase() === 'true';
  4016. }
  4017. var archive = isTrue(req.body.archive);
  4018. Cards.direct.update(
  4019. {
  4020. _id: paramCardId,
  4021. listId: paramListId,
  4022. boardId: paramBoardId,
  4023. archived: !archive,
  4024. },
  4025. {
  4026. $set: {
  4027. archived: archive,
  4028. },
  4029. },
  4030. );
  4031. updated = true;
  4032. }
  4033. // Check if update is true or false
  4034. if (!updated) {
  4035. JsonRoutes.sendResult(res, {
  4036. code: 404,
  4037. data: {
  4038. message: 'Error',
  4039. },
  4040. });
  4041. return;
  4042. }
  4043. JsonRoutes.sendResult(res, {
  4044. code: 200,
  4045. data: {
  4046. _id: paramCardId,
  4047. },
  4048. });
  4049. },
  4050. );
  4051. /**
  4052. * @operation delete_card
  4053. * @summary Delete a card from a board
  4054. *
  4055. * @description This operation **deletes** a card, and therefore the card
  4056. * is not put in the recycle bin.
  4057. *
  4058. * @param {string} boardId the board ID of the card
  4059. * @param {string} list the list ID of the card
  4060. * @param {string} cardId the ID of the card
  4061. * @return_type {_id: string}
  4062. */
  4063. JsonRoutes.add(
  4064. 'DELETE',
  4065. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  4066. function(req, res) {
  4067. const paramBoardId = req.params.boardId;
  4068. const paramListId = req.params.listId;
  4069. const paramCardId = req.params.cardId;
  4070. Authentication.checkBoardAccess(req.userId, paramBoardId);
  4071. const card = ReactiveCache.getCard(paramCardId);
  4072. Cards.direct.remove({
  4073. _id: paramCardId,
  4074. listId: paramListId,
  4075. boardId: paramBoardId,
  4076. });
  4077. cardRemover(req.body.authorId, card);
  4078. JsonRoutes.sendResult(res, {
  4079. code: 200,
  4080. data: {
  4081. _id: paramCardId,
  4082. },
  4083. });
  4084. },
  4085. );
  4086. /**
  4087. * @operation get_cards_by_custom_field
  4088. * @summary Get all Cards that matchs a value of a specific custom field
  4089. *
  4090. * @param {string} boardId the board ID
  4091. * @param {string} customFieldId the list ID
  4092. * @param {string} customFieldValue the value to look for
  4093. * @return_type [{_id: string,
  4094. * title: string,
  4095. * description: string,
  4096. * listId: string,
  4097. * swinlaneId: string}]
  4098. */
  4099. JsonRoutes.add(
  4100. 'GET',
  4101. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  4102. function(req, res) {
  4103. const paramBoardId = req.params.boardId;
  4104. const paramCustomFieldId = req.params.customFieldId;
  4105. const paramCustomFieldValue = req.params.customFieldValue;
  4106. Authentication.checkBoardAccess(req.userId, paramBoardId);
  4107. JsonRoutes.sendResult(res, {
  4108. code: 200,
  4109. data: ReactiveCache.getCards({
  4110. boardId: paramBoardId,
  4111. customFields: {
  4112. $elemMatch: {
  4113. _id: paramCustomFieldId,
  4114. value: paramCustomFieldValue,
  4115. },
  4116. },
  4117. archived: false,
  4118. }),
  4119. });
  4120. },
  4121. );
  4122. /**
  4123. * @operation edit_card_custom_field
  4124. * @summary Edit Custom Field in a Card
  4125. *
  4126. * @description Edit a custom field value in a card
  4127. * @param {string} boardId the board ID of the card
  4128. * @param {string} listId the list ID of the card
  4129. * @param {string} cardId the ID of the card
  4130. * @param {string} customFieldId the ID of the custom field
  4131. * @param {string} value the new custom field value
  4132. * @return_type {_id: string, customFields: [{_id: string, value: object}]}
  4133. */
  4134. JsonRoutes.add(
  4135. 'POST',
  4136. '/api/boards/:boardId/lists/:listId/cards/:cardId/customFields/:customFieldId',
  4137. function(req, res) {
  4138. const paramBoardId = req.params.boardId;
  4139. const paramCardId = req.params.cardId;
  4140. const paramListId = req.params.listId;
  4141. const paramCustomFieldId = req.params.customFieldId;
  4142. const paramCustomFieldValue = req.body.value;
  4143. Authentication.checkBoardAccess(req.userId, paramBoardId);
  4144. const card = ReactiveCache.getCard({
  4145. _id: paramCardId,
  4146. listId: paramListId,
  4147. boardId: paramBoardId,
  4148. archived: false,
  4149. });
  4150. if (!card) {
  4151. throw new Meteor.Error(404, 'Card not found');
  4152. }
  4153. const customFields = card.customFields || [];
  4154. const updatedCustomFields = customFields.map(cf => {
  4155. if (cf._id === paramCustomFieldId) {
  4156. return {
  4157. _id: cf._id,
  4158. value: paramCustomFieldValue,
  4159. };
  4160. }
  4161. return cf;
  4162. });
  4163. Cards.direct.update(
  4164. {
  4165. _id: paramCardId,
  4166. listId: paramListId,
  4167. boardId: paramBoardId,
  4168. archived: false,
  4169. },
  4170. { $set: { customFields: updatedCustomFields } },
  4171. );
  4172. JsonRoutes.sendResult(res, {
  4173. code: 200,
  4174. data: {
  4175. _id: paramCardId,
  4176. customFields: updatedCustomFields,
  4177. },
  4178. });
  4179. },
  4180. );
  4181. }
  4182. // Position history tracking methods
  4183. Cards.helpers({
  4184. /**
  4185. * Track the original position of this card
  4186. */
  4187. trackOriginalPosition() {
  4188. const existingHistory = PositionHistory.findOne({
  4189. boardId: this.boardId,
  4190. entityType: 'card',
  4191. entityId: this._id,
  4192. });
  4193. if (!existingHistory) {
  4194. PositionHistory.insert({
  4195. boardId: this.boardId,
  4196. entityType: 'card',
  4197. entityId: this._id,
  4198. originalPosition: {
  4199. sort: this.sort,
  4200. title: this.title,
  4201. },
  4202. originalSwimlaneId: this.swimlaneId || null,
  4203. originalListId: this.listId || null,
  4204. originalTitle: this.title,
  4205. createdAt: new Date(),
  4206. updatedAt: new Date(),
  4207. });
  4208. }
  4209. },
  4210. /**
  4211. * Get the original position history for this card
  4212. */
  4213. getOriginalPosition() {
  4214. return PositionHistory.findOne({
  4215. boardId: this.boardId,
  4216. entityType: 'card',
  4217. entityId: this._id,
  4218. });
  4219. },
  4220. /**
  4221. * Check if this card has moved from its original position
  4222. */
  4223. hasMovedFromOriginalPosition() {
  4224. const history = this.getOriginalPosition();
  4225. if (!history) return false;
  4226. const currentSwimlaneId = this.swimlaneId || null;
  4227. const currentListId = this.listId || null;
  4228. return history.originalPosition.sort !== this.sort ||
  4229. history.originalSwimlaneId !== currentSwimlaneId ||
  4230. history.originalListId !== currentListId;
  4231. },
  4232. /**
  4233. * Get a description of the original position
  4234. */
  4235. getOriginalPositionDescription() {
  4236. const history = this.getOriginalPosition();
  4237. if (!history) return 'No original position data';
  4238. const swimlaneInfo = history.originalSwimlaneId ?
  4239. ` in swimlane ${history.originalSwimlaneId}` :
  4240. ' in default swimlane';
  4241. const listInfo = history.originalListId ?
  4242. ` in list ${history.originalListId}` :
  4243. '';
  4244. return `Original position: ${history.originalPosition.sort || 0}${swimlaneInfo}${listInfo}`;
  4245. },
  4246. });
  4247. export default Cards;