diff --git a/packages/modules/b2c-core/src/subscribers/notification-seller-new-order.ts b/packages/modules/b2c-core/src/subscribers/notification-seller-new-order.ts index c307c43c..83808206 100644 --- a/packages/modules/b2c-core/src/subscribers/notification-seller-new-order.ts +++ b/packages/modules/b2c-core/src/subscribers/notification-seller-new-order.ts @@ -8,52 +8,58 @@ import { export default async function sellerNewOrderHandler({ event, container, -}: SubscriberArgs<{ id: string }>) { +}: SubscriberArgs<{ order_ids: string[] }>) { const notificationService = container.resolve(Modules.NOTIFICATION); const query = container.resolve(ContainerRegistrationKeys.QUERY); - const { - data: [order], - } = await query.graph({ - entity: "order", - fields: [ - "id", - "display_id", - "items.*", - "seller.email", - "seller.name", - "seller.id", - "customer.first_name", - "customer.last_name", - ], - filters: { - id: event.data.id, - }, - }); + for (const orderId of event.data.order_ids) { + try { + const { + data: [order], + } = await query.graph({ + entity: "order", + fields: [ + "id", + "display_id", + "items.*", + "seller.email", + "seller.name", + "seller.id", + "customer.first_name", + "customer.last_name", + ], + filters: { + id: orderId, + }, + }); - if (!order) { - console.error("Order not found:", event.data.id); - return; - } + if (!order) { + console.error("Order not found:", orderId); + continue; + } - const sellerEmail = order.seller?.email; - if (!sellerEmail) { - console.error("Seller email not found for order:", order.id); - return; - } + const sellerEmail = order.seller?.email; + if (!sellerEmail) { + console.error("Seller email not found for order:", order.id); + continue; + } - const customer_name = `${order.customer?.first_name || ""} ${order.customer?.last_name || ""}`; - await notificationService.createNotifications([ - { - to: order.seller?.id, - channel: "seller_feed", - template: "seller_new_order_notification", - data: { - order_id: order.id, - customer_name, - }, - }, - ]); + const customer_name = `${order.customer?.first_name || ""} ${order.customer?.last_name || ""}`; + await notificationService.createNotifications([ + { + to: order.seller?.id, + channel: "seller_feed", + template: "seller_new_order_notification", + data: { + order_id: order.id, + customer_name, + }, + }, + ]); + } catch (error) { + console.error(`Error processing seller notification for order ${orderId}:`, error); + } + } } export const config: SubscriberConfig = { diff --git a/packages/modules/b2c-core/src/workflows/cart/workflows/split-and-complete-cart.ts b/packages/modules/b2c-core/src/workflows/cart/workflows/split-and-complete-cart.ts index e4110fbe..4cdca11b 100644 --- a/packages/modules/b2c-core/src/workflows/cart/workflows/split-and-complete-cart.ts +++ b/packages/modules/b2c-core/src/workflows/cart/workflows/split-and-complete-cart.ts @@ -351,9 +351,9 @@ export const splitAndCompleteCartWorkflow = createWorkflow( const orderEvents = transform({ createdOrders }, ({ createdOrders }) => ({ eventName: OrderWorkflowEvents.PLACED, - data: createdOrders.map((order) => ({ - id: order.id, - })), + data: { + order_ids: createdOrders.map((order) => order.id), + }, })); parallelize( diff --git a/packages/modules/resend/src/subscribers/notification-buyer-new-order.ts b/packages/modules/resend/src/subscribers/notification-buyer-new-order.ts index a607a948..0b9a4080 100644 --- a/packages/modules/resend/src/subscribers/notification-buyer-new-order.ts +++ b/packages/modules/resend/src/subscribers/notification-buyer-new-order.ts @@ -12,57 +12,63 @@ import { Hosts, buildHostAddress } from "@mercurjs/framework"; export default async function orderCreatedHandler({ event, container, -}: SubscriberArgs<{ id: string }>) { +}: SubscriberArgs<{ order_ids: string[] }>) { const notificationService = container.resolve(Modules.NOTIFICATION); const query = container.resolve(ContainerRegistrationKeys.QUERY); - const { - data: [order], - } = await query.graph({ - entity: "order", - fields: [ - "*", - "customer.*", - "items.*", - "shipping_address.*", - "shipping_methods.*", - "summary.*", - "order_set.*" - ], - filters: { - id: event.data.id, - }, - }); + for (const orderId of event.data.order_ids) { + try { + const { + data: [order], + } = await query.graph({ + entity: "order", + fields: [ + "*", + "customer.*", + "items.*", + "shipping_address.*", + "shipping_methods.*", + "summary.*", + "order_set.*" + ], + filters: { + id: orderId, + }, + }); - if (!order ) { - return; - } + if (!order ) { + continue; + } - const orderUrl = buildHostAddress( - Hosts.STOREFRONT, - `/user/orders/${order.order_set.id ?? order.id}` - ).toString(); + const orderUrl = buildHostAddress( + Hosts.STOREFRONT, + `/user/orders/${order.order_set.id ?? order.id}` + ).toString(); - await notificationService.createNotifications({ - to: order.email, - channel: "email", - template: ResendNotificationTemplates.BUYER_NEW_ORDER, - content: { - subject: `Order Confirmation - #${order.display_id}`, - }, - data: { - data: { - user_name: order.customer?.first_name || "Customer", - order_id: order.id, - order_address: orderUrl, - order: { - ...order, - display_id: order.display_id, - total: order.summary?.current_order_total || 0, + await notificationService.createNotifications({ + to: order.email, + channel: "email", + template: ResendNotificationTemplates.BUYER_NEW_ORDER, + content: { + subject: `Order Confirmation - #${order.display_id}`, }, - }, - }, - }); + data: { + data: { + user_name: order.customer?.first_name || "Customer", + order_id: order.id, + order_address: orderUrl, + order: { + ...order, + display_id: order.display_id, + total: order.summary?.current_order_total || 0, + }, + }, + }, + }); + } catch (error) { + console.error(`Error processing buyer notification for order ${orderId}:`, error); + } + } } export const config: SubscriberConfig = { diff --git a/packages/modules/resend/src/subscribers/notification-seller-new-order.ts b/packages/modules/resend/src/subscribers/notification-seller-new-order.ts index 710bfc1a..6bbf9dcc 100644 --- a/packages/modules/resend/src/subscribers/notification-seller-new-order.ts +++ b/packages/modules/resend/src/subscribers/notification-seller-new-order.ts @@ -10,68 +10,74 @@ import { ResendNotificationTemplates } from "../providers/resend"; export default async function sellerNewOrderHandler({ event, container, -}: SubscriberArgs<{ id: string }>) { +}: SubscriberArgs<{ order_ids: string[] }>) { const notificationService = container.resolve(Modules.NOTIFICATION); const query = container.resolve(ContainerRegistrationKeys.QUERY); - const { - data: [order], - } = await query.graph({ - entity: "order", - fields: [ - "id", - "display_id", - "items.*", - "seller.email", - "seller.name", - "seller.id", - "customer.first_name", - "customer.last_name", - ], - filters: { - id: event.data.id, - }, - }); + for (const orderId of event.data.order_ids) { + try { + const { + data: [order], + } = await query.graph({ + entity: "order", + fields: [ + "id", + "display_id", + "items.*", + "seller.email", + "seller.name", + "seller.id", + "customer.first_name", + "customer.last_name", + ], + filters: { + id: orderId, + }, + }); - if (!order) { - console.error("Order not found:", event.data.id); - return; - } + if (!order) { + console.error("Order not found:", orderId); + continue; + } - const sellerEmail = order.seller?.email; - if (!sellerEmail) { - console.error("Seller email not found for order:", order.id); - return; - } + const sellerEmail = order.seller?.email; + if (!sellerEmail) { + console.error("Seller email not found for order:", order.id); + continue; + } - const customer_name = `${order.customer?.first_name || ""} ${order.customer?.last_name || ""}`; - await notificationService.createNotifications([ - { - to: order.seller?.id, - channel: "seller_feed", - template: "seller_new_order_notification", - data: { - order_id: order.id, - customer_name, - }, - }, - { - to: sellerEmail, - channel: "email", - template: ResendNotificationTemplates.SELLER_NEW_ORDER, - content: { - subject: `New order #${order.display_id} received`, - }, - data: { - data: { - order_id: order.id, - order, - customer_name, - seller_name: order.seller?.name || "", + const customer_name = `${order.customer?.first_name || ""} ${order.customer?.last_name || ""}`; + await notificationService.createNotifications([ + { + to: order.seller?.id, + channel: "seller_feed", + template: "seller_new_order_notification", + data: { + order_id: order.id, + customer_name, + }, }, - }, - }, - ]); + { + to: sellerEmail, + channel: "email", + template: ResendNotificationTemplates.SELLER_NEW_ORDER, + content: { + subject: `New order #${order.display_id} received`, + }, + data: { + data: { + order_id: order.id, + order, + customer_name, + seller_name: order.seller?.name || "", + }, + }, + }, + ]); + } catch (error) { + console.error(`Error processing seller notification for order ${orderId}:`, error); + } + } } export const config: SubscriberConfig = {