import { type IStatusPagesRepository } from "@/repositories/index.js"; import { ISettingsService } from "@/service/system/settingsService.js"; import { DEFAULT_STATUS_PAGE_THEME, DEFAULT_STATUS_PAGE_THEME_MODE, StatusPage } from "@/types/index.js"; export interface IStatusPageService { createStatusPage(userId: string, teamId: string, image: Express.Multer.File | undefined, data: Partial): Promise; getStatusPageByUrl(url: string): Promise; getStatusPagesByTeamId(teamId: string): Promise; updateStatusPage(id: string, teamId: string, image: Express.Multer.File | undefined, data: Partial): Promise; deleteStatusPage(statusPageId: string, teamId: string): Promise; } export class StatusPageService implements IStatusPageService { private statusPagesRepository: IStatusPagesRepository; private settingsService: ISettingsService; constructor(statusPagesRepository: IStatusPagesRepository, settingsService: ISettingsService) { this.statusPagesRepository = statusPagesRepository; this.settingsService = settingsService; } private withoutThemeFields = (data: Partial): Partial => { 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): Partial => this.settingsService.areStatusPageThemesEnabled() ? data : this.withoutThemeFields(data); createStatusPage = async ( userId: string, teamId: string, image: Express.Multer.File | undefined, data: Partial ): Promise => { const created = await this.statusPagesRepository.create(userId, teamId, image, this.normalizeInput(data)); return this.normalizeTheme(created); }; getStatusPageByUrl = async (url: string): Promise => { const statusPage = await this.statusPagesRepository.findByUrl(url); return this.normalizeTheme(statusPage); }; getStatusPagesByTeamId = async (teamId: string): Promise => { const statusPages = await this.statusPagesRepository.findByTeamId(teamId); return statusPages.map((sp) => this.normalizeTheme(sp)); }; updateStatusPage = async (id: string, teamId: string, image: Express.Multer.File | undefined, data: Partial): Promise => { const updated = await this.statusPagesRepository.updateById(id, teamId, image, this.normalizeInput(data)); return this.normalizeTheme(updated); }; deleteStatusPage = async (statusPageId: string, teamId: string): Promise => { return await this.statusPagesRepository.deleteById(statusPageId, teamId); }; }