import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { UserEntity } from '../../database/entities/user.entity'; import { SettingEntity } from '../../database/entities/setting.entity'; import { ThemeLoaderService } from '../../common/utils/theme-loader.service'; import { SETTINGS_KEYS } from '../../config/env.schema'; export interface BootstrapPayload { initialized: boolean; pageTitle: string; logo: string; welcomeMarkdown: string; theme: any; defaultChallengeIp: string; registrationsEnabled: boolean; } @Injectable() export class SystemService { constructor( @InjectRepository(UserEntity) private readonly users: Repository, @InjectRepository(SettingEntity) private readonly settings: Repository, private readonly themes: ThemeLoaderService, ) {} async bootstrap(): Promise { const adminCount = await this.users.count({ where: { role: 'admin' } }); const themeKey = await this.getSetting(SETTINGS_KEYS.THEME_KEY, 'classic'); return { initialized: adminCount > 0, pageTitle: await this.getSetting(SETTINGS_KEYS.PAGE_TITLE, 'HIPCTF'), logo: await this.getSetting(SETTINGS_KEYS.LOGO, ''), welcomeMarkdown: await this.getSetting(SETTINGS_KEYS.WELCOME_MARKDOWN, ''), theme: this.themes.getTheme(themeKey), defaultChallengeIp: await this.getSetting(SETTINGS_KEYS.DEFAULT_CHALLENGE_IP, '127.0.0.1'), registrationsEnabled: (await this.getSetting(SETTINGS_KEYS.REGISTRATIONS_ENABLED, 'false')) === 'true', }; } private async getSetting(key: string, fallback: string): Promise { const row = await this.settings.findOne({ where: { key } }); return row?.value ?? fallback; } }