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.

implement limits for domains, tags, and users

Steven Tey610b4d67dd44
diff --git a/apps/web/app/api/callback/stripe/route.ts b/apps/web/app/api/callback/stripe/route.ts
index 0d2a9a3aec..dbf3b5d495 100644
--- a/apps/web/app/api/callback/stripe/route.ts
+++ b/apps/web/app/api/callback/stripe/route.ts
@@ -2,7 +2,7 @@ import { limiter } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { stripe } from "@/lib/stripe";
import { redis } from "@/lib/upstash";
-import { getPlanFromPriceId, log } from "@dub/utils";
+import { PLANS, getPlanFromPriceId, log } from "@dub/utils";
import { resend, sendEmail } from "emails";
import UpgradeEmail from "emails/upgrade-email";
import { NextResponse } from "next/server";
@@ -76,6 +76,7 @@ export const POST = async (req: Request) => {
billingCycleStart: new Date().getDate(),
usageLimit: plan.limits.clicks!,
linksLimit: plan.limits.links!,
+ domainsLimit: plan.limits.domains!,
plan: plan.name.toLowerCase(),
},
select: {
@@ -158,6 +159,7 @@ export const POST = async (req: Request) => {
data: {
usageLimit: plan.limits.clicks!,
linksLimit: plan.limits.links!,
+ domainsLimit: plan.limits.domains!,
plan: plan.name.toLowerCase(),
},
select: {
@@ -224,15 +226,18 @@ export const POST = async (req: Request) => {
pipeline.del(`root:${domain}`);
});
+ const FREE_PLAN = PLANS.find((plan) => plan.name === "free")!;
+
await Promise.allSettled([
prisma.project.update({
where: {
stripeId,
},
data: {
- usageLimit: 1000,
- linksLimit: 25,
plan: "free",
+ usageLimit: FREE_PLAN.limits.clicks!,
+ linksLimit: FREE_PLAN.limits.links!,
+ domainsLimit: FREE_PLAN.limits.domains!,
},
}),
pipeline.exec(),
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index a80c4e12ce..8af082462e 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -3,6 +3,7 @@ import { withAuth } from "@/lib/auth";
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { capitalize, getBillingStartDate } from "@dub/utils";
+import { exceededLimitError } from "@/lib/api/errors";
// POST /api/links/bulk – bulk create up to 100 links
export const POST = withAuth(
@@ -33,11 +34,11 @@ export const POST = withAuth(
}
if (project.linksUsage + links.length > project.linksLimit) {
return new Response(
- `You can only create ${
- project.linksLimit
- }/month short links on the ${capitalize(
- project.plan,
- )} plan. Upgrade to create more links.`,
+ exceededLimitError({
+ plan: project.plan,
+ limit: project.linksLimit,
+ type: "links",
+ }),
{ status: 403, headers },
);
}
diff --git a/apps/web/app/api/projects/[slug]/domains/route.ts b/apps/web/app/api/projects/[slug]/domains/route.ts
index 8f013fa903..aedc12a48d 100644
--- a/apps/web/app/api/projects/[slug]/domains/route.ts
+++ b/apps/web/app/api/projects/[slug]/domains/route.ts
@@ -6,6 +6,8 @@ import {
setRootDomain,
validateDomain,
} from "@/lib/api/domains";
+import { capitalize } from "@dub/utils";
+import { exceededLimitError } from "@/lib/api/errors";
// GET /api/projects/[slug]/domains – get all domains for a project
export const GET = withAuth(async ({ project }) => {
@@ -29,6 +31,18 @@ export const GET = withAuth(async ({ project }) => {
// POST /api/projects/[slug]/domains - add a domain
export const POST = withAuth(async ({ req, project }) => {
const { slug: domain, primary, target, type } = await req.json();
+
+ if (project.domains && project.domains?.length >= project.domainsLimit) {
+ return new Response(
+ exceededLimitError({
+ plan: project.plan,
+ limit: project.domainsLimit,
+ type: "domains",
+ }),
+ { status: 403 },
+ );
+ }
+
const validDomain = await validateDomain(domain);
if (validDomain !== true) {
return new Response(validDomain, { status: 422 });
diff --git a/apps/web/app/api/projects/[slug]/invites/route.tsx b/apps/web/app/api/projects/[slug]/invites/route.tsx
index efad5c7d73..cbb2a0a1b3 100644
--- a/apps/web/app/api/projects/[slug]/invites/route.tsx
+++ b/apps/web/app/api/projects/[slug]/invites/route.tsx
@@ -2,6 +2,7 @@ import { withAuth } from "@/lib/auth";
import { NextResponse } from "next/server";
import { inviteUser } from "@/lib/api/users";
import prisma from "@/lib/prisma";
+import { exceededLimitError } from "@/lib/api/errors";
// GET /api/projects/[slug]/invites – get invites for a specific project
export const GET = withAuth(async ({ project }) => {
@@ -35,25 +36,27 @@ export const POST = withAuth(
});
}
- if (project.plan === "free") {
- const users = await prisma.projectUsers.count({
- where: {
- projectId: project.id,
- },
- });
- const invites = await prisma.projectInvite.count({
- where: {
- projectId: project.id,
+ const users = await prisma.projectUsers.count({
+ where: {
+ projectId: project.id,
+ },
+ });
+ const invites = await prisma.projectInvite.count({
+ where: {
+ projectId: project.id,
+ },
+ });
+ if (users + invites >= project.usersLimit) {
+ return new Response(
+ exceededLimitError({
+ plan: project.plan,
+ limit: project.usersLimit,
+ type: "users",
+ }),
+ {
+ status: 403,
},
- });
- if (users + invites >= 3) {
- return new Response(
- "You've reached the maximum number of users for the free plan.",
- {
- status: 400,
- },
- );
- }
+ );
}
try {
diff --git a/apps/web/app/api/projects/[slug]/tags/route.ts b/apps/web/app/api/projects/[slug]/tags/route.ts
index f8d6a4de12..1c8d18963c 100644
--- a/apps/web/app/api/projects/[slug]/tags/route.ts
+++ b/apps/web/app/api/projects/[slug]/tags/route.ts
@@ -1,3 +1,4 @@
+import { exceededLimitError } from "@/lib/api/errors";
import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { randomBadgeColor } from "@/ui/links/tag-badge";
@@ -28,9 +29,13 @@ export const POST = withAuth(async ({ req, project, headers }) => {
projectId: project.id,
},
});
- if (project.plan === "free" && tagsCount >= 3) {
+ if (tagsCount >= project.tagsLimit) {
return new Response(
- "You can only create 3 tags in the Free plan. Upgrade to Pro to create unlimited tags.",
+ exceededLimitError({
+ plan: project.plan,
+ limit: project.tagsLimit,
+ type: "tags",
+ }),
{
status: 403,
},
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
new file mode 100644
index 0000000000..8ba018eb0e
--- /dev/null
+++ b/apps/web/lib/api/errors.ts
@@ -0,0 +1,18 @@
+import { capitalize } from "@dub/utils";
+import { PlanProps } from "../types";
+
+export const exceededLimitError = ({
+ plan,
+ limit,
+ type,
+}: {
+ plan: PlanProps;
+ limit: number;
+ type: "links" | "domains" | "tags" | "users";
+}) => {
+ return `You've reached your ${
+ type === "links" ? "monthly" : ""
+ } limit of ${limit} ${
+ limit === 1 ? type.slice(0, -1) : type
+ } on the ${capitalize(plan)} plan. Please upgrade to add more ${type}.`;
+};
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index d8a78ecd77..06e9f3db94 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -12,8 +12,6 @@ import {
DUB_PROJECT_ID,
LEGAL_USER_ID,
SHORT_DOMAIN,
- capitalize,
- getBillingStartDate,
getDomainWithoutWWW,
getParamsFromURL,
getUrlFromString,
@@ -26,6 +24,7 @@ import cloudinary from "cloudinary";
import { isIframeable } from "../middleware/utils";
import { LinkProps, ProjectProps, SimpleLinkProps } from "../types";
import { Session } from "../auth";
+import { exceededLimitError } from "./errors";
export async function getLinksForProject({
projectId,
@@ -273,11 +272,11 @@ export async function processLink({
if (project.linksUsage >= project.linksLimit) {
return {
link: payload,
- error: `You can only create ${
- project.linksLimit
- }/month short links on the ${capitalize(
- project.plan,
- )} plan. Upgrade to create more links.`,
+ error: exceededLimitError({
+ plan: project.plan,
+ limit: project.linksLimit,
+ type: "links",
+ }),
status: 403,
};
}
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index f1d029c7c0..8f48eb021b 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -216,6 +216,8 @@ export const withAuth =
linksUsage: true,
linksLimit: true,
domainsLimit: true,
+ tagsLimit: true,
+ usersLimit: true,
plan: true,
stripeId: true,
billingCycleStart: true,
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 99842a518f..7f5265acc5 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -46,6 +46,8 @@ export interface ProjectProps {
linksUsage: number;
linksLimit: number;
domainsLimit: number;
+ tagsLimit: number;
+ usersLimit: number;
plan: PlanProps;
stripeId?: string;
billingCycleStart: number;
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index e1c43f5b8f..fb4abf377c 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -86,8 +86,6 @@ model Project {
slug String @unique
logo String?
- users ProjectUsers[]
- invites ProjectInvite[]
plan String @default("free")
stripeId String? @unique // Stripe subscription ID
billingCycleStart Int // day of the month when the billing cycle starts
@@ -96,11 +94,15 @@ model Project {
linksUsage Int @default(0)
linksLimit Int @default(25)
domainsLimit Int @default(1)
+ tagsLimit Int @default(3)
+ usersLimit Int @default(1)
monitoringId String? @unique // cron job ID for monitoring
+ users ProjectUsers[]
+ invites ProjectInvite[]
sentEmails SentEmail[]
- domains Domain[]
links Link[]
+ domains Domain[]
tags Tag[]
createdAt DateTime @default(now())
diff --git a/apps/web/scripts/sync-limits.ts b/apps/web/scripts/sync-limits.ts
new file mode 100644
index 0000000000..0dc8ba9984
--- /dev/null
+++ b/apps/web/scripts/sync-limits.ts
@@ -0,0 +1,18 @@
+import "dotenv-flow/config";
+import prisma from "@/lib/prisma";
+
+async function main() {
+ const response = await prisma.project.updateMany({
+ where: {
+ plan: "pro",
+ },
+ data: {
+ linksLimit: 250,
+ domainsLimit: 10,
+ },
+ });
+
+ console.log(response);
+}
+
+main();
diff --git a/packages/ui/package.json b/packages/ui/package.json
index 517fa0cab4..1fc502f295 100644
--- a/packages/ui/package.json
+++ b/packages/ui/package.json
@@ -1,7 +1,7 @@
{
"name": "@dub/ui",
"description": "UI components for Dub.co",
- "version": "0.0.41",
+ "version": "0.0.42",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
diff --git a/packages/utils/package.json b/packages/utils/package.json
index e4454fe0c7..b206882397 100644
--- a/packages/utils/package.json
+++ b/packages/utils/package.json
@@ -1,7 +1,7 @@
{
"name": "@dub/utils",
"description": "Utility functions and constants for Dub.co",
- "version": "0.0.19",
+ "version": "0.0.21",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
diff --git a/packages/utils/src/constants/pricing.tsx b/packages/utils/src/constants/pricing.tsx
index 528bebed44..72e82c2d90 100644
--- a/packages/utils/src/constants/pricing.tsx
+++ b/packages/utils/src/constants/pricing.tsx
@@ -1,5 +1,3 @@
-import { APP_DOMAIN, HOME_DOMAIN } from ".";
-
export const PLANS = [
{
name: "Free",
@@ -11,6 +9,7 @@ export const PLANS = [
limits: {
links: 25,
clicks: 1000,
+ domains: 1,
},
colors: {
bg: "bg-black",
@@ -18,7 +17,7 @@ export const PLANS = [
},
cta: {
text: "Start for free",
- href: APP_DOMAIN,
+ href: "https://app.dub.co/register",
color: "bg-black border-black hover:text-black",
},
featureTitle: "What's included:",
@@ -39,7 +38,7 @@ export const PLANS = [
footnote: {
title: "Programatically manage your links using our REST API.",
cta: "Learn more.",
- href: `${HOME_DOMAIN}/docs/api-reference/introduction`,
+ href: "https://dub.co/docs/api-reference/introduction",
},
},
],
@@ -58,6 +57,7 @@ export const PLANS = [
limits: {
links: 250,
clicks: 25000,
+ domains: 10,
},
colors: {
bg: "bg-blue-500",
@@ -66,7 +66,7 @@ export const PLANS = [
cta: {
text: "Get started with Pro",
shortText: "Get started",
- href: APP_DOMAIN,
+ href: "https://app.dub.co/register",
color: "bg-blue-500 border-blue-500 hover:text-blue-500",
},
featureTitle: "Everything in Free, plus:",
@@ -85,7 +85,7 @@ export const PLANS = [
title:
"Redirect vistors that land on the root of your domain (e.g. yourdomain.com) to a page of your choice.",
cta: "Learn more.",
- href: `${HOME_DOMAIN}/help/article/how-to-redirect-root-domain`,
+ href: "https://dub.co/help/article/how-to-redirect-root-domain",
},
},
{
@@ -106,6 +106,7 @@ export const PLANS = [
limits: {
links: 1000,
clicks: 100000,
+ domains: 20,
},
colors: {
bg: "bg-sky-900",
@@ -114,7 +115,7 @@ export const PLANS = [
cta: {
text: "Get started with Business",
shortText: "Get started",
- href: APP_DOMAIN,
+ href: "https://app.dub.co/register",
color: "bg-sky-900 border-sky-900 hover:text-sky-900",
},
featureTitle: "Everything in Pro, plus:",
@@ -133,7 +134,7 @@ export const PLANS = [
title:
"Set custom QR code logos, password-protected links logos, and more.",
cta: "Learn more.",
- href: `${HOME_DOMAIN}/help/article/custom-qr-codes`,
+ href: "https://dub.co/help/article/custom-qr-codes",
},
},
{
@@ -151,6 +152,7 @@ export const PLANS = [
limits: {
links: null,
clicks: null,
+ domains: null,
},
colors: {
bg: "bg-violet-600",