Skip to content

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

update-name team scoped

Aashish-Upadhyay-10161c9d636a6f3
diff --git a/pages/api/teams/[teamId]/documents/[id]/update-name.ts b/pages/api/teams/[teamId]/documents/[id]/update-name.ts
index fa165cab..af3123b2 100644
--- a/pages/api/teams/[teamId]/documents/[id]/update-name.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/update-name.ts
@@ -1,56 +1,56 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
-import { authOptions } from "../../auth/[...nextauth]";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { CustomUser } from "@/lib/types";
+import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
+import { errorHanlder } from "@/lib/errorHandler";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
- // GET /api/documents/:id/update-name
+ // GET /api/teams/:teamId/documents/:id/update-name
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({
- where: {
- id: id,
+ await getTeamWithUsersAndDocument({
+ teamId,
+ userId,
+ docId,
+ checkOwner: true,
+ options: {
+ select: {
+ id: true,
+ ownerId: true,
+ },
},
- select: {
- ownerId: true,
- }
});
- // Check if the user is the owner of the document
- if (document?.ownerId !== (session.user as CustomUser).id) {
- return res.status(401).end("Unauthorized");
- }
-
await prisma.document.update({
where: {
- id: id,
+ id: docId,
},
data: {
name: req.body.name,
- }
+ },
});
- res.status(200).json({ message: "Document name updated!"});
+ res.status(200).json({ message: "Document name updated!" });
} catch (error) {
- return res.status(500).json({
- message: "Internal Server Error",
- error: (error as Error).message,
- });
+ errorHanlder(error, res);
}
} else {
- // We only allow GET and POST requests
- res.setHeader("Allow", ["GET", "POST"]);
+ // We only allow POST requests
+ res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}