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.

Add missing tagNames logic

Tim Wilsondaac9183af65
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 9ae487b84e..2a10ea9bb2 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -78,14 +78,17 @@ export const POST = withAuth(
},
select: {
id: true,
+ name: true,
},
});
const workspaceTagIds = workspaceTags.map(({ id }) => id);
+ const workspaceTagNames = workspaceTags.map(({ name }) => name);
validLinks.forEach((link, index) => {
- const combinedTagIds = combineTagIds({
- tagId: link.tagId,
- tagIds: link.tagIds,
- });
+ const combinedTagIds =
+ combineTagIds({
+ tagId: link.tagId,
+ tagIds: link.tagIds,
+ }) ?? [];
const invalidTagIds = combinedTagIds.filter(
(id) => !workspaceTagIds.includes(id),
);
@@ -98,6 +101,18 @@ export const POST = withAuth(
code: "unprocessable_entity",
});
}
+
+ const invalidTagNames = link.tagNames?.filter((name) =>
+ workspaceTagNames.includes(name),
+ );
+ if (invalidTagNames?.length) {
+ validLinks = validLinks.filter((_, i) => i !== index);
+ errorLinks.push({
+ link,
+ error: `Invalid tagNames detected: ${invalidTagNames.join(", ")}`,
+ code: "unprocessable_entity",
+ });
+ }
});
const validLinksResponse =
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index 949690ce4e..6397787c05 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -15,7 +15,7 @@ export async function bulkCreateLinks({
// create links via $transaction (because Prisma doesn't support nested createMany)
// ref: https://github.com/prisma/prisma/issues/8131#issuecomment-997667070
const createdLinks = await prisma.$transaction(
- links.map(({ tagId, tagIds, ...link }) => {
+ links.map(({ tagId, tagIds, tagNames, ...link }) => {
const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
getParamsFromURL(link.url);
@@ -33,13 +33,33 @@ export async function bulkCreateLinks({
utm_content,
expiresAt: link.expiresAt ? new Date(link.expiresAt) : null,
geo: link.geo || undefined,
- ...(combinedTagIds.length > 0 && {
- tags: {
- createMany: {
- data: combinedTagIds.map((tagId) => ({ tagId })),
+
+ // Associate tags by tagNames
+ ...(tagNames?.length &&
+ link.projectId && {
+ tags: {
+ create: tagNames.map((tagName) => ({
+ tag: {
+ connect: {
+ name_projectId: {
+ name: tagName,
+ projectId: link.projectId as string,
+ },
+ },
+ },
+ })),
},
- },
- }),
+ }),
+
+ // Associate tags by IDs (takes priority over tagNames)
+ ...(combinedTagIds &&
+ combinedTagIds.length > 0 && {
+ tags: {
+ createMany: {
+ data: combinedTagIds.map((tagId) => ({ tagId })),
+ },
+ },
+ }),
},
include: {
tags: {
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index bc85ae21e8..af0d7cb784 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -16,7 +16,7 @@ export async function createLink(link: ProcessedLinkProps) {
const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
getParamsFromURL(url);
- const { tagId, tagIds, ...rest } = link;
+ const { tagId, tagIds, tagNames, ...rest } = link;
const response = await prisma.link.create({
data: {
@@ -33,13 +33,33 @@ export async function createLink(link: ProcessedLinkProps) {
utm_content,
expiresAt: expiresAt ? new Date(expiresAt) : null,
geo: geo || Prisma.JsonNull,
- ...(combinedTagIds.length > 0 && {
- tags: {
- createMany: {
- data: combinedTagIds.map((tagId) => ({ tagId })),
+
+ // Associate tags by tagNames
+ ...(tagNames?.length &&
+ link.projectId && {
+ tags: {
+ create: tagNames.map((tagName) => ({
+ tag: {
+ connect: {
+ name_projectId: {
+ name: tagName,
+ projectId: link.projectId as string,
+ },
+ },
+ },
+ })),
},
- },
- }),
+ }),
+
+ // Associate tags by IDs (takes priority over tagNames)
+ ...(combinedTagIds &&
+ combinedTagIds.length > 0 && {
+ tags: {
+ createMany: {
+ data: combinedTagIds.map((tagId) => ({ tagId })),
+ },
+ },
+ }),
},
include: {
tags: {
diff --git a/apps/web/lib/api/links/edit-link.ts b/apps/web/lib/api/links/edit-link.ts
index 2a65263643..a46764a5c9 100644
--- a/apps/web/lib/api/links/edit-link.ts
+++ b/apps/web/lib/api/links/edit-link.ts
@@ -43,6 +43,7 @@ export async function editLink({
updatedAt,
tagId,
tagIds,
+ tagNames,
...rest
} = updatedLink;
@@ -76,17 +77,34 @@ export async function editLink({
utm_content,
expiresAt: expiresAt ? new Date(expiresAt) : null,
geo: geo || Prisma.JsonNull,
- tags: {
- deleteMany: {
- tagId: {
- notIn: combinedTagIds,
+
+ // Associate tags by tagNames
+ ...(tagNames &&
+ updatedLink.projectId && {
+ tags: {
+ deleteMany: {},
+ create: tagNames.map((tagName) => ({
+ tag: {
+ connect: {
+ name_projectId: {
+ name: tagName,
+ projectId: updatedLink.projectId as string,
+ },
+ },
+ },
+ })),
},
+ }),
+
+ // Associate tags by IDs (takes priority over tagNames)
+ ...(combinedTagIds && {
+ tags: {
+ deleteMany: {},
+ create: combinedTagIds.map((tagId) => ({
+ tagId,
+ })),
},
- connectOrCreate: combinedTagIds.map((tagId) => ({
- where: { linkId_tagId: { linkId: id, tagId } },
- create: { tagId },
- })),
- },
+ }),
},
include: {
tags: {
diff --git a/apps/web/lib/api/links/get-links-count.ts b/apps/web/lib/api/links/get-links-count.ts
index e36c68cd00..ae159fd134 100644
--- a/apps/web/lib/api/links/get-links-count.ts
+++ b/apps/web/lib/api/links/get-links-count.ts
@@ -59,15 +59,16 @@ export async function getLinksCount({
some: {},
},
}),
- ...(combinedTagIds.length > 0 && {
- tags: {
- some: {
- tagId: {
- in: tagIds,
+ ...(combinedTagIds &&
+ combinedTagIds.length > 0 && {
+ tags: {
+ some: {
+ tagId: {
+ in: tagIds,
+ },
},
},
- },
- }),
+ }),
};
if (groupBy === "domain") {
diff --git a/apps/web/lib/api/links/get-links-for-workspace.ts b/apps/web/lib/api/links/get-links-for-workspace.ts
index 3a04b06e59..65df2feb44 100644
--- a/apps/web/lib/api/links/get-links-for-workspace.ts
+++ b/apps/web/lib/api/links/get-links-for-workspace.ts
@@ -40,7 +40,7 @@ export async function getLinksForWorkspace({
some: {},
},
}),
- ...(combinedTagIds.length > 0
+ ...(combinedTagIds && combinedTagIds.length > 0
? {
tags: { some: { tagId: { in: combinedTagIds } } },
}
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index f23e99dda0..7078d227ed 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -175,7 +175,7 @@ export async function processLink<T extends Record<string, any>>({
// only perform tag validity checks if:
// - not bulk creation (we do that check separately in the route itself)
// - tagIds are present
- } else if (tagIds.length > 0) {
+ } else if (tagIds && tagIds.length > 0) {
const tags = await prisma.tag.findMany({
select: {
id: true,
diff --git a/apps/web/lib/api/links/utils.ts b/apps/web/lib/api/links/utils.ts
index 8d9ede14a9..4ae096cbaa 100644
--- a/apps/web/lib/api/links/utils.ts
+++ b/apps/web/lib/api/links/utils.ts
@@ -21,18 +21,21 @@ type LinkWithTags = Link & {
tags: { tag: Pick<Tag, "id" | "name" | "color"> }[];
};
+/**
+ * Combines tagIds into a single string array or undefined from tagId and tagIds arguments
+ */
export function combineTagIds({
tagId,
tagIds,
}: {
tagId?: string | null;
tagIds?: string[];
-}): string[] {
+}): string[] | undefined {
// Use tagIds if present, fall back to tagId
- if (tagIds && Array.isArray(tagIds) && tagIds.length > 0) {
+ if (tagIds && Array.isArray(tagIds)) {
return tagIds;
}
- return tagId ? [tagId] : [];
+ return tagId === null ? [] : tagId !== undefined ? [tagId] : undefined;
}
export async function keyChecks({