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.

fix: clear subscriptionCanceledAt on subscription.created

Cursor Agent75c2dec32dda
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts
index 6e2cd5fd9e..1e145e678f 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts
@@ -9,23 +9,45 @@ import { WebhookHandlerInput, WebhookHandlerResponse } from "./types";
import { getConnectedCustomer } from "./utils/get-connected-customer";
// Handle event "customer.subscription.created"
-// only used for recording free trial creations
+// - non-trial: clear subscriptionCanceledAt (e.g. resubscriptions)
+// - trialing: record free trial lead (when enabled)
export async function customerSubscriptionCreated({
event,
mode,
workspace,
}: WebhookHandlerInput<Stripe.CustomerSubscriptionCreatedEvent>): Promise<WebhookHandlerResponse> {
const createdSubscription = event.data.object;
+ const stripeAccountId = event.account as string;
+ const stripeCustomerId = createdSubscription.customer as string;
+ // Non-trial subscription created — clear any prior cancellation timestamp
if (createdSubscription.status !== "trialing") {
+ const customer = await prisma.customer.findUnique({
+ where: {
+ stripeCustomerId,
+ },
+ });
+
+ if (!customer) {
+ return {
+ response: `Customer with stripeCustomerId ${stripeCustomerId} not found, skipping...`,
+ };
+ }
+
+ await prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ subscriptionCanceledAt: null,
+ },
+ });
+
return {
- response: "Subscription is not in trialing status, skipping...",
+ response: `Subscription created (non-trial), cleared subscriptionCanceledAt for customer ${customer.id}`,
};
}
- const stripeAccountId = event.account as string;
- const stripeCustomerId = createdSubscription.customer as string;
-
const installedIntegration = await prisma.installedIntegration.findFirst({
where: {
projectId: workspace.id,
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
index d5f2f8b5c9..6a1136e27e 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
@@ -222,7 +222,7 @@ export async function invoicePaid({
linkId,
});
- const [_sale, linkUpdated, , customerUpdated] = await Promise.all([
+ const [_sale, linkUpdated] = await Promise.all([
recordSale(saleData),
// update link stats
@@ -278,7 +278,6 @@ export async function invoicePaid({
increment: invoiceSaleAmount,
},
firstSaleAt: customer.firstSaleAt ? undefined : new Date(),
- subscriptionCanceledAt: null,
},
}),
]);
@@ -395,7 +394,7 @@ export async function invoicePaid({
...saleData,
clickedAt: customer.clickedAt || customer.createdAt,
link: linkUpdated,
- customer: customerUpdated,
+ customer,
partner: result?.webhookPartner,
metadata: null,
}),
@@ -424,7 +423,7 @@ export async function invoicePaid({
...saleData,
clickedAt: customer.clickedAt || customer.createdAt,
link: linkUpdated,
- customer: customerUpdated,
+ customer,
},
}),
]