Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down