Skip to content

Commit

Permalink
feat(settings) initConfigSiteSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Apr 11, 2024
1 parent 619fdf4 commit be1d0e2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -57,10 +59,20 @@ const resolvers = [
const app = async (args: RoadmanBuild): Promise<RoadmanBuild> => {
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,
Expand Down
50 changes: 46 additions & 4 deletions src/settings/settings.methods.ts
Original file line number Diff line number Diff line change
@@ -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<SiteSettings | null> => {
try {
Expand All @@ -17,10 +59,10 @@ export const getSiteSettings = async (client = false): Promise<SiteSettings | nu
throw new Error("error getting site settings");
}

if(client){
if (client) {
return pick(settings, siteSettingsClientSelectors) as SiteSettings;
}

return settings;
} catch (error) {
console.error("error getting site settings", error);
Expand Down
4 changes: 3 additions & 1 deletion src/settings/settings.resolver.admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Resolver,
UseMiddleware,
} from "type-graphql";
import { getCSRFToken, getSiteSettings } from "./settings.methods";
import { emitSiteSettingsRefresh, getCSRFToken, getSiteSettings } from "./settings.methods";

import _get from "lodash/get";
import { initSiteSettings } from "../_startup/startup";
Expand Down Expand Up @@ -50,6 +50,8 @@ export class AdminSettingsResolver {
const updatedSiteSettings = await SiteSettingsModel.updateById(siteSettings.id, newSiteSettings);

// TODO restart
// TODO check if backend envs changed
emitSiteSettingsRefresh();
return { success: true, data: updatedSiteSettings };

}
Expand Down

0 comments on commit be1d0e2

Please sign in to comment.