Skip to content

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

team scoped: get single doc and delete doc

Aashish-Upadhyay-101ffdd37b9d246
diff --git a/lib/api/teams.ts b/lib/api/teams.ts
deleted file mode 100644
index 748421bc..00000000
--- a/lib/api/teams.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import prisma from "@/lib/prisma";
-
-export const teamExists = async (teamId: string) => {
- const team = await prisma.team.findUnique({
- where: {
- id: teamId,
- },
- });
-
- return team ? true : false;
-};
-
-export const teamHasUser = async (teamId: string, userId: string) => {
- const team = await prisma.team.findUnique({
- where: {
- id: teamId,
- },
- include: {
- users: {
- select: {
- userId: true,
- },
- },
- },
- });
-
- return team?.users.some((user) => user.userId === userId);
-};
-
-export const teamHasDocument = async (teamId: string, docId: string) => {
- const team = await prisma.team.findUnique({
- where: {
- id: teamId,
- },
- include: {
- documents: {
- select: {
- id: true,
- },
- },
- },
- });
-
- return team?.documents.some((doc) => doc.id === docId);
-};
diff --git a/pages/api/teams/[teamId]/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.ts
index b9d51d9f..a3f8b6aa 100644
--- a/pages/api/teams/[teamId]/documents/[id]/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts
@@ -10,37 +10,61 @@ export default async function handle(
res: NextApiResponse
) {
if (req.method === "GET") {
- // GET /api/documents/:id
+ // GET /api/teams/:teamId/documents/:id
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 document = await prisma.document.findUnique({
+ const team = await prisma.team.findUnique({
where: {
- id: id,
+ id: teamId,
},
include: {
- // Get the latest primary version of the document
- versions: {
- where: { isPrimary: true },
- orderBy: { createdAt: "desc" },
- take: 1,
+ users: {
+ select: {
+ userId: true,
+ },
+ },
+ documents: {
+ include: {
+ // Get the latest primary version of the document
+ versions: {
+ where: { isPrimary: true },
+ orderBy: { createdAt: "desc" },
+ take: 1,
+ },
+ },
},
},
});
+ // 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(404).end("Document not found");
+ return res.status(400).end("Document doesn't exists in the team");
}
// Check that the user is owner of the document, otherwise return 401
- if (document.ownerId !== (session.user as CustomUser).id) {
- return res.status(401).end("Unauthorized to access this document");
- }
+ // if (document.ownerId !== (session.user as CustomUser).id) {
+ // return res.status(401).end("Unauthorized to access this document");
+ // }
return res.status(200).json(document);
} catch (error) {
@@ -50,28 +74,57 @@ export default async function handle(
});
}
} else if (req.method === "DELETE") {
- // DELETE /api/document/:id
+ // DELETE /api/teams/:teamId/document/:id
const session = await getServerSession(req, res, authOptions);
if (!session) {
res.status(401).end("Unauthorized");
return;
}
- 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 document = await prisma.document.findUnique({
+ const team = await prisma.team.findUnique({
where: {
- id: id,
+ id: teamId,
+ },
+ include: {
+ users: {
+ select: {
+ userId: true,
+ },
+ },
+ documents: {
+ select: {
+ id: true,
+ ownerId: true,
+ file: 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(404).end("Document not found");
+ return res.status(400).end("Document doesn't exists in the team");
}
// check that the user is owner of the document, otherwise return 401
- if (document.ownerId !== (session.user as CustomUser).id) {
+ if (document.ownerId !== userId) {
return res.status(401).end("Unauthorized to access the document");
}
@@ -80,7 +133,7 @@ export default async function handle(
// delete the document from database
await prisma.document.delete({
where: {
- id: id,
+ id: docId,
},
});
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index f362ae35..df7326e3 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -5,7 +5,6 @@ import { authOptions } from "../../../auth/[...nextauth]";
import { CustomUser } from "@/lib/types";
import { getExtension, log } from "@/lib/utils";
import { identifyUser, trackAnalytics } from "@/lib/analytics";
-import { teamExists, teamHasUser } from "@/lib/api/teams";
export default async function handle(
req: NextApiRequest,
@@ -23,13 +22,32 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: {
+ select: {
+ userId: true,
+ },
+ },
+ documents: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ });
+
// check if the team exists
- if (!(await teamExists(teamId))) {
+ if (!team) {
res.status(400).end("Team doesn't exists");
}
// check if the user is part the team
- if (!(await teamHasUser(teamId, userId))) {
+ const teamHasUser = team?.users.some((user) => user.userId === userId);
+ if (!teamHasUser) {
res.status(401).end("You are not a member of the team");
}
@@ -72,13 +90,32 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: {
+ select: {
+ userId: true,
+ },
+ },
+ documents: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ });
+
// check if the team exists
- if (!(await teamExists(teamId))) {
+ if (!team) {
res.status(400).end("Team doesn't exists");
}
// check if the user is part the team
- if (!(await teamHasUser(teamId, userId))) {
+ const teamHasUser = team?.users.some((user) => user.userId === userId);
+ if (!teamHasUser) {
res.status(401).end("You are not a member of the team");
}
diff --git a/pages/api/teams/[teamId]/documents/update.ts b/pages/api/teams/[teamId]/documents/update.ts
index 4b092de0..046ef687 100644
--- a/pages/api/teams/[teamId]/documents/update.ts
+++ b/pages/api/teams/[teamId]/documents/update.ts
@@ -4,7 +4,6 @@ import prisma from "@/lib/prisma";
import { authOptions } from "../../../auth/[...nextauth]";
import { CustomUser } from "@/lib/types";
import { getExtension, log } from "@/lib/utils";
-import { teamExists, teamHasDocument, teamHasUser } from "@/lib/api/teams";
export default async function handle(
req: NextApiRequest,
@@ -26,18 +25,40 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: {
+ select: {
+ userId: true,
+ },
+ },
+ documents: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ });
+
// check if the team exists
- if (!(await teamExists(teamId))) {
+ if (!team) {
res.status(400).end("Team doesn't exists");
}
// check if the user is part the team
- if (!(await teamHasUser(teamId, userId))) {
+ 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
- if (!(await teamHasDocument(teamId, documentId))) {
+ const teamHasDocument = team?.documents.some(
+ (doc) => doc.id === documentId
+ );
+ if (!teamHasDocument) {
return res.status(400).end("Document doesn't exists in the team");
}