Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

improve the handler

Kiran Ke812f380638f
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.ts
index f48f705dd8..8ad164779b 100644
--- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts
+++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts
@@ -1,7 +1,45 @@
+import { prisma } from "@dub/prisma";
import type Stripe from "stripe";
import { createNewCustomer } from "./utils";
// Handle event "customer.created"
export async function customerCreated(event: Stripe.Event) {
+ const stripeCustomer = event.data.object as Stripe.Customer;
+ const stripeAccountId = event.account as string;
+ const externalId = stripeCustomer.metadata?.dubCustomerId;
+
+ if (externalId) {
+ // Check the customer is not already created
+ // Find customer using projectConnectId and externalId (the customer's ID in the client app)
+ const customerFound = await prisma.customer.findUnique({
+ where: {
+ projectConnectId_externalId: {
+ projectConnectId: stripeAccountId,
+ externalId,
+ },
+ },
+ });
+
+ if (customerFound) {
+ // if customer exists (created via /track/lead)
+ // update it with the Stripe customer ID (for future reference by invoice.paid)
+ await prisma.customer.update({
+ where: {
+ id: customerFound.id,
+ },
+ data: {
+ stripeCustomerId: stripeCustomer.id,
+ projectConnectId: stripeAccountId,
+ },
+ });
+
+ return `Dub customer with ID ${customerFound.id} updated with Stripe customer ID ${stripeCustomer.id}`;
+ }
+ }
+
return await createNewCustomer(event);
}
+
+// customers.create -> customers.update
+// /track/lead -> customers.create -> customers.update
+// /track/lead -> customers.update
\ No newline at end of file
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-updated.ts b/apps/web/app/api/stripe/integration/webhook/customer-updated.ts
index 8f4fed00b0..d8bb5742a1 100644
--- a/apps/web/app/api/stripe/integration/webhook/customer-updated.ts
+++ b/apps/web/app/api/stripe/integration/webhook/customer-updated.ts
@@ -6,21 +6,22 @@ import { createNewCustomer } from "./utils";
export async function customerUpdated(event: Stripe.Event) {
const stripeCustomer = event.data.object as Stripe.Customer;
const stripeCustomerId = stripeCustomer.id;
+ const externalId = stripeCustomer.metadata?.dubCustomerId;
- // An existing customer found with valid stripeCustomerId
+ // Check if an existing Stripe customer exists
const customerFound = await prisma.customer.findUnique({
where: {
stripeCustomerId,
},
});
- // update the customer
if (customerFound) {
await prisma.customer.update({
where: {
id: customerFound.id,
},
data: {
+ externalId,
name: stripeCustomer.name,
email: stripeCustomer.email,
},
@@ -29,6 +30,5 @@ export async function customerUpdated(event: Stripe.Event) {
return `Dub customer with ID ${customerFound.id} updated.`;
}
- // Let's check if the metadata has the dubClickId and dubCustomerId if it does, we can create a new customer
return await createNewCustomer(event);
}
diff --git a/apps/web/app/api/stripe/integration/webhook/utils.ts b/apps/web/app/api/stripe/integration/webhook/utils.ts
index 850686ea89..79c8fe409a 100644
--- a/apps/web/app/api/stripe/integration/webhook/utils.ts
+++ b/apps/web/app/api/stripe/integration/webhook/utils.ts
@@ -3,7 +3,6 @@ import { getClickEvent, recordLead } from "@/lib/tinybird";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformLeadEventData } from "@/lib/webhook/transform";
import { prisma } from "@dub/prisma";
-import { Customer } from "@dub/prisma/client";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
@@ -39,102 +38,74 @@ export async function createNewCustomer(event: Stripe.Event) {
return `Link with ID ${linkId} not found or does not have a project, skipping...`;
}
- // Check the customer is not already created
- // Find customer using projectConnectId and externalId (the customer's ID in the client app)
- const customerFound = await prisma.customer.findUnique({
- where: {
- projectConnectId_externalId: {
- projectConnectId: stripeAccountId,
- externalId,
- },
+ // else create a new customer
+ const customer = await prisma.customer.create({
+ data: {
+ id: createId({ prefix: "cus_" }),
+ name: stripeCustomer.name,
+ email: stripeCustomer.email,
+ stripeCustomerId: stripeCustomer.id,
+ projectConnectId: stripeAccountId,
+ externalId,
+ projectId: link.projectId,
+ linkId,
+ clickId,
+ clickedAt: new Date(clickData.timestamp + "Z"),
+ country: clickData.country,
},
});
- let customer: Customer;
+ const leadData = {
+ ...clickData,
+ event_id: nanoid(16),
+ event_name: "New customer",
+ customer_id: customer.id,
+ };
+
+ const [_lead, _link, workspace] = await Promise.all([
+ // Record lead
+ recordLead(leadData),
- if (customerFound) {
- // if customer exists (created via /track/lead)
- // update it with the Stripe customer ID (for future reference by invoice.paid)
- customer = await prisma.customer.update({
+ // update link leads count
+ prisma.link.update({
where: {
- id: customerFound.id,
- },
- data: {
- stripeCustomerId: stripeCustomer.id,
- projectConnectId: stripeAccountId,
+ id: linkId,
},
- });
- return `Dub customer with ID ${customer.id} updated with Stripe customer ID ${stripeCustomer.id}`;
- } else {
- // else create a new customer
- customer = await prisma.customer.create({
data: {
- id: createId({ prefix: "cus_" }),
- name: stripeCustomer.name,
- email: stripeCustomer.email,
- stripeCustomerId: stripeCustomer.id,
- projectConnectId: stripeAccountId,
- externalId,
- projectId: link.projectId,
- linkId,
- clickId,
- clickedAt: new Date(clickData.timestamp + "Z"),
- country: clickData.country,
- },
- });
-
- const leadData = {
- ...clickData,
- event_id: nanoid(16),
- event_name: "New customer",
- customer_id: customer.id,
- };
-
- const [_lead, _link, workspace] = await Promise.all([
- // Record lead
- recordLead(leadData),
-
- // update link leads count
- prisma.link.update({
- where: {
- id: linkId,
- },
- data: {
- leads: {
- increment: 1,
- },
+ leads: {
+ increment: 1,
},
- }),
+ },
+ }),
- // update workspace usage
- prisma.project.update({
- where: {
- id: customer.projectId,
- },
- data: {
- usage: {
- increment: 1,
- },
+ // update workspace usage
+ prisma.project.update({
+ where: {
+ id: customer.projectId,
+ },
+ data: {
+ usage: {
+ increment: 1,
},
- }),
- ]);
+ },
+ }),
+ ]);
- waitUntil(
- sendWorkspaceWebhook({
- trigger: "lead.created",
- workspace,
- data: transformLeadEventData({
- ...leadData,
- link,
- customerId: customer.id,
- customerExternalId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- customerCreatedAt: customer.createdAt,
- }),
+ waitUntil(
+ sendWorkspaceWebhook({
+ trigger: "lead.created",
+ workspace,
+ data: transformLeadEventData({
+ ...leadData,
+ link,
+ customerId: customer.id,
+ customerExternalId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ customerCreatedAt: customer.createdAt,
}),
- );
- return `New Dub customer created: ${customer.id}. Lead event recorded: ${leadData.event_id}`;
- }
+ }),
+ );
+ return `New Dub customer created: ${customer.id}. Lead event recorded: ${leadData.event_id}`;
}