Skip to content

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

refactor: remove massive team/document query for atomic ones

Marc Seitz4fb5a581a46a
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/links.ts b/pages/api/teams/[teamId]/datarooms/[id]/links.ts
index 146908c1..f03f27ea 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/links.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/links.ts
@@ -1,11 +1,9 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { Link } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser, LinkWithViews } from "@/lib/types";
import { decryptEncrpytedPassword, log } from "@/lib/utils";
diff --git a/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts b/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts
index 0973edd5..a4abf23c 100644
--- a/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts
+++ b/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts
@@ -6,7 +6,6 @@ import { z } from "zod";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
@@ -46,13 +45,32 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ });
+
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
// Verify user has access to document
- await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId,
+ const document = await prisma.document.findUnique({
+ where: {
+ id: docId,
+ teamId,
+ },
+ select: { id: true },
});
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
// Verify annotation exists and belongs to document
const annotation = await prisma.documentAnnotation.findFirst({
where: {
diff --git a/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId]/images.ts b/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId]/images.ts
index a5d6c2e0..2f26ce38 100644
--- a/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId]/images.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId]/images.ts
@@ -5,7 +5,6 @@ import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
@@ -36,13 +35,32 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ });
+
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
// Verify user has access to document
- await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId,
+ const document = await prisma.document.findUnique({
+ where: {
+ id: docId,
+ teamId,
+ },
+ select: { id: true },
});
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
// Verify annotation exists and belongs to document
const annotation = await prisma.documentAnnotation.findFirst({
where: {
diff --git a/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts b/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts
index fc0c0d5f..220d2771 100644
--- a/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts
@@ -7,7 +7,6 @@ import { z } from "zod";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
@@ -41,7 +40,30 @@ export default async function handle(
try {
// Validate access; avoid heavy includes
- await getTeamWithUsersAndDocument({ teamId, userId, docId });
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ });
+
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+ const document = await prisma.document.findUnique({
+ where: {
+ id: docId,
+ teamId,
+ },
+ select: { id: true },
+ });
+
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
const annotations = await prisma.documentAnnotation.findMany({
where: { documentId: docId, teamId },
include: {
@@ -72,13 +94,31 @@ export default async function handle(
try {
const validatedData = createAnnotationSchema.parse(req.body);
- // Verify user has access to document
- await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId,
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ });
+
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const document = await prisma.document.findUnique({
+ where: {
+ id: docId,
+ teamId,
+ },
+ select: { id: true },
});
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
const annotation = await prisma.documentAnnotation.create({
data: {
...validatedData,
diff --git a/pages/api/teams/[teamId]/documents/[id]/links.ts b/pages/api/teams/[teamId]/documents/[id]/links.ts
index fef4f77f..1f6b72f7 100644
--- a/pages/api/teams/[teamId]/documents/[id]/links.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/links.ts
@@ -4,7 +4,6 @@ import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { decryptEncrpytedPassword, log } from "@/lib/utils";
diff --git a/pages/api/teams/[teamId]/documents/[id]/overview.ts b/pages/api/teams/[teamId]/documents/[id]/overview.ts
index faac3dfe..4227fb42 100644
--- a/pages/api/teams/[teamId]/documents/[id]/overview.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/overview.ts
@@ -7,8 +7,6 @@ import { getServerSession } from "next-auth/next";
import { getFeatureFlags } from "@/lib/featureFlags";
import prisma from "@/lib/prisma";
-import { usePlan } from "@/lib/swr/use-billing";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { serializeFileSize } from "@/lib/utils";
@@ -35,19 +33,19 @@ export default async function handle(
try {
// First verify user has access to the team
- const team = await prisma.team.findUnique({
+ const teamAccess = await prisma.team.findUnique({
where: {
id: teamId,
users: {
some: {
- userId: userId,
+ userId,
},
},
},
select: { plan: true },
});
- if (!team) {
+ if (!teamAccess) {
return res.status(401).end("Unauthorized");
}
diff --git a/pages/api/teams/[teamId]/documents/[id]/update-name.ts b/pages/api/teams/[teamId]/documents/[id]/update-name.ts
index b4118310..2576138f 100644
--- a/pages/api/teams/[teamId]/documents/[id]/update-name.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/update-name.ts
@@ -5,7 +5,6 @@ import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
export default async function handle(
@@ -24,19 +23,31 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId,
- checkOwner: true,
- options: {
- select: {
- id: true,
- ownerId: true,
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
},
},
});
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const document = await prisma.document.findUnique({
+ where: {
+ id: docId,
+ teamId,
+ },
+ select: { id: true },
+ });
+
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
await prisma.document.update({
where: {
id: docId,
diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
index 29f381d3..d1b7a3ae 100644
--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
@@ -1,12 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
-import { DocumentStorageType } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";
import { processVideo } from "@/lib/trigger/optimize-video-files";
import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";
@@ -54,24 +52,39 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- const { team, document } = await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId: documentId,
- checkOwner: true,
- options: {
- select: {
- id: true,
- advancedExcelEnabled: true,
- versions: {
- orderBy: { createdAt: "desc" },
- take: 1,
- select: { versionNumber: true },
- },
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
},
},
});
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const document = await prisma.document.findUnique({
+ where: {
+ id: documentId,
+ teamId,
+ },
+ select: {
+ id: true,
+ advancedExcelEnabled: true,
+ versions: {
+ orderBy: { createdAt: "desc" },
+ take: 1,
+ select: { versionNumber: true },
+ },
+ },
+ });
+
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
// create a new document version
const currentVersionNumber = document?.versions
? document.versions[0].versionNumber
diff --git a/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts
index ca4a68b4..77f9cba1 100644
--- a/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.ts
@@ -4,7 +4,7 @@ import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
+import prisma from "@/lib/prisma";
import { getViewPageDuration } from "@/lib/tinybird";
import { CustomUser } from "@/lib/types";
@@ -32,19 +32,31 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId,
- checkOwner: true,
- options: {
- select: {
- id: true,
- ownerId: true,
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
},
},
});
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const document = await prisma.document.findUnique({
+ where: {
+ id: docId,
+ teamId,
+ },
+ select: { id: true },
+ });
+
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
const duration = await getViewPageDuration({
documentId: docId,
viewId: viewId,
diff --git a/pages/api/teams/[teamId]/documents/agreement.ts b/pages/api/teams/[teamId]/documents/agreement.ts
index cd914bf0..09c3f1fa 100644
--- a/pages/api/teams/[teamId]/documents/agreement.ts
+++ b/pages/api/teams/[teamId]/documents/agreement.ts
@@ -1,11 +1,9 @@
import { NextApiRequest, NextApiResponse } from "next";
-import { DocumentStorageType } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";
import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";
import { CustomUser } from "@/lib/types";
@@ -61,11 +59,22 @@ export default async function handle(
} = validationResult.data;
try {
- const { team } = await getTeamWithUsersAndDocument({
- teamId,
- userId,
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: {
+ userId,
+ },
+ },
+ },
+ select: { plan: true },
});
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
const folder = await prisma.folder.findUnique({
where: {
teamId_path: {
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index 68234926..e6cf2c67 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -1,14 +1,13 @@
import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
-import { DocumentStorageType, Prisma } from "@prisma/client";
+import { Prisma } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import { hashToken } from "@/lib/api/auth/token";
import { processDocument } from "@/lib/api/documents/process-document";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log, serializeFileSize } from "@/lib/utils";
import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";
@@ -270,11 +269,22 @@ export default async function handle(
} = validationResult.data;
try {
- const { team } = await getTeamWithUsersAndDocument({
- teamId,
- userId,
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: {
+ userId,
+ },
+ },
+ },
+ select: { plan: true, enableExcelAdvancedMode: true },
});
+ if (!team) {
+ return res.status(404).end("Team not found");
+ }
+
const document = await processDocument({
documentData: {
name,
diff --git a/pages/api/teams/[teamId]/documents/update.ts b/pages/api/teams/[teamId]/documents/update.ts
index 349be458..1836cd8d 100644
--- a/pages/api/teams/[teamId]/documents/update.ts
+++ b/pages/api/teams/[teamId]/documents/update.ts
@@ -1,14 +1,12 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
-import { getExtension, log } from "@/lib/utils";
-
-import { authOptions } from "../../../auth/[...nextauth]";
+import { log } from "@/lib/utils";
export default async function handle(
req: NextApiRequest,
@@ -30,12 +28,31 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- await getTeamWithUsersAndDocument({
- teamId,
- userId,
- docId: documentId,
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
});
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const document = await prisma.document.findUnique({
+ where: {
+ id: documentId,
+ teamId,
+ },
+ select: { id: true },
+ });
+
+ if (!document) {
+ return res.status(404).json({ error: "Document not found" });
+ }
+
// Save data to the database
await prisma.document.update({
where: { id: documentId },