The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Rate limiting changes when plan change
Kiran K5c8c6509a160
diff --git a/apps/web/app/api/stripe/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/webhook/checkout-session-completed.tsindex caf14863b5..24ab8af4d5 100644--- a/apps/web/app/api/stripe/webhook/checkout-session-completed.ts+++ b/apps/web/app/api/stripe/webhook/checkout-session-completed.ts@@ -1,6 +1,8 @@+import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";import { limiter } from "@/lib/cron/limiter";import { prisma } from "@/lib/prisma";import { stripe } from "@/lib/stripe";+import { PlanProps } from "@/lib/types";import { getPlanFromPriceId, log } from "@dub/utils";import { sendEmail } from "emails";import UpgradeEmail from "emails/upgrade-email";@@ -36,6 +38,8 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {}const stripeId = checkoutSession.customer.toString();+ const workspaceId = checkoutSession.client_reference_id;+ const planName = plan.name.toLowerCase();// when the workspace subscribes to a plan, set their stripe customer ID// in the database for easy identification in future webhook events@@ -43,12 +47,12 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {const workspace = await prisma.project.update({where: {- id: checkoutSession.client_reference_id,+ id: workspaceId,},data: {stripeId,billingCycleStart: new Date().getDate(),- plan: plan.name.toLowerCase(),+ plan: planName,usageLimit: plan.limits.clicks!,linksLimit: plan.limits.links!,domainsLimit: plan.limits.domains!,@@ -70,6 +74,15 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {},});+ await prisma.restrictedToken.updateMany({+ where: {+ projectId: workspaceId,+ },+ data: {+ rateLimit: getAPIRateLimitForPlan(planName as PlanProps),+ },+ });+const users = workspace.users.map(({ user }) => ({name: user.name,email: user.email,diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.tsindex f32aa59746..7ad174dccb 100644--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts@@ -1,3 +1,4 @@+import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";import { prisma } from "@/lib/prisma";import { redis } from "@/lib/upstash";import { FREE_PLAN, log } from "@dub/utils";@@ -73,6 +74,16 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {usersLimit: FREE_PLAN.limits.users!,},}),++ prisma.restrictedToken.updateMany({+ where: {+ projectId: workspace.id,+ },+ data: {+ rateLimit: getAPIRateLimitForPlan("free"),+ },+ }),+pipeline.exec(),log({message:diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.tsindex 75c50ea530..15c8e7419b 100644--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts@@ -1,4 +1,6 @@+import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";import { prisma } from "@/lib/prisma";+import { PlanProps } from "@/lib/types";import { getPlanFromPriceId, log } from "@dub/utils";import { NextResponse } from "next/server";import Stripe from "stripe";@@ -54,5 +56,14 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {usersLimit: plan.limits.users!,},});++ prisma.restrictedToken.updateMany({+ where: {+ projectId: workspace.id,+ },+ data: {+ rateLimit: getAPIRateLimitForPlan(newPlan as PlanProps),+ },+ });}}diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.tsindex 03dc311104..3457ab7e3c 100644--- a/apps/web/app/api/tokens/route.ts+++ b/apps/web/app/api/tokens/route.ts@@ -1,4 +1,4 @@-import { getRateLimitForPlan } from "@/lib/api/tokens/ratelimit";+import { getAPIRateLimitForPlan } from "@/lib/api/tokens/ratelimit";import { parseRequestBody } from "@/lib/api/utils";import { hashToken, withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";@@ -51,7 +51,7 @@ export const POST = withWorkspace(partialKey,userId: isMachine ? machineUser?.id! : session.user.id,projectId: workspace.id,- rateLimit: getRateLimitForPlan(workspace.plan),+ rateLimit: getAPIRateLimitForPlan(workspace.plan),scopes:scopes && scopes.length > 0 ? [...new Set(scopes)].join(" ") : null,},diff --git a/apps/web/lib/api/tokens/ratelimit.ts b/apps/web/lib/api/tokens/ratelimit.tsindex 86e14ddffd..8ce69af105 100644--- a/apps/web/lib/api/tokens/ratelimit.ts+++ b/apps/web/lib/api/tokens/ratelimit.ts@@ -1,7 +1,7 @@import { PlanProps } from "@/lib/types";-// Get rate limit for a given plan in requests per minute-export const getRateLimitForPlan = (plan: PlanProps) => {+// Get API rate limit for a given plan in requests per minute+export const getAPIRateLimitForPlan = (plan: PlanProps) => {switch (plan) {case "free":return 600;