implement platform config
This commit is contained in:
46
src/AppConfig/appconfig.constant.ts
Normal file
46
src/AppConfig/appconfig.constant.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export const ConfigConstants: Record<
|
||||
string,
|
||||
{
|
||||
name: string
|
||||
key: string
|
||||
value: string
|
||||
visible: boolean
|
||||
}
|
||||
> = {
|
||||
SLOT_DURATION: {
|
||||
name: 'Slot Duration',
|
||||
key: 'SLOT_DURATION',
|
||||
value: '60',
|
||||
visible: true,
|
||||
},
|
||||
SLOT_BREAK_DURATION: {
|
||||
name: 'Slot Break Duration',
|
||||
key: 'SLOT_BREAK_DURATION',
|
||||
value: '15',
|
||||
visible: true,
|
||||
},
|
||||
MID_DAY_BREAK_TIME_START: {
|
||||
name: 'Mid Day Break Time Start',
|
||||
key: 'MID_DAY_BREAK_TIME_START',
|
||||
value: new Date(new Date().setHours(12, 0, 0, 0)).toISOString(),
|
||||
visible: true,
|
||||
},
|
||||
MID_DAY_BREAK_TIME_END: {
|
||||
name: 'Mid Day Break Time End',
|
||||
key: 'MID_DAY_BREAK_TIME_END',
|
||||
value: new Date(new Date().setHours(13, 0, 0, 0)).toISOString(),
|
||||
visible: true,
|
||||
},
|
||||
SLOT_START_TIME: {
|
||||
name: 'Slot Start Time',
|
||||
key: 'SLOT_START_TIME',
|
||||
value: new Date(new Date().setHours(8, 0, 0, 0)).toISOString(),
|
||||
visible: true,
|
||||
},
|
||||
SLOT_END_TIME: {
|
||||
name: 'Slot End Time',
|
||||
key: 'SLOT_END_TIME',
|
||||
value: new Date(new Date().setHours(22, 0, 0, 0)).toISOString(),
|
||||
visible: true,
|
||||
},
|
||||
}
|
||||
@@ -49,7 +49,7 @@ export class AppConfigSchema extends PothosSchema {
|
||||
type: [this.appConfig()],
|
||||
description: 'Get all app configs',
|
||||
args: this.builder.generator.findManyArgs('Config'),
|
||||
resolve: async (query, root, args) => {
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.config.findMany({
|
||||
...query,
|
||||
where: args.filter ?? undefined,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common'
|
||||
|
||||
// import { ConfigConstant } from 'src/common/constant/config.constant';
|
||||
import { PrismaService } from 'src/Prisma/prisma.service'
|
||||
import { ConfigConstants } from './appconfig.constant'
|
||||
import { Config } from '@prisma/client'
|
||||
|
||||
@Injectable()
|
||||
export class AppConfigService implements OnModuleInit {
|
||||
@@ -9,21 +10,49 @@ export class AppConfigService implements OnModuleInit {
|
||||
|
||||
async onModuleInit() {
|
||||
// get each config from database, if not exist, create default config
|
||||
// const configs = await this.prisma.config.findMany();
|
||||
// if (configs.length === 0) {
|
||||
// await this.prisma.config.createMany({
|
||||
// data: Object.values(ConfigConstant).map((config) => ({
|
||||
// ...config,
|
||||
// })),
|
||||
// });
|
||||
// }
|
||||
// // regenerate missing config
|
||||
// for (const config of Object.values(ConfigConstant)) {
|
||||
// if (!configs.find((c) => c.key === config.key)) {
|
||||
// await this.prisma.config.create({
|
||||
// data: 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 },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user