Files
Checkmate/server/src/service/business/statusPageService.ts
T

67 lines
2.9 KiB
TypeScript
Raw Normal View History

2026-01-22 20:57:20 +00:00
import { type IStatusPagesRepository } from "@/repositories/index.js";
2026-04-29 10:33:46 -07:00
import { ISettingsService } from "@/service/system/settingsService.js";
import { DEFAULT_STATUS_PAGE_THEME, DEFAULT_STATUS_PAGE_THEME_MODE, StatusPage } from "@/types/index.js";
2026-01-22 20:57:20 +00:00
export interface IStatusPageService {
createStatusPage(userId: string, teamId: string, image: Express.Multer.File | undefined, data: Partial<StatusPage>): Promise<StatusPage>;
getStatusPageByUrl(url: string): Promise<StatusPage>;
getStatusPagesByTeamId(teamId: string): Promise<StatusPage[]>;
updateStatusPage(id: string, teamId: string, image: Express.Multer.File | undefined, data: Partial<StatusPage>): Promise<StatusPage>;
deleteStatusPage(statusPageId: string, teamId: string): Promise<StatusPage>;
}
export class StatusPageService implements IStatusPageService {
private statusPagesRepository: IStatusPagesRepository;
2026-04-29 10:33:46 -07:00
private settingsService: ISettingsService;
constructor(statusPagesRepository: IStatusPagesRepository, settingsService: ISettingsService) {
2026-01-22 20:57:20 +00:00
this.statusPagesRepository = statusPagesRepository;
2026-04-29 10:33:46 -07:00
this.settingsService = settingsService;
2026-01-22 20:57:20 +00:00
}
2026-04-29 10:33:46 -07:00
private withoutThemeFields = (data: Partial<StatusPage>): Partial<StatusPage> => {
const { theme: _theme, themeMode: _themeMode, ...rest } = data;
return rest;
};
private applyDefaultTheme = (statusPage: StatusPage): StatusPage => ({
...statusPage,
theme: DEFAULT_STATUS_PAGE_THEME,
themeMode: DEFAULT_STATUS_PAGE_THEME_MODE,
});
private normalizeTheme = (statusPage: StatusPage): StatusPage =>
this.settingsService.areStatusPageThemesEnabled() ? statusPage : this.applyDefaultTheme(statusPage);
private normalizeInput = (data: Partial<StatusPage>): Partial<StatusPage> =>
this.settingsService.areStatusPageThemesEnabled() ? data : this.withoutThemeFields(data);
2026-01-22 20:57:20 +00:00
createStatusPage = async (
userId: string,
teamId: string,
image: Express.Multer.File | undefined,
data: Partial<StatusPage>
): Promise<StatusPage> => {
2026-04-29 10:33:46 -07:00
const created = await this.statusPagesRepository.create(userId, teamId, image, this.normalizeInput(data));
return this.normalizeTheme(created);
2026-01-22 20:57:20 +00:00
};
getStatusPageByUrl = async (url: string): Promise<StatusPage> => {
2026-04-29 10:33:46 -07:00
const statusPage = await this.statusPagesRepository.findByUrl(url);
return this.normalizeTheme(statusPage);
2026-01-22 20:57:20 +00:00
};
getStatusPagesByTeamId = async (teamId: string): Promise<StatusPage[]> => {
2026-04-29 10:33:46 -07:00
const statusPages = await this.statusPagesRepository.findByTeamId(teamId);
return statusPages.map((sp) => this.normalizeTheme(sp));
2026-01-22 20:57:20 +00:00
};
updateStatusPage = async (id: string, teamId: string, image: Express.Multer.File | undefined, data: Partial<StatusPage>): Promise<StatusPage> => {
2026-04-29 10:33:46 -07:00
const updated = await this.statusPagesRepository.updateById(id, teamId, image, this.normalizeInput(data));
return this.normalizeTheme(updated);
2026-01-22 20:57:20 +00:00
};
deleteStatusPage = async (statusPageId: string, teamId: string): Promise<StatusPage> => {
return await this.statusPagesRepository.deleteById(statusPageId, teamId);
};
}