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/stats

Aashish-Upadhyay-1018cdadcb4b43d
diff --git a/pages/api/teams/[teamId]/documents/[id]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/stats.ts
index aeb93d62..43425a56 100644
--- a/pages/api/teams/[teamId]/documents/[id]/stats.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/stats.ts
@@ -3,33 +3,77 @@ import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
import { authOptions } from "../../../../auth/[...nextauth]";
import { getTotalAvgPageDuration } from "@/lib/tinybird";
+import { CustomUser } from "@/lib/types";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "GET") {
- // GET /api/documents/:id/stats
+ // GET /api/teams/:teamId/documents/:id/stats
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 views = await prisma.view.findMany({
- where: { documentId: id },
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: {
+ select: {
+ userId: true,
+ },
+ },
+ documents: {
+ select: {
+ ownerId: true,
+ id: true,
+ 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");
+ }
+
+ // 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 views = document.views;
+
const groupedViews = await prisma.view.groupBy({
by: ["viewerEmail"],
- where: { documentId: id },
+ where: { documentId: docId },
_count: { id: true },
});
const duration = await getTotalAvgPageDuration({
- documentId: id,
+ documentId: docId,
since: 0,
});
@@ -40,8 +84,6 @@ export default async function handle(
const stats = { views, groupedViews, duration, total_duration };
- // TODO: Check that the user is owner of the document, otherwise return 401
-
res.status(200).json(stats);
} catch (error) {
return res.status(500).json({
@@ -50,7 +92,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`);
}