Skip to content

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

team scopoed: document/:id/ links

Aashish-Upadhyay-1011ee9d575a071
diff --git a/pages/api/teams/[teamId]/documents/[id]/links.ts b/pages/api/teams/[teamId]/documents/[id]/links.ts
index 14b3802b..7a1aede0 100644
--- a/pages/api/teams/[teamId]/documents/[id]/links.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/links.ts
@@ -3,45 +3,85 @@ import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
import { authOptions } from "../../../../auth/[...nextauth]";
import { log } from "@/lib/utils";
+import { CustomUser } from "@/lib/types";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "GET") {
- // GET /api/documents/:id
+ // GET /api/teams/:teamId/documents/:id/links
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}
- const { id } = req.query as { id: string };
+ const { teamId, id: docId } = req.query as { teamId: string; id: string };
+
+ const userId = (session.user as CustomUser).id;
try {
- const links = await prisma.link.findMany({
+ const team = await prisma.team.findUnique({
where: {
- documentId: id,
- },
- orderBy: {
- createdAt: "asc",
+ id: teamId,
},
include: {
- views: {
- orderBy: {
- viewedAt: "desc",
+ users: {
+ select: {
+ userId: true,
},
},
- _count: {
- select: { views: true },
+ documents: {
+ select: {
+ ownerId: true,
+ id: true,
+ links: {
+ orderBy: {
+ createdAt: "asc",
+ },
+ include: {
+ views: {
+ orderBy: {
+ viewedAt: "desc",
+ },
+ },
+ _count: {
+ select: { views: true },
+ },
+ },
+ },
+ },
},
},
});
+ // check if the team exists
+ if (!team) {
+ res.status(400).end("Team doesn't exists");
+ }
+
+ // check if the user is part the team
+ const teamHasUser = team?.users.some((user) => user.userId === userId);
+ if (!teamHasUser) {
+ res.status(401).end("You are not a member of the team");
+ }
+
+ // check if the document exists in the team
+ const document = team?.documents.find((doc) => doc.id === docId);
+ if (!document) {
+ return res.status(400).end("Document doesn't exists in the team");
+ }
+
// TODO: Check that the user is owner of the document, otherwise return 401
+ const isUserOwnerOfDocument = document.ownerId === userId;
+ if (!isUserOwnerOfDocument) {
+ return res.status(401).end("Unauthorized access to the document");
+ }
+ const links = document.links;
res.status(200).json(links);
} catch (error) {
- log(`Failed to get links for document ${id}. Error: \n\n ${error}`);
+ log(`Failed to get links for document ${docId}. Error: \n\n ${error}`);
return res.status(500).json({
message: "Internal Server Error",
error: (error as Error).message,