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

148 lines
4.7 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-02-03 19:24:50 +00:00
import type { StatusPage, StatusPageLogo, StatusPageLogoDocument } from "@/types/statusPage.js";
2026-01-15 19:36:41 +00:00
import mongoose from "mongoose";
2026-01-22 20:57:20 +00:00
import { AppError } from "@/utils/AppError.js";
2026-01-15 19:36:41 +00:00
2026-02-03 19:28:55 +00:00
// Type for update data that can include document-level fields (Buffer for logo)
type StatusPageUpdateData = Partial<Omit<StatusPage, "id" | "userId" | "teamId" | "logo" | "createdAt" | "updatedAt">> & {
logo?: StatusPageLogoDocument | null;
};
2026-01-15 19:36:41 +00:00
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)) ?? [];
};
2026-02-03 19:24:50 +00:00
private mapLogo = (logo?: StatusPageLogoDocument | null): StatusPageLogo | undefined => {
2026-01-15 20:55:01 +00:00
if (!logo) {
return undefined;
}
2026-02-03 19:24:50 +00:00
// Convert Buffer to base64 string for JSON serialization
2026-02-03 22:40:26 +00:00
const base64Data = Buffer.isBuffer(logo.data) ? logo.data.toString("base64") : logo.data;
2026-01-15 20:55:01 +00:00
return {
2026-02-03 19:24:50 +00:00
data: base64Data,
2026-01-15 20:55:01 +00:00
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
2026-01-22 20:57:20 +00:00
private mapDocuments = (documents: StatusPageDocument[]): StatusPage[] => {
if (!documents?.length) {
return [];
}
return documents.map((doc) => this.toEntity(doc));
};
create = async (userId: string, teamId: string, image: Express.Multer.File | undefined, data: Partial<StatusPage>): Promise<StatusPage> => {
2026-02-03 19:28:55 +00:00
const { logo: _logo, ...restData } = data;
2026-01-22 20:57:20 +00:00
const statusPage = new StatusPageModel({
2026-02-03 19:28:55 +00:00
...restData,
2026-01-22 20:57:20 +00:00
userId,
teamId,
});
if (image) {
statusPage.logo = {
2026-02-03 19:24:50 +00:00
data: image.buffer as Buffer,
2026-01-22 20:57:20 +00:00
contentType: image.mimetype,
};
}
await statusPage.save();
return this.toEntity(statusPage);
};
findByUrl = async (url: string): Promise<StatusPage> => {
const statusPage = await StatusPageModel.findOne({
url,
});
if (!statusPage) {
throw new AppError({ message: "Status page not found", status: 404 });
}
return this.toEntity(statusPage);
// Get status page
};
findByTeamId = async (teamId: string): Promise<StatusPage[]> => {
const statusPages = await StatusPageModel.find({ teamId });
return this.mapDocuments(statusPages);
};
2026-02-03 22:40:26 +00:00
updateById = async (
id: string,
teamId: string,
image: Express.Multer.File | undefined,
patch: Partial<StatusPage> & { removeLogo?: string }
): Promise<StatusPage> => {
2026-02-03 20:11:47 +00:00
const { logo: _logo, removeLogo, ...restPatch } = patch;
2026-02-03 19:28:55 +00:00
const updateData: StatusPageUpdateData = { ...restPatch };
2026-01-22 20:57:20 +00:00
if (image) {
2026-02-03 19:24:50 +00:00
updateData.logo = {
data: image.buffer as Buffer,
2026-01-22 20:57:20 +00:00
contentType: image.mimetype,
};
2026-02-03 20:11:47 +00:00
} else if (removeLogo === "true") {
updateData.logo = null;
2026-01-22 20:57:20 +00:00
}
2026-02-03 19:24:50 +00:00
const statusPage = await StatusPageModel.findOneAndUpdate({ teamId, _id: id }, updateData, {
2026-01-22 20:57:20 +00:00
new: true,
});
if (!statusPage) {
throw new AppError({ message: "Status page not found", status: 404 });
}
return this.toEntity(statusPage);
};
deleteById = async (id: string, teamId: string): Promise<StatusPage> => {
const statusPage = await StatusPageModel.findOneAndDelete({ _id: id, teamId });
if (!statusPage) {
throw new AppError({ message: "Status page not found", status: 404 });
}
return this.toEntity(statusPage);
};
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;