From 8b14f77d7879430c2f093bf91bf3b308a20f9fe1 Mon Sep 17 00:00:00 2001 From: mirarifhasan Date: Mon, 27 May 2024 13:02:01 +0600 Subject: [PATCH] feat: email auth provider disabled on smtp disable --- .../src/admin/infra.resolver.ts | 19 +++++++++ .../src/infra-config/infra-config.service.ts | 41 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/packages/hoppscotch-backend/src/admin/infra.resolver.ts b/packages/hoppscotch-backend/src/admin/infra.resolver.ts index 83bdd6a9c..e7aa9ef4a 100644 --- a/packages/hoppscotch-backend/src/admin/infra.resolver.ts +++ b/packages/hoppscotch-backend/src/admin/infra.resolver.ts @@ -367,4 +367,23 @@ export class InfraResolver { return true; } + + @Mutation(() => Boolean, { + description: 'Enable or Disable SMTP for sending emails', + }) + @UseGuards(GqlAuthGuard, GqlAdminGuard) + async enableAndDisableSMTP( + @Args({ + name: 'status', + type: () => ServiceStatus, + description: 'Toggle SMTP', + }) + status: ServiceStatus, + ) { + const isUpdated = await this.infraConfigService.enableAndDisableSMTP( + status, + ); + if (E.isLeft(isUpdated)) throwErr(isUpdated.left); + return true; + } } 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 845c33130..e7e225ac3 100644 --- a/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts +++ b/packages/hoppscotch-backend/src/infra-config/infra-config.service.ts @@ -218,6 +218,47 @@ export class InfraConfigService implements OnModuleInit { return E.right(isUpdated.right.value === 'true'); } + /** + * Enable or Disable SMTP + * @param status Status to enable or disable + * @returns Either true or an error + */ + async enableAndDisableSMTP(status: ServiceStatus) { + const isUpdated = await this.toggleServiceStatus( + InfraConfigEnum.MAILER_SMTP_ENABLE, + status, + true, + ); + if (E.isLeft(isUpdated)) return E.left(isUpdated.left); + + if (status === ServiceStatus.DISABLE) { + this.enableAndDisableSSO([{ provider: AuthProvider.EMAIL, status }]); + } + return E.right(true); + } + + /** + * Enable or Disable Service (i.e. ALLOW_AUDIT_LOGS, ALLOW_ANALYTICS_COLLECTION, ALLOW_DOMAIN_WHITELISTING, SITE_PROTECTION) + * @param configName Name of the InfraConfigEnum + * @param status Status to enable or disable + * @param restartEnabled If true, restart the app after updating the InfraConfig + * @returns Either true or an error + */ + async toggleServiceStatus( + configName: InfraConfigEnum, + status: ServiceStatus, + restartEnabled = false, + ) { + const isUpdated = await this.update( + configName, + status === ServiceStatus.ENABLE ? 'true' : 'false', + restartEnabled, + ); + if (E.isLeft(isUpdated)) return E.left(isUpdated.left); + + return E.right(true); + } + /** * Enable or Disable SSO for login/signup * @param provider Auth Provider to enable or disable