mirror of
https://github.com/hanzoai/dataroom.git
synced 2026-11-02 22:22:45 +00:00
137 lines
3.7 KiB
TypeScript
137 lines
3.7 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { errorhandler } from "@/lib/errorHandler";
|
|
import prisma from "@/lib/prisma";
|
|
import { CustomUser, LinkWithViews } from "@/lib/types";
|
|
import { decryptEncrpytedPassword, log } from "@/lib/utils";
|
|
|
|
import { authOptions } from "../../../../auth/[...nextauth]";
|
|
|
|
export default async function handle(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
if (req.method === "GET") {
|
|
// GET /api/teams/:teamId/datarooms/:id/links
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const { teamId, id: dataroomId } = req.query as {
|
|
teamId: string;
|
|
id: string;
|
|
};
|
|
|
|
const userId = (session.user as CustomUser).id;
|
|
|
|
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 links = await prisma.link.findMany({
|
|
where: {
|
|
dataroomId,
|
|
linkType: "DATAROOM_LINK",
|
|
teamId: teamId,
|
|
deletedAt: null, // exclude deleted links
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
include: {
|
|
views: {
|
|
where: {
|
|
viewType: "DATAROOM_VIEW",
|
|
},
|
|
orderBy: {
|
|
viewedAt: "desc",
|
|
},
|
|
take: 1,
|
|
},
|
|
customFields: true,
|
|
visitorGroups: {
|
|
select: { visitorGroupId: true },
|
|
},
|
|
_count: {
|
|
select: { views: { where: { viewType: "DATAROOM_VIEW" } } },
|
|
},
|
|
},
|
|
});
|
|
|
|
let extendedLinks: LinkWithViews[] = links as LinkWithViews[];
|
|
// Decrypt the password for each link
|
|
if (extendedLinks && extendedLinks.length > 0) {
|
|
extendedLinks = await Promise.all(
|
|
extendedLinks.map(async (link) => {
|
|
// Decrypt the password if it exists
|
|
if (link.password !== null) {
|
|
link.password = decryptEncrpytedPassword(link.password);
|
|
}
|
|
if (link.enableUpload && link.uploadFolderId !== null) {
|
|
const folder = await prisma.dataroomFolder.findUnique({
|
|
where: {
|
|
id: link.uploadFolderId,
|
|
},
|
|
select: {
|
|
name: true,
|
|
},
|
|
});
|
|
link.uploadFolderName = folder?.name;
|
|
}
|
|
const tags = await prisma.tag.findMany({
|
|
where: {
|
|
items: {
|
|
some: {
|
|
linkId: link.id,
|
|
itemType: "LINK_TAG",
|
|
},
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
color: true,
|
|
description: true,
|
|
},
|
|
});
|
|
|
|
return {
|
|
...link,
|
|
tags,
|
|
};
|
|
}),
|
|
);
|
|
}
|
|
|
|
// console.log("links", links);
|
|
return res.status(200).json(extendedLinks);
|
|
} catch (error) {
|
|
log({
|
|
message: `Failed to get links for dataroom: _${dataroomId}_. \n\n ${error} \n\n*Metadata*: \`{teamId: ${teamId}, userId: ${userId}}\``,
|
|
type: "error",
|
|
});
|
|
errorhandler(error, res);
|
|
}
|
|
} else {
|
|
// We only allow GET requests
|
|
res.setHeader("Allow", ["GET"]);
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
}
|