From 6517c444e8cd19b0b8802d43525cd24bd89d8014 Mon Sep 17 00:00:00 2001 From: mirarifhasan Date: Wed, 17 Apr 2024 17:47:17 +0600 Subject: [PATCH] chore: add getMissingInfraConfigEntries to avoid code duplication --- .../src/infra-config/helper.ts | 26 ++++++++++++++----- .../src/infra-config/infra-config.service.ts | 16 +++++------- 2 files changed, 27 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..4e2718e03 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 });