Merge upstream/develop

This commit is contained in:
Alex Holliday committed 2024-10-01 11:02:46 +08:00
commit 1f2a19fb43
6 files changed
+49 -64

No files matched your search

+30 -8
View File
@@ -1,5 +1,5 @@
const { env } = require("process");
const AppSettings = require("../db/models/AppSettings");
const AppSettings = require("../models/AppSettings");
const SERVICE_NAME = "SettingsService";
const envConfig = {
logLevel: undefined,
@@ -17,19 +17,34 @@ const envConfig = {
systemEmailAddress: process.env.SYSTEM_EMAIL_ADDRESS,
systemEmailPassword: process.env.SYSTEM_EMAIL_PASSWORD,
};
/**
* SettingsService
*
* This service is responsible for loading and managing the application settings.
* It gives priority to environment variables and will only load settings
* from the database if they are not set in the environment.
*/
class SettingsService {
constructor() {
/**
* Constructs a new SettingsService
* @constructor
* @throws {Error}
*/ constructor() {
this.settings = { ...envConfig };
}
async loadSettings() {
/**
* Load settings from the database and merge with environment settings.
* If there are any settings that weren't set by environment variables, use user settings from the database.
* @returns {Promise<Object>} The merged settings.
* @throws Will throw an error if settings are not found in the database or if settings have not been loaded.
*/ async loadSettings() {
try {
const dbSettings = await AppSettings.findOne();
if (!this.settings) {
throw new Error("Settings not found");
}
// Try to load settings from env first, if not found, load from db
// If there are any settings that weren't set by environment variables, use user settings from DB
for (const key in envConfig) {
if (envConfig[key] === undefined && dbSettings[key] !== undefined) {
this.settings[key] = dbSettings[key];
@@ -46,11 +61,18 @@ class SettingsService {
throw error;
}
}
/**
* Reload settings by calling loadSettings.
* @returns {Promise<Object>} The reloaded settings.
*/
async reloadSettings() {
return this.loadSettings();
}
/**
* Get the current settings.
* @returns {Object} The current settings.
* @throws Will throw an error if settings have not been loaded.
*/
getSettings() {
if (!this.settings) {
throw new Error("Settings have not been loaded");