|
1 | 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { ConfigService } from '@nestjs/config'; |
| 3 | +import * as nodemailer from 'nodemailer'; |
2 | 4 |
|
3 | 5 | @Injectable() |
4 | 6 | export class MailService { |
5 | 7 | private readonly logger = new Logger(MailService.name); |
| 8 | + private readonly transporter: nodemailer.Transporter; |
| 9 | + |
| 10 | + constructor(private readonly configService: ConfigService) { |
| 11 | + this.transporter = nodemailer.createTransport({ |
| 12 | + host: configService.get<string>('SMTP_HOST', ''), |
| 13 | + port: parseInt(configService.get<string>('SMTP_PORT', '587'), 10), |
| 14 | + secure: configService.get<string>('SMTP_SECURE', 'false') === 'true', |
| 15 | + auth: { |
| 16 | + user: configService.get<string>('SMTP_USER', ''), |
| 17 | + pass: configService.get<string>('SMTP_PASS', ''), |
| 18 | + }, |
| 19 | + }); |
| 20 | + } |
6 | 21 |
|
7 | 22 | async sendPasswordResetEmail(email: string, resetLink: string) { |
8 | | - this.logger.log(`[MailService] Sending password reset email to ${email}. Link: ${resetLink}`); |
9 | | - // Inn a real application, implement NodeMailer or similar here. |
| 23 | + await this.sendTemplateEmail(email, 'Password Reset', 'password-reset', { resetLink }); |
| 24 | + } |
| 25 | + |
| 26 | + async sendWelcomeEmail(email: string, name: string) { |
| 27 | + await this.sendTemplateEmail(email, 'Welcome to AssetsUp', 'welcome', { name }); |
| 28 | + } |
| 29 | + |
| 30 | + async sendTemplateEmail(to: string, subject: string, template: string, context: Record<string, any>) { |
| 31 | + const html = this.renderTemplate(template, context); |
| 32 | + try { |
| 33 | + await this.transporter.sendMail({ |
| 34 | + from: this.configService.get<string>('MAIL_FROM', 'noreply@assetsup.local'), |
| 35 | + to, |
| 36 | + subject, |
| 37 | + html, |
| 38 | + }); |
| 39 | + this.logger.log(`Email sent to ${to} with subject "${subject}"`); |
| 40 | + } catch (error) { |
| 41 | + this.logger.error(`Failed to send email to ${to}: ${error.message}`); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private renderTemplate(template: string, context: Record<string, any>): string { |
| 46 | + const templates: Record<string, (ctx: Record<string, any>) => string> = { |
| 47 | + 'password-reset': (ctx) => ` |
| 48 | + <h1>Password Reset</h1> |
| 49 | + <p>Click the link below to reset your password:</p> |
| 50 | + <a href="${ctx.resetLink}">${ctx.resetLink}</a> |
| 51 | + <p>This link expires in 1 hour.</p> |
| 52 | + `, |
| 53 | + 'welcome': (ctx) => ` |
| 54 | + <h1>Welcome to AssetsUp!</h1> |
| 55 | + <p>Hello ${ctx.name},</p> |
| 56 | + <p>Your account has been created successfully.</p> |
| 57 | + `, |
| 58 | + 'asset-checkout': (ctx) => ` |
| 59 | + <h1>Asset Checked Out</h1> |
| 60 | + <p>Asset <strong>${ctx.assetName}</strong> has been checked out to ${ctx.assignedTo}.</p> |
| 61 | + <p>Due date: ${ctx.dueDate}</p> |
| 62 | + `, |
| 63 | + 'maintenance-due': (ctx) => ` |
| 64 | + <h1>Maintenance Due</h1> |
| 65 | + <p>Asset <strong>${ctx.assetName}</strong> has scheduled maintenance due on ${ctx.dueDate}.</p> |
| 66 | + `, |
| 67 | + 'warranty-expiry': (ctx) => ` |
| 68 | + <h1>Warranty Expiry Notice</h1> |
| 69 | + <p>The warranty for <strong>${ctx.assetName}</strong> expires on ${ctx.expiryDate}.</p> |
| 70 | + `, |
| 71 | + }; |
| 72 | + const render = templates[template]; |
| 73 | + if (!render) { |
| 74 | + this.logger.warn(`Unknown email template: ${template}`); |
| 75 | + return `<p>${subject}</p>`; |
| 76 | + } |
| 77 | + return render(context); |
10 | 78 | } |
11 | 79 | } |
12 | | -// async sendPasswordResetEmail(email: string, resetLink: string) { |
13 | | -// this.logger.log(`[MailService] Sending password reset email to ${email}. Link: ${resetLink}`); |
14 | | -// // Inn a real application, implement NodeMailer or similar here. |
15 | | -// } |
16 | | -// } |
|
0 commit comments