import { Resend } from "resend"; let resendClient: Resend | null = null; function getResendClient(): Resend { if (!resendClient) { if (!process.env.RESEND_API_KEY) { throw new Error("RESEND_API_KEY environment variable is not set"); } resendClient = new Resend(process.env.RESEND_API_KEY); } return resendClient; } const FROM_EMAIL = process.env.FROM_EMAIL || "support@mail.manoonoils.com"; const FROM_NAME = process.env.FROM_NAME || "ManoonOils"; const ADMIN_EMAILS = (process.env.ADMIN_EMAILS || "me@hytham.me,tamara@hytham.me").split(","); const SITE_URL = process.env.SITE_URL || "https://dev.manoonoils.com"; export { FROM_EMAIL, FROM_NAME, ADMIN_EMAILS, SITE_URL }; function formatPrice(amount: number, currency: string): string { if (currency === "RSD") { return new Intl.NumberFormat("sr-RS", { style: "currency", currency: "RSD", minimumFractionDigits: 2, maximumFractionDigits: 2, }).format(amount); } return new Intl.NumberFormat("en-US", { style: "currency", currency, minimumFractionDigits: 2, maximumFractionDigits: 2, }).format(amount); } function getCustomerName(order: any): string { if (order.user?.firstName) { return `${order.user.firstName} ${order.user.lastName || ""}`.trim(); } if (order.billingAddress?.firstName) { return `${order.billingAddress.firstName} ${order.billingAddress.lastName || ""}`.trim(); } return "Customer"; } function formatAddress(address: any): string { if (!address) return ""; const parts = [ address.streetAddress1, address.streetAddress2, address.city, address.postalCode, address.country?.code, ].filter(Boolean); return parts.join(", "); } async function sendEmail({ to, subject, html, tags, idempotencyKey, }: { to: string | string[]; subject: string; html: string; tags?: { name: string; value: string }[]; idempotencyKey?: string; }) { const resend = getResendClient(); const { data, error } = await resend.emails.send({ from: `${FROM_NAME} <${FROM_EMAIL}>`, to: Array.isArray(to) ? to : [to], subject, html, tags, ...(idempotencyKey && { idempotencyKey }), }); if (error) { console.error("Failed to send email:", error); throw error; } return data; } export async function sendOrderConfirmationEmail(order: any) { const customerName = getCustomerName(order); const total = formatPrice(order.total?.gross?.amount || 0, order.total?.gross?.currency || "RSD"); const html = `

Order Confirmation #${order.number}

Hello ${customerName},

Thank you for your order! Here are your order details:

Order #${order.number}

Total: ${total}

${order.shippingAddress ? `

Shipping Address:

${formatAddress(order.shippingAddress)}

` : ""}

You can view your order details here.

Thank you for shopping with us!

`; return sendEmail({ to: order.userEmail || order.user?.email || "", subject: `Order Confirmation #${order.number}`, html, tags: [{ name: "type", value: "order-confirmation" }], idempotencyKey: `order-confirmed/${order.id}`, }); } export async function sendOrderShippedEmail(order: any, trackingNumber?: string, trackingUrl?: string) { const customerName = getCustomerName(order); const html = `

Your Order #${order.number} Has Shipped!

Hello ${customerName},

Great news! Your order has been shipped.

${trackingNumber ? `

Tracking Number: ${trackingNumber}

` : ""} ${trackingUrl ? `

Track your package

` : ""}

You can view your order details here.

`; return sendEmail({ to: order.userEmail || order.user?.email || "", subject: `Your Order #${order.number} Has Shipped!`, html, tags: [{ name: "type", value: "order-shipped" }], idempotencyKey: `order-shipped/${order.id}`, }); } export async function sendOrderCancelledEmail(order: any, reason?: string) { const customerName = getCustomerName(order); const html = `

Order #${order.number} Cancelled

Hello ${customerName},

Your order #${order.number} has been cancelled.

${reason ? `

Reason: ${reason}

` : ""}

If you have any questions, please contact us.

`; return sendEmail({ to: order.userEmail || order.user?.email || "", subject: `Order #${order.number} Cancelled`, html, tags: [{ name: "type", value: "order-cancelled" }], idempotencyKey: `order-cancelled/${order.id}`, }); } export async function sendOrderPaidEmail(order: any) { const customerName = getCustomerName(order); const html = `

Payment Received - Order #${order.number}

Hello ${customerName},

We have received your payment for order #${order.number}.

Total Paid: ${formatPrice(order.total?.gross?.amount || 0, order.total?.gross?.currency || "RSD")}

Thank you for your purchase!

`; return sendEmail({ to: order.userEmail || order.user?.email || "", subject: `Payment Received - Order #${order.number}`, html, tags: [{ name: "type", value: "order-paid" }], idempotencyKey: `order-paid/${order.id}`, }); } export async function sendAdminNotification(order: any, eventType: string) { if (ADMIN_EMAILS.length === 0) { console.warn("No admin emails configured"); return; } const total = formatPrice(order.total?.gross?.amount || 0, order.total?.gross?.currency || "RSD"); const html = `

[Admin] ${eventType} - Order #${order.number}

Customer: ${order.userEmail || order.user?.email || "N/A"}

Total: ${total}

Items: ${order.lines?.length || 0}

View Order

`; return sendEmail({ to: ADMIN_EMAILS, subject: `[Admin] ${eventType} - Order #${order.number}`, html, tags: [{ name: "type", value: "admin-notification" }], idempotencyKey: `admin-${eventType}/${order.id}`, }); }