feat: theme config service — REST API with versioning + backup

- ThemeService: read/write/validate theme.json, auto-backup on save
- ThemeController: GET/PUT/POST /api/config/theme (public GET, versioned PUT)
- ThemeConfig type with version + updatedAt fields
- Default theme: Global Hospital blue scale
- ConfigThemeModule registered in AppModule

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-02 15:50:51 +05:30
parent f231f6fd73
commit 8cc1bdc812
15 changed files with 828 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ import { RecordingsModule } from './recordings/recordings.module';
import { EventsModule } from './events/events.module';
import { CallerResolutionModule } from './caller/caller-resolution.module';
import { RulesEngineModule } from './rules-engine/rules-engine.module';
import { ConfigThemeModule } from './config/config-theme.module';
@Module({
imports: [
@@ -42,6 +43,7 @@ import { RulesEngineModule } from './rules-engine/rules-engine.module';
EventsModule,
CallerResolutionModule,
RulesEngineModule,
ConfigThemeModule,
],
})
export class AppModule {}

View File

@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ThemeController } from './theme.controller';
import { ThemeService } from './theme.service';
@Module({
controllers: [ThemeController],
providers: [ThemeService],
exports: [ThemeService],
})
export class ConfigThemeModule {}

View File

@@ -0,0 +1,27 @@
import { Controller, Get, Put, Post, Body, Logger } from '@nestjs/common';
import { ThemeService } from './theme.service';
import type { ThemeConfig } from './theme.defaults';
@Controller('api/config')
export class ThemeController {
private readonly logger = new Logger(ThemeController.name);
constructor(private readonly theme: ThemeService) {}
@Get('theme')
getTheme() {
return this.theme.getTheme();
}
@Put('theme')
updateTheme(@Body() body: Partial<ThemeConfig>) {
this.logger.log('Theme update request');
return this.theme.updateTheme(body);
}
@Post('theme/reset')
resetTheme() {
this.logger.log('Theme reset request');
return this.theme.resetTheme();
}
}

View File

@@ -0,0 +1,79 @@
export type ThemeConfig = {
version?: number;
updatedAt?: string;
brand: {
name: string;
hospitalName: string;
logo: string;
favicon: string;
};
colors: {
brand: Record<string, string>;
};
typography: {
body: string;
display: string;
};
login: {
title: string;
subtitle: string;
showGoogleSignIn: boolean;
showForgotPassword: boolean;
poweredBy: { label: string; url: string };
};
sidebar: {
title: string;
subtitle: string;
};
ai: {
quickActions: Array<{ label: string; prompt: string }>;
};
};
export const DEFAULT_THEME: ThemeConfig = {
brand: {
name: 'Helix Engage',
hospitalName: 'Global Hospital',
logo: '/helix-logo.png',
favicon: '/favicon.ico',
},
colors: {
brand: {
'25': 'rgb(239 246 255)',
'50': 'rgb(219 234 254)',
'100': 'rgb(191 219 254)',
'200': 'rgb(147 197 253)',
'300': 'rgb(96 165 250)',
'400': 'rgb(59 130 246)',
'500': 'rgb(37 99 235)',
'600': 'rgb(29 78 216)',
'700': 'rgb(30 64 175)',
'800': 'rgb(30 58 138)',
'900': 'rgb(23 37 84)',
'950': 'rgb(15 23 42)',
},
},
typography: {
body: "'Satoshi', var(--font-inter, 'Inter'), -apple-system, 'Segoe UI', Roboto, Arial, sans-serif",
display: "'General Sans', var(--font-inter, 'Inter'), -apple-system, 'Segoe UI', Roboto, Arial, sans-serif",
},
login: {
title: 'Sign in to Helix Engage',
subtitle: 'Global Hospital',
showGoogleSignIn: true,
showForgotPassword: true,
poweredBy: { label: 'Powered by F0rty2.ai', url: 'https://f0rty2.ai' },
},
sidebar: {
title: 'Helix Engage',
subtitle: 'Global Hospital \u00b7 {role}',
},
ai: {
quickActions: [
{ label: 'Doctor availability', prompt: 'What doctors are available and what are their visiting hours?' },
{ label: 'Clinic timings', prompt: 'What are the clinic locations and timings?' },
{ label: 'Patient history', prompt: "Can you summarize this patient's history?" },
{ label: 'Treatment packages', prompt: 'What treatment packages are available?' },
],
},
};

