Add detailed logging to email send endpoint
This commit is contained in:
@@ -232,6 +232,8 @@ export const emailRoutes = new Elysia({ prefix: '/emails' })
|
||||
|
||||
// Send email
|
||||
.post('/:id/send', async ({ params, user }: { params: { id: string }; user: User }) => {
|
||||
console.log(`[${new Date().toISOString()}] Send email request for id: ${params.id}`);
|
||||
|
||||
// Get email
|
||||
const [email] = await db.select({
|
||||
email: communications,
|
||||
@@ -247,19 +249,29 @@ export const emailRoutes = new Elysia({ prefix: '/emails' })
|
||||
.limit(1);
|
||||
|
||||
if (!email) {
|
||||
console.log(`[${new Date().toISOString()}] Email not found or already sent`);
|
||||
throw new Error('Email not found or already sent');
|
||||
}
|
||||
|
||||
if (!email.client.email) {
|
||||
console.log(`[${new Date().toISOString()}] Client has no email address`);
|
||||
throw new Error('Client has no email address');
|
||||
}
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Sending email to: ${email.client.email}`);
|
||||
|
||||
// Send via Resend
|
||||
try {
|
||||
await sendEmail({
|
||||
to: email.client.email,
|
||||
subject: email.email.subject || 'Message from your advisor',
|
||||
content: email.email.content,
|
||||
});
|
||||
console.log(`[${new Date().toISOString()}] Email sent successfully`);
|
||||
} catch (e) {
|
||||
console.error(`[${new Date().toISOString()}] Failed to send email:`, e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Update status
|
||||
const [updated] = await db.update(communications)
|
||||
|
||||
@@ -23,8 +23,12 @@ export interface SendEmailParams {
|
||||
|
||||
export async function sendEmail(params: SendEmailParams) {
|
||||
const client = getResendClient();
|
||||
const fromAddress = params.from || process.env.DEFAULT_FROM_EMAIL || 'onboarding@resend.dev';
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Resend: sending from ${fromAddress} to ${params.to}`);
|
||||
|
||||
const { data, error } = await client.emails.send({
|
||||
from: params.from || process.env.DEFAULT_FROM_EMAIL || 'onboarding@resend.dev',
|
||||
from: fromAddress,
|
||||
to: params.to,
|
||||
subject: params.subject,
|
||||
text: params.content,
|
||||
@@ -32,8 +36,10 @@ export async function sendEmail(params: SendEmailParams) {
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error(`[${new Date().toISOString()}] Resend error:`, error);
|
||||
throw new Error(`Failed to send email: ${error.message}`);
|
||||
}
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Resend success:`, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user