Merge pull request #1147 from Hexastack/1146-bug---missing-mailservice-usermodule
Some checks failed
Build and Push Docker API Image / build-and-push (push) Has been cancelled
Build and Push Docker Base Image / build-and-push (push) Has been cancelled
Build and Push Docker UI Image / build-and-push (push) Has been cancelled

fix(api): reset mailerService to be optional [HOTFIX]
This commit is contained in:
Med Marrouchi 2025-06-19 11:05:12 +01:00 committed by GitHub
commit 0e3187c844
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 69 deletions

View File

@ -12,6 +12,7 @@ import {
Inject, Inject,
Injectable, Injectable,
InternalServerErrorException, InternalServerErrorException,
Optional,
} from '@nestjs/common'; } from '@nestjs/common';
import { JwtService, JwtSignOptions } from '@nestjs/jwt'; import { JwtService, JwtSignOptions } from '@nestjs/jwt';
@ -40,7 +41,7 @@ export class InvitationService extends BaseService<
@Inject(JwtService) private readonly jwtService: JwtService, @Inject(JwtService) private readonly jwtService: JwtService,
protected readonly i18n: I18nService, protected readonly i18n: I18nService,
public readonly languageService: LanguageService, public readonly languageService: LanguageService,
private readonly mailerService: MailerService, @Optional() private readonly mailerService?: MailerService,
) { ) {
super(repository); super(repository);
} }
@ -60,28 +61,31 @@ export class InvitationService extends BaseService<
*/ */
async create(dto: InvitationCreateDto): Promise<Invitation> { async create(dto: InvitationCreateDto): Promise<Invitation> {
const jwt = await this.sign({ ...dto }); const jwt = await this.sign({ ...dto });
try { if (this.mailerService) {
const defaultLanguage = await this.languageService.getDefaultLanguage(); try {
await this.mailerService.sendMail({ const defaultLanguage = await this.languageService.getDefaultLanguage();
to: dto.email, await this.mailerService.sendMail({
template: 'invitation.mjml', to: dto.email,
context: { template: 'invitation.mjml',
appName: config.parameters.appName, context: {
appUrl: config.uiBaseUrl, appName: config.parameters.appName,
token: jwt, appUrl: config.uiBaseUrl,
// TODO: Which language should we use? token: jwt,
t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), // TODO: Which language should we use?
}, t: (key: string) =>
subject: this.i18n.t('invitation_subject'), this.i18n.t(key, { lang: defaultLanguage.code }),
}); },
} catch (e) { subject: this.i18n.t('invitation_subject'),
this.logger.error( });
'Could not send email', } catch (e) {
e.message, this.logger.error(
e.stack, 'Could not send email',
'InvitationService', e.message,
); e.stack,
throw new InternalServerErrorException('Could not send email'); 'InvitationService',
);
throw new InternalServerErrorException('Could not send email');
}
} }
const newInvitation = await super.create({ ...dto, token: jwt }); const newInvitation = await super.create({ ...dto, token: jwt });
return { ...newInvitation, token: jwt }; return { ...newInvitation, token: jwt };

View File

@ -14,6 +14,7 @@ import {
Injectable, Injectable,
InternalServerErrorException, InternalServerErrorException,
NotFoundException, NotFoundException,
Optional,
UnauthorizedException, UnauthorizedException,
} from '@nestjs/common'; } from '@nestjs/common';
import { JwtService, JwtSignOptions } from '@nestjs/jwt'; import { JwtService, JwtSignOptions } from '@nestjs/jwt';
@ -36,7 +37,7 @@ export class PasswordResetService {
private readonly userService: UserService, private readonly userService: UserService,
public readonly i18n: I18nService, public readonly i18n: I18nService,
public readonly languageService: LanguageService, public readonly languageService: LanguageService,
private readonly mailerService: MailerService, @Optional() private readonly mailerService?: MailerService,
) {} ) {}
public readonly jwtSignOptions: JwtSignOptions = { public readonly jwtSignOptions: JwtSignOptions = {
@ -59,28 +60,31 @@ export class PasswordResetService {
} }
const jwt = await this.sign({ ...dto }); const jwt = await this.sign({ ...dto });
try { if (this.mailerService) {
const defaultLanguage = await this.languageService.getDefaultLanguage(); try {
await this.mailerService.sendMail({ const defaultLanguage = await this.languageService.getDefaultLanguage();
to: dto.email, await this.mailerService.sendMail({
template: 'password_reset.mjml', to: dto.email,
context: { template: 'password_reset.mjml',
appName: config.parameters.appName, context: {
appUrl: config.uiBaseUrl, appName: config.parameters.appName,
token: jwt, appUrl: config.uiBaseUrl,
first_name: user.first_name, token: jwt,
t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), first_name: user.first_name,
}, t: (key: string) =>
subject: this.i18n.t('password_reset_subject'), this.i18n.t(key, { lang: defaultLanguage.code }),
}); },
} catch (e) { subject: this.i18n.t('password_reset_subject'),
this.logger.error( });
'Could not send email', } catch (e) {
e.message, this.logger.error(
e.stack, 'Could not send email',
'InvitationService', e.message,
); e.stack,
throw new InternalServerErrorException('Could not send email'); 'PasswordResetService',
);
throw new InternalServerErrorException('Could not send email');
}
} }
// TODO: hash the token before saving it // TODO: hash the token before saving it

View File

@ -12,6 +12,7 @@ import {
Inject, Inject,
Injectable, Injectable,
InternalServerErrorException, InternalServerErrorException,
Optional,
UnauthorizedException, UnauthorizedException,
} from '@nestjs/common'; } from '@nestjs/common';
import { JwtService, JwtSignOptions } from '@nestjs/jwt'; import { JwtService, JwtSignOptions } from '@nestjs/jwt';
@ -39,7 +40,7 @@ export class ValidateAccountService {
private logger: LoggerService, private logger: LoggerService,
private readonly i18n: I18nService, private readonly i18n: I18nService,
private readonly languageService: LanguageService, 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 }); const confirmationToken = await this.sign({ email: dto.email });
try { if (this.mailerService) {
const defaultLanguage = await this.languageService.getDefaultLanguage(); try {
await this.mailerService.sendMail({ const defaultLanguage = await this.languageService.getDefaultLanguage();
to: dto.email, await this.mailerService.sendMail({
template: 'account_confirmation.mjml', to: dto.email,
context: { template: 'account_confirmation.mjml',
appName: config.parameters.appName, context: {
appUrl: config.uiBaseUrl, appName: config.parameters.appName,
token: confirmationToken, appUrl: config.uiBaseUrl,
first_name: dto.first_name, token: confirmationToken,
t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), first_name: dto.first_name,
}, t: (key: string) =>
subject: this.i18n.t('account_confirmation_subject'), this.i18n.t(key, { lang: defaultLanguage.code }),
}); },
} catch (e) { subject: this.i18n.t('account_confirmation_subject'),
this.logger.error( });
'Could not send email', } catch (e) {
e.message, this.logger.error(
e.stack, 'Could not send email',
'ValidateAccount', e.message,
); e.stack,
throw new InternalServerErrorException('Could not send email'); 'ValidateAccount',
);
throw new InternalServerErrorException('Could not send email');
}
} }
} }