Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: res.end and res.json
Marc Seitz665c363bcea8
diff --git a/pages/api/documents/[id]/index.ts b/pages/api/documents/[id]/index.tsnew file mode 100644index 00000000..8633e28b--- /dev/null+++ b/pages/api/documents/[id]/index.ts@@ -0,0 +1,40 @@+import { NextApiRequest, NextApiResponse } from "next";+import { getServerSession } from "next-auth/next";+import prisma from "@/lib/prisma";+import { authOptions } from "../../auth/[...nextauth]";++export default async function handle(+ req: NextApiRequest,+ res: NextApiResponse+) {+ if (req.method === "GET") {+ // GET /api/documents/:id+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }++ const { id } = req.query as { id: string };++ try {+ const document = await prisma.document.findUnique({+ where: {+ id: id,+ },+ });++ // TODO: Check that the user is owner of the document, otherwise return 401++ res.status(200).json(document);+ } 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"]);+ return res.status(405).end(`Method ${req.method} Not Allowed`);+ }+}diff --git a/pages/api/documents/[id]/links.ts b/pages/api/documents/[id]/links.tsnew file mode 100644index 00000000..7cd2e3b1--- /dev/null+++ b/pages/api/documents/[id]/links.ts@@ -0,0 +1,49 @@+import { NextApiRequest, NextApiResponse } from "next";+import { getServerSession } from "next-auth/next";+import prisma from "@/lib/prisma";+import { authOptions } from "../../auth/[...nextauth]";++export default async function handle(+ req: NextApiRequest,+ res: NextApiResponse+) {+ if (req.method === "GET") {+ // GET /api/documents/:id+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }++ const { id } = req.query as { id: string };++ try {+ const links = await prisma.link.findMany({+ where: {+ documentId: id,+ },+ orderBy: {+ createdAt: "asc",+ },+ include: {+ views: true,+ _count: {+ select: { views: true },+ },+ },+ });++ // TODO: Check that the user is owner of the document, otherwise return 401++ res.status(200).json(links);+ } 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", "POST"]);+ return res.status(405).end(`Method ${req.method} Not Allowed`);+ }+}diff --git a/pages/api/documents/[id]/stats.ts b/pages/api/documents/[id]/stats.tsnew file mode 100644index 00000000..46fe0f25--- /dev/null+++ b/pages/api/documents/[id]/stats.ts@@ -0,0 +1,46 @@+import { NextApiRequest, NextApiResponse } from "next";+import { getServerSession } from "next-auth/next";+import prisma from "@/lib/prisma";+import { authOptions } from "../../auth/[...nextauth]";++export default async function handle(+ req: NextApiRequest,+ res: NextApiResponse+) {+ if (req.method === "GET") {+ // GET /api/documents/:id+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }++ const { id } = req.query as { id: string };++ try {+ const views = await prisma.view.findMany({+ where: { documentId: id },+ });++ const groupedViews = await prisma.view.groupBy({+ by: ["viewerEmail"],+ where: { documentId: id },+ _count: { id: true },+ });++ const stats = { views, groupedViews };++ // 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({+ message: "Internal Server Error",+ error: (error as Error).message,+ });+ }+ } else {+ // We only allow GET and POST requests+ res.setHeader("Allow", ["GET"]);+ return res.status(405).end(`Method ${req.method} Not Allowed`);+ }+}diff --git a/pages/api/documents/index.ts b/pages/api/documents/index.tsnew file mode 100644index 00000000..3d865f27--- /dev/null+++ b/pages/api/documents/index.ts@@ -0,0 +1,92 @@+import { NextApiRequest, NextApiResponse } from "next";+import { getServerSession } from "next-auth/next";+import prisma from "@/lib/prisma";+import { authOptions } from "../auth/[...nextauth]";+import { CustomUser } from "@/lib/types";+import { getExtension } from "@/lib/utils";++export default async function handle(+ req: NextApiRequest,+ res: NextApiResponse+) {+ if (req.method === "GET") {+ // GET /api/documents+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }++ try {+ const documents = await prisma.document.findMany({+ where: {+ ownerId: (session.user as CustomUser).id,+ },+ orderBy: {+ createdAt: "desc",+ },+ include: {+ _count: {+ select: { links: true },+ },+ links: {+ take: 1,+ select: { id: true },+ },+ },+ });++ res.status(200).json(documents);+ } catch (error) {+ return res.status(500).json({+ message: "Internal Server Error",+ error: (error as Error).message,+ });+ }+ } else if (req.method === "POST") {+ // POST /api/documents+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ res.status(401).end("Unauthorized");+ return;+ }++ // Assuming data is an object with `name` and `description` properties+ const { name, url, description } = req.body;++ // Get the file extension and save it as the type+ const type = getExtension(name);++ // You could perform some validation here++ try {+ // Save data to the database+ const document = await prisma.document.create({+ data: {+ name: name,+ description: description,+ file: url,+ type: type,+ ownerId: (session.user as CustomUser).id,+ },+ });++ const link = await prisma.link.create({+ data: {+ documentId: document.id,+ // url: nanoid(),+ },+ });++ res.status(201).json({ document, linkId: link.id });+ } catch (error) {+ 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", "POST"]);+ return res.status(405).end(`Method ${req.method} Not Allowed`);+ }+}diff --git a/pages/api/links.ts b/pages/api/links.tsindex 26738a0e..55d056d0 100644--- a/pages/api/links.ts+++ b/pages/api/links.ts@@ -1,11 +1,18 @@import { NextApiRequest, NextApiResponse } from "next";import prisma from "@/lib/prisma";+import { getServerSession } from "next-auth/next";+import { authOptions } from "./auth/[...nextauth]";export default async function handler(req: NextApiRequest,res: NextApiResponse) {if (req.method === "POST") {+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }+const { documentId } = req.body;// Fetch the link and its related document from the databasediff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.tsnew file mode 100644index 00000000..43cc24a9--- /dev/null+++ b/pages/api/links/[id]/index.ts@@ -0,0 +1,35 @@+import { NextApiRequest, NextApiResponse } from "next";+import prisma from "@/lib/prisma";++export default async function handle(+ req: NextApiRequest,+ res: NextApiResponse+) {+ if (req.method === "GET") {+ // GET /api/links/:id+ const { id } = req.query as { id: string };++ try {+ const link = await prisma.link.findUnique({+ where: {+ id: id,+ },+ select: {+ id: true,+ document: { select: { id: true, name: true, file: true } },+ },+ });++ res.status(200).json(link);+ } 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"]);+ return res.status(405).end(`Method ${req.method} Not Allowed`);+ }+}