From a079e0f04b02878c4f706a4773ab24a3355b71d9 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Fri, 19 Apr 2024 13:13:43 +0600 Subject: [PATCH] refactor: infra-config code refactor (#3982) * chore: add getMissingInfraConfigEntries to avoid code duplication * feat: isServiceConfigured modified * docs: add missing function doc * feat: argon2 version updated and removed types-argon2 depricated package * Revert "feat: argon2 version updated and removed types-argon2 depricated package" This reverts commit b99f3c5aaea0bec3701bd6f81c105b490f9aaa55. * Revert "feat: isServiceConfigured modified" This reverts commit eaa6f105bbed494de72cea472043b825bee2e206. --- .../src/infra-config/helper.ts | 26 ++++++++++++++----- .../src/infra-config/infra-config.service.ts | 17 ++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/packages/hoppscotch-backend/src/infra-config/helper.ts b/packages/hoppscotch-backend/src/infra-config/helper.ts index dcbae99dc..9238788a6 100644 --- a/packages/hoppscotch-backend/src/infra-config/helper.ts +++ b/packages/hoppscotch-backend/src/infra-config/helper.ts @@ -156,6 +156,25 @@ export async function getDefaultInfraConfigs(): Promise< return infraConfigDefaultObjs; } +/** + * Get the missing entries in the 'infra_config' table + * @returns Array of InfraConfig + */ +export async function getMissingInfraConfigEntries() { + const prisma = new PrismaService(); + const [dbInfraConfigs, infraConfigDefaultObjs] = await Promise.all([ + prisma.infraConfig.findMany(), + getDefaultInfraConfigs(), + ]); + + const missingEntries = infraConfigDefaultObjs.filter( + (config) => + !dbInfraConfigs.some((dbConfig) => dbConfig.name === config.name), + ); + + return missingEntries; +} + /** * Verify if 'infra_config' table is loaded with all entries * @returns boolean @@ -163,12 +182,7 @@ export async function getDefaultInfraConfigs(): Promise< export async function isInfraConfigTablePopulated(): Promise { const prisma = new PrismaService(); try { - const dbInfraConfigs = await prisma.infraConfig.findMany(); - const infraConfigDefaultObjs = await getDefaultInfraConfigs(); - - const propsRemainingToInsert = infraConfigDefaultObjs.filter( - (p) => !dbInfraConfigs.find((e) => e.name === p.name), - ); + const propsRemainingToInsert = await getMissingInfraConfigEntries(); if (propsRemainingToInsert.length > 0) { console.log( diff --git a/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts b/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts index 9b2aa8c7d..a6e5f8186 100644 --- a/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts +++ b/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts @@ -21,7 +21,12 @@ import { validateUrl, } from 'src/utils'; import { ConfigService } from '@nestjs/config'; -import { ServiceStatus, getDefaultInfraConfigs, stopApp } from './helper'; +import { + ServiceStatus, + getDefaultInfraConfigs, + getMissingInfraConfigEntries, + stopApp, +} from './helper'; import { EnableAndDisableSSOArgs, InfraConfigArgs } from './input-args'; import { AuthProvider } from 'src/auth/helper'; @@ -56,14 +61,7 @@ export class InfraConfigService implements OnModuleInit { */ async initializeInfraConfigTable() { try { - // Fetch the default values (value in .env) for configs to be saved in 'infra_config' table - const infraConfigDefaultObjs = await getDefaultInfraConfigs(); - - // Eliminate the rows (from 'infraConfigDefaultObjs') that are already present in the database table - const dbInfraConfigs = await this.prisma.infraConfig.findMany(); - const propsToInsert = infraConfigDefaultObjs.filter( - (p) => !dbInfraConfigs.find((e) => e.name === p.name), - ); + const propsToInsert = await getMissingInfraConfigEntries(); if (propsToInsert.length > 0) { await this.prisma.infraConfig.createMany({ data: propsToInsert }); @@ -285,6 +283,7 @@ export class InfraConfigService implements OnModuleInit { /** * Get InfraConfigs by names * @param names Names of the InfraConfigs + * @param checkDisallowedKeys If true, check if the names are allowed to fetch by client * @returns InfraConfig model */ async getMany(names: InfraConfigEnum[], checkDisallowedKeys: boolean = true) {