Files
Checkmate/server/src/service/system/settingsService.ts
T

78 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-01-21 13:01:31 -08:00
import { ISettingsRepository } from "@/repositories/index.js";
import { Settings } from "@/types/index.js";
import { AppError } from "@/utils/AppError.js";
2026-03-02 21:52:22 +00:00
import { ValidatedEnv } from "@/validation/envValidation.js";
2026-01-21 13:01:31 -08:00
2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "SettingsService";
2025-07-25 15:15:14 -07:00
2026-01-21 11:18:13 -08:00
export type EnvConfig = {
2026-03-02 21:52:22 +00:00
jwtSecret: string;
jwtTTL: string;
nodeEnv: string;
logLevel: string;
clientHost: string;
dbConnectionString: string;
2025-04-20 11:29:53 -07:00
};
2026-01-21 11:18:13 -08:00
export interface ISettingsService {
readonly serviceName: string;
loadSettings(): EnvConfig;
getSettings(): EnvConfig;
2026-01-21 13:01:31 -08:00
getDBSettings(): Promise<Settings>;
2026-01-21 11:18:13 -08:00
}
class SettingsService implements ISettingsService {
2026-03-03 23:01:35 +00:00
static SERVICE_NAME = SERVICE_NAME;
2026-01-21 11:18:13 -08:00
private settings: EnvConfig;
2026-01-21 13:01:31 -08:00
private settingsRepository: ISettingsRepository;
2025-07-25 15:15:14 -07:00
2026-03-02 21:52:22 +00:00
constructor(settingsRepository: ISettingsRepository, env: ValidatedEnv) {
2026-01-21 13:01:31 -08:00
this.settingsRepository = settingsRepository;
2026-03-02 21:52:22 +00:00
this.settings = {
jwtSecret: env.JWT_SECRET,
jwtTTL: env.TOKEN_TTL,
nodeEnv: env.NODE_ENV,
logLevel: env.LOG_LEVEL,
clientHost: env.CLIENT_HOST,
dbConnectionString: env.DB_CONNECTION_STRING,
};
2025-04-20 11:29:53 -07:00
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return SettingsService.SERVICE_NAME;
}
2025-05-08 13:11:51 -07:00
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
getSettings() {
if (!this.settings) {
throw new Error("Settings have not been loaded");
}
return this.settings;
}
2026-01-21 13:01:31 -08:00
updateDbSettings = async (newSettings: Partial<Settings>) => {
return await this.settingsRepository.update(newSettings);
};
getDBSettings = async () => {
2025-05-13 12:58:01 -07:00
// Remove any old settings
2026-01-21 13:01:31 -08:00
await this.settingsRepository.deleteLegacy();
2025-05-13 12:58:01 -07:00
2026-01-21 13:01:31 -08:00
let settings = await this.settingsRepository.findSingleton();
2025-05-09 13:25:50 -07:00
if (settings === null) {
2026-01-21 13:01:31 -08:00
await this.settingsRepository.create({});
settings = await this.settingsRepository.findSingleton();
}
if (!settings) {
throw new AppError({ message: "Settings not found", status: 500 });
2025-05-09 13:25:50 -07:00
}
2026-01-10 21:29:15 +00:00
return settings;
2026-01-21 13:01:31 -08:00
};
2025-04-20 11:29:53 -07:00
}
export default SettingsService;