import { Injectable, OnModuleInit } from '@nestjs/common' import { PrismaService } from 'src/Prisma/prisma.service' import { ConfigConstants } from './appconfig.constant' import { Config } from '@prisma/client' @Injectable() export class AppConfigService implements OnModuleInit { constructor(private readonly prisma: PrismaService) {} async onModuleInit() { // get each config from database, if not exist, create default config const configs = await this.prisma.config.findMany() if (configs.length === 0) { Object.entries(ConfigConstants).forEach(async ([_key, value]) => { await this.prisma.config.create({ data: { name: value.name, key: value.key, value: value.value, visible: value.visible, }, }) }) } } async getConfig(key: string) { return await this.prisma.config.findUnique({ where: { key }, }) } async getVisibleConfigs() { return await this.prisma.config.findMany({ where: { visible: true }, }) } async updateConfig(key: string, value: string) { return await this.prisma.config.update({ where: { key }, data: { value }, }) } async updateVisibleConfigs(configs: Config[]) { return await this.prisma.config.updateMany({ data: configs, }) } async deleteConfig(key: string) { return await this.prisma.config.delete({ where: { key }, }) } }