Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

refactor: infra-config code refactor (#3982)

Mir Arif Hasana079e0f04b02
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<boolean> {
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) {