2022-11-28 17:50:36 +01:00
|
|
|
import {
|
|
|
|
BadRequestException,
|
|
|
|
Inject,
|
|
|
|
Injectable,
|
|
|
|
NotFoundException,
|
|
|
|
} from "@nestjs/common";
|
2022-11-28 15:04:32 +01:00
|
|
|
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`);
|
|
|
|
|
2022-12-01 23:07:49 +01:00
|
|
|
if (configVariable.type == "number") return parseInt(configVariable.value);
|
|
|
|
if (configVariable.type == "boolean") return configVariable.value == "true";
|
|
|
|
if (configVariable.type == "string") return configVariable.value;
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async listForAdmin() {
|
2022-12-01 23:07:49 +01:00
|
|
|
return await this.prisma.config.findMany({
|
|
|
|
where: { locked: { equals: false } },
|
|
|
|
});
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async list() {
|
2022-12-01 23:07:49 +01:00
|
|
|
return await this.prisma.config.findMany({
|
2022-11-28 15:04:32 +01:00
|
|
|
where: { secret: { equals: false } },
|
|
|
|
});
|
|
|
|
}
|
2022-11-28 17:50:36 +01:00
|
|
|
|
|
|
|
async update(key: string, value: string | number | boolean) {
|
|
|
|
const configVariable = await this.prisma.config.findUnique({
|
|
|
|
where: { key },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!configVariable || configVariable.locked)
|
|
|
|
throw new NotFoundException("Config variable not found");
|
|
|
|
|
|
|
|
if (typeof value != configVariable.type)
|
|
|
|
throw new BadRequestException(
|
|
|
|
`Config variable must be of type ${configVariable.type}`
|
|
|
|
);
|
|
|
|
|
2022-12-01 23:07:49 +01:00
|
|
|
const updatedVariable = await this.prisma.config.update({
|
2022-11-28 17:50:36 +01:00
|
|
|
where: { key },
|
|
|
|
data: { value: value.toString() },
|
|
|
|
});
|
2022-12-01 23:07:49 +01:00
|
|
|
|
|
|
|
this.configVariables = await this.prisma.config.findMany();
|
|
|
|
|
|
|
|
return updatedVariable;
|
|
|
|
}
|
|
|
|
|
|
|
|
async finishSetup() {
|
|
|
|
return await this.prisma.config.update({
|
|
|
|
where: { key: "setupFinished" },
|
|
|
|
data: { value: "true" },
|
|
|
|
});
|
2022-11-28 17:50:36 +01:00
|
|
|
}
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|