Files
Checkmate/server/src/service/v1/system/settingsService.js
T

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "SettingsService";
2025-07-25 15:15:14 -07:00
2025-04-20 11:29:53 -07:00
const envConfig = {
2025-07-29 10:08:45 -07:00
jwtSecret: process.env.JWT_SECRET,
jwtTTL: process.env.TOKEN_TTL,
systemEmailHost: process.env.SYSTEM_EMAIL_HOST,
2025-05-09 09:33:35 -07:00
nodeEnv: process.env.NODE_ENV,
2025-04-20 11:29:53 -07:00
logLevel: process.env.LOG_LEVEL,
2025-07-28 15:25:33 -07:00
clientHost: process.env.CLIENT_HOST,
dbConnectionString: process.env.DB_CONNECTION_STRING,
port: process.env.PORT,
2025-04-20 11:29:53 -07:00
};
/**
* SettingsService
*
* This service is responsible for loading and managing the application settings.
*/
class SettingsService {
2025-07-25 15:15:14 -07:00
static SERVICE_NAME = "SettingsService";
2025-04-20 11:29:53 -07:00
/**
* Constructs a new SettingsService
* @constructor
* @throws {Error}
2025-06-17 10:58:30 +08:00
*/ constructor(AppSettings) {
this.AppSettings = AppSettings;
2025-04-20 11:29:53 -07:00
this.settings = { ...envConfig };
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return SettingsService.SERVICE_NAME;
}
2025-04-20 11:29:53 -07:00
/**
2025-05-08 13:11:51 -07:00
* Load settings from env settings
* @returns {Object>} The settings.
*/
loadSettings() {
return this.settings;
2025-04-20 11:29:53 -07:00
}
2025-07-28 14:37:18 -07:00
2025-04-20 11:29:53 -07:00
/**
* 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");
}
return this.settings;
}
async getDBSettings() {
2025-05-13 12:58:01 -07:00
// Remove any old settings
2025-06-17 10:58:30 +08:00
await this.AppSettings.deleteMany({ version: { $exists: false } });
2025-05-13 12:58:01 -07:00
2025-07-21 10:34:16 -07:00
let settings = await this.AppSettings.findOne({ singleton: true }).select("-__v -_id -createdAt -updatedAt -singleton").lean();
2025-05-09 13:25:50 -07:00
if (settings === null) {
2025-06-17 10:58:30 +08:00
await this.AppSettings.create({});
2025-07-21 10:34:16 -07:00
settings = await this.AppSettings.findOne({ singleton: true }).select("-__v -_id -createdAt -updatedAt -singleton").lean();
2025-05-09 13:25:50 -07:00
}
return settings;
}
2025-04-20 11:29:53 -07:00
}
export default SettingsService;