Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
team scoped: teams/:teamId/documents/update.ts
Aashish-Upadhyay-1017ebb74661dc2
diff --git a/pages/api/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.tssimilarity index 94%rename from pages/api/documents/[id]/index.tsrename to pages/api/teams/[teamId]/documents/[id]/index.tsindex e37f523d..b9d51d9f 100644--- a/pages/api/documents/[id]/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts@@ -1,7 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";-import { authOptions } from "../../auth/[...nextauth]";+import { authOptions } from "../../../../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { del } from "@vercel/blob";@@ -30,7 +30,7 @@ export default async function handle(orderBy: { createdAt: "desc" },take: 1,},- }+ },});if (!document) {@@ -52,14 +52,14 @@ export default async function handle(} else if (req.method === "DELETE") {// DELETE /api/document/:idconst session = await getServerSession(req, res, authOptions);- if (!session) {+ if (!session) {res.status(401).end("Unauthorized");return;}const { id } = req.query as { id: string };- try{+ try {const document = await prisma.document.findUnique({where: {id: id,@@ -76,13 +76,13 @@ export default async function handle(}// delete the document from vercel blob- await del(document.file)+ await del(document.file);// delete the document from databaseawait prisma.document.delete({where: {id: id,},- })+ });res.status(204).end(); // 204 No Content response for successful deletes} catch (error) {@@ -91,7 +91,6 @@ export default async function handle(error: (error as Error).message,});}-} else {// We only allow GET and DELETE requestsres.setHeader("Allow", ["GET", "DELETE"]);diff --git a/pages/api/documents/[id]/links.ts b/pages/api/teams/[teamId]/documents/[id]/links.tssimilarity index 94%rename from pages/api/documents/[id]/links.tsrename to pages/api/teams/[teamId]/documents/[id]/links.tsindex 85752619..14b3802b 100644--- a/pages/api/documents/[id]/links.ts+++ b/pages/api/teams/[teamId]/documents/[id]/links.ts@@ -1,7 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";-import { authOptions } from "../../auth/[...nextauth]";+import { authOptions } from "../../../../auth/[...nextauth]";import { log } from "@/lib/utils";export default async function handle(@@ -29,7 +29,7 @@ export default async function handle(views: {orderBy: {viewedAt: "desc",- }+ },},_count: {select: { views: true },@@ -41,7 +41,7 @@ export default async function handle(res.status(200).json(links);} catch (error) {- log(`Failed to get links for document ${id}. Error: \n\n ${error}`)+ log(`Failed to get links for document ${id}. Error: \n\n ${error}`);return res.status(500).json({message: "Internal Server Error",error: (error as Error).message,diff --git a/pages/api/documents/[id]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/stats.tssimilarity index 96%rename from pages/api/documents/[id]/stats.tsrename to pages/api/teams/[teamId]/documents/[id]/stats.tsindex aa6d97af..aeb93d62 100644--- a/pages/api/documents/[id]/stats.ts+++ b/pages/api/teams/[teamId]/documents/[id]/stats.ts@@ -1,7 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";-import { authOptions } from "../../auth/[...nextauth]";+import { authOptions } from "../../../../auth/[...nextauth]";import { getTotalAvgPageDuration } from "@/lib/tinybird";export default async function handle(diff --git a/pages/api/documents/[id]/views/[viewId]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.tssimilarity index 100%rename from pages/api/documents/[id]/views/[viewId]/stats.tsrename to pages/api/teams/[teamId]/documents/[id]/views/[viewId]/stats.tsdiff --git a/pages/api/documents/[id]/views/index.ts b/pages/api/teams/[teamId]/documents/[id]/views/index.tssimilarity index 100%rename from pages/api/documents/[id]/views/index.tsrename to pages/api/teams/[teamId]/documents/[id]/views/index.tsdiff --git a/pages/api/documents/index.ts b/pages/api/teams/[teamId]/documents/index.tssimilarity index 95%rename from pages/api/documents/index.tsrename to pages/api/teams/[teamId]/documents/index.tsindex 44f02a3c..1c3d90ee 100644--- a/pages/api/documents/index.ts+++ b/pages/api/teams/[teamId]/documents/index.ts@@ -1,7 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";-import { authOptions } from "../auth/[...nextauth]";+import { authOptions } from "../../../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { getExtension, log } from "@/lib/utils";import { identifyUser, trackAnalytics } from "@/lib/analytics";@@ -69,7 +69,7 @@ export default async function handle(type: type,ownerId: (session.user as CustomUser).id,links: {- create: {}+ create: {},},versions: {create: {@@ -112,7 +112,7 @@ export default async function handle(res.status(201).json(document);} catch (error) {- log(`Failed to create document. Error: \n\n ${error}`)+ log(`Failed to create document. Error: \n\n ${error}`);res.status(500).json({message: "Internal Server Error",error: (error as Error).message,diff --git a/pages/api/documents/update.ts b/pages/api/teams/[teamId]/documents/update.tssimilarity index 60%rename from pages/api/documents/update.tsrename to pages/api/teams/[teamId]/documents/update.tsindex a0b2d732..cfc0a555 100644--- a/pages/api/documents/update.ts+++ b/pages/api/teams/[teamId]/documents/update.ts@@ -1,7 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";-import { authOptions } from "../auth/[...nextauth]";+import { authOptions } from "../../../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { getExtension, log } from "@/lib/utils";@@ -10,7 +10,7 @@ export default async function handle(res: NextApiResponse) {if (req.method === "POST") {- // POST /api/documents/update+ // POST /api/teams/:teamId/documents/updateconst session = await getServerSession(req, res, authOptions);if (!session) {res.status(401).end("Unauthorized");@@ -20,11 +20,35 @@ export default async function handle(// Assuming data is an object with `name` and `description` propertiesconst { documentId, numPages } = req.body;+ const { teamId } = req.query as { teamId: string };+try {+ // check if the document exists in the team or not+ const team = await prisma.team.findUnique({+ where: {+ id: teamId,+ },+ include: {+ documents: {+ select: {+ id: true,+ },+ },+ },+ });++ const documentExists = team?.documents.some(+ (document) => document.id === documentId+ );++ if (!documentExists) {+ return res.status(400).end("Document doesn't exists in the team");+ }+// Save data to the databaseawait prisma.document.update({where: { id: documentId },- data: {+ data: {numPages: numPages,// versions: {// update: {@@ -35,7 +59,7 @@ export default async function handle(},});- res.status(201).json({message: "Document updated successfully"});+ res.status(201).json({ message: "Document updated successfully" });} catch (error) {log(`Failed to create document. Error: \n\n ${error}`);res.status(500).json({@@ -44,8 +68,8 @@ export default async function handle(});}} 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`);}}