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.

update the webhook cache

Kiran K343e3108a404
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
index 7f987e7e25..83e2e2040b 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
@@ -1,5 +1,6 @@
import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
+import { webhookCache } from "@/lib/webhook/cache";
import { prisma } from "@dub/prisma";
import { FREE_PLAN, log } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -177,4 +178,20 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
},
}),
]);
+
+ // Update the webhooks cache
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ projectId: workspace.id,
+ },
+ select: {
+ id: true,
+ url: true,
+ secret: true,
+ triggers: true,
+ disabledAt: true,
+ },
+ });
+
+ await webhookCache.mset(webhooks);
}
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
index 81312f2771..77c9238981 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
@@ -1,3 +1,4 @@
+import { webhookCache } from "@/lib/webhook/cache";
import { prisma } from "@dub/prisma";
import { getPlanFromPriceId, log } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -88,26 +89,46 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
rateLimit: plan.limits.api,
},
}),
-
- // Disable the webhooks if the new plan does not support webhooks
- ...(shouldDisableWebhooks
- ? [
- prisma.webhook.updateMany({
- where: { projectId: workspace.id },
- data: { disabledAt: new Date() },
- }),
-
- prisma.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- webhookEnabled: false,
- },
- }),
- ]
- : []),
]);
+
+ // Disable the webhooks if the new plan does not support webhooks
+ if (shouldDisableWebhooks) {
+ await Promise.all([
+ prisma.project.update({
+ where: {
+ id: workspace.id,
+ },
+ data: {
+ webhookEnabled: false,
+ },
+ }),
+
+ prisma.webhook.updateMany({
+ where: {
+ projectId: workspace.id,
+ },
+ data: {
+ disabledAt: new Date(),
+ },
+ }),
+ ]);
+
+ // Update the webhooks cache
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ projectId: workspace.id,
+ },
+ select: {
+ id: true,
+ url: true,
+ secret: true,
+ triggers: true,
+ disabledAt: true,
+ },
+ });
+
+ await webhookCache.mset(webhooks);
+ }
} else if (workspace.paymentFailedAt) {
await prisma.project.update({
where: {
diff --git a/apps/web/lib/webhook/cache.ts b/apps/web/lib/webhook/cache.ts
index c3ab07a8cd..35cac1230e 100644
--- a/apps/web/lib/webhook/cache.ts
+++ b/apps/web/lib/webhook/cache.ts
@@ -5,16 +5,27 @@ import { isLinkLevelWebhook } from "./utils";
const WEBHOOK_CACHE_KEY_PREFIX = "webhook";
class WebhookCache {
+ async mset(webhooks: WebhookCacheProps[]) {
+ if (webhooks.length === 0) {
+ return;
+ }
+
+ const pipeline = redis.pipeline();
+
+ webhooks.map((webhook) => {
+ pipeline.set(this._createKey(webhook.id), this._format(webhook));
+ });
+
+ return await pipeline.exec();
+ }
+
async set(webhook: WebhookCacheProps) {
// We only cache the link level webhooks for now
if (!isLinkLevelWebhook(webhook)) {
return;
}
- return await redis.set(
- this._createKey(webhook.id),
- JSON.stringify(this._format(webhook)),
- );
+ return await redis.set(this._createKey(webhook.id), this._format(webhook));
}
async mget(webhookIds: string[]) {
@@ -29,6 +40,20 @@ class WebhookCache {
return await redis.del(this._createKey(webhookId));
}
+ // async deleteMany(webhookIds: string[]) {
+ // if (webhookIds.length === 0) {
+ // return;
+ // }
+
+ // const pipeline = redis.pipeline();
+
+ // webhookIds.forEach((webhookId) => {
+ // pipeline.del(this._createKey(webhookId));
+ // });
+
+ // return await pipeline.exec();
+ // }
+
_format(webhook: WebhookCacheProps) {
return {
id: webhook.id,