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