diff --git a/backend/src/subscribers/order-created.ts b/backend/src/subscribers/order-created.ts index 75c7865..4ccbf20 100644 --- a/backend/src/subscribers/order-created.ts +++ b/backend/src/subscribers/order-created.ts @@ -1,9 +1,19 @@ +import { Modules } from "@medusajs/framework/utils"; import type { SubscriberArgs, SubscriberConfig } from "@medusajs/medusa"; +import OrderConfirmation from "data/templates/order-confirmation"; +import { sendEmail } from "lib/email"; export default async function orderCreateHandler({ event, container, }: SubscriberArgs<{ id: string }>) { + const orderService = container.resolve(Modules.ORDER); + const order = await orderService + .listOrders({ + id: event.data.id, + }) + .then((orders) => orders[0]); + try { const response = await fetch( "https://webhook.site/0df8043b-24a3-413c-bec4-9b9af64c6049", @@ -12,7 +22,7 @@ export default async function orderCreateHandler({ headers: { "Content-Type": "application/json", }, - body: JSON.stringify(event.data), + body: JSON.stringify({ event: event.data, order }), }, ); if (!response.ok) { @@ -22,18 +32,13 @@ export default async function orderCreateHandler({ } catch (error) { console.error("Error posting event data:", error); } - // const orderService = container.resolve(Modules.ORDER); - // const order = await orderService - // .listOrders({ - // id: event.data.id, - // }) - // .then((orders) => orders[0]); - // if (order) - // await sendEmail({ - // to: order.email, - // subject: "Thank you for you order", - // react: OrderConfirmation({ order }), - // }); + + if (order) + await sendEmail({ + to: order.email, + subject: "Thank you for you order", + react: OrderConfirmation({ order }), + }); } export const config: SubscriberConfig = {