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

94 lines
2.3 KiB
TypeScript
Raw Normal View History

2026-01-21 13:01:31 -08:00
import { ISettingsRepository } from "@/repositories/index.js";
2026-03-26 09:47:10 -07:00
import { type DbType, Settings, SettingsUpdate } from "@/types/index.js";
2026-01-21 13:01:31 -08:00
import { AppError } from "@/utils/AppError.js";
2026-03-02 21:52:22 +00:00
import { ValidatedEnv } from "@/validation/envValidation.js";
import type { StringValue } from "ms";
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: StringValue;
2026-03-02 21:52:22 +00:00
nodeEnv: string;
logLevel: string;
clientHost: string;
dbConnectionString: string;
2026-03-26 09:47:10 -07:00
dbType: DbType;
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>;
updateDbSettings(newSettings: SettingsUpdate): Promise<Settings>;
2026-01-21 11:18:13 -08:00
}
2026-03-05 14:46:28 -08:00
export 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-03-25 13:48:36 -07:00
private settingsRepository: ISettingsRepository | null = null;
2025-07-25 15:15:14 -07:00
2026-03-25 13:48:36 -07:00
constructor(env: ValidatedEnv) {
2026-03-02 21:52:22 +00:00
this.settings = {
jwtSecret: env.JWT_SECRET,
jwtTTL: env.TOKEN_TTL as StringValue,
2026-03-02 21:52:22 +00:00
nodeEnv: env.NODE_ENV,
logLevel: env.LOG_LEVEL,
clientHost: env.CLIENT_HOST,
dbConnectionString: env.DB_CONNECTION_STRING,
2026-03-26 09:47:10 -07:00
dbType: env.DB_TYPE,
2026-03-02 21:52:22 +00:00
};
2025-04-20 11:29:53 -07:00
}
2025-07-25 15:15:14 -07:00
2026-03-25 13:48:36 -07:00
setRepository(settingsRepository: ISettingsRepository) {
this.settingsRepository = settingsRepository;
}
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-03-25 13:48:36 -07:00
private getRepository(): ISettingsRepository {
if (!this.settingsRepository) {
throw new AppError({
message: "Settings repository not initialized. Call setRepository() after DB connect.",
status: 500,
service: SERVICE_NAME,
});
}
return this.settingsRepository;
}
updateDbSettings = async (newSettings: SettingsUpdate) => {
2026-03-25 13:48:36 -07:00
return await this.getRepository().update(newSettings);
2026-01-21 13:01:31 -08:00
};
getDBSettings = async () => {
2026-03-25 13:48:36 -07:00
const repo = this.getRepository();
2025-05-13 12:58:01 -07:00
// Remove any old settings
2026-03-25 13:48:36 -07:00
await repo.deleteLegacy();
2025-05-13 12:58:01 -07:00
2026-03-25 13:48:36 -07:00
let settings = await repo.findSingleton();
2025-05-09 13:25:50 -07:00
if (settings === null) {
2026-03-25 13:48:36 -07:00
await repo.create({});
settings = await repo.findSingleton();
2026-01-21 13:01:31 -08:00
}
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
}