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.

simplify to `syncCustomer`

Steven Teyf8b594ae88a8
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
index cf7b50cef2..9a9a6e218b 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
@@ -9,13 +9,12 @@ import { accountApplicationDeauthorized } from "./account-application-deauthoriz
import { chargeRefunded } from "./charge-refunded";
import { checkoutSessionCompleted } from "./checkout-session-completed";
import { couponDeleted } from "./coupon-deleted";
-import { customerCreated } from "./customer-created";
import { customerSubscriptionCreated } from "./customer-subscription-created";
import { customerSubscriptionDeleted } from "./customer-subscription-deleted";
-import { customerUpdated } from "./customer-updated";
import { invoicePaid } from "./invoice-paid";
import { promotionCodeUpdated } from "./promotion-code-updated";
import { resolveWebhookWorkspace } from "./utils/resolve-webhook-workspace";
+import { syncCustomer } from "./utils/sync-customer";
import { StripeWebhookOutput } from "./utils/types";
const relevantEvents = new Set([
@@ -136,13 +135,8 @@ export const POST = withAxiom(async (req: Request) => {
});
break;
case "customer.created":
- result = await customerCreated({
- event,
- workspace,
- });
- break;
case "customer.updated":
- result = await customerUpdated({
+ result = await syncCustomer({
event,
workspace,
});
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts
index 968c5a3471..5eeb066067 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts
@@ -15,15 +15,14 @@ import { nanoid, pick } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
-
-type SyncCustomerResponse = {
- response: string;
- workspaceId?: string;
-};
-
-export async function syncCustomer(
- event: Stripe.CustomerCreatedEvent | Stripe.CustomerUpdatedEvent,
-): Promise<SyncCustomerResponse> {
+import { StripeWebhookInput, StripeWebhookOutput } from "./types";
+
+export async function syncCustomer({
+ event,
+ workspace,
+}: Omit<StripeWebhookInput, "mode"> & {
+ event: Stripe.CustomerCreatedEvent | Stripe.CustomerUpdatedEvent;
+}): Promise<StripeWebhookOutput> {
const stripeCustomer = event.data.object;
const stripeAccountId = event.account as string;
const dubCustomerExternalId =
@@ -43,22 +42,6 @@ export async function syncCustomer(
};
}
- const workspace = await prisma.project.findUnique({
- where: {
- stripeConnectId: stripeAccountId,
- },
- select: {
- id: true,
- webhookEnabled: true,
- },
- });
-
- if (!workspace) {
- return {
- response: `Workspace not found for Stripe account ${stripeAccountId}, skipping...`,
- };
- }
-
const existingCustomer = await prisma.customer.findFirst({
where: {
OR: [
@@ -94,7 +77,6 @@ export async function syncCustomer(
if (!clickId) {
return {
response: "Click ID not found in Stripe customer metadata, skipping...",
- workspaceId: workspace.id,
};
}
@@ -105,7 +87,6 @@ export async function syncCustomer(
if (!clickData) {
return {
response: `Click event with ID ${clickId} not found, skipping...`,
- workspaceId: workspace.id,
};
}
@@ -125,14 +106,12 @@ export async function syncCustomer(
if (!link || !link.projectId) {
return {
response: `Link with ID ${linkId} not found or does not have a project, skipping...`,
- workspaceId: workspace.id,
};
}
if (link.projectId !== workspace.id) {
return {
response: `Link ${linkId} does not belong to workspace ${workspace.id}, skipping...`,
- workspaceId: workspace.id,
};
}
@@ -302,19 +281,17 @@ export async function syncCustomer(
return {
response: `New Dub customer created: ${customer.id}. Lead event recorded: ${leadData.event_id}`,
- workspaceId: workspace.id,
};
}
async function updateStripeCustomer({
customerId,
- workspaceId,
data,
}: {
customerId: string;
workspaceId: string;
data: Prisma.CustomerUncheckedUpdateInput;
-}): Promise<SyncCustomerResponse> {
+}): Promise<{ response: string }> {
try {
await prisma.customer.update({
where: {
@@ -325,12 +302,10 @@ async function updateStripeCustomer({
return {
response: `Dub customer with ID ${customerId} updated.`,
- workspaceId,
};
} catch (error) {
return {
response: `Error updating Dub customer with ID ${customerId}: ${error}`,
- workspaceId,
};
}
}