This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
import { Resend } from 'resend';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ReportExpenseData, ReportDayLogsData } from '@utils/types';
|
||||
|
||||
const SWEEGO_API_URL = 'https://api.sweego.io/send';
|
||||
|
||||
export class ExpenseReporter {
|
||||
private resend: Resend;
|
||||
private senderEmail: string;
|
||||
private recipientEmail: string;
|
||||
private sweegoApiKey: string;
|
||||
|
||||
constructor() {
|
||||
if (!process.env.RESEND_API_KEY) {
|
||||
throw new Error('RESEND_API_KEY environment variable is not set');
|
||||
if (!process.env.SWEEGO_API_KEY) {
|
||||
throw new Error('SWEEGO_API_KEY environment variable is not set');
|
||||
}
|
||||
if (!process.env.SENDER_EMAIL) {
|
||||
throw new Error('SENDER_EMAIL environment variable is not set');
|
||||
if (!process.env.SWEEGO_FROM) {
|
||||
throw new Error('SWEEGO_FROM environment variable is not set');
|
||||
}
|
||||
if (!process.env.RECIPIENT_EMAIL) {
|
||||
throw new Error('RECIPIENT_EMAIL environment variable is not set');
|
||||
}
|
||||
this.resend = new Resend(process.env.RESEND_API_KEY);
|
||||
this.senderEmail = process.env.SENDER_EMAIL;
|
||||
this.sweegoApiKey = process.env.SWEEGO_API_KEY;
|
||||
this.senderEmail = process.env.SWEEGO_FROM;
|
||||
this.recipientEmail = process.env.RECIPIENT_EMAIL;
|
||||
}
|
||||
|
||||
@@ -276,16 +277,28 @@ export class ExpenseReporter {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.resend.emails.send({
|
||||
from: this.senderEmail,
|
||||
to: this.recipientEmail,
|
||||
subject: `Diary Report: ${from.toLocaleDateString('en-GB')} - ${to.toLocaleDateString('en-GB')}`,
|
||||
html: htmlContent,
|
||||
attachments
|
||||
const response = await fetch(SWEEGO_API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Api-Key': this.sweegoApiKey
|
||||
},
|
||||
body: JSON.stringify({
|
||||
channel: 'email',
|
||||
provider: 'sweego',
|
||||
recipients: [{ email: this.recipientEmail }],
|
||||
from: {
|
||||
email: this.senderEmail
|
||||
},
|
||||
subject: `Diary Report: ${from.toLocaleDateString('en-GB')} - ${to.toLocaleDateString('en-GB')}`,
|
||||
'message-html': htmlContent,
|
||||
...(attachments.length > 0 && { attachments })
|
||||
})
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
throw new Error('Failed to send email: No id returned from Resend');
|
||||
if (!response.ok) {
|
||||
const error = await response.text();
|
||||
throw new Error(`Sweego API error: ${response.status} ${error}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to send email:', error);
|
||||
|
||||
Reference in New Issue
Block a user