From b5efcbbef91834002bea2b43ee407fac07c23dcc Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Thu, 19 Jun 2025 10:55:48 +0100 Subject: [PATCH] fix(api): make mailerService optional --- api/src/user/services/invitation.service.ts | 50 ++++++++++--------- .../user/services/passwordReset.service.ts | 50 ++++++++++--------- .../user/services/validate-account.service.ts | 50 ++++++++++--------- 3 files changed, 81 insertions(+), 69 deletions(-) diff --git a/api/src/user/services/invitation.service.ts b/api/src/user/services/invitation.service.ts index 3b9f258c..b510038d 100644 --- a/api/src/user/services/invitation.service.ts +++ b/api/src/user/services/invitation.service.ts @@ -12,6 +12,7 @@ import { Inject, Injectable, InternalServerErrorException, + Optional, } from '@nestjs/common'; import { JwtService, JwtSignOptions } from '@nestjs/jwt'; @@ -40,7 +41,7 @@ export class InvitationService extends BaseService< @Inject(JwtService) private readonly jwtService: JwtService, protected readonly i18n: I18nService, public readonly languageService: LanguageService, - private readonly mailerService: MailerService, + @Optional() private readonly mailerService?: MailerService, ) { super(repository); } @@ -60,28 +61,31 @@ export class InvitationService extends BaseService< */ async create(dto: InvitationCreateDto): Promise { const jwt = await this.sign({ ...dto }); - try { - const defaultLanguage = await this.languageService.getDefaultLanguage(); - await this.mailerService.sendMail({ - to: dto.email, - template: 'invitation.mjml', - context: { - appName: config.parameters.appName, - appUrl: config.uiBaseUrl, - token: jwt, - // TODO: Which language should we use? - t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), - }, - subject: this.i18n.t('invitation_subject'), - }); - } catch (e) { - this.logger.error( - 'Could not send email', - e.message, - e.stack, - 'InvitationService', - ); - throw new InternalServerErrorException('Could not send email'); + if (this.mailerService) { + try { + const defaultLanguage = await this.languageService.getDefaultLanguage(); + await this.mailerService.sendMail({ + to: dto.email, + template: 'invitation.mjml', + context: { + appName: config.parameters.appName, + appUrl: config.uiBaseUrl, + token: jwt, + // TODO: Which language should we use? + t: (key: string) => + this.i18n.t(key, { lang: defaultLanguage.code }), + }, + subject: this.i18n.t('invitation_subject'), + }); + } catch (e) { + this.logger.error( + 'Could not send email', + e.message, + e.stack, + 'InvitationService', + ); + throw new InternalServerErrorException('Could not send email'); + } } const newInvitation = await super.create({ ...dto, token: jwt }); return { ...newInvitation, token: jwt }; diff --git a/api/src/user/services/passwordReset.service.ts b/api/src/user/services/passwordReset.service.ts index 8a7db8fa..6081d485 100644 --- a/api/src/user/services/passwordReset.service.ts +++ b/api/src/user/services/passwordReset.service.ts @@ -14,6 +14,7 @@ import { Injectable, InternalServerErrorException, NotFoundException, + Optional, UnauthorizedException, } from '@nestjs/common'; import { JwtService, JwtSignOptions } from '@nestjs/jwt'; @@ -36,7 +37,7 @@ export class PasswordResetService { private readonly userService: UserService, public readonly i18n: I18nService, public readonly languageService: LanguageService, - private readonly mailerService: MailerService, + @Optional() private readonly mailerService?: MailerService, ) {} public readonly jwtSignOptions: JwtSignOptions = { @@ -59,28 +60,31 @@ export class PasswordResetService { } const jwt = await this.sign({ ...dto }); - try { - const defaultLanguage = await this.languageService.getDefaultLanguage(); - await this.mailerService.sendMail({ - to: dto.email, - template: 'password_reset.mjml', - context: { - appName: config.parameters.appName, - appUrl: config.uiBaseUrl, - token: jwt, - first_name: user.first_name, - t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), - }, - subject: this.i18n.t('password_reset_subject'), - }); - } catch (e) { - this.logger.error( - 'Could not send email', - e.message, - e.stack, - 'InvitationService', - ); - throw new InternalServerErrorException('Could not send email'); + if (this.mailerService) { + try { + const defaultLanguage = await this.languageService.getDefaultLanguage(); + await this.mailerService.sendMail({ + to: dto.email, + template: 'password_reset.mjml', + context: { + appName: config.parameters.appName, + appUrl: config.uiBaseUrl, + token: jwt, + first_name: user.first_name, + t: (key: string) => + this.i18n.t(key, { lang: defaultLanguage.code }), + }, + subject: this.i18n.t('password_reset_subject'), + }); + } catch (e) { + this.logger.error( + 'Could not send email', + e.message, + e.stack, + 'InvitationService', + ); + throw new InternalServerErrorException('Could not send email'); + } } // TODO: hash the token before saving it diff --git a/api/src/user/services/validate-account.service.ts b/api/src/user/services/validate-account.service.ts index b0048182..f8fd354c 100644 --- a/api/src/user/services/validate-account.service.ts +++ b/api/src/user/services/validate-account.service.ts @@ -12,6 +12,7 @@ import { Inject, Injectable, InternalServerErrorException, + Optional, UnauthorizedException, } from '@nestjs/common'; import { JwtService, JwtSignOptions } from '@nestjs/jwt'; @@ -39,7 +40,7 @@ export class ValidateAccountService { private logger: LoggerService, private readonly i18n: I18nService, private readonly languageService: LanguageService, - private readonly mailerService: MailerService, + @Optional() private readonly mailerService?: MailerService, ) {} /** @@ -76,28 +77,31 @@ export class ValidateAccountService { ) { const confirmationToken = await this.sign({ email: dto.email }); - try { - const defaultLanguage = await this.languageService.getDefaultLanguage(); - await this.mailerService.sendMail({ - to: dto.email, - template: 'account_confirmation.mjml', - context: { - appName: config.parameters.appName, - appUrl: config.uiBaseUrl, - token: confirmationToken, - first_name: dto.first_name, - t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), - }, - subject: this.i18n.t('account_confirmation_subject'), - }); - } catch (e) { - this.logger.error( - 'Could not send email', - e.message, - e.stack, - 'ValidateAccount', - ); - throw new InternalServerErrorException('Could not send email'); + if (this.mailerService) { + try { + const defaultLanguage = await this.languageService.getDefaultLanguage(); + await this.mailerService.sendMail({ + to: dto.email, + template: 'account_confirmation.mjml', + context: { + appName: config.parameters.appName, + appUrl: config.uiBaseUrl, + token: confirmationToken, + first_name: dto.first_name, + t: (key: string) => + this.i18n.t(key, { lang: defaultLanguage.code }), + }, + subject: this.i18n.t('account_confirmation_subject'), + }); + } catch (e) { + this.logger.error( + 'Could not send email', + e.message, + e.stack, + 'ValidateAccount', + ); + throw new InternalServerErrorException('Could not send email'); + } } }