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, private readonly mailerService: MailerService, ) {} public async sendVerificationToken(email: string, token: Token['token']) { const domain = this.configService.getOrThrow('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('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('ALLOWED_ORIGIN'); const html = await render(AccountDeletionTemplate({ domain })); void this.sendMail(email, 'Удаление аккаунта', html); } public async sendEnableTwoFactor(email: string) { const domain = this.configService.getOrThrow('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, }); } }