refactor reloadSettings

This commit is contained in:
Alex Holliday committed 2024-09-30 09:52:47 +08:00
1 parent f4bbbc5fa9
commit 114f4ffb05
2 files changed
+11 -18

No files matched your search

+2 -2
View File
@@ -31,11 +31,11 @@ const updateAppSettings = async (req, res, next) => {
try {
const settings = await req.db.updateAppSettings(req.body);
await req.settingsService.reloadSettings();
const updatedSettings = await req.settingsService.reloadSettings();
return res.status(200).json({
success: true,
msg: successMessages.UPDATE_APP_SETTINGS,
data: settings,
data: updatedSettings,
});
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
+9 -16
View File
@@ -15,6 +15,7 @@ const envConfig = {
systemEmailAddress: process.env.SYSTEM_EMAIL_ADDRESS,
systemEmailPassword: process.env.SYSTEM_EMAIL_PASSWORD,
};
class SettingsService {
constructor() {
this.settings = envConfig;
@@ -26,11 +27,18 @@ class SettingsService {
if (!this.settings) {
throw new Error("Settings not found");
}
// Try to load settings from env first, if not found, load from db
for (const key in envConfig) {
if (envConfig[key] === undefined && dbSettings[key] !== undefined) {
this.settings[key] = dbSettings[key];
}
}
if (!this.settings) {
throw new Error("Settings not found");
}
return this.settings;
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
@@ -40,22 +48,7 @@ class SettingsService {
}
async reloadSettings() {
try {
const dbSettings = await AppSettings.findOne();
for (const key in envConfig) {
if (envConfig[key] === undefined && dbSettings[key] !== undefined) {
this.settings[key] = dbSettings[key];
}
}
if (!this.settings) {
throw new Error("Settings not found");
}
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "reloadSettings") : null;
throw error;
}
return this.loadSettings();
}
getSettings() {