feat: add new config strategy to backend
This commit is contained in:
18
backend/src/config/config.controller.ts
Normal file
18
backend/src/config/config.controller.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import { ConfigService } from "./config.service";
|
||||
import { ConfigDTO } from "./dto/config.dto";
|
||||
|
||||
@Controller("configs")
|
||||
export class ConfigController {
|
||||
constructor(private configService: ConfigService) {}
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
return new ConfigDTO().fromList(await this.configService.list())
|
||||
}
|
||||
|
||||
@Get("admin")
|
||||
async listForAdmin() {
|
||||
return await this.configService.listForAdmin();
|
||||
}
|
||||
}
|
||||
21
backend/src/config/config.module.ts
Normal file
21
backend/src/config/config.module.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Global, Module } from "@nestjs/common";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
import { ConfigController } from "./config.controller";
|
||||
import { ConfigService } from "./config.service";
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [
|
||||
{
|
||||
provide: "CONFIG_VARIABLES",
|
||||
useFactory: async (prisma: PrismaService) => {
|
||||
return await prisma.config.findMany();
|
||||
},
|
||||
inject: [PrismaService],
|
||||
},
|
||||
ConfigService,
|
||||
],
|
||||
controllers: [ConfigController],
|
||||
exports: [ConfigService],
|
||||
})
|
||||
export class ConfigModule {}
|
||||
41
backend/src/config/config.service.ts
Normal file
41
backend/src/config/config.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
import { Config } from "@prisma/client";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
|
||||
@Injectable()
|
||||
export class ConfigService {
|
||||
constructor(
|
||||
@Inject("CONFIG_VARIABLES") private configVariables: Config[],
|
||||
private prisma: PrismaService
|
||||
) {}
|
||||
|
||||
get(key: string): any {
|
||||
const configVariable = this.configVariables.filter(
|
||||
(variable) => variable.key == key
|
||||
)[0];
|
||||
|
||||
if (!configVariable) throw new Error(`Config variable ${key} not found`);
|
||||
|
||||
const value = configVariable.value ?? configVariable.default;
|
||||
|
||||
if (configVariable.type == "number") return parseInt(value);
|
||||
if (configVariable.type == "boolean") return value == "true";
|
||||
if (configVariable.type == "string") return value;
|
||||
}
|
||||
|
||||
async listForAdmin() {
|
||||
return await this.prisma.config.findMany();
|
||||
}
|
||||
|
||||
async list() {
|
||||
const configVariables = await this.prisma.config.findMany({
|
||||
where: { secret: { equals: false } },
|
||||
});
|
||||
|
||||
return configVariables.map((configVariable) => {
|
||||
if (!configVariable.value) configVariable.value = configVariable.default;
|
||||
|
||||
return configVariable;
|
||||
});
|
||||
}
|
||||
}
|
||||
18
backend/src/config/dto/config.dto.ts
Normal file
18
backend/src/config/dto/config.dto.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Expose, plainToClass } from "class-transformer";
|
||||
|
||||
export class ConfigDTO {
|
||||
@Expose()
|
||||
key: string;
|
||||
|
||||
@Expose()
|
||||
value: string;
|
||||
|
||||
@Expose()
|
||||
type: string;
|
||||
|
||||
fromList(partial: Partial<ConfigDTO>[]) {
|
||||
return partial.map((part) =>
|
||||
plainToClass(ConfigDTO, part, { excludeExtraneousValues: true })
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user