Files
Checkmate/server/src/domain/app-settings/app-settings.repository.mongo.ts
T

88 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-07-15 10:20:16 -07:00
import { type UpdateQuery } from "mongoose";
2026-06-09 12:34:15 -07:00
import { ISettingsRepository } from "@/domain/app-settings/app-settings-repository.interface.js";
import type { Settings, SettingsUpdate } from "@/domain/app-settings/app-settings.type.js";
import { AppSettingsModel, type AppSettingsDocument } from "@/domain/app-settings/app-settings.model.js";
2026-07-15 10:20:16 -07:00
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
2026-01-17 18:29:42 +00:00
class MongoSettingsRepository implements ISettingsRepository {
2026-07-20 10:01:48 -07:00
private toEntity = (doc: AppSettingsDocument): Settings => {
2026-01-17 18:29:42 +00:00
return {
2026-07-15 10:20:16 -07:00
id: toStringId(doc._id),
2026-01-17 18:29:42 +00:00
checkTTL: doc.checkTTL,
language: doc.language,
2026-01-17 19:08:12 +00:00
jwtSecret: doc.jwtSecret ?? undefined,
2026-01-17 18:29:42 +00:00
pagespeedApiKey: doc.pagespeedApiKey ?? undefined,
systemEmailHost: doc.systemEmailHost ?? undefined,
systemEmailPort: doc.systemEmailPort ?? undefined,
systemEmailAddress: doc.systemEmailAddress ?? undefined,
systemEmailPassword: doc.systemEmailPassword ?? undefined,
systemEmailUser: doc.systemEmailUser ?? undefined,
systemEmailConnectionHost: doc.systemEmailConnectionHost ?? undefined,
systemEmailTLSServername: doc.systemEmailTLSServername ?? undefined,
systemEmailSecure: doc.systemEmailSecure ?? false,
systemEmailPool: doc.systemEmailPool ?? false,
systemEmailIgnoreTLS: doc.systemEmailIgnoreTLS ?? false,
systemEmailRequireTLS: doc.systemEmailRequireTLS ?? false,
systemEmailRejectUnauthorized: doc.systemEmailRejectUnauthorized ?? true,
showURL: doc.showURL ?? false,
singleton: doc.singleton,
version: doc.version ?? 1,
globalThresholds: doc.globalThresholds ?? undefined,
2026-08-20 11:16:07 -07:00
globalProxyEnabled: doc.globalProxyEnabled ?? false,
globalProxyId: doc.globalProxyId ?? undefined,
2026-07-15 10:20:16 -07:00
createdAt: toDateString(doc.createdAt),
updatedAt: toDateString(doc.updatedAt),
2026-01-17 18:29:42 +00:00
};
};
2026-01-21 13:01:31 -08:00
create = async (settings: Partial<Settings>) => {
const newSettings = await AppSettingsModel.create(settings);
return this.toEntity(newSettings);
};
findSingleton = async () => {
2026-03-04 17:50:27 +00:00
const settings = await AppSettingsModel.findOne({ singleton: true }).select("-__v -_id -createdAt -updatedAt -singleton").lean();
2026-01-21 13:01:31 -08:00
if (!settings) {
return null;
}
return this.toEntity(settings);
};
update = async (settings: SettingsUpdate) => {
2026-03-12 10:05:36 -07:00
const $set: Record<string, unknown> = {};
const $unset: Record<string, string> = {};
2026-01-17 18:29:42 +00:00
2026-02-18 18:47:58 +00:00
// Iterate through settings and separate into $set and $unset
Object.entries(settings).forEach(([key, value]) => {
if (value === undefined || value === null) {
2026-03-12 10:05:36 -07:00
$unset[key] = "";
2026-02-18 18:47:58 +00:00
} else {
2026-03-12 10:05:36 -07:00
$set[key] = value;
2026-02-18 18:47:58 +00:00
}
});
2026-01-17 18:29:42 +00:00
2026-03-12 10:05:36 -07:00
const update: UpdateQuery<AppSettingsDocument> = {
...(Object.keys($set).length > 0 && { $set }),
...(Object.keys($unset).length > 0 && { $unset }),
};
2026-01-17 18:29:42 +00:00
await AppSettingsModel.findOneAndUpdate({}, update, {
upsert: true,
});
2026-01-17 19:08:12 +00:00
const updatedSettings = await AppSettingsModel.findOneAndUpdate({}, update, {
upsert: true,
new: true,
projection: "-__v -_id -createdAt -updatedAt -singleton",
});
return this.toEntity(updatedSettings);
2026-01-17 18:29:42 +00:00
};
2026-01-21 13:01:31 -08:00
deleteLegacy = async () => {
const res = await AppSettingsModel.deleteMany({ version: { $exists: false } });
return res.deletedCount > 0;
};
2026-01-17 18:29:42 +00:00
}
export default MongoSettingsRepository;