Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
team scopoed: document/:id/views/:viewId/stats
Aashish-Upadhyay-10192681e23669e
diff --git a/pages/api/teams/[teamId]/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.tsindex a3f8b6aa..bdb6d37e 100644--- a/pages/api/teams/[teamId]/documents/[id]/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts@@ -46,13 +46,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// check if the document exists in the team@@ -108,13 +108,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// check if the document exists in the team@@ -137,7 +137,7 @@ export default async function handle(},});- res.status(204).end(); // 204 No Content response for successful deletes+ return res.status(204).end(); // 204 No Content response for successful deletes} catch (error) {return res.status(500).json({message: "Internal Server Error",diff --git a/pages/api/teams/[teamId]/documents/[id]/links.ts b/pages/api/teams/[teamId]/documents/[id]/links.tsindex 07f47621..b906f66c 100644--- a/pages/api/teams/[teamId]/documents/[id]/links.ts+++ b/pages/api/teams/[teamId]/documents/[id]/links.ts@@ -57,13 +57,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// check if the document exists in the team@@ -79,7 +79,7 @@ export default async function handle(}const links = document.links;- res.status(200).json(links);+ return res.status(200).json(links);} catch (error) {log(`Failed to get links for document ${docId}. Error: \n\n ${error}`);return res.status(500).json({diff --git a/pages/api/teams/[teamId]/documents/[id]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/stats.tsindex 43425a56..913a1a93 100644--- a/pages/api/teams/[teamId]/documents/[id]/stats.ts+++ b/pages/api/teams/[teamId]/documents/[id]/stats.ts@@ -43,13 +43,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// check if the document exists in the team@@ -84,7 +84,7 @@ export default async function handle(const stats = { views, groupedViews, duration, total_duration };- res.status(200).json(stats);+ return res.status(200).json(stats);} catch (error) {return res.status(500).json({message: "Internal Server Error",diff --git a/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.tsindex 1d1bf809..0840aaf9 100644--- a/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts+++ b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts@@ -2,24 +2,77 @@ import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import { getViewPageDuration } from "@/lib/tinybird";import { authOptions } from "@/pages/api/auth/[...nextauth]";+import { CustomUser } from "@/lib/types";+import prisma from "@/lib/prisma";export default async function handle(req: NextApiRequest,res: NextApiResponse) {if (req.method === "GET") {- // GET /api/documents/:id/views/:viewId/stats+ // GET /api/teams/:teamId/documents/:id/views/:viewId/statsconst session = await getServerSession(req, res, authOptions);if (!session) {return res.status(401).end("Unauthorized");}- const { id, viewId } = req.query as { id: string, viewId: string };+ const {+ teamId,+ id: docId,+ viewId,+ } = req.query as {+ teamId: string;+ id: string;+ viewId: string;+ };++ 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,+ ownerId: true,+ },+ },+ },+ });++ // check if the team exists+ if (!team) {+ return 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) {+ return 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");+ }++ // 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 duration = await getViewPageDuration({- documentId: id,+ documentId: docId,viewId: viewId,since: 0,});@@ -31,9 +84,7 @@ export default async function handle(const stats = { duration, total_duration };- // TODO: Check that the user is owner of the document, otherwise return 401-- res.status(200).json(stats);+ return res.status(200).json(stats);} catch (error) {return res.status(500).json({message: "Internal Server Error",diff --git a/pages/api/teams/[teamId]/documents/[id]/views/index.ts b/pages/api/teams/[teamId]/documents/[id]/views/index.tsindex 4bb3a355..67f5e43e 100644--- a/pages/api/teams/[teamId]/documents/[id]/views/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/views/index.ts@@ -64,13 +64,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// check if the document exists in the team@@ -126,7 +126,7 @@ export default async function handle(// console.log("viewsWithDuration:", viewsWithDuration)- res.status(200).json(viewsWithDuration);+ return res.status(200).json(viewsWithDuration);} catch (error) {log(`Failed to get views for link ${docId}. Error: \n\n ${error}`);return res.status(500).json({diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.tsindex df7326e3..b6d3a646 100644--- a/pages/api/teams/[teamId]/documents/index.ts+++ b/pages/api/teams/[teamId]/documents/index.ts@@ -42,13 +42,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}const documents = await prisma.document.findMany({@@ -70,7 +70,7 @@ export default async function handle(},});- res.status(200).json(documents);+ return res.status(200).json(documents);} catch (error) {return res.status(500).json({message: "Internal Server Error",@@ -110,13 +110,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// Assuming data is an object with `name` and `description` properties@@ -178,7 +178,7 @@ export default async function handle(customDomain: null,});- res.status(201).json(document);+ return res.status(201).json(document);} catch (error) {log(`Failed to create document. Error: \n\n ${error}`);res.status(500).json({diff --git a/pages/api/teams/[teamId]/documents/update.ts b/pages/api/teams/[teamId]/documents/update.tsindex 046ef687..3a9229de 100644--- a/pages/api/teams/[teamId]/documents/update.ts+++ b/pages/api/teams/[teamId]/documents/update.ts@@ -45,13 +45,13 @@ export default async function handle(// check if the team existsif (!team) {- res.status(400).end("Team doesn't exists");+ return res.status(400).end("Team doesn't exists");}// check if the user is part the teamconst teamHasUser = team?.users.some((user) => user.userId === userId);if (!teamHasUser) {- res.status(401).end("You are not a member of the team");+ return res.status(401).end("You are not a member of the team");}// check if the document exists in the team@@ -76,7 +76,7 @@ export default async function handle(},});- res.status(201).json({ message: "Document updated successfully" });+ return res.status(201).json({ message: "Document updated successfully" });} catch (error) {log(`Failed to create document. Error: \n\n ${error}`);res.status(500).json({