({
event,
workspace,
link,
}: {
event: unknown;
workspace: Pick<WorkspaceProps, "id" | "defaultProgramId" | "webhookEnabled">;
link: Link;
})
| 77 | } |
| 78 | |
| 79 | export 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); |
no test coverage detected
searching dependent graphs…