Files
Checkmate/server/src/repositories/status-pages/MongoStatusPagesRepository.ts
T

65 lines
2.1 KiB
TypeScript
Raw Normal View History

2026-01-15 19:36:41 +00:00
import { IStatusPagesRepository } from "@/repositories/index.js";
import { type StatusPageDocument, StatusPageModel } from "@/db/models/StatusPage.js";
2026-01-15 20:55:01 +00:00
import type { StatusPage, StatusPageLogo } from "@/types/statusPage.js";
2026-01-15 19:36:41 +00:00
import mongoose from "mongoose";
class MongoStatusPagesRepository implements IStatusPagesRepository {
2026-01-15 20:55:01 +00:00
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
if (!value) {
return "";
}
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
};
2026-01-15 19:36:41 +00:00
2026-01-15 20:55:01 +00:00
private toDateString = (value?: Date | string | null): string => {
if (!value) {
return new Date(0).toISOString();
}
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
};
2026-01-15 19:36:41 +00:00
2026-01-15 20:55:01 +00:00
private mapIdArray = (values?: Array<mongoose.Types.ObjectId | string>): string[] => {
return values?.map((value) => this.toStringId(value)) ?? [];
};
private mapLogo = (logo?: StatusPageLogo | null): StatusPageLogo | undefined => {
if (!logo) {
return undefined;
}
return {
data: logo.data,
contentType: logo.contentType,
2026-01-15 19:36:41 +00:00
};
2026-01-15 20:55:01 +00:00
};
2026-01-15 19:36:41 +00:00
2026-01-15 20:55:01 +00:00
private toEntity = (doc: StatusPageDocument): StatusPage => {
2026-01-15 19:36:41 +00:00
return {
2026-01-15 20:55:01 +00:00
id: this.toStringId(doc._id),
userId: this.toStringId(doc.userId),
teamId: this.toStringId(doc.teamId),
2026-01-15 19:36:41 +00:00
type: doc.type,
companyName: doc.companyName,
url: doc.url,
timezone: doc.timezone ?? undefined,
color: doc.color,
2026-01-15 20:55:01 +00:00
monitors: this.mapIdArray(doc.monitors),
subMonitors: this.mapIdArray(doc.subMonitors),
originalMonitors: this.mapIdArray(doc.originalMonitors),
logo: this.mapLogo(doc.logo),
2026-01-15 19:36:41 +00:00
isPublished: doc.isPublished,
showCharts: doc.showCharts,
showUptimePercentage: doc.showUptimePercentage,
showAdminLoginLink: doc.showAdminLoginLink,
customCSS: doc.customCSS,
2026-01-15 20:55:01 +00:00
createdAt: this.toDateString(doc.createdAt),
updatedAt: this.toDateString(doc.updatedAt),
2026-01-15 19:36:41 +00:00
};
2026-01-15 20:55:01 +00:00
};
2026-01-15 19:36:41 +00:00
removeMonitorFromStatusPages = async (monitorId: string): Promise<number> => {
const res = await StatusPageModel.updateMany({ monitors: monitorId }, { $pull: { monitors: monitorId } });
return res.modifiedCount;
};
}
export default MongoStatusPagesRepository;