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.

remove plan requirement for sync

Steven Tey6a713321631c
diff --git a/apps/web/app/api/links/sync/route.ts b/apps/web/app/api/links/sync/route.ts
index 617417af1d..251b58baca 100644
--- a/apps/web/app/api/links/sync/route.ts
+++ b/apps/web/app/api/links/sync/route.ts
@@ -2,67 +2,74 @@ import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
import { LinkProps, SimpleLinkProps } from "@/lib/types";
+import { exceededLimitError } from "@/lib/api/errors";
// POST /api/links/sync – sync user's publicly created links to their accounts
-export const POST = withAuth(
- async ({ req, session, project }) => {
- let links: SimpleLinkProps[] = [];
- try {
- links = await req.json();
- if (!Array.isArray(links)) {
- throw new Error("Invalid request body.");
- }
- } catch (e) {
- return new Response("Invalid request body.", { status: 400 });
+export const POST = withAuth(async ({ req, session, project }) => {
+ let links: SimpleLinkProps[] = [];
+ try {
+ links = await req.json();
+ if (!Array.isArray(links)) {
+ throw new Error("Invalid request body.");
}
+ } catch (e) {
+ return new Response("Invalid request body.", { status: 400 });
+ }
- const unclaimedLinks = (await Promise.all(
- links.map(async (link) => {
- return await prisma.link.findUnique({
- where: {
- domain_key: {
- domain: link.domain,
- key: link.key,
- },
- userId: null,
- },
- });
- }),
- )) as LinkProps[];
-
- if (unclaimedLinks.length === 0) {
- return new Response("No links created.", { status: 400 });
- }
-
- const response = await Promise.all([
- prisma.link.updateMany({
+ const unclaimedLinks = (await Promise.all(
+ links.map(async (link) => {
+ return await prisma.link.findUnique({
where: {
- id: {
- in: unclaimedLinks.map((link) => link.id),
+ domain_key: {
+ domain: link.domain,
+ key: link.key,
},
+ userId: null,
},
- data: {
- userId: session.user.id,
- projectId: project.id,
- publicStats: false,
- },
+ });
+ }),
+ )) as LinkProps[];
+
+ if (unclaimedLinks.length === 0) {
+ return new Response("No links created.", { status: 400 });
+ }
+
+ if (project.linksUsage + unclaimedLinks.length > project.linksLimit) {
+ return new Response(
+ exceededLimitError({
+ plan: project.plan,
+ limit: project.linksLimit,
+ type: "links",
}),
- prisma.project.update({
- where: {
- id: project.id,
+ { status: 403 },
+ );
+ }
+
+ const response = await Promise.all([
+ prisma.link.updateMany({
+ where: {
+ id: {
+ in: unclaimedLinks.map((link) => link.id),
},
- data: {
- // TODO: sync clicks usage as well
- linksUsage: {
- increment: unclaimedLinks.length,
- },
+ },
+ data: {
+ userId: session.user.id,
+ projectId: project.id,
+ publicStats: false,
+ },
+ }),
+ prisma.project.update({
+ where: {
+ id: project.id,
+ },
+ data: {
+ // TODO: sync clicks usage as well
+ linksUsage: {
+ increment: unclaimedLinks.length,
},
- }),
- ]);
+ },
+ }),
+ ]);
- return NextResponse.json(response);
- },
- {
- requiredPlan: ["pro", "enterprise"],
- },
-);
+ return NextResponse.json(response);
+});