feat(status-page): add theme and themeMode fields (backend)

## Changes
- Add `theme` (refined | modern | bold | editorial, default "refined") and
  `themeMode` (auto | light | dark, default "auto") fields to the StatusPage
  Mongoose model and the shared `StatusPage` type.
- Extend the create/update Zod validator to accept both fields optionally.
- Map both fields in the repository's `toEntity` so GET responses include them.
- Document both fields in the create and update endpoints in `openapi.json`.
- Add `STATUS_PAGE_THEMES_ENABLED` env var (default `true`) wired through
  `SettingsService` for ops-level rollback. When disabled, the controller
  strips theme fields on write and forces defaults on read.

## Notes
- No migration needed; Mongoose applies defaults for legacy documents.
- Controller short-circuits to zero-cost when the feature flag is on.
- `DEFAULT_STATUS_PAGE_THEME` / `DEFAULT_STATUS_PAGE_THEME_MODE` constants
  exported so the frontend PR can consume the same values.
This commit is contained in:
gorkem-bwl committed 2026-04-23 22:45:50 -04:00
1 parent 6ed0818bad
commit ad964446de
11 files changed
+408 -8

No files matched your search

+30 -6
View File
@@ -12,6 +12,7 @@ import { IStatusPageService } from "@/service/business/statusPageService.js";
import { IMonitorsRepository } from "@/repositories/index.js";
import { ISettingsService } from "@/service/system/settingsService.js";
import { NormalizeData } from "@/utils/dataUtils.js";
import { DEFAULT_STATUS_PAGE_THEME, DEFAULT_STATUS_PAGE_THEME_MODE, type StatusPage } from "@/types/statusPage.js";
const SERVICE_NAME = "statusPageController";
@@ -39,6 +40,19 @@ class StatusPageController implements IStatusPageController {
return StatusPageController.SERVICE_NAME;
}
private withoutThemeFields = (body: Record<string, unknown>): Record<string, unknown> => {
const rest = { ...body };
delete rest.theme;
delete rest.themeMode;
return rest;
};
private applyDefaultTheme = (statusPage: StatusPage): StatusPage => ({
...statusPage,
theme: DEFAULT_STATUS_PAGE_THEME,
themeMode: DEFAULT_STATUS_PAGE_THEME_MODE,
});
createStatusPage = async (req: Request, res: Response, next: NextFunction) => {
try {
createStatusPageBodyValidation.parse(req.body);
@@ -46,14 +60,16 @@ class StatusPageController implements IStatusPageController {
imageValidation.parse(req.file);
}
const themesEnabled = this.settingsService.areStatusPageThemesEnabled();
const body = themesEnabled ? req.body : this.withoutThemeFields(req.body);
const teamId = requireTeamId(req?.user?.teamId);
const userId = requireUserId(req?.user?.id);
const statusPage = await this.statusPageService.createStatusPage(userId, teamId, req.file, req.body);
const statusPage = await this.statusPageService.createStatusPage(userId, teamId, req.file, body);
return res.status(200).json({
success: true,
msg: "Status page created successfully",
data: statusPage,
data: themesEnabled ? statusPage : this.applyDefaultTheme(statusPage),
});
} catch (error) {
next(error);
@@ -66,19 +82,22 @@ class StatusPageController implements IStatusPageController {
if (req.file) {
imageValidation.parse(req.file);
}
const themesEnabled = this.settingsService.areStatusPageThemesEnabled();
const body = themesEnabled ? req.body : this.withoutThemeFields(req.body);
const teamId = requireTeamId(req?.user?.teamId);
const statusPageId = req.params.id as string;
if (!statusPageId) {
throw new AppError({ message: "Status page ID is required", status: 400 });
}
const statusPage = await this.statusPageService.updateStatusPage(statusPageId, teamId, req.file, req.body);
const statusPage = await this.statusPageService.updateStatusPage(statusPageId, teamId, req.file, body);
if (statusPage === null) {
throw new AppError({ message: "Status page not found", status: 404 });
}
res.status(200).json({
success: true,
msg: "Status page updated successfully",
data: statusPage,
data: themesEnabled ? statusPage : this.applyDefaultTheme(statusPage),
});
} catch (error) {
next(error);
@@ -124,10 +143,14 @@ class StatusPageController implements IStatusPageController {
}
return { ...monitor, checks: normalizedChecks };
});
const themesEnabled = this.settingsService.areStatusPageThemesEnabled();
return res.status(200).json({
success: true,
msg: "Status page retrieved successfully",
data: { statusPage, monitors: normalizedMonitors },
data: {
statusPage: themesEnabled ? statusPage : this.applyDefaultTheme(statusPage),
monitors: normalizedMonitors,
},
});
} catch (error) {
next(error);
@@ -138,10 +161,11 @@ class StatusPageController implements IStatusPageController {
const teamId = requireTeamId(req.user?.teamId);
const statusPages = await this.statusPageService.getStatusPagesByTeamId(teamId);
const themesEnabled = this.settingsService.areStatusPageThemesEnabled();
return res.status(200).json({
success: true,
msg: "Status pages retrieved successfully",
data: statusPages,
data: themesEnabled ? statusPages : statusPages.map((sp) => this.applyDefaultTheme(sp)),
});
} catch (error) {
next(error);
+11 -1
View File
@@ -1,6 +1,6 @@
import { Schema, model, type Types } from "mongoose";
import type { StatusPage, StatusPageLogoDocument } from "@/types/statusPage.js";
import { StatusPageTypes } from "@/types/statusPage.js";
import { StatusPageTypes, StatusPageThemes, StatusPageThemeModes } from "@/types/statusPage.js";
type StatusPageDocumentBase = Omit<
StatusPage,
@@ -108,6 +108,16 @@ const StatusPageSchema = new Schema<StatusPageDocument>(
type: String,
default: "",
},
theme: {
type: String,
enum: StatusPageThemes,
default: "refined",
},
themeMode: {
type: String,
enum: StatusPageThemeModes,
default: "auto",
},
},
{ timestamps: true }
);
@@ -59,6 +59,8 @@ class MongoStatusPagesRepository implements IStatusPagesRepository {
showAdminLoginLink: doc.showAdminLoginLink,
showInfrastructure: doc.showInfrastructure,
customCSS: doc.customCSS,
theme: doc.theme,
themeMode: doc.themeMode,
createdAt: this.toDateString(doc.createdAt),
updatedAt: this.toDateString(doc.updatedAt),
};
@@ -13,12 +13,14 @@ export type EnvConfig = {
clientHost: string;
dbConnectionString: string;
dbType: DbType;
statusPageThemesEnabled: boolean;
};
export interface ISettingsService {
readonly serviceName: string;
loadSettings(): EnvConfig;
getSettings(): EnvConfig;
areStatusPageThemesEnabled(): boolean;
getDBSettings(): Promise<Settings>;
updateDbSettings(newSettings: SettingsUpdate): Promise<Settings>;
}
@@ -37,6 +39,7 @@ export class SettingsService implements ISettingsService {
clientHost: env.CLIENT_HOST,
dbConnectionString: env.DB_CONNECTION_STRING,
dbType: env.DB_TYPE,
statusPageThemesEnabled: env.STATUS_PAGE_THEMES_ENABLED,
};
}
@@ -56,6 +59,10 @@ export class SettingsService implements ISettingsService {
return this.settings;
}
areStatusPageThemesEnabled() {
return this.settings.statusPageThemesEnabled;
}
private getRepository(): ISettingsRepository {
if (!this.settingsRepository) {
throw new AppError({
+10
View File
@@ -1,6 +1,14 @@
export const StatusPageTypes = ["uptime", "infrastructure"] as const;
export type StatusPageType = (typeof StatusPageTypes)[number];
export const StatusPageThemes = ["refined", "modern", "bold", "editorial"] as const;
export type StatusPageTheme = (typeof StatusPageThemes)[number];
export const DEFAULT_STATUS_PAGE_THEME: StatusPageTheme = "refined";
export const StatusPageThemeModes = ["auto", "light", "dark"] as const;
export type StatusPageThemeMode = (typeof StatusPageThemeModes)[number];
export const DEFAULT_STATUS_PAGE_THEME_MODE: StatusPageThemeMode = "auto";
export interface StatusPageLogo {
data: string;
contentType: string;
@@ -30,6 +38,8 @@ export interface StatusPage {
showAdminLoginLink: boolean;
showInfrastructure: boolean;
customCSS: string;
theme: StatusPageTheme;
themeMode: StatusPageThemeMode;
createdAt: string;
updatedAt: string;
}
+4
View File
@@ -1,5 +1,6 @@
import { z } from "zod";
import { DbTypes } from "@/types/settings.js";
import { booleanCoercion } from "./shared.js";
const envSchema = z.object({
// Server Configuration
@@ -20,6 +21,9 @@ const envSchema = z.object({
// Optional
ORIGIN: z.string().optional(),
// Feature flags
STATUS_PAGE_THEMES_ENABLED: booleanCoercion.default(true),
});
export type ValidatedEnv = z.infer<typeof envSchema>;
@@ -1,6 +1,6 @@
import { z } from "zod";
import { booleanCoercion } from "./shared.js";
import { StatusPageTypes } from "@/types/statusPage.js";
import { StatusPageTypes, StatusPageThemes, StatusPageThemeModes } from "@/types/statusPage.js";
//****************************************
// Status Page Validations
@@ -33,6 +33,8 @@ export const createStatusPageBodyValidation = z
showAdminLoginLink: booleanCoercion.optional(),
showInfrastructure: booleanCoercion.optional(),
removeLogo: z.union([z.literal("true"), z.literal("false")]).optional(),
theme: z.enum(StatusPageThemes).optional(),
themeMode: z.enum(StatusPageThemeModes).optional(),
})
.strip();