Files
Checkmate/server/src/repositories/settings/MongoSettingsRepository.ts
T

99 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-03-12 10:05:36 -07:00
import mongoose, { type UpdateQuery } from "mongoose";
2026-01-17 18:29:42 +00:00
import { ISettingsRepository } from "@/repositories/settings/ISettingsRepository.js";
import type { Settings, SettingsUpdate } from "@/types/index.js";
2026-01-21 13:01:31 -08:00
import { AppSettingsModel, type AppSettingsDocument } from "@/db/models/index.js";
2026-01-17 18:29:42 +00:00
class MongoSettingsRepository implements ISettingsRepository {
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
if (!value) {
return "";
}
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
};
private toDateString = (value?: Date | string | null): string => {
if (!value) {
return new Date(0).toISOString();
}
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
};
protected toEntity = (doc: AppSettingsDocument): Settings => {
return {
id: this.toStringId(doc._id),
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,
createdAt: this.toDateString(doc.createdAt),
updatedAt: this.toDateString(doc.updatedAt),
};
};
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;