Files

131 lines
3.4 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { sortItemsByIndexAndName } from "@/lib/utils/sort-items-by-index-name";
import { folderPathSchema } from "@/lib/zod/schemas/folders";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method === "GET") {
// GET /api/teams/:teamId/datarooms/:id/folders/documents/:name
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}
const userId = (session.user as CustomUser).id;
const {
teamId,
id: dataroomId,
name,
} = req.query as { teamId: string; id: string; name: string[] };
// Validate that name is an array of strings using shared Zod schema
const nameValidation = folderPathSchema.safeParse(name);
if (!nameValidation.success) {
return res.status(400).json({
error: "Invalid folder path format",
details: nameValidation.error.issues.map((issue) => issue.message),
});
}
const validatedName = nameValidation.data;
const path = "/" + validatedName.join("/"); // construct the materialized path
try {
// Check if the user is part of the team
const team = await prisma.team.findUnique({
where: {
id: teamId,
users: {
some: {
userId: userId,
},
},
},
});
if (!team) {
return res.status(401).end("Unauthorized");
}
const folder = await prisma.dataroomFolder.findUnique({
where: {
dataroomId_path: {
dataroomId,
path,
},
},
select: {
id: true,
parentId: true,
},
});
if (!folder) {
return res.status(404).end("Folder not found");
}
const documents = await prisma.dataroomDocument.findMany({
where: {
dataroomId: dataroomId,
folderId: folder.id,
},
orderBy: [
{ orderIndex: "asc" },
{
document: {
name: "asc",
},
},
],
select: {
id: true,
dataroomId: true,
folderId: true,
createdAt: true,
updatedAt: true,
orderIndex: true,
hierarchicalIndex: true,
document: {
select: {
id: true,
name: true,
type: true,
versions: {
select: { id: true, hasPages: true },
},
isExternalUpload: true,
_count: {
select: {
views: { where: { dataroomId } },
versions: true,
},
},
},
},
},
});
const sortedDocuments = sortItemsByIndexAndName(documents);
return res.status(200).json(sortedDocuments);
} catch (error) {
console.error("Request error", error);
return res
.status(500)
.json({ error: "Error fetching dataroom folder documents" });
}
} else {
// We only allow GET requests
res.setHeader("Allow", ["GET"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}