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.

listen for promotion_code.updated" webhook

Kiran K94ed3ff5f798
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts
new file mode 100644
index 0000000000..6184792b1a
--- /dev/null
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts
@@ -0,0 +1,54 @@
+import { prisma } from "@dub/prisma";
+import type Stripe from "stripe";
+
+// Handle event "promotion_code.updated"
+export async function promotionCodeUpdated(event: Stripe.Event) {
+ const promotionCode = event.data.object as Stripe.PromotionCode;
+ const stripeAccountId = event.account as string;
+
+ const workspace = await prisma.project.findUnique({
+ where: {
+ stripeConnectId: stripeAccountId,
+ },
+ select: {
+ id: true,
+ slug: true,
+ defaultProgramId: true,
+ stripeConnectId: true,
+ },
+ });
+
+ if (!workspace) {
+ return `Workspace not found for Stripe account ${stripeAccountId}.`;
+ }
+
+ if (!workspace.defaultProgramId) {
+ return `Workspace ${workspace.id} for stripe account ${stripeAccountId} has no programs.`;
+ }
+
+ if (promotionCode.active) {
+ return `Promotion code ${promotionCode.id} is active.`;
+ }
+
+ // If the promotion code is not active, we need to remove them from Dub
+ const discountCode = await prisma.discountCode.findUnique({
+ where: {
+ programId_code: {
+ programId: workspace.defaultProgramId,
+ code: promotionCode.code,
+ },
+ },
+ });
+
+ if (!discountCode) {
+ return `Discount code not found for Stripe promotion code ${promotionCode.id}.`;
+ }
+
+ await prisma.discountCode.delete({
+ where: {
+ id: discountCode.id,
+ },
+ });
+
+ return `Discount code ${discountCode.id} deleted from the program ${workspace.defaultProgramId}.`;
+}
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 2bce10e331..2ac7876f2f 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
@@ -9,6 +9,7 @@ import { couponDeleted } from "./coupon-deleted";
import { customerCreated } from "./customer-created";
import { customerUpdated } from "./customer-updated";
import { invoicePaid } from "./invoice-paid";
+import { promotionCodeUpdated } from "./promotion-code-updated";
const relevantEvents = new Set([
"customer.created",
@@ -18,6 +19,7 @@ const relevantEvents = new Set([
"charge.refunded",
"account.application.deauthorized",
"coupon.deleted",
+ "promotion_code.updated",
]);
// POST /api/stripe/integration/webhook – listen to Stripe webhooks (for Stripe Integration)
@@ -80,6 +82,9 @@ export const POST = withAxiom(async (req: Request) => {
case "coupon.deleted":
response = await couponDeleted(event);
break;
+ case "promotion_code.updated":
+ response = await promotionCodeUpdated(event);
+ break;
}
return logAndRespond(response);