2025-07-30 05:23:26 +03:00

69 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MailerService } from '@nestjs-modules/mailer';
import { render } from '@react-email/components';
import { ProcessEnv } from '@/src/shared/types/env';
import { SessionInfo } from '@/src/shared/types/session-metadata.types';
import { Token } from '@prisma/generated';
import {
EnableTwoFactorTemplate, VerifyChannelTemplate, VerificationTemplate, PasswordRecoveryTemplate, DeactivateTemplate, AccountDeletionTemplate,
} from './templates';
@Injectable()
export class MailService {
constructor(
private readonly configService: ConfigService<ProcessEnv>,
private readonly mailerService: MailerService,
) {}
public async sendVerificationToken(email: string, token: Token['token']) {
const domain = this.configService.getOrThrow<string>('ALLOWED_ORIGIN');
const html = await render(VerificationTemplate({ domain, token }));
void this.sendMail(email, 'Верификация аккаунта', html);
}
public async sendPasswordResetToken(email: string, token: Token['token'], metadata: SessionInfo) {
const domain = this.configService.getOrThrow<string>('ALLOWED_ORIGIN');
const html = await render(PasswordRecoveryTemplate({ domain, token, metadata }));
void this.sendMail(email, 'Сброс пароля', html);
}
public async sendDeactivateToken(email: string, token: Token['token'], metadata: SessionInfo) {
const html = await render(DeactivateTemplate({ token, metadata }));
void this.sendMail(email, 'Деактивация аккаунта', html);
}
public async sendAccountDeletion(email: string) {
const domain = this.configService.getOrThrow<string>('ALLOWED_ORIGIN');
const html = await render(AccountDeletionTemplate({ domain }));
void this.sendMail(email, 'Удаление аккаунта', html);
}
public async sendEnableTwoFactor(email: string) {
const domain = this.configService.getOrThrow<string>('ALLOWED_ORIGIN');
const html = await render(EnableTwoFactorTemplate({ domain }));
void this.sendMail(email, 'Обеспечьте свою безопасность', html);
}
public async sendVerifyChannel(email: string) {
const html = await render(VerifyChannelTemplate());
void this.sendMail(email, 'Ваш канал верифицирован', html);
}
private async sendMail(email: string, subject: string, html: string) {
return this.mailerService.sendMail({
to: email,
subject,
html,
});
}
}