feat: remove InfraConfigEnumForClient enum and add exclude const variable

This commit is contained in:
mirarifhasan
2024-03-06 14:24:39 +06:00
committed by Andrew Bastin
parent f0adc5b2e4
commit f92c70e1ff
7 changed files with 68 additions and 71 deletions

View File

@@ -32,7 +32,7 @@ import {
EnableAndDisableSSOArgs, EnableAndDisableSSOArgs,
InfraConfigArgs, InfraConfigArgs,
} from 'src/infra-config/input-args'; } from 'src/infra-config/input-args';
import { InfraConfigEnumForClient } from 'src/types/InfraConfig'; import { InfraConfigEnum } from 'src/types/InfraConfig';
import { ServiceStatus } from 'src/infra-config/helper'; import { ServiceStatus } from 'src/infra-config/helper';
@UseGuards(GqlThrottlerGuard) @UseGuards(GqlThrottlerGuard)
@@ -274,10 +274,10 @@ export class InfraResolver {
async infraConfigs( async infraConfigs(
@Args({ @Args({
name: 'configNames', name: 'configNames',
type: () => [InfraConfigEnumForClient], type: () => [InfraConfigEnum],
description: 'Configs to fetch', description: 'Configs to fetch',
}) })
names: InfraConfigEnumForClient[], names: InfraConfigEnum[],
) { ) {
const infraConfigs = await this.infraConfigService.getMany(names); const infraConfigs = await this.infraConfigService.getMany(names);
if (E.isLeft(infraConfigs)) throwErr(infraConfigs.left); if (E.isLeft(infraConfigs)) throwErr(infraConfigs.left);

View File

@@ -731,6 +731,13 @@ export const INFRA_CONFIG_INVALID_INPUT = 'infra_config/invalid_input' as const;
export const INFRA_CONFIG_SERVICE_NOT_CONFIGURED = export const INFRA_CONFIG_SERVICE_NOT_CONFIGURED =
'infra_config/service_not_configured' as const; 'infra_config/service_not_configured' as const;
/**
* Infra Config update/fetch operation not allowed
* (InfraConfigService)
*/
export const INFRA_CONFIG_OPERATION_NOT_ALLOWED =
'infra_config/operation_not_allowed';
/** /**
* Error message for when the database table does not exist * Error message for when the database table does not exist
* (InfraConfigService) * (InfraConfigService)

View File

@@ -5,7 +5,7 @@ import * as E from 'fp-ts/Either';
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { RESTAdminGuard } from 'src/admin/guards/rest-admin.guard'; import { RESTAdminGuard } from 'src/admin/guards/rest-admin.guard';
import { RESTError } from 'src/types/RESTError'; import { RESTError } from 'src/types/RESTError';
import { InfraConfigEnumForClient } from 'src/types/InfraConfig'; import { InfraConfigEnum } from 'src/types/InfraConfig';
import { throwHTTPErr } from 'src/utils'; import { throwHTTPErr } from 'src/utils';
@UseGuards(ThrottlerBehindProxyGuard) @UseGuards(ThrottlerBehindProxyGuard)
@@ -17,7 +17,7 @@ export class SiteController {
@UseGuards(JwtAuthGuard, RESTAdminGuard) @UseGuards(JwtAuthGuard, RESTAdminGuard)
async fetchSetupInfo() { async fetchSetupInfo() {
const status = await this.infraConfigService.get( const status = await this.infraConfigService.get(
InfraConfigEnumForClient.IS_FIRST_TIME_INFRA_SETUP, InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
); );
if (E.isLeft(status)) if (E.isLeft(status))
@@ -32,7 +32,7 @@ export class SiteController {
@UseGuards(JwtAuthGuard, RESTAdminGuard) @UseGuards(JwtAuthGuard, RESTAdminGuard)
async setSetupAsComplete() { async setSetupAsComplete() {
const res = await this.infraConfigService.update( const res = await this.infraConfigService.update(
InfraConfigEnumForClient.IS_FIRST_TIME_INFRA_SETUP, InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
false.toString(), false.toString(),
false, false,
); );

View File

@@ -1,6 +1,6 @@
import { Field, ObjectType, registerEnumType } from '@nestjs/graphql'; import { Field, ObjectType, registerEnumType } from '@nestjs/graphql';
import { AuthProvider } from 'src/auth/helper'; import { AuthProvider } from 'src/auth/helper';
import { InfraConfigEnumForClient } from 'src/types/InfraConfig'; import { InfraConfigEnum } from 'src/types/InfraConfig';
import { ServiceStatus } from './helper'; import { ServiceStatus } from './helper';
@ObjectType() @ObjectType()
@@ -8,7 +8,7 @@ export class InfraConfig {
@Field({ @Field({
description: 'Infra Config Name', description: 'Infra Config Name',
}) })
name: InfraConfigEnumForClient; name: InfraConfigEnum;
@Field({ @Field({
description: 'Infra Config Value', description: 'Infra Config Value',
@@ -16,7 +16,7 @@ export class InfraConfig {
value: string; value: string;
} }
registerEnumType(InfraConfigEnumForClient, { registerEnumType(InfraConfigEnum, {
name: 'InfraConfigEnum', name: 'InfraConfigEnum',
}); });

View File

@@ -3,19 +3,16 @@ import { InfraConfig } from './infra-config.model';
import { PrismaService } from 'src/prisma/prisma.service'; import { PrismaService } from 'src/prisma/prisma.service';
import { InfraConfig as DBInfraConfig } from '@prisma/client'; import { InfraConfig as DBInfraConfig } from '@prisma/client';
import * as E from 'fp-ts/Either'; import * as E from 'fp-ts/Either';
import { import { InfraConfigEnum } from 'src/types/InfraConfig';
InfraConfigEnum,
InfraConfigEnumForClient,
} from 'src/types/InfraConfig';
import { import {
AUTH_PROVIDER_NOT_SPECIFIED, AUTH_PROVIDER_NOT_SPECIFIED,
DATABASE_TABLE_NOT_EXIST, DATABASE_TABLE_NOT_EXIST,
INFRA_CONFIG_INVALID_INPUT, INFRA_CONFIG_INVALID_INPUT,
INFRA_CONFIG_NOT_FOUND, INFRA_CONFIG_NOT_FOUND,
INFRA_CONFIG_NOT_LISTED,
INFRA_CONFIG_RESET_FAILED, INFRA_CONFIG_RESET_FAILED,
INFRA_CONFIG_UPDATE_FAILED, INFRA_CONFIG_UPDATE_FAILED,
INFRA_CONFIG_SERVICE_NOT_CONFIGURED, INFRA_CONFIG_SERVICE_NOT_CONFIGURED,
INFRA_CONFIG_OPERATION_NOT_ALLOWED,
} from 'src/errors'; } from 'src/errors';
import { import {
throwErr, throwErr,
@@ -24,13 +21,7 @@ import {
validateUrl, validateUrl,
} from 'src/utils'; } from 'src/utils';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { import { ServiceStatus, getDefaultInfraConfigs, stopApp } from './helper';
ServiceStatus,
getDefaultInfraConfigs,
stopApp,
generateAnalyticsUserId,
getConfiguredSSOProviders,
} 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';
@@ -41,6 +32,20 @@ export class InfraConfigService implements OnModuleInit {
private readonly configService: ConfigService, private readonly configService: ConfigService,
) {} ) {}
// Following fields are not updatable by `infraConfigs` Mutation. Use dedicated mutations for these fields instead.
EXCLUDE_FROM_UPDATE_CONFIGS = [
InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
InfraConfigEnum.ALLOW_ANALYTICS_COLLECTION,
InfraConfigEnum.ANALYTICS_USER_ID,
InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
];
// Following fields can not be fetched by `infraConfigs` Query. Use dedicated queries for these fields instead.
EXCLUDE_FROM_FETCH_CONFIGS = [
InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
InfraConfigEnum.ANALYTICS_USER_ID,
InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
];
async onModuleInit() { async onModuleInit() {
await this.initializeInfraConfigTable(); await this.initializeInfraConfigTable();
} }
@@ -109,11 +114,7 @@ export class InfraConfigService implements OnModuleInit {
* @param restartEnabled If true, restart the app after updating the InfraConfig * @param restartEnabled If true, restart the app after updating the InfraConfig
* @returns InfraConfig model * @returns InfraConfig model
*/ */
async update( async update(name: InfraConfigEnum, value: string, restartEnabled = false) {
name: InfraConfigEnumForClient | InfraConfigEnum,
value: string,
restartEnabled = false,
) {
const isValidate = this.validateEnvValues([{ name, value }]); const isValidate = this.validateEnvValues([{ name, value }]);
if (E.isLeft(isValidate)) return E.left(isValidate.left); if (E.isLeft(isValidate)) return E.left(isValidate.left);
@@ -137,6 +138,11 @@ export class InfraConfigService implements OnModuleInit {
* @returns InfraConfig model * @returns InfraConfig model
*/ */
async updateMany(infraConfigs: InfraConfigArgs[]) { async updateMany(infraConfigs: InfraConfigArgs[]) {
for (let i = 0; i < infraConfigs.length; i++) {
if (this.EXCLUDE_FROM_UPDATE_CONFIGS.includes(infraConfigs[i].name))
return E.left(INFRA_CONFIG_OPERATION_NOT_ALLOWED);
}
const isValidate = this.validateEnvValues(infraConfigs); const isValidate = this.validateEnvValues(infraConfigs);
if (E.isLeft(isValidate)) return E.left(isValidate.left); if (E.isLeft(isValidate)) return E.left(isValidate.left);
@@ -264,7 +270,7 @@ export class InfraConfigService implements OnModuleInit {
* @param name Name of the InfraConfig * @param name Name of the InfraConfig
* @returns InfraConfig model * @returns InfraConfig model
*/ */
async get(name: InfraConfigEnumForClient) { async get(name: InfraConfigEnum) {
try { try {
const infraConfig = await this.prisma.infraConfig.findUniqueOrThrow({ const infraConfig = await this.prisma.infraConfig.findUniqueOrThrow({
where: { name }, where: { name },
@@ -281,7 +287,15 @@ export class InfraConfigService implements OnModuleInit {
* @param names Names of the InfraConfigs * @param names Names of the InfraConfigs
* @returns InfraConfig model * @returns InfraConfig model
*/ */
async getMany(names: InfraConfigEnumForClient[]) { async getMany(names: InfraConfigEnum[], checkDisallowedKeys: boolean = true) {
if (checkDisallowedKeys) {
// Check if the names are allowed to fetch by client
for (let i = 0; i < names.length; i++) {
if (this.EXCLUDE_FROM_FETCH_CONFIGS.includes(names[i]))
return E.left(INFRA_CONFIG_OPERATION_NOT_ALLOWED);
}
}
try { try {
const infraConfigs = await this.prisma.infraConfig.findMany({ const infraConfigs = await this.prisma.infraConfig.findMany({
where: { name: { in: names } }, where: { name: { in: names } },
@@ -341,60 +355,60 @@ export class InfraConfigService implements OnModuleInit {
*/ */
validateEnvValues( validateEnvValues(
infraConfigs: { infraConfigs: {
name: InfraConfigEnumForClient | InfraConfigEnum; name: InfraConfigEnum;
value: string; value: string;
}[], }[],
) { ) {
for (let i = 0; i < infraConfigs.length; i++) { for (let i = 0; i < infraConfigs.length; i++) {
switch (infraConfigs[i].name) { switch (infraConfigs[i].name) {
case InfraConfigEnumForClient.MAILER_SMTP_URL: case InfraConfigEnum.MAILER_SMTP_URL:
const isValidUrl = validateSMTPUrl(infraConfigs[i].value); const isValidUrl = validateSMTPUrl(infraConfigs[i].value);
if (!isValidUrl) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!isValidUrl) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.MAILER_ADDRESS_FROM: case InfraConfigEnum.MAILER_ADDRESS_FROM:
const isValidEmail = validateSMTPEmail(infraConfigs[i].value); const isValidEmail = validateSMTPEmail(infraConfigs[i].value);
if (!isValidEmail) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!isValidEmail) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GOOGLE_CLIENT_ID: case InfraConfigEnum.GOOGLE_CLIENT_ID:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GOOGLE_CLIENT_SECRET: case InfraConfigEnum.GOOGLE_CLIENT_SECRET:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GOOGLE_CALLBACK_URL: case InfraConfigEnum.GOOGLE_CALLBACK_URL:
if (!validateUrl(infraConfigs[i].value)) if (!validateUrl(infraConfigs[i].value))
return E.left(INFRA_CONFIG_INVALID_INPUT); return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GOOGLE_SCOPE: case InfraConfigEnum.GOOGLE_SCOPE:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GITHUB_CLIENT_ID: case InfraConfigEnum.GITHUB_CLIENT_ID:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GITHUB_CLIENT_SECRET: case InfraConfigEnum.GITHUB_CLIENT_SECRET:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GITHUB_CALLBACK_URL: case InfraConfigEnum.GITHUB_CALLBACK_URL:
if (!validateUrl(infraConfigs[i].value)) if (!validateUrl(infraConfigs[i].value))
return E.left(INFRA_CONFIG_INVALID_INPUT); return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.GITHUB_SCOPE: case InfraConfigEnum.GITHUB_SCOPE:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.MICROSOFT_CLIENT_ID: case InfraConfigEnum.MICROSOFT_CLIENT_ID:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.MICROSOFT_CLIENT_SECRET: case InfraConfigEnum.MICROSOFT_CLIENT_SECRET:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.MICROSOFT_CALLBACK_URL: case InfraConfigEnum.MICROSOFT_CALLBACK_URL:
if (!validateUrl(infraConfigs[i].value)) if (!validateUrl(infraConfigs[i].value))
return E.left(INFRA_CONFIG_INVALID_INPUT); return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.MICROSOFT_SCOPE: case InfraConfigEnum.MICROSOFT_SCOPE:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
case InfraConfigEnumForClient.MICROSOFT_TENANT: case InfraConfigEnum.MICROSOFT_TENANT:
if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT); if (!infraConfigs[i].value) return E.left(INFRA_CONFIG_INVALID_INPUT);
break; break;
default: default:

View File

@@ -1,14 +1,14 @@
import { Field, InputType } from '@nestjs/graphql'; import { Field, InputType } from '@nestjs/graphql';
import { InfraConfigEnumForClient } from 'src/types/InfraConfig'; import { InfraConfigEnum } from 'src/types/InfraConfig';
import { ServiceStatus } from './helper'; import { ServiceStatus } from './helper';
import { AuthProvider } from 'src/auth/helper'; import { AuthProvider } from 'src/auth/helper';
@InputType() @InputType()
export class InfraConfigArgs { export class InfraConfigArgs {
@Field(() => InfraConfigEnumForClient, { @Field(() => InfraConfigEnum, {
description: 'Infra Config Name', description: 'Infra Config Name',
}) })
name: InfraConfigEnumForClient; name: InfraConfigEnum;
@Field({ @Field({
description: 'Infra Config Value', description: 'Infra Config Value',

View File

@@ -24,27 +24,3 @@ export enum InfraConfigEnum {
ANALYTICS_USER_ID = 'ANALYTICS_USER_ID', ANALYTICS_USER_ID = 'ANALYTICS_USER_ID',
IS_FIRST_TIME_INFRA_SETUP = 'IS_FIRST_TIME_INFRA_SETUP', IS_FIRST_TIME_INFRA_SETUP = 'IS_FIRST_TIME_INFRA_SETUP',
} }
export enum InfraConfigEnumForClient {
MAILER_SMTP_URL = 'MAILER_SMTP_URL',
MAILER_ADDRESS_FROM = 'MAILER_ADDRESS_FROM',
GOOGLE_CLIENT_ID = 'GOOGLE_CLIENT_ID',
GOOGLE_CLIENT_SECRET = 'GOOGLE_CLIENT_SECRET',
GOOGLE_CALLBACK_URL = 'GOOGLE_CALLBACK_URL',
GOOGLE_SCOPE = 'GOOGLE_SCOPE',
GITHUB_CLIENT_ID = 'GITHUB_CLIENT_ID',
GITHUB_CLIENT_SECRET = 'GITHUB_CLIENT_SECRET',
GITHUB_CALLBACK_URL = 'GITHUB_CALLBACK_URL',
GITHUB_SCOPE = 'GITHUB_SCOPE',
MICROSOFT_CLIENT_ID = 'MICROSOFT_CLIENT_ID',
MICROSOFT_CLIENT_SECRET = 'MICROSOFT_CLIENT_SECRET',
MICROSOFT_CALLBACK_URL = 'MICROSOFT_CALLBACK_URL',
MICROSOFT_SCOPE = 'MICROSOFT_SCOPE',
MICROSOFT_TENANT = 'MICROSOFT_TENANT',
ALLOW_ANALYTICS_COLLECTION = 'ALLOW_ANALYTICS_COLLECTION',
IS_FIRST_TIME_INFRA_SETUP = 'IS_FIRST_TIME_INFRA_SETUP',
}