Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
update code to use helper functions
Aashish-Upadhyay-1010945568d3776
diff --git a/lib/api/teams.ts b/lib/api/teams.tsnew file mode 100644index 00000000..748421bc--- /dev/null+++ b/lib/api/teams.ts@@ -0,0 +1,45 @@+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/index.ts b/pages/api/teams/[teamId]/documents/index.tsindex ca01a09c..f362ae35 100644--- a/pages/api/teams/[teamId]/documents/index.ts+++ b/pages/api/teams/[teamId]/documents/index.ts@@ -5,6 +5,7 @@ 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,@@ -19,7 +20,19 @@ export default async function handle(const { teamId } = req.query as { teamId: string };+ const userId = (session.user as CustomUser).id;+try {+ // check if the team exists+ if (!(await teamExists(teamId))) {+ res.status(400).end("Team doesn't exists");+ }++ // check if the user is part the team+ if (!(await teamHasUser(teamId, userId))) {+ res.status(401).end("You are not a member of the team");+ }+const documents = await prisma.document.findMany({where: {// ownerId: (session.user as CustomUser).id,@@ -54,43 +67,29 @@ export default async function handle(return;}- // Assuming data is an object with `name` and `description` properties- const { name, url, numPages } = req.body;-- // Get the file extension and save it as the type- const type = getExtension(name);-- // You could perform some validation here-const { teamId } = req.query as { teamId: string };- try {- const team = await prisma.team.findUnique({- where: {- id: teamId,- },- include: {- users: {- select: {- userId: true,- },- },- },- });+ const userId = (session.user as CustomUser).id;- // check if team exists- if (!team) {- return res.status(400).end("Team doesn't exists");+ try {+ // check if the team exists+ if (!(await teamExists(teamId))) {+ res.status(400).end("Team doesn't exists");}- // check if user is part of the team- const isTeamHasUser = team.users.some(- (user) => user.userId === (session.user as CustomUser).id- );- if (!isTeamHasUser) {- return res.status(401).end("You are not a member of this team");+ // check if the user is part the team+ if (!(await teamHasUser(teamId, userId))) {+ res.status(401).end("You are not a member of the team");}+ // Assuming data is an object with `name` and `description` properties+ const { name, url, numPages } = req.body;++ // Get the file extension and save it as the type+ const type = getExtension(name);++ // You could perform some validation here+// Save data to the databaseconst document = await prisma.document.create({data: {diff --git a/pages/api/teams/[teamId]/documents/update.ts b/pages/api/teams/[teamId]/documents/update.tsindex cfc0a555..4b092de0 100644--- a/pages/api/teams/[teamId]/documents/update.ts+++ b/pages/api/teams/[teamId]/documents/update.ts@@ -4,6 +4,7 @@ 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,@@ -22,26 +23,21 @@ export default async function handle(const { teamId } = req.query as { teamId: string };+ const userId = (session.user as CustomUser).id;+try {- // check if the document exists in the team or not- const team = await prisma.team.findUnique({- where: {- id: teamId,- },- include: {- documents: {- select: {- id: true,- },- },- },- });+ // check if the team exists+ if (!(await teamExists(teamId))) {+ res.status(400).end("Team doesn't exists");+ }- const documentExists = team?.documents.some(- (document) => document.id === documentId- );+ // check if the user is part the team+ if (!(await teamHasUser(teamId, userId))) {+ res.status(401).end("You are not a member of the team");+ }- if (!documentExists) {+ // check if the document exists in the team+ if (!(await teamHasDocument(teamId, documentId))) {return res.status(400).end("Document doesn't exists in the team");}