cards.js 101 KB

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