feat: infra config key check added instead of count check

This commit is contained in:
mirarifhasan
2024-02-08 14:19:08 +06:00
committed by Andrew Bastin
parent 2d7fb8e23a
commit 01fd27f81a
2 changed files with 9 additions and 7 deletions

View File

@@ -163,18 +163,20 @@ export async function getDefaultInfraConfigs(): Promise<
export async function isInfraConfigTablePopulated(): Promise<boolean> {
const prisma = new PrismaService();
try {
const infraConfigCountInDB = await prisma.infraConfig.count();
const infraConfigCountShouldBe = getDefaultInfraConfigs().length;
const dbInfraConfigs = await prisma.infraConfig.findMany();
const infraConfigDefaultObjs = getDefaultInfraConfigs();
const isPopulated = infraConfigCountInDB === infraConfigCountShouldBe;
const propsRemainingToInsert = infraConfigDefaultObjs.filter(
(p) => !dbInfraConfigs.find((e) => e.name === p.name),
);
if (!isPopulated) {
if (propsRemainingToInsert.length > 0) {
console.log(
'Infra Config table is not populated with all entries. Populating now...',
);
}
return isPopulated;
return true;
} catch (error) {
return false;
}

View File

@@ -51,13 +51,13 @@ export class InfraConfigService implements OnModuleInit {
*/
async initializeInfraConfigTable() {
try {
// Get all the 'names' of the properties to be saved in the 'infra_config' table
// Get all the 'names' of the properties from ENUM to be saved in the 'infra_config' table
const enumValues = Object.values(InfraConfigEnum);
// Fetch the default values (value in .env) for configs to be saved in 'infra_config' table
const infraConfigDefaultObjs = await getDefaultInfraConfigs();
// Check if all the 'names' are listed in the default values
// Cross-check if all the 'names' are listed in the default-values-list and ENUM at the same time
if (enumValues.length !== infraConfigDefaultObjs.length) {
throw new Error(INFRA_CONFIG_NOT_LISTED);
}