MCPcopy
hub / github.com/dubinc/dub / attributeViaDiscountCode

Function attributeViaDiscountCode

apps/web/lib/integrations/shopify/process-order.ts:79–233  ·  view source on GitHub ↗
({
  event,
  workspace,
  link,
}: {
  event: unknown;
  workspace: Pick<WorkspaceProps, "id" | "defaultProgramId" | "webhookEnabled">;
  link: Link;
})

Source from the content-addressed store, hash-verified

77}
78
79export async function attributeViaDiscountCode({
80 event,
81 workspace,
82 link,
83}: {
84 event: unknown;
85 workspace: Pick<WorkspaceProps, "id" | "defaultProgramId" | "webhookEnabled">;
86 link: Link;
87}) {
88 const { customer: orderCustomer, billing_address: billingAddress } =
89 orderSchema.parse(event);
90
91 const billingAddressCountry = billingAddress?.country_code?.toUpperCase();
92 // Record a fake click for this event
93 const clickEvent = await recordFakeClick({
94 link,
95 customer: {
96 continent: billingAddressCountry
97 ? COUNTRIES_TO_CONTINENTS[billingAddressCountry] ?? "Unknown"
98 : "Unknown",
99 country: billingAddressCountry ?? "Unknown",
100 region: billingAddress?.province ?? "Unknown",
101 },
102 });
103
104 const customerId = createId({ prefix: "cus_" });
105
106 const customer = await prisma.customer.create({
107 data: {
108 id: customerId,
109 name: orderCustomer
110 ? `${orderCustomer.first_name} ${orderCustomer.last_name}`.trim()
111 : generateRandomName(),
112 email: orderCustomer?.email,
113 externalId: orderCustomer?.id?.toString() || customerId,
114 linkId: clickEvent.link_id,
115 clickId: clickEvent.click_id,
116 clickedAt: new Date(clickEvent.timestamp + "Z"),
117 country: billingAddress?.country_code,
118 projectId: workspace.id,
119 programId: link.programId,
120 partnerId: link.partnerId,
121 },
122 });
123
124 // Prepare the payload for the lead event
125 const { timestamp, ...rest } = clickEvent;
126
127 const leadEvent = {
128 ...rest,
129 workspace_id: clickEvent.workspace_id || customer.projectId, // in case for some reason the click event doesn't have workspace_id
130 event_id: nanoid(16),
131 event_name: "Checkout with discount code",
132 customer_id: customer.id,
133 metadata: "",
134 };
135
136 await recordLead(leadEvent);

Callers 1

ordersPaidFunction · 0.90

Calls 11

recordFakeClickFunction · 0.90
createIdFunction · 0.90
generateRandomNameFunction · 0.90
nanoidFunction · 0.90
executeWorkflowsFunction · 0.90
syncPartnerLinksStatsFunction · 0.90
sendWorkspaceWebhookFunction · 0.90
transformLeadEventDataFunction · 0.90
sendPartnerPostbackFunction · 0.90
createMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…