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.

Fix domains race condition

Steven Tey9871d00c5d4c
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index fe7e2c4eeb..189a012030 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -12,6 +12,7 @@ import {
} from "@/lib/zod/schemas/domains";
import { prisma } from "@dub/prisma";
import { combineWords, DEFAULT_LINK_PROPS, nanoid } from "@dub/utils";
+import { Prisma } from "@prisma/client";
import { NextResponse } from "next/server";
// GET /api/domains – get all domains for a workspace
@@ -91,23 +92,6 @@ export const POST = withWorkspace(
appleAppSiteAssociation,
} = createDomainBodySchema.parse(body);
- const totalDomains = await prisma.domain.count({
- where: {
- projectId: workspace.id,
- },
- });
-
- if (totalDomains >= workspace.domainsLimit) {
- return new Response(
- exceededLimitError({
- plan: workspace.plan,
- limit: workspace.domainsLimit,
- type: "domains",
- }),
- { status: 403 },
- );
- }
-
if (workspace.plan === "free") {
if (
logo ||
@@ -157,34 +141,57 @@ export const POST = withWorkspace(
? await storage.upload(`domains/${domainId}/logo_${nanoid(7)}`, logo)
: null;
- const [domainRecord, _] = await Promise.all([
- prisma.domain.create({
- data: {
- id: domainId,
- slug: slug,
- projectId: workspace.id,
- primary: totalDomains === 0,
- ...(placeholder && { placeholder }),
- expiredUrl,
- notFoundUrl,
- ...(logoUploaded && { logo: logoUploaded.url }),
- ...(assetLinks && { assetLinks: JSON.parse(assetLinks) }),
- ...(appleAppSiteAssociation && {
- appleAppSiteAssociation: JSON.parse(appleAppSiteAssociation),
- }),
- },
- }),
+ const domainRecord = await prisma.$transaction(
+ async (tx) => {
+ const totalDomains = await tx.domain.count({
+ where: {
+ projectId: workspace.id,
+ },
+ });
- createLink({
- ...DEFAULT_LINK_PROPS,
- domain: slug,
- key: "_root",
- url: "",
- tags: undefined,
- userId: session.user.id,
- projectId: workspace.id,
- }),
- ]);
+ if (totalDomains >= workspace.domainsLimit) {
+ throw new DubApiError({
+ code: "exceeded_limit",
+ message: exceededLimitError({
+ plan: workspace.plan,
+ limit: workspace.domainsLimit,
+ type: "domains",
+ }),
+ });
+ }
+ return await tx.domain.create({
+ data: {
+ id: domainId,
+ slug: slug,
+ projectId: workspace.id,
+ primary: totalDomains === 0,
+ ...(placeholder && { placeholder }),
+ expiredUrl,
+ notFoundUrl,
+ ...(logoUploaded && { logo: logoUploaded.url }),
+ ...(assetLinks && { assetLinks: JSON.parse(assetLinks) }),
+ ...(appleAppSiteAssociation && {
+ appleAppSiteAssociation: JSON.parse(appleAppSiteAssociation),
+ }),
+ },
+ });
+ },
+ {
+ isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
+ maxWait: 5000,
+ timeout: 5000,
+ },
+ );
+
+ await createLink({
+ ...DEFAULT_LINK_PROPS,
+ domain: slug,
+ key: "_root",
+ url: "",
+ tags: undefined,
+ userId: session.user.id,
+ projectId: workspace.id,
+ });
return NextResponse.json(
transformDomain({