From be1d0e2c4e37edd4ebddc2e3a96c80e722b5f20b Mon Sep 17 00:00:00 2001 From: ceddybi Date: Thu, 11 Apr 2024 13:27:50 -0400 Subject: [PATCH] feat(settings) initConfigSiteSettings --- src/index.ts | 12 ++++++ src/settings/settings.methods.ts | 50 +++++++++++++++++++++++-- src/settings/settings.resolver.admin.ts | 4 +- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index dd3f785..0da7efa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,6 +33,8 @@ import { mediaRoadman } from './media/media.app'; import { walletRouter as moneroxWalletRouter } from "@roadmanjs/monerox"; import { DisputeAdminResolver, DisputesResolver } from './disputes'; import AdminSettingsResolver from './settings/settings.resolver.admin'; +import { initSiteSettings } from './_config/site'; +import { getSiteSettings, initConfigSiteSettings } from './settings/settings.methods'; const resolvers = [ ...getAuthResolvers(), @@ -57,10 +59,20 @@ const resolvers = [ const app = async (args: RoadmanBuild): Promise => { const { app } = args; app.use("/wallet", moneroxWalletRouter); + + app.get("/client/settings", async (req, res) => { + const siteSettings = await getSiteSettings(true); + const settings = siteSettings ? siteSettings : initSiteSettings; + res.json(settings); + }); + return args; } const run = async () => { + + await initConfigSiteSettings(); + const roadmanStarted = await roadman({ roadmen: [ app, diff --git a/src/settings/settings.methods.ts b/src/settings/settings.methods.ts index 3f1095b..a985da6 100644 --- a/src/settings/settings.methods.ts +++ b/src/settings/settings.methods.ts @@ -1,9 +1,51 @@ import { SiteSettings, SiteSettingsModel, siteSettingsClientSelectors } from "./settings.model"; - +import Queue from "bull" import { awaitTo } from "couchset/dist/utils"; import { initSiteSettings } from "../_startup/startup" -import { isEmpty, pick } from "lodash"; +import { isEmpty, map, omit, pick, set } from "lodash"; import { sign } from 'jsonwebtoken'; +import { REDIS_URL, verbose } from 'roadman'; +import { couchsetRoadman } from "@roadmanjs/couchset"; + +const queueName = "site-stats"; +const siteStatsQueue = new Queue(queueName, REDIS_URL, {}); +siteStatsQueue.on(`global:${queueName}:refresh`, () => { + console.log(`global:${queueName}:refresh`); + process.exit(0); +}); +siteStatsQueue.process(async (job, done) => { + console.log(`Processing job ${job.id}`); + console.log(job.data); + done(); +}); + +export const initConfigSiteSettings = async () => { + await couchsetRoadman(null as any); + const siteSettings = await getSiteSettings(); + if (siteSettings) { + const config = omit(siteSettings, ["feePrices", "_type", "_scope"]); + + const configEnvs = map(config, (value, key) => { + return { [key]: value?.toString() }; + }).reduce((acc, curr) => { + return { ...acc, ...curr }; + }, {}); + + verbose("configEnvs", configEnvs); + + process.env = { ...process.env, ...configEnvs }; + }; +} + +export const emitSiteSettingsRefresh = (): void => { + setTimeout(() => { + const job = siteStatsQueue.emit(`global:${queueName}:refresh`); + return job; + }, 3000); +} +// export const siteStatsQueue = () => { + +// }; export const getSiteSettings = async (client = false): Promise => { try { @@ -17,10 +59,10 @@ export const getSiteSettings = async (client = false): Promise