The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Handle null customer from Shopify webhooks
Steven Teya369fd886128
diff --git a/apps/web/app/(ee)/api/shopify/integration/webhook/orders-paid.ts b/apps/web/app/(ee)/api/shopify/integration/webhook/orders-paid.tsindex f0c5681165..a2e4a77be4 100644--- a/apps/web/app/(ee)/api/shopify/integration/webhook/orders-paid.ts+++ b/apps/web/app/(ee)/api/shopify/integration/webhook/orders-paid.ts@@ -12,29 +12,32 @@ export async function ordersPaid({event: any;workspaceId: string;}) {- const {- customer: { id: externalId },- checkout_token: checkoutToken,- } = orderSchema.parse(event);+ const { customer: orderCustomer, checkout_token: checkoutToken } =+ orderSchema.parse(event);- const customer = await prisma.customer.findUnique({- where: {- projectId_externalId: {- projectId: workspaceId,- externalId: externalId.toString(),- },- },- });+ // If no customer, th+ if (orderCustomer) {+ const { id: externalId } = orderCustomer;- // customer is found, process the order right away- if (customer) {- await processOrder({- event,- workspaceId,- customerId: customer.id,+ const customer = await prisma.customer.findUnique({+ where: {+ projectId_externalId: {+ projectId: workspaceId,+ externalId: externalId.toString(),+ },+ },});- return "[Shopify] Order event processed successfully.";+ // customer is found, process the order right away+ if (customer) {+ await processOrder({+ event,+ workspaceId,+ customerId: customer.id,+ });++ return "[Shopify] Order event processed successfully.";+ }}// Check the cache to see the pixel event for this checkout token exist before publishing the event to the queuediff --git a/apps/web/lib/integrations/shopify/create-lead.ts b/apps/web/lib/integrations/shopify/create-lead.tsindex 6bd5d5778f..76980a2f64 100644--- a/apps/web/lib/integrations/shopify/create-lead.ts+++ b/apps/web/lib/integrations/shopify/create-lead.ts@@ -19,9 +19,19 @@ export async function createShopifyLead({workspaceId: string;event: any;}) {- const {- customer: { id: externalId, email, first_name, last_name },- } = orderSchema.parse(event);+ const { customer: orderCustomer } = orderSchema.parse(event);++ const customerId = createId({ prefix: "cus_" });+ /*+ if orderCustomer is undefined (guest checkout):+ - use the customerId as the externalId+ - generate random name + email+ */+ const externalId = orderCustomer?.id?.toString() || customerId; // need to convert to string because Shopify customer ID is a number+ const name = orderCustomer+ ? `${orderCustomer.first_name} ${orderCustomer.last_name}`.trim()+ : generateRandomName();+ const email = orderCustomer?.email;// find clickconst clickEvent = await getClickEvent({ clickId });@@ -32,11 +42,10 @@ export async function createShopifyLead({// create customerconst customer = await prisma.customer.create({data: {- id: createId({ prefix: "cus_" }),- // need to convert to string because Shopify customer ID is a number- externalId: externalId.toString(),- name: `${first_name} ${last_name}`.trim() || generateRandomName(),- email: email || null,+ id: customerId,+ externalId,+ name,+ email,projectId: workspaceId,clickedAt: new Date(timestamp + "Z"),clickId,diff --git a/apps/web/lib/integrations/shopify/schema.ts b/apps/web/lib/integrations/shopify/schema.tsindex 33740842bb..aed0590c19 100644--- a/apps/web/lib/integrations/shopify/schema.ts+++ b/apps/web/lib/integrations/shopify/schema.ts@@ -3,12 +3,14 @@ import { z } from "zod";export const orderSchema = z.object({confirmation_number: z.string(),checkout_token: z.string(),- customer: z.object({- id: z.number(),- email: z.string().nullish(),- first_name: z.string().nullish(),- last_name: z.string().nullish(),- }),+ customer: z+ .object({+ id: z.number(),+ email: z.string().nullish(),+ first_name: z.string().nullish(),+ last_name: z.string().nullish(),+ })+ .nullish(),current_subtotal_price_set: z.object({shop_money: z.object({