fix: forward order created webhook to N8N
- Updated to forward to N8N at /webhook/saleor-order-created - N8N will handle multi-language email sending
This commit is contained in:
@@ -4,7 +4,9 @@ import {
|
|||||||
OrderCreatedWebhookPayloadFragment,
|
OrderCreatedWebhookPayloadFragment,
|
||||||
} from "@/generated/graphql";
|
} from "@/generated/graphql";
|
||||||
import { saleorApp } from "@/saleor-app";
|
import { saleorApp } from "@/saleor-app";
|
||||||
import { sendOrderConfirmationEmail, formatPrice } from "@/lib/resend";
|
|
||||||
|
// N8N webhook URL for Order Created
|
||||||
|
const N8N_WEBHOOK_URL = "https://n8n.nodecrew.me/webhook/saleor-order-created";
|
||||||
|
|
||||||
export const orderCreatedWebhook = new SaleorAsyncWebhook<OrderCreatedWebhookPayloadFragment>({
|
export const orderCreatedWebhook = new SaleorAsyncWebhook<OrderCreatedWebhookPayloadFragment>({
|
||||||
name: "Order Created in Saleor",
|
name: "Order Created in Saleor",
|
||||||
@@ -15,73 +17,29 @@ export const orderCreatedWebhook = new SaleorAsyncWebhook<OrderCreatedWebhookPay
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default orderCreatedWebhook.createHandler(async (req, res, ctx) => {
|
export default orderCreatedWebhook.createHandler(async (req, res, ctx) => {
|
||||||
const { payload, event, baseUrl, authData } = ctx;
|
const { payload, event, authData } = ctx;
|
||||||
const order = payload.order;
|
|
||||||
|
|
||||||
if (!order) {
|
|
||||||
console.error("No order data in webhook payload");
|
|
||||||
return res.status(200).end();
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`🎉 Order #${order.number} created for customer: ${order.userEmail} (${order.languageCode || "EN"})`);
|
console.log(`Order created: ${payload.order?.number} for ${payload.order?.userEmail}`);
|
||||||
|
console.log(`Forwarding to N8N: ${N8N_WEBHOOK_URL}`);
|
||||||
|
|
||||||
const items = ((order as any).lines || []).map((line: any) => ({
|
|
||||||
id: line.id,
|
|
||||||
name: line.variant?.product?.name || "Unknown Product",
|
|
||||||
quantity: line.quantity,
|
|
||||||
price: formatPrice(line.totalPrice?.gross?.amount || 0, line.totalPrice?.gross?.currency || "USD"),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const orderData = {
|
|
||||||
orderId: order.id,
|
|
||||||
orderNumber: order.number || "Unknown",
|
|
||||||
customerEmail: order.userEmail || "",
|
|
||||||
customerName: (order as any).shippingAddress?.firstName
|
|
||||||
? `${(order as any).shippingAddress.firstName} ${(order as any).shippingAddress.lastName || ""}`.trim()
|
|
||||||
: order.userEmail?.split("@")[0] || "Customer",
|
|
||||||
items,
|
|
||||||
total: formatPrice((order as any).total?.gross?.amount || 0, (order as any).total?.gross?.currency || "USD"),
|
|
||||||
shippingAddress: (order as any).shippingAddress
|
|
||||||
? `${(order as any).shippingAddress.firstName || ""} ${(order as any).shippingAddress.lastName || ""}\n${(order as any).shippingAddress.streetAddress1 || ""}\n${(order as any).shippingAddress.postalCode || ""} ${(order as any).shippingAddress.city || ""}\n${(order as any).shippingAddress.country?.country || ""}${(order as any).shippingAddress.phone ? `\nPhone: ${(order as any).shippingAddress.phone}` : ""}`
|
|
||||||
: undefined,
|
|
||||||
billingAddress: (order as any).billingAddress
|
|
||||||
? `${(order as any).billingAddress.firstName || ""} ${(order as any).billingAddress.lastName || ""}\n${(order as any).billingAddress.streetAddress1 || ""}\n${(order as any).billingAddress.postalCode || ""} ${(order as any).billingAddress.city || ""}\n${(order as any).billingAddress.country?.country || ""}${(order as any).billingAddress.phone ? `\nPhone: ${(order as any).billingAddress.phone}` : ""}`
|
|
||||||
: undefined,
|
|
||||||
phone: (order as any).shippingAddress?.phone,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send admin notification
|
|
||||||
try {
|
try {
|
||||||
const adminEmails = process.env.ADMIN_EMAILS?.split(",").map(e => e.trim()).filter(e => e) || [];
|
// Forward to N8N
|
||||||
|
const response = await fetch(N8N_WEBHOOK_URL, {
|
||||||
if (adminEmails.length > 0) {
|
method: "POST",
|
||||||
await sendOrderConfirmationEmail({
|
headers: {
|
||||||
to: adminEmails,
|
"Content-Type": "application/json",
|
||||||
orderData,
|
"x-saleor-event": "order.created",
|
||||||
isAdmin: true,
|
},
|
||||||
});
|
body: JSON.stringify(payload),
|
||||||
console.log(`✅ Admin notification sent for order #${order.number} to: ${adminEmails.join(", ")}`);
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(`N8N returned ${response.status}: ${await response.text()}`);
|
||||||
} else {
|
} else {
|
||||||
console.log("⚠️ No admin emails configured, skipping admin notification");
|
console.log(`Successfully forwarded to N8N: ${response.status}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("❌ Failed to send admin email:", error);
|
console.error(`Failed to forward to N8N:`, error);
|
||||||
}
|
|
||||||
|
|
||||||
// Send customer confirmation
|
|
||||||
try {
|
|
||||||
if (order.userEmail) {
|
|
||||||
await sendOrderConfirmationEmail({
|
|
||||||
to: order.userEmail,
|
|
||||||
orderData,
|
|
||||||
isAdmin: false,
|
|
||||||
});
|
|
||||||
console.log(`✅ Customer confirmation sent for order #${order.number} to: ${order.userEmail}`);
|
|
||||||
} else {
|
|
||||||
console.log("⚠️ No customer email found, skipping customer notification");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("❌ Failed to send customer email:", error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).end();
|
return res.status(200).end();
|
||||||
@@ -91,4 +49,4 @@ export const config = {
|
|||||||
api: {
|
api: {
|
||||||
bodyParser: false,
|
bodyParser: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user