chore: smtp url validation added

This commit is contained in:
Mir Arif Hasan
2023-11-28 18:50:39 +06:00
parent 180b9d776f
commit 97d71e5032
3 changed files with 71 additions and 5 deletions

View File

@@ -9,12 +9,13 @@ import {
} from 'src/types/InfraConfig';
import {
DATABASE_TABLE_NOT_EXIST,
INFRA_CONFIG_INVALID_INPUT,
INFRA_CONFIG_NOT_FOUND,
INFRA_CONFIG_NOT_LISTED,
INFRA_CONFIG_RESET_FAILED,
INFRA_CONFIG_UPDATE_FAILED,
} from 'src/errors';
import { throwErr } from 'src/utils';
import { throwErr, validateUrl } from 'src/utils';
import { ConfigService } from '@nestjs/config';
import { AuthProviderStatus, stopApp } from './helper';
import { InfraConfigArgs } from './input-args';
@@ -130,7 +131,13 @@ export class InfraConfigService implements OnModuleInit {
* @param value Value of the InfraConfig
* @returns InfraConfig model
*/
async update(name: InfraConfigEnum, value: string) {
async update(
name: InfraConfigEnumForClient | InfraConfigEnum,
value: string,
) {
const isValidate = this.validateEnvValues([{ name, value }]);
if (E.isLeft(isValidate)) return E.left(isValidate.left);
try {
const infraConfig = await this.prisma.infraConfig.update({
where: { name },
@@ -151,6 +158,9 @@ export class InfraConfigService implements OnModuleInit {
* @returns InfraConfig model
*/
async updateMany(infraConfigs: InfraConfigArgs[]) {
const isValidate = this.validateEnvValues(infraConfigs);
if (E.isLeft(isValidate)) return E.left(isValidate.left);
try {
await this.prisma.$transaction(async (tx) => {
const deleteCount = await tx.infraConfig.deleteMany({
@@ -165,7 +175,7 @@ export class InfraConfigService implements OnModuleInit {
});
if (deleteCount.count !== createCount.count) {
throwErr(INFRA_CONFIG_UPDATE_FAILED);
return E.left(INFRA_CONFIG_UPDATE_FAILED);
}
});
@@ -205,7 +215,7 @@ export class InfraConfigService implements OnModuleInit {
InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
newEnabledAuthProviders.join(','),
);
if (E.isLeft(isUpdated)) throwErr(isUpdated.left);
if (E.isLeft(isUpdated)) return E.left(isUpdated.left);
return E.right(true);
}
@@ -275,4 +285,20 @@ export class InfraConfigService implements OnModuleInit {
return E.left(INFRA_CONFIG_RESET_FAILED);
}
}
validateEnvValues(
infraConfigs: {
name: InfraConfigEnumForClient | InfraConfigEnum;
value: string;
}[],
) {
for (let i = 0; i < infraConfigs.length; i++) {
if (infraConfigs[i].name === InfraConfigEnumForClient.MAILER_SMTP_URL) {
const isValidUrl = validateUrl(infraConfigs[i].value);
if (!isValidUrl) return E.left(INFRA_CONFIG_INVALID_INPUT);
}
}
return E.right(true);
}
}