Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Document delete (#81)
Aashish Upadhyayb36522361146
diff --git a/pages/api/documents/[id]/index.ts b/pages/api/documents/[id]/index.tsindex 2c89775a..167825b4 100644--- a/pages/api/documents/[id]/index.ts+++ b/pages/api/documents/[id]/index.ts@@ -3,6 +3,7 @@ import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";import { authOptions } from "../../auth/[...nextauth]";import { CustomUser } from "@/lib/types";+import { del } from "@vercel/blob";export default async function handle(req: NextApiRequest,@@ -40,9 +41,55 @@ export default async function handle(error: (error as Error).message,});}+ } else if (req.method === "DELETE") {+ // DELETE /api/document/:id+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ res.status(401).end("Unauthorized");+ return;+ }++ const { id } = req.query as { id: string };++ try{+ const document = await prisma.document.findUnique({+ where: {+ id: id,+ },+ });++ if (!document) {+ return res.status(404).end("Document not found");+ }++ // check that the user is owner of the document, otherwise return 401+ if (document.ownerId !== (session.user as CustomUser).id) {+ return res.status(401).end("Unauthorized to access the document");+ }++ await Promise.all([+ // delete the document from vercel blob+ await del(document.file),++ // delete the document from database+ await prisma.document.delete({+ where: {+ id: id,+ }+ }),+ ]);++ res.status(204).end(); // 204 No Content response for successful deletes+ } catch (error) {+ return res.status(500).json({+ message: "Internal Server Error",+ error: (error as Error).message,+ });+ }+} else {- // We only allow GET and POST requests- res.setHeader("Allow", ["GET"]);+ // We only allow GET and DELETE requests+ res.setHeader("Allow", ["GET", "DELETE"]);return res.status(405).end(`Method ${req.method} Not Allowed`);}}