chore: add getMissingInfraConfigEntries to avoid code duplication
This commit is contained in:
@@ -156,6 +156,25 @@ export async function getDefaultInfraConfigs(): Promise<
|
|||||||
return infraConfigDefaultObjs;
|
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
|
* Verify if 'infra_config' table is loaded with all entries
|
||||||
* @returns boolean
|
* @returns boolean
|
||||||
@@ -163,12 +182,7 @@ export async function getDefaultInfraConfigs(): Promise<
|
|||||||
export async function isInfraConfigTablePopulated(): Promise<boolean> {
|
export async function isInfraConfigTablePopulated(): Promise<boolean> {
|
||||||
const prisma = new PrismaService();
|
const prisma = new PrismaService();
|
||||||
try {
|
try {
|
||||||
const dbInfraConfigs = await prisma.infraConfig.findMany();
|
const propsRemainingToInsert = await getMissingInfraConfigEntries();
|
||||||
const infraConfigDefaultObjs = await getDefaultInfraConfigs();
|
|
||||||
|
|
||||||
const propsRemainingToInsert = infraConfigDefaultObjs.filter(
|
|
||||||
(p) => !dbInfraConfigs.find((e) => e.name === p.name),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (propsRemainingToInsert.length > 0) {
|
if (propsRemainingToInsert.length > 0) {
|
||||||
console.log(
|
console.log(
|
||||||
|
|||||||
@@ -21,7 +21,12 @@ import {
|
|||||||
validateUrl,
|
validateUrl,
|
||||||
} from 'src/utils';
|
} from 'src/utils';
|
||||||
import { ConfigService } from '@nestjs/config';
|
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 { EnableAndDisableSSOArgs, InfraConfigArgs } from './input-args';
|
||||||
import { AuthProvider } from 'src/auth/helper';
|
import { AuthProvider } from 'src/auth/helper';
|
||||||
|
|
||||||
@@ -56,14 +61,7 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
*/
|
*/
|
||||||
async initializeInfraConfigTable() {
|
async initializeInfraConfigTable() {
|
||||||
try {
|
try {
|
||||||
// Fetch the default values (value in .env) for configs to be saved in 'infra_config' table
|
const propsToInsert = await getMissingInfraConfigEntries();
|
||||||
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),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (propsToInsert.length > 0) {
|
if (propsToInsert.length > 0) {
|
||||||
await this.prisma.infraConfig.createMany({ data: propsToInsert });
|
await this.prisma.infraConfig.createMany({ data: propsToInsert });
|
||||||
|
|||||||
Reference in New Issue
Block a user