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[],
|
2023-08-17 19:54:26 +07:00
|
|
|
private prisma: PrismaService,
|
2022-11-28 15:04:32 +01:00
|
|
|
) {}
|
|
|
|
|
|
2023-03-04 23:29:00 +01:00
|
|
|
get(key: `${string}.${string}`): any {
|
2022-11-28 15:04:32 +01:00
|
|
|
const configVariable = this.configVariables.filter(
|
2023-08-17 19:54:26 +07:00
|
|
|
(variable) => `${variable.category}.${variable.name}` == key,
|
2022-11-28 15:04:32 +01:00
|
|
|
)[0];
|
|
|
|
|
|
|
|
|
|
if (!configVariable) throw new Error(`Config variable ${key} not found`);
|
|
|
|
|
|
2023-03-23 14:31:21 +07:00
|
|
|
const value = configVariable.value ?? configVariable.defaultValue;
|
|
|
|
|
|
|
|
|
|
if (configVariable.type == "number") return parseInt(value);
|
|
|
|
|
if (configVariable.type == "boolean") return value == "true";
|
2022-12-15 21:44:04 +01:00
|
|
|
if (configVariable.type == "string" || configVariable.type == "text")
|
2023-03-23 14:31:21 +07:00
|
|
|
return value;
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-04 23:29:00 +01:00
|
|
|
async getByCategory(category: string) {
|
|
|
|
|
const configVariables = await this.prisma.config.findMany({
|
2023-01-26 14:06:25 +01:00
|
|
|
orderBy: { order: "asc" },
|
2023-03-04 23:29:00 +01:00
|
|
|
where: { category, locked: { equals: false } },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return configVariables.map((variable) => {
|
|
|
|
|
return {
|
|
|
|
|
...variable,
|
2023-03-23 14:31:21 +07:00
|
|
|
key: `${variable.category}.${variable.name}`,
|
|
|
|
|
value: variable.value ?? variable.defaultValue,
|
2023-03-04 23:29:00 +01:00
|
|
|
};
|
2022-12-01 23:07:49 +01:00
|
|
|
});
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async list() {
|
2023-03-04 23:29:00 +01:00
|
|
|
const configVariables = await this.prisma.config.findMany({
|
2022-11-28 15:04:32 +01:00
|
|
|
where: { secret: { equals: false } },
|
|
|
|
|
});
|
2023-03-04 23:29:00 +01:00
|
|
|
|
|
|
|
|
return configVariables.map((variable) => {
|
|
|
|
|
return {
|
|
|
|
|
...variable,
|
2023-03-23 14:31:21 +07:00
|
|
|
key: `${variable.category}.${variable.name}`,
|
|
|
|
|
value: variable.value ?? variable.defaultValue,
|
2023-03-04 23:29:00 +01:00
|
|
|
};
|
|
|
|
|
});
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|
2022-11-28 17:50:36 +01:00
|
|
|
|
2022-12-30 14:40:23 +01:00
|
|
|
async updateMany(data: { key: string; value: string | number | boolean }[]) {
|
2023-03-04 23:29:00 +01:00
|
|
|
const response: Config[] = [];
|
|
|
|
|
|
2022-12-30 14:40:23 +01:00
|
|
|
for (const variable of data) {
|
2023-03-04 23:29:00 +01:00
|
|
|
response.push(await this.update(variable.key, variable.value));
|
2022-12-30 14:40:23 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-04 23:29:00 +01:00
|
|
|
return response;
|
2022-12-30 14:40:23 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 17:50:36 +01:00
|
|
|
async update(key: string, value: string | number | boolean) {
|
|
|
|
|
const configVariable = await this.prisma.config.findUnique({
|
2023-03-04 23:29:00 +01:00
|
|
|
where: {
|
|
|
|
|
name_category: {
|
|
|
|
|
category: key.split(".")[0],
|
|
|
|
|
name: key.split(".")[1],
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-11-28 17:50:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!configVariable || configVariable.locked)
|
|
|
|
|
throw new NotFoundException("Config variable not found");
|
|
|
|
|
|
2023-04-07 23:13:44 +02:00
|
|
|
if (value === "") {
|
2023-03-23 14:31:21 +07:00
|
|
|
value = null;
|
|
|
|
|
} else if (
|
2022-12-15 21:44:04 +01:00
|
|
|
typeof value != configVariable.type &&
|
|
|
|
|
typeof value == "string" &&
|
|
|
|
|
configVariable.type != "text"
|
|
|
|
|
) {
|
2022-11-28 17:50:36 +01:00
|
|
|
throw new BadRequestException(
|
2023-08-17 19:54:26 +07:00
|
|
|
`Config variable must be of type ${configVariable.type}`,
|
2022-11-28 17:50:36 +01:00
|
|
|
);
|
2022-12-15 21:44:04 +01:00
|
|
|
}
|
2022-11-28 17:50:36 +01:00
|
|
|
|
2022-12-01 23:07:49 +01:00
|
|
|
const updatedVariable = await this.prisma.config.update({
|
2023-03-04 23:29:00 +01:00
|
|
|
where: {
|
|
|
|
|
name_category: {
|
|
|
|
|
category: key.split(".")[0],
|
|
|
|
|
name: key.split(".")[1],
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-04-07 23:13:44 +02:00
|
|
|
data: { value: value === null ? null : value.toString() },
|
2022-11-28 17:50:36 +01:00
|
|
|
});
|
2022-12-01 23:07:49 +01:00
|
|
|
|
|
|
|
|
this.configVariables = await this.prisma.config.findMany();
|
|
|
|
|
|
|
|
|
|
return updatedVariable;
|
|
|
|
|
}
|
2022-11-28 15:04:32 +01:00
|
|
|
}
|