Skip to content

Commit

Permalink
migrate script, free projects limit
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Jan 21, 2024
1 parent 9cd1a5b commit 92e6c05
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
4 changes: 1 addition & 3 deletions apps/web/app/api/callback/stripe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { PLANS, getPlanFromPriceId, log } from "@dub/utils";
import { FREE_PLAN, getPlanFromPriceId, log } from "@dub/utils";
import { resend, sendEmail } from "emails";
import UpgradeEmail from "emails/upgrade-email";
import { NextResponse } from "next/server";
Expand Down Expand Up @@ -231,8 +231,6 @@ 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: {
Expand Down
10 changes: 7 additions & 3 deletions apps/web/app/api/projects/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
import { withSession } from "@/lib/auth";
import { isReservedKey } from "@/lib/edge-config";
import prisma from "@/lib/prisma";
import { DEFAULT_REDIRECTS, validSlugRegex } from "@dub/utils";
import {
DEFAULT_REDIRECTS,
FREE_PROJECTS_LIMIT,
validSlugRegex,
} from "@dub/utils";

// GET /api/projects - get all projects for the current user
export const GET = withSession(async ({ session }) => {
Expand Down Expand Up @@ -80,9 +84,9 @@ export const POST = withSession(async ({ req, session }) => {
},
});

if (freeProjects >= 1) {
if (freeProjects >= FREE_PROJECTS_LIMIT) {
return new Response(
"You can only create up to 2 free projects. Additional projects require a paid plan.",
`You can only create up to ${FREE_PROJECTS_LIMIT} free projects. Additional projects require a paid plan.`,
{ status: 403 },
);
}
Expand Down
56 changes: 56 additions & 0 deletions apps/web/scripts/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "dotenv-flow/config";
import prisma from "@/lib/prisma";

const DOMAIN = "x.com";
const PROJECT_ID = "xxx";

async function main() {
const [links, project] = await Promise.all([
prisma.link.updateMany({
where: {
domain: DOMAIN,
},
data: {
projectId: PROJECT_ID,
},
}),
prisma.project.findFirst({
where: {
domains: {
some: {
slug: DOMAIN,
},
},
},
}),
]);

const response = await Promise.all([
prisma.domain.update({
where: {
slug: DOMAIN,
},
data: {
projectId: PROJECT_ID,
primary: false,
},
}),
prisma.project.update({
where: {
id: PROJECT_ID,
},
data: {
usage: {
increment: project?.usage,
},
linksUsage: {
increment: links.count,
},
},
}),
]);

console.log({ response });
}

main();
4 changes: 2 additions & 2 deletions apps/web/ui/projects/create-project-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import useProjects from "@/lib/swr/use-projects";
import { ModalContext } from "@/ui/modals/provider";
import { Button } from "@dub/ui";
import { TooltipContent } from "@dub/ui/src/tooltip";
import { HOME_DOMAIN } from "@dub/utils";
import { FREE_PROJECTS_LIMIT, HOME_DOMAIN } from "@dub/utils";
import { useContext } from "react";

export default function CreateProjectButton() {
Expand All @@ -18,7 +18,7 @@ export default function CreateProjectButton() {
disabledTooltip={
exceedingFreeProjects ? (
<TooltipContent
title="You can only create up to 2 free projects. Additional projects require a paid plan."
title={`You can only create up to ${FREE_PROJECTS_LIMIT} free projects. Additional projects require a paid plan.`}
cta="Upgrade to Pro"
href={
freeProjects
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/constants/pricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ export const PLANS = [
},
];

export const FREE_PLAN = PLANS.find((plan) => plan.name === "free")!;

export const SELF_SERVE_PAID_PLANS = PLANS.filter(
(p) => p.name === "Pro" || p.name === "Business",
);

export const FREE_PROJECTS_LIMIT = 2;

0 comments on commit 92e6c05

Please sign in to comment.