Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: change tag item table relations

Marc Seitz5f6b42d2e56b
diff --git a/lib/types.ts b/lib/types.ts
index 1eee0308..737dd149 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -380,8 +380,8 @@ export interface TagProps {
id: string;
name: string;
description: string | null;
- color: TagColorProps;
+ color: TagColorProps | string;
_count?: {
- taggedItems: number;
+ items: number;
};
}
diff --git a/pages/api/links/[id]/archive.ts b/pages/api/links/[id]/archive.ts
index 24be9dc5..a9bc89c5 100644
--- a/pages/api/links/[id]/archive.ts
+++ b/pages/api/links/[id]/archive.ts
@@ -38,14 +38,14 @@ export default async function handle(
_count: {
select: { views: true },
},
- taggedItems: {
+ tags: {
select: {
tag: {
select: {
+ id: true,
name: true,
description: true,
color: true,
- id: true,
},
},
},
@@ -56,14 +56,14 @@ export default async function handle(
return res.status(404).json({ error: "Link not found" });
}
- const { taggedItems, ...rest } = updatedLink;
- const tags = taggedItems.map((t) => t.tag);
+ const { tags, ...rest } = updatedLink;
+ const linkTags = tags.map((t) => t.tag);
await fetch(
`${process.env.NEXTAUTH_URL}/api/revalidate?secret=${process.env.REVALIDATE_TOKEN}&linkId=${id}&hasDomain=${updatedLink.domainId ? "true" : "false"}`,
);
- return res.status(200).json({ ...rest, tags });
+ return res.status(200).json({ ...rest, tags: linkTags });
} catch (error) {
errorhandler(error, res);
}
diff --git a/pages/api/links/[id]/duplicate.ts b/pages/api/links/[id]/duplicate.ts
index 04c62eb8..c2ea179e 100644
--- a/pages/api/links/[id]/duplicate.ts
+++ b/pages/api/links/[id]/duplicate.ts
@@ -50,7 +50,7 @@ export default async function handle(
const link = await prisma.link.findUnique({
where: { id, teamId },
include: {
- taggedItems: {
+ tags: {
select: {
tag: {
select: {
@@ -66,8 +66,8 @@ export default async function handle(
return res.status(404).json({ error: "Link not found" });
}
- const { taggedItems, ...rest } = link;
- const linkTags = taggedItems.map((t) => t.tag.id);
+ const { tags, ...rest } = link;
+ const linkTags = tags.map((t) => t.tag.id);
const newLinkName = link.name
? link.name + " (Copy)"
@@ -87,10 +87,10 @@ export default async function handle(
});
if (linkTags?.length) {
- await tx.taggedItem.createMany({
+ await tx.tagItem.createMany({
data: linkTags.map((tagId: string) => ({
tagId,
- itemType: "LINK",
+ itemType: "LINK_TAG",
linkId: createdLink.id,
taggedBy: (session.user as CustomUser).id,
})),
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index bcab9649..a579be49 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -342,19 +342,19 @@ export default async function handle(
});
if (linkData.tags?.length) {
// Remove only tags that are not in the new list
- await tx.taggedItem.deleteMany({
+ await tx.tagItem.deleteMany({
where: {
linkId: id,
- itemType: "LINK",
+ itemType: "LINK_TAG",
tagId: { notIn: linkData.tags },
},
});
// Add new tags while avoiding duplicates
- await tx.taggedItem.createMany({
+ await tx.tagItem.createMany({
data: linkData.tags.map((tagId: string) => ({
tagId,
- itemType: "LINK",
+ itemType: "LINK_TAG",
linkId: id,
taggedBy: userId,
})),
@@ -362,16 +362,17 @@ export default async function handle(
});
} else {
// If all tags are removed, delete all tagged items for this link
- await tx.taggedItem.deleteMany({
+ await tx.tagItem.deleteMany({
where: {
linkId: id,
- itemType: "LINK",
+ itemType: "LINK_TAG",
},
});
}
+
const tags = await tx.tag.findMany({
where: {
- taggedItems: {
+ items: {
some: { linkId: link.id },
},
},
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 197e72f6..d478c2a2 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { LinkAudienceType } from "@prisma/client";
+import { LinkAudienceType, Tag } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { getServerSession } from "next-auth/next";
@@ -205,24 +205,25 @@ export default async function handler(
},
});
+ let tags: Partial<Tag>[] = [];
if (linkData.tags?.length) {
- await tx.taggedItem.createMany({
+ // create tag items
+ await tx.tagItem.createMany({
data: linkData.tags.map((tagId: string) => ({
tagId,
- itemType: "LINK",
+ itemType: "LINK_TAG",
linkId: link.id,
taggedBy: userId,
})),
skipDuplicates: true,
});
- }
- const tags = linkData.tags?.length
- ? await tx.tag.findMany({
- where: { id: { in: linkData.tags } },
- select: { id: true, name: true, color: true, description: true },
- })
- : [];
+ // return tags
+ tags = await tx.tag.findMany({
+ where: { id: { in: linkData.tags } },
+ select: { id: true, name: true, color: true, description: true },
+ });
+ }
return { ...link, tags };
});
@@ -241,9 +242,9 @@ export default async function handler(
sendLinkCreatedWebhook({
teamId,
data: {
- link_id: link.id,
- document_id: link.documentId,
- dataroom_id: link.dataroomId,
+ link_id: linkWithView.id,
+ document_id: linkWithView.documentId,
+ dataroom_id: linkWithView.dataroomId,
},
}),
);
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/links.ts b/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/links.ts
index 688db22b..f18ae435 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/links.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/links.ts
@@ -85,9 +85,10 @@ export default async function handle(
}
const tags = await prisma.tag.findMany({
where: {
- taggedItems: {
+ items: {
some: {
linkId: link.id,
+ itemType: "LINK_TAG",
},
},
},
@@ -101,7 +102,7 @@ export default async function handle(
return {
...link,
- ["tags"]: tags,
+ tags,
};
}),
);
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/links.ts b/pages/api/teams/[teamId]/datarooms/[id]/links.ts
index 98cdf56d..146908c1 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/links.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/links.ts
@@ -93,9 +93,10 @@ export default async function handle(
}
const tags = await prisma.tag.findMany({
where: {
- taggedItems: {
+ items: {
some: {
linkId: link.id,
+ itemType: "LINK_TAG",
},
},
},
@@ -109,7 +110,7 @@ export default async function handle(
return {
...link,
- ["tags"]: tags,
+ tags,
};
}),
);
diff --git a/pages/api/teams/[teamId]/documents/[id]/links.ts b/pages/api/teams/[teamId]/documents/[id]/links.ts
index d2ea6d67..46d59846 100644
--- a/pages/api/teams/[teamId]/documents/[id]/links.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/links.ts
@@ -85,9 +85,10 @@ export default async function handle(
}
const tags = await prisma.tag.findMany({
where: {
- taggedItems: {
+ items: {
some: {
linkId: link.id,
+ itemType: "LINK_TAG",
},
},
},
@@ -101,7 +102,7 @@ export default async function handle(
return {
...link,
- ["tags"]: tags,
+ tags,
};
}),
);