import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; const POLICY_KEYS = ['privacy', 'terms'] as const; type PolicyKey = typeof POLICY_KEYS[number]; @Injectable() export class SettingsService { constructor(private prisma: PrismaService) {} async getGlobal() { const setting = await this.prisma.settings.findUnique({ where: { key: 'global' } }); return setting?.value; } async updateGlobal(value: Record) { return this.prisma.settings.upsert({ where: { key: 'global' }, create: { key: 'global', value }, update: { value }, }); } async getPolicy(key: string): Promise { const setting = await this.prisma.settings.findUnique({ where: { key: `policy_${key}` } }); return (setting?.value as any)?.content ?? null; } async updatePolicy(key: PolicyKey, content: string) { return this.prisma.settings.upsert({ where: { key: `policy_${key}` }, create: { key: `policy_${key}`, value: { content } }, update: { value: { content } }, }); } async getPolicies() { const [privacy, terms] = await Promise.all([ this.getPolicy('privacy'), this.getPolicy('terms'), ]); return { privacy, terms }; } async getMapsKey(): Promise { const setting = await this.prisma.settings.findUnique({ where: { key: 'maps_config' } }); return (setting?.value as any)?.api_key ?? null; } async saveMapsKey(api_key: string) { return this.prisma.settings.upsert({ where: { key: 'maps_config' }, create: { key: 'maps_config', value: { api_key } }, update: { value: { api_key } }, }); } }