mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-11-02 22:04:20 +00:00
Same failure as skipDuplicates, confirmed the same way against a database
built from this schema: Prisma does not ignore these on sqlite, it rejects
the call.
mode: "insensitive" — broke document search and listing, dataroom search,
and the two download-by-email lookups. Plain `contains` goes through LIKE,
which sqlite already treats case-insensitively for ascii, so dropping the
argument keeps the behaviour. `equals` does NOT: sqlite compares with `=`
case-sensitively, so the two lookups that matched a visitor's address that
way now narrow with `contains` and settle it with an exact comparison —
verify.ts was already doing that comparison, by-email.ts was not.
One of these was invisible to tsc: the documents where clause is assembled
from spreads into a variable, so nothing checked it against Prisma's type
and only the runtime rejected it.
array_contains — every webhook delivery was throwing instead of fanning out.
Three copies of the same query, now one `subscribers()` that matches the
trigger against the list. `Webhook.triggers` is another postgres String[]
that became Json, so it is narrowed like the rest.
99 errors -> 11, no file worse than it was.
Co-authored-by: Hanzo Dev <[email protected]>
290 lines
7.7 KiB
TypeScript
290 lines
7.7 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]";
|
|
import { Prisma } from "@prisma/client";
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { newId } from "@/lib/id-helper";
|
|
import prisma from "@/lib/prisma";
|
|
import { CustomUser } from "@/lib/types";
|
|
|
|
export const config = {
|
|
maxDuration: 180,
|
|
};
|
|
|
|
export default async function handle(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
if (req.method === "GET") {
|
|
// GET /api/teams/:teamId/datarooms
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const userId = (session.user as CustomUser).id;
|
|
const { teamId, search, status, tags, simple } = req.query as {
|
|
teamId: string;
|
|
search?: string;
|
|
status?: string;
|
|
tags?: string;
|
|
simple?: string;
|
|
};
|
|
|
|
// Simple mode: return minimal data without filters, tags, or aggregations
|
|
const isSimpleMode = simple === "true";
|
|
|
|
try {
|
|
const teamAccess = await prisma.userTeam.findUnique({
|
|
where: {
|
|
userId_teamId: {
|
|
userId: userId,
|
|
teamId: teamId,
|
|
},
|
|
},
|
|
select: {
|
|
teamId: true,
|
|
},
|
|
});
|
|
|
|
if (!teamAccess) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
// Simple mode: return minimal data without filters, tags, or aggregations
|
|
if (isSimpleMode) {
|
|
const datarooms = await prisma.dataroom.findMany({
|
|
where: {
|
|
teamId: teamId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
internalName: true,
|
|
createdAt: true,
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
});
|
|
|
|
return res.status(200).json({ datarooms });
|
|
}
|
|
|
|
const now = new Date();
|
|
const activeLinkFilter: Prisma.LinkWhereInput = {
|
|
linkType: "DATAROOM_LINK",
|
|
deletedAt: null,
|
|
isArchived: false,
|
|
OR: [{ expiresAt: null }, { expiresAt: { gte: now } }],
|
|
};
|
|
|
|
// Build where clause based on filters
|
|
const whereClause: Prisma.DataroomWhereInput = {
|
|
teamId: teamId,
|
|
};
|
|
|
|
// Search filter - search both name and internalName
|
|
if (search) {
|
|
whereClause.OR = [
|
|
{
|
|
name: {
|
|
contains: search,
|
|
},
|
|
},
|
|
{
|
|
internalName: {
|
|
contains: search,
|
|
},
|
|
},
|
|
];
|
|
}
|
|
|
|
// Tags filter
|
|
if (tags) {
|
|
const tagNames = tags.split(",").filter(Boolean);
|
|
if (tagNames.length > 0) {
|
|
whereClause.tags = {
|
|
some: {
|
|
tag: {
|
|
name: {
|
|
in: tagNames,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
// if (status === "active") {
|
|
// whereClause.links = { some: activeLinkFilter };
|
|
// } else if (status === "inactive") {
|
|
// whereClause.links = { none: activeLinkFilter };
|
|
// }
|
|
|
|
const [totalCount, datarooms] = await Promise.all([
|
|
prisma.dataroom.count({
|
|
where: {
|
|
teamId: teamId,
|
|
},
|
|
}),
|
|
prisma.dataroom.findMany({
|
|
where: whereClause,
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
createdAt: true,
|
|
_count: {
|
|
select: { documents: true, views: true },
|
|
},
|
|
tags: {
|
|
include: {
|
|
tag: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
color: true,
|
|
description: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
}),
|
|
]);
|
|
|
|
const dataroomIds = datarooms.map((dataroom) => dataroom.id);
|
|
const [activeLinkCounts, lastViewedAtByDataroom] = dataroomIds.length
|
|
? await Promise.all([
|
|
prisma.link.groupBy({
|
|
by: ["dataroomId"],
|
|
where: {
|
|
dataroomId: { in: dataroomIds },
|
|
...activeLinkFilter,
|
|
},
|
|
_count: {
|
|
_all: true,
|
|
},
|
|
}),
|
|
prisma.view.groupBy({
|
|
by: ["dataroomId"],
|
|
where: {
|
|
dataroomId: { in: dataroomIds },
|
|
},
|
|
_max: {
|
|
viewedAt: true,
|
|
},
|
|
}),
|
|
])
|
|
: [[], []];
|
|
|
|
const activeLinkCountMap = new Map(
|
|
activeLinkCounts.map((entry) => [entry.dataroomId, entry._count._all]),
|
|
);
|
|
const lastViewedAtMap = new Map(
|
|
lastViewedAtByDataroom.map((entry) => [
|
|
entry.dataroomId,
|
|
entry._max.viewedAt,
|
|
]),
|
|
);
|
|
|
|
const dataroomsWithStats = datarooms.map((dataroom) => ({
|
|
...dataroom,
|
|
activeLinkCount: activeLinkCountMap.get(dataroom.id) ?? 0,
|
|
lastViewedAt: lastViewedAtMap.get(dataroom.id) ?? null,
|
|
}));
|
|
|
|
return res.status(200).json({
|
|
datarooms: dataroomsWithStats,
|
|
totalCount,
|
|
});
|
|
} catch (error) {
|
|
console.error("Request error", error);
|
|
return res.status(500).json({ error: "Error fetching datarooms" });
|
|
}
|
|
} else if (req.method === "POST") {
|
|
// POST /api/teams/:teamId/datarooms
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
res.status(401).end("Unauthorized");
|
|
return;
|
|
}
|
|
|
|
const userId = (session.user as CustomUser).id;
|
|
|
|
const { teamId } = req.query as { teamId: string };
|
|
const { name, internalName } = req.body as { name: string; internalName?: string };
|
|
|
|
try {
|
|
// Check if the user is part of the team
|
|
const team = await prisma.team.findUnique({
|
|
where: {
|
|
id: teamId,
|
|
plan: {
|
|
// exclude all teams not on `business`, `datarooms`, `datarooms-plus`, `datarooms-premium`, `business+old`, `datarooms+old`, `datarooms-plus+old`, `datarooms-premium+old` plan
|
|
in: [
|
|
"business",
|
|
"datarooms",
|
|
"datarooms-plus",
|
|
"datarooms-premium",
|
|
"business+old",
|
|
"datarooms+old",
|
|
"datarooms-plus+old",
|
|
"datarooms-premium+old",
|
|
"datarooms+drtrial",
|
|
"business+drtrial",
|
|
"datarooms-plus+drtrial",
|
|
"datarooms-premium+drtrial",
|
|
],
|
|
},
|
|
users: {
|
|
some: {
|
|
userId: userId,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!team) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
// Limits: Check if the user has reached the limit of datarooms in the team
|
|
const dataroomCount = await prisma.dataroom.count({
|
|
where: {
|
|
teamId: teamId,
|
|
},
|
|
});
|
|
|
|
const pId = newId("dataroom");
|
|
|
|
const dataroom = await prisma.dataroom.create({
|
|
data: {
|
|
name: name,
|
|
teamId: teamId,
|
|
pId: pId,
|
|
...(internalName && { internalName: internalName.trim() }),
|
|
},
|
|
});
|
|
|
|
const dataroomWithCount = {
|
|
...dataroom,
|
|
_count: { documents: 0 },
|
|
};
|
|
|
|
res.status(201).json({ dataroom: dataroomWithCount });
|
|
} catch (error) {
|
|
console.error("Request error", error);
|
|
res.status(500).json({ error: "Error creating dataroom" });
|
|
}
|
|
} else {
|
|
// We only allow POST requests
|
|
res.setHeader("Allow", ["GET", "POST"]);
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
}
|