Skip to content

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

feat: cleanup

Marc Seitz8f95c2557af2
diff --git a/lib/api/links/link-data.ts b/lib/api/links/link-data.ts
index af3a8392..2f60b184 100644
--- a/lib/api/links/link-data.ts
+++ b/lib/api/links/link-data.ts
@@ -5,9 +5,11 @@ import { sortItemsByIndexAndName } from "@/lib/utils/sort-items-by-index-name";
export async function fetchDataroomLinkData({
linkId,
+ teamId,
groupId,
}: {
linkId: string;
+ teamId: string;
groupId?: string;
}) {
let groupPermissions: ViewerGroupAccessControls[] = [];
@@ -33,7 +35,7 @@ export async function fetchDataroomLinkData({
}
const linkData = await prisma.link.findUnique({
- where: { id: linkId },
+ where: { id: linkId, teamId },
select: {
dataroom: {
select: {
@@ -112,9 +114,15 @@ export async function fetchDataroomLinkData({
return { linkData, brand };
}
-export async function fetchDocumentLinkData({ linkId }: { linkId: string }) {
+export async function fetchDocumentLinkData({
+ linkId,
+ teamId,
+}: {
+ linkId: string;
+ teamId: string;
+}) {
const linkData = await prisma.link.findUnique({
- where: { id: linkId },
+ where: { id: linkId, teamId },
select: {
document: {
select: {
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index c8cbfb7c..9648cc41 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -57,6 +57,7 @@ export default async function handle(
watermarkConfig: true,
groupId: true,
audienceType: true,
+ teamId: true,
},
});
@@ -76,12 +77,16 @@ export default async function handle(
let linkData: any;
if (linkType === "DOCUMENT_LINK") {
- const data = await fetchDocumentLinkData({ linkId: id });
+ const data = await fetchDocumentLinkData({
+ linkId: id,
+ teamId: link.teamId!,
+ });
linkData = data.linkData;
brand = data.brand;
} else if (linkType === "DATAROOM_LINK") {
const data = await fetchDataroomLinkData({
linkId: id,
+ teamId: link.teamId!,
...(link.audienceType === LinkAudienceType.GROUP &&
link.groupId && {
groupId: link.groupId,
@@ -110,13 +115,40 @@ export default async function handle(
return res.status(401).end("Unauthorized");
}
+ const userId = (session.user as CustomUser).id;
const { id } = req.query as { id: string };
- const { targetId, linkType, password, expiresAt, ...linkDomainData } =
- req.body;
+ const {
+ targetId,
+ linkType,
+ password,
+ expiresAt,
+ teamId,
+ ...linkDomainData
+ } = req.body;
const dataroomLink = linkType === "DATAROOM_LINK";
const documentLink = linkType === "DOCUMENT_LINK";
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: { userId },
+ },
+ },
+ });
+
+ if (!team) {
+ return res.status(400).json({ error: "Team not found." });
+ }
+ } catch (error) {
+ return res.status(500).json({
+ message: "Internal Server Error",
+ error: (error as Error).message,
+ });
+ }
+
const hashedPassword =
password && password.length > 0
? await generateEncrpytedPassword(password)
@@ -180,7 +212,7 @@ export default async function handle(
// Update the link in the database
const updatedLink = await prisma.link.update({
- where: { id: id },
+ where: { id, teamId },
data: {
documentId: documentLink ? targetId : null,
dataroomId: dataroomLink ? targetId : null,
diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].ts
index d1160191..9fb9f377 100644
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@ -67,24 +67,10 @@ export default async function handle(
watermarkConfig: true,
groupId: true,
audienceType: true,
- document: {
+ teamId: true,
+ team: {
select: {
- team: {
- select: {
- id: true,
- plan: true,
- },
- },
- },
- },
- dataroom: {
- select: {
- team: {
- select: {
- id: true,
- plan: true,
- },
- },
+ plan: true,
},
},
},
@@ -110,10 +96,10 @@ export default async function handle(
});
}
- const teamPlan = link.document?.team?.plan || link.dataroom?.team.plan;
- const teamId = link.document?.team?.id || link.dataroom?.team.id;
+ const teamPlan = link.team?.plan || "free";
+ const teamId = link.teamId;
// if owner of document is on free plan, return 404
- if (teamPlan === "free") {
+ if (teamPlan.includes("free")) {
log({
message: `Link is from a free team _${teamId}_ for custom domain _${domain}/${slug}_`,
type: "info",
@@ -130,12 +116,16 @@ export default async function handle(
let linkData: any;
if (linkType === "DOCUMENT_LINK") {
- const data = await fetchDocumentLinkData({ linkId: link.id });
+ const data = await fetchDocumentLinkData({
+ linkId: link.id,
+ teamId: link.teamId!,
+ });
linkData = data.linkData;
brand = data.brand;
} else if (linkType === "DATAROOM_LINK") {
const data = await fetchDataroomLinkData({
linkId: link.id,
+ teamId: link.teamId!,
...(link.audienceType === LinkAudienceType.GROUP &&
link.groupId && {
groupId: link.groupId,
@@ -148,6 +138,8 @@ export default async function handle(
// remove document and domain from link
const sanitizedLink = {
...link,
+ teamId: undefined,
+ team: undefined,
document: undefined,
dataroom: undefined,
};
@@ -171,7 +163,7 @@ export default async function handle(
});
}
} else {
- // We only allow GET and POST requests
+ // We only allow GET requests
res.setHeader("Allow", ["GET"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 51ae9163..0bf76ab0 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -25,59 +25,32 @@ export default async function handler(
return res.status(401).end("Unauthorized");
}
- const { targetId, linkType, password, expiresAt, ...linkDomainData } =
- req.body;
+ const {
+ targetId,
+ linkType,
+ password,
+ expiresAt,
+ teamId,
+ ...linkDomainData
+ } = req.body;
const userId = (session.user as CustomUser).id;
- let teamId: string | null = null;
const dataroomLink = linkType === "DATAROOM_LINK";
const documentLink = linkType === "DOCUMENT_LINK";
try {
- if (documentLink) {
- // check if the team that own the document has the current user
- const { document } = await getDocumentWithTeamAndUser({
- docId: targetId,
- userId,
- options: {
- team: {
- select: {
- users: {
- select: {
- userId: true,
- },
- },
- },
- },
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: { userId },
},
- });
-
- // set teamId to the teamId of the document
- teamId = document.teamId;
- }
-
- if (dataroomLink) {
- const dataroom = await prisma.dataroom.findUnique({
- where: {
- id: targetId,
- team: {
- users: {
- some: {
- userId: userId,
- },
- },
- },
- },
- select: { id: true, teamId: true },
- });
-
- if (!dataroom) {
- return res.status(400).json({ error: "Dataroom not found." });
- }
+ },
+ });
- // set teamId to the teamId of the dataroom
- teamId = dataroom.teamId;
+ if (!team) {
+ return res.status(400).json({ error: "Team not found." });
}
const hashedPassword =
diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.ts
index 424a719c..21d51ef6 100644
--- a/pages/api/views-dataroom.ts
+++ b/pages/api/views-dataroom.ts
@@ -535,7 +535,7 @@ export default async function handle(
dataroomId: dataroomId,
viewType: "DOCUMENT_VIEW",
viewerId: viewer?.id ?? undefined,
- teamId: link.dataroom?.teamId,
+ teamId: link.teamId,
...(link.enableAgreement &&
link.agreementId &&
hasConfirmedAgreement && {