View File

@@ -0,0 +1,98 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs';
import { join, dirname } from 'path';
import { DEFAULT_THEME, type ThemeConfig } from './theme.defaults';
const THEME_PATH = join(process.cwd(), 'data', 'theme.json');
const BACKUP_DIR = join(process.cwd(), 'data', 'theme-backups');
@Injectable()
export class ThemeService implements OnModuleInit {
private readonly logger = new Logger(ThemeService.name);
private cached: ThemeConfig | null = null;
onModuleInit() {
this.load();
}
getTheme(): ThemeConfig {
if (this.cached) return this.cached;
return this.load();
}
updateTheme(updates: Partial<ThemeConfig>): ThemeConfig {
const current = this.getTheme();
const merged: ThemeConfig = {
brand: { ...current.brand, ...updates.brand },
colors: {
brand: { ...current.colors.brand, ...updates.colors?.brand },
},
typography: { ...current.typography, ...updates.typography },
login: { ...current.login, ...updates.login, poweredBy: { ...current.login.poweredBy, ...updates.login?.poweredBy } },
sidebar: { ...current.sidebar, ...updates.sidebar },
ai: {
quickActions: updates.ai?.quickActions ?? current.ai.quickActions,
},
};
merged.version = (current.version ?? 0) + 1;
merged.updatedAt = new Date().toISOString();
this.backup();
const dir = dirname(THEME_PATH);
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
writeFileSync(THEME_PATH, JSON.stringify(merged, null, 2), 'utf8');
this.cached = merged;
this.logger.log(`Theme updated to v${merged.version}`);
return merged;
}
resetTheme(): ThemeConfig {
this.backup();
const dir = dirname(THEME_PATH);
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
writeFileSync(THEME_PATH, JSON.stringify(DEFAULT_THEME, null, 2), 'utf8');
this.cached = DEFAULT_THEME;
this.logger.log('Theme reset to defaults');
return DEFAULT_THEME;
}
private load(): ThemeConfig {
try {
if (existsSync(THEME_PATH)) {
const raw = readFileSync(THEME_PATH, 'utf8');
const parsed = JSON.parse(raw);
this.cached = {
brand: { ...DEFAULT_THEME.brand, ...parsed.brand },
colors: { brand: { ...DEFAULT_THEME.colors.brand, ...parsed.colors?.brand } },
typography: { ...DEFAULT_THEME.typography, ...parsed.typography },
login: { ...DEFAULT_THEME.login, ...parsed.login, poweredBy: { ...DEFAULT_THEME.login.poweredBy, ...parsed.login?.poweredBy } },
sidebar: { ...DEFAULT_THEME.sidebar, ...parsed.sidebar },
ai: { quickActions: parsed.ai?.quickActions ?? DEFAULT_THEME.ai.quickActions },
};
this.logger.log('Theme loaded from file');
return this.cached;
}
} catch (err) {
this.logger.warn(`Failed to load theme: ${err}`);
}
this.cached = DEFAULT_THEME;
this.logger.log('Using default theme');
return DEFAULT_THEME;
}
private backup() {
try {
if (!existsSync(THEME_PATH)) return;
if (!existsSync(BACKUP_DIR)) mkdirSync(BACKUP_DIR, { recursive: true });
const ts = new Date().toISOString().replace(/[:.]/g, '-');
copyFileSync(THEME_PATH, join(BACKUP_DIR, `theme-${ts}.json`));
} catch (err) {
this.logger.warn(`Backup failed: ${err}`);
}
}
}