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,
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<Invitation> {
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 };

View File

@ -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,
'PasswordResetService',
);
throw new InternalServerErrorException('Could not send email');
}
}
// TODO: hash the token before saving it

View File

@ -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');
}
}
}