forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.service.ts
More file actions
146 lines (132 loc) · 5.14 KB
/
Copy pathemail.service.ts
File metadata and controls
146 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import * as nodemailer from "nodemailer";
import { ExportFormat, ReportType } from "./entities/export-job.entity";
@Injectable()
export class EmailService {
private readonly logger = new Logger(EmailService.name);
// Initialised directly in the constructor so TypeScript can verify
// definite assignment — avoids TS2564 without needing `!`
private readonly transporter: nodemailer.Transporter;
constructor(private readonly configService: ConfigService) {
this.transporter = nodemailer.createTransport({
host: this.configService.get<string>("SMTP_HOST"),
port: parseInt(this.configService.get<string>("SMTP_PORT") ?? "587", 10),
secure: this.configService.get<string>("SMTP_SECURE") === "true",
auth: {
user: this.configService.get<string>("SMTP_USER"),
pass: this.configService.get<string>("SMTP_PASSWORD"),
},
});
}
async sendExportEmail(
to: string,
fileName: string,
downloadUrl: string,
format: ExportFormat,
reportType: ReportType,
expiresAt: Date,
): Promise<void> {
const formatNames: Record<ExportFormat, string> = {
[ExportFormat.CSV]: "CSV",
[ExportFormat.PDF]: "PDF",
[ExportFormat.JSON]: "JSON",
[ExportFormat.QBO]: "QuickBooks",
[ExportFormat.OFX]: "OFX",
[ExportFormat.XLSX]: "Excel",
};
const reportNames: Record<ReportType, string> = {
[ReportType.MONTHLY_SUMMARY]: "Monthly Summary",
[ReportType.ANNUAL_TAX_REPORT]: "Annual Tax Report",
[ReportType.CATEGORY_BREAKDOWN]: "Category Breakdown",
[ReportType.PARTNER_WISE_SUMMARY]: "Partner-wise Summary",
[ReportType.PAYMENT_HISTORY]: "Payment History",
[ReportType.CUSTOM]: "Custom Report",
};
const subject = `Your ${reportNames[reportType]} Export from StellarSplit`;
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Export Ready</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background-color: #4F46E5; color: white; padding: 20px; text-align: center; border-radius: 5px 5px 0 0; }
.content { background-color: #f9f9f9; padding: 30px; border-radius: 0 0 5px 5px; }
.button { display: inline-block; padding: 12px 24px; background-color: #4F46E5; color: white; text-decoration: none; border-radius: 5px; font-weight: bold; margin: 20px 0; }
.footer { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; font-size: 12px; color: #666; }
.details { background-color: white; padding: 15px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #4F46E5; }
.details dt { font-weight: bold; margin-top: 10px; }
.details dd { margin-left: 0; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="header"><h1>📊 Your Export is Ready!</h1></div>
<div class="content">
<p>Hello,</p>
<p>Your expense report export has been generated and is ready for download.</p>
<div class="details">
<dl>
<dt>Report Type:</dt><dd>${reportNames[reportType]}</dd>
<dt>File Format:</dt><dd>${formatNames[format]}</dd>
<dt>File Name:</dt><dd>${fileName}</dd>
<dt>Generated At:</dt><dd>${new Date().toLocaleString()}</dd>
</dl>
</div>
<p><a href="${downloadUrl}" class="button">Open Export</a></p>
<p><strong>Important:</strong> This export will expire on ${expiresAt.toLocaleString()}.</p>
<p>If you did not request this export, please contact our support team.</p>
<div class="footer">
<p>Best regards,<br>The StellarSplit Team</p>
<p><small>Need help? Contact <a href="mailto:support@stellarsplit.com">support@stellarsplit.com</a></small></p>
</div>
</div>
</body>
</html>`;
const text = `
Your Export is Ready!
Report Type: ${reportNames[reportType]}
File Format: ${formatNames[format]}
File Name: ${fileName}
Generated At: ${new Date().toLocaleString()}
Open export: ${downloadUrl}
This export expires at ${expiresAt.toLocaleString()}.
Best regards,
The StellarSplit Team
support@stellarsplit.com`;
try {
await this.transporter.sendMail({
from:
this.configService.get<string>("EMAIL_FROM") ??
"exports@stellarsplit.com",
to,
subject,
text,
html,
});
this.logger.log(`Export email sent to ${to}`);
} catch (error) {
this.logger.error(`Failed to send export email:`, error);
throw error;
}
}
async sendScheduledReportEmail(
recipients: string[],
fileName: string,
fileUrl: string,
_templateName: string,
): Promise<void> {
for (const recipient of recipients) {
await this.sendExportEmail(
recipient,
fileName,
fileUrl,
ExportFormat.PDF,
ReportType.MONTHLY_SUMMARY,
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
);
}
}
}