Merge branch 'feat/be/in-app-settings' into feat/fe/in-app-settings

This commit is contained in:
Alex Holliday
2024-09-27 13:02:20 +08:00
2 changed files with 54 additions and 12 deletions
+1
View File
@@ -31,6 +31,7 @@ const updateAppSettings = async (req, res, next) => {
try {
const settings = await req.db.updateAppSettings(req.body);
await req.settingsService.reloadSettings();
return res.status(200).json({
success: true,
msg: successMessages.UPDATE_APP_SETTINGS,
+53 -12
View File
@@ -1,24 +1,65 @@
const AppSettings = require("../models/AppSettings");
const SERVICE_NAME = "SettingsService";
CLIENT_HOST = process.env.CLIENT_HOST;
JWT_SECRET = process.env.JWT_SECRET;
DB_TYPE = process.env.DB_TYPE;
DB_CONNECTION_STRING = process.env.DB_CONNECTION_STRING;
REDIS_HOST = process.env.REDIS_HOST;
REDIS_PORT = process.env.REDIS_PORT;
TOKEN_TTL = process.env.TOKEN_TTL;
PAGESPEED_API_KEY = process.env.PAGESPEED_API_KEY;
SYSTEM_EMAIL_HOST = process.env.SYSTEM_EMAIL_HOST;
SYSTEM_EMAIL_PORT = process.env.SYSTEM_EMAIL_PORT;
SYSTEM_EMAIL_ADDRESS = process.env.SYSTEM_EMAIL_ADDRESS;
SYSTEM_EMAIL_PASSWORD = process.env.SYSTEM_EMAIL_PASSWORD;
class SettingsService {
constructor() {
this.settings = null;
this.settings = {
clientHost: CLIENT_HOST,
jwtSecret: JWT_SECRET,
dbType: DB_TYPE,
dbConnectionString: DB_CONNECTION_STRING,
redisHost: REDIS_HOST,
redisPort: REDIS_PORT,
tokenTTL: TOKEN_TTL,
pagespeedApiKey: PAGESPEED_API_KEY,
systemEmailHost: SYSTEM_EMAIL_HOST,
systemEmailPort: SYSTEM_EMAIL_PORT,
systemEmailAddress: SYSTEM_EMAIL_ADDRESS,
systemEmailPassword: SYSTEM_EMAIL_PASSWORD,
};
}
async loadSettings() {
if (!this.settings) {
try {
this.settings = await AppSettings.findOne();
if (!this.settings) {
throw new Error("Settings not found");
}
return this.settings;
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "loadSettings") : null;
throw error;
try {
const dbSettings = await AppSettings.findOne();
if (!this.settings) {
throw new Error("Settings not found");
}
for (const key in this.settings) {
if (this.settings[key] === undefined && dbSettings[key] !== undefined) {
this.settings[key] = dbSettings[key];
}
}
return this.settings;
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "loadSettings") : null;
throw error;
}
}
async reloadSettings() {
try {
this.settings = await AppSettings.findOne();
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;
}
}