cards.js 102 KB

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