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.

update skipIdentifierChecks

Steven Tey908b06cdae79
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 2c83265bca..1bacf840d9 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -106,6 +106,10 @@ export const PATCH = withWorkspace(
link.domain === updatedLink.domain &&
link.key.toLowerCase() === updatedLink.key?.toLowerCase();
+ // if externalId is the same, we don't need to check if it exists
+ const skipExternalIdChecks =
+ link.externalId?.toLowerCase() === updatedLink.externalId?.toLowerCase();
+
// if identifier is the same, we don't need to check if it exists
const skipIdentifierChecks =
link.identifier?.toLowerCase() === updatedLink.identifier?.toLowerCase();
@@ -118,6 +122,7 @@ export const PATCH = withWorkspace(
payload: updatedLink,
workspace,
skipKeyChecks,
+ skipExternalIdChecks,
skipIdentifierChecks,
});
@@ -150,13 +155,6 @@ export const PATCH = withWorkspace(
headers,
});
} catch (error) {
- if (error.code === "P2002") {
- throw new DubApiError({
- code: "conflict",
- message: "A link with this externalId already exists.",
- });
- }
-
throw new DubApiError({
code: "unprocessable_entity",
message: error.message,
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 5c5f821ee3..fec5a263d8 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -68,6 +68,7 @@ export const POST = withWorkspace(
workspace,
userId: session.user.id,
bulk: true,
+ skipExternalIdChecks: true,
skipIdentifierChecks: true,
}),
),
@@ -233,6 +234,7 @@ export const PATCH = withWorkspace(
userId: link.userId ?? undefined,
bulk: true,
skipKeyChecks: true,
+ skipExternalIdChecks: true,
skipIdentifierChecks: true,
}),
),
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 6f405ffea1..a853a7651b 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -112,13 +112,6 @@ export const POST = withWorkspace(
return NextResponse.json(response, { headers });
} catch (error) {
- if (error.code === "P2002") {
- throw new DubApiError({
- code: "conflict",
- message: "A link with this externalId already exists.",
- });
- }
-
throw new DubApiError({
code: "unprocessable_entity",
message: error.message,
diff --git a/apps/web/app/api/links/upsert/route.ts b/apps/web/app/api/links/upsert/route.ts
index f1654ea78e..78ab53b675 100644
--- a/apps/web/app/api/links/upsert/route.ts
+++ b/apps/web/app/api/links/upsert/route.ts
@@ -80,6 +80,11 @@ export const PUT = withWorkspace(
link.domain === updatedLink.domain &&
link.key.toLowerCase() === updatedLink.key?.toLowerCase();
+ // if externalId is the same, we don't need to check if it exists
+ const skipExternalIdChecks =
+ link.externalId?.toLowerCase() ===
+ updatedLink.externalId?.toLowerCase();
+
// if identifier is the same, we don't need to check if it exists
const skipIdentifierChecks =
link.identifier?.toLowerCase() ===
@@ -93,6 +98,7 @@ export const PUT = withWorkspace(
payload: updatedLink,
workspace,
skipKeyChecks,
+ skipExternalIdChecks,
skipIdentifierChecks,
});
@@ -117,13 +123,6 @@ export const PUT = withWorkspace(
headers,
});
} catch (error) {
- if (error.code === "P2002") {
- throw new DubApiError({
- code: "conflict",
- message: "A link with this externalId already exists.",
- });
- }
-
throw new DubApiError({
code: "unprocessable_entity",
message: error.message,
@@ -148,13 +147,6 @@ export const PUT = withWorkspace(
const response = await createLink(link);
return NextResponse.json(response, { headers });
} catch (error) {
- if (error.code === "P2002") {
- throw new DubApiError({
- code: "conflict",
- message: "A link with this externalId already exists.",
- });
- }
-
throw new DubApiError({
code: "unprocessable_entity",
message: error.message,
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index 4f5d518447..e77b03d813 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -26,6 +26,7 @@ export async function processLink<T extends Record<string, any>>({
bulk = false,
skipKeyChecks = false, // only skip when key doesn't change (e.g. when editing a link)
skipIdentifierChecks = false, // only skip when identifier doesn't change (e.g. when editing a link)
+ skipExternalIdChecks = false, // only skip when externalId doesn't change (e.g. when editing a link)
}: {
payload: NewLinkProps & T;
workspace?: Pick<WorkspaceProps, "id" | "plan" | "conversionEnabled">;
@@ -33,6 +34,7 @@ export async function processLink<T extends Record<string, any>>({
bulk?: boolean;
skipKeyChecks?: boolean;
skipIdentifierChecks?: boolean;
+ skipExternalIdChecks?: boolean;
}): Promise<
| {
link: NewLinkProps & T;
@@ -62,6 +64,7 @@ export async function processLink<T extends Record<string, any>>({
geo,
doIndex,
tagNames,
+ externalId,
identifier,
} = payload;
@@ -260,6 +263,25 @@ export async function processLink<T extends Record<string, any>>({
}
}
+ if (externalId && workspace && !skipExternalIdChecks) {
+ const link = await prisma.link.findUnique({
+ where: {
+ projectId_externalId: {
+ projectId: workspace.id,
+ externalId,
+ },
+ },
+ });
+
+ if (link) {
+ return {
+ link: payload,
+ error: "A link with this externalId already exists.",
+ code: "conflict",
+ };
+ }
+ }
+
if (identifier && workspace && !skipIdentifierChecks) {
const link = await prisma.link.findUnique({
where: {
@@ -273,8 +295,8 @@ export async function processLink<T extends Record<string, any>>({
if (link) {
return {
link: payload,
- error: "The identifier is already taken.",
- code: "unprocessable_entity",
+ error: "A link with this identifier already exists.",
+ code: "conflict",
};
}
}
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 9f50b4fedf..806455a9d6 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -346,6 +346,7 @@ export const bulkUpdateLinksBodySchema = z.object({
domain: true,
key: true,
externalId: true,
+ identifier: true,
prefix: true,
})
.merge(