mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
`skipDuplicates` is not merely ignored on sqlite — Prisma rejects the whole call with "Unknown argument `skipDuplicates`". Verified against a database built from this schema, so all seventeen call sites were throwing at runtime: applying dataroom and folder permissions, adding group members, saving link visitor groups and tags, bulk and folder downloads, and the year-in-review queue. `insert()` keeps the behaviour those sites asked for. The batch insert is still one statement; only a uniqueness collision falls back to row-at-a-time, skipping what is already there. That fallback is idempotent, so it is correct even if the batch wrote some rows before colliding, and a caught P2002 leaves an open transaction usable — checked, since four of these run inside one. Anything that is not a duplicate still throws. The nested customFields write just drops the argument: CustomField has no unique constraint, so nothing could ever have been skipped there. 99 errors -> 21, no file worse than it was. Co-authored-by: Hanzo Dev <[email protected]>
210 lines
6.0 KiB
TypeScript
210 lines
6.0 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]";
|
|
import { Prisma } from "@prisma/client";
|
|
import { after } from "@/lib/after";
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { errorhandler } from "@/lib/errorHandler";
|
|
import prisma from "@/lib/prisma";
|
|
import { CustomUser } from "@/lib/types";
|
|
import { sendLinkCreatedWebhook } from "@/lib/webhook/triggers/link-created";
|
|
import { insert } from "@/lib/insert";
|
|
|
|
export const config = {
|
|
// so background work can outlive the response
|
|
supportsResponseStreaming: true,
|
|
};
|
|
|
|
export default async function handle(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
if (req.method === "POST") {
|
|
// PUT /api/links/:id/duplicate
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const { id } = req.query as { id: string };
|
|
const { teamId } = req.body as { teamId: string };
|
|
const userId = (session.user as CustomUser).id;
|
|
|
|
try {
|
|
const teamAccess = await prisma.userTeam.findUnique({
|
|
where: {
|
|
userId_teamId: {
|
|
userId,
|
|
teamId,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!teamAccess) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const link = await prisma.link.findUnique({
|
|
where: { id, teamId },
|
|
include: {
|
|
tags: {
|
|
select: {
|
|
tag: {
|
|
select: {
|
|
id: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
permissionGroup: {
|
|
include: {
|
|
accessControls: true,
|
|
},
|
|
},
|
|
customFields: true,
|
|
visitorGroups: true,
|
|
},
|
|
});
|
|
|
|
if (!link) {
|
|
return res.status(404).json({ error: "Link not found" });
|
|
}
|
|
|
|
if (link.deletedAt) {
|
|
return res.status(404).json({ error: "Link has been deleted" });
|
|
}
|
|
|
|
const {
|
|
tags,
|
|
permissionGroup,
|
|
permissionGroupId,
|
|
customFields,
|
|
visitorGroups,
|
|
...rest
|
|
} = link;
|
|
const linkTags = tags.map((t) => t.tag.id);
|
|
|
|
const newLinkName = link.name
|
|
? link.name + " (Copy)"
|
|
: `Link #${link.id.slice(-5)} (Copy)`;
|
|
|
|
const newLink = await prisma.$transaction(async (tx) => {
|
|
// Duplicate permission group if it exists
|
|
let newPermissionGroupId: string | null = null;
|
|
if (permissionGroup) {
|
|
// Create the new permission group
|
|
const newPermissionGroup = await tx.permissionGroup.create({
|
|
data: {
|
|
name: permissionGroup.name + " (Copy)",
|
|
description: permissionGroup.description,
|
|
dataroomId: permissionGroup.dataroomId,
|
|
teamId: permissionGroup.teamId,
|
|
},
|
|
});
|
|
|
|
// Duplicate all access controls
|
|
if (permissionGroup.accessControls.length > 0) {
|
|
await tx.permissionGroupAccessControls.createMany({
|
|
data: permissionGroup.accessControls.map((control) => ({
|
|
groupId: newPermissionGroup.id,
|
|
itemId: control.itemId,
|
|
itemType: control.itemType,
|
|
canView: control.canView,
|
|
canDownload: control.canDownload,
|
|
canDownloadOriginal: control.canDownloadOriginal,
|
|
})),
|
|
});
|
|
}
|
|
|
|
newPermissionGroupId = newPermissionGroup.id;
|
|
}
|
|
|
|
const createdLink = await tx.link.create({
|
|
data: {
|
|
...rest,
|
|
id: undefined,
|
|
slug: link.slug ? link.slug + "-copy" : null,
|
|
name: newLinkName,
|
|
watermarkConfig: link.watermarkConfig || Prisma.JsonNull,
|
|
createdAt: undefined,
|
|
updatedAt: undefined,
|
|
permissionGroupId: newPermissionGroupId,
|
|
ownerId: userId,
|
|
...(customFields.length > 0 && {
|
|
customFields: {
|
|
createMany: {
|
|
data: customFields.map((field) => ({
|
|
type: field.type,
|
|
identifier: field.identifier,
|
|
label: field.label,
|
|
placeholder: field.placeholder,
|
|
required: field.required,
|
|
disabled: field.disabled,
|
|
orderIndex: field.orderIndex,
|
|
})),
|
|
},
|
|
},
|
|
}),
|
|
...(visitorGroups.length > 0 && {
|
|
visitorGroups: {
|
|
createMany: {
|
|
data: visitorGroups.map((vg) => ({
|
|
visitorGroupId: vg.visitorGroupId,
|
|
})),
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
include: {
|
|
customFields: true,
|
|
visitorGroups: true,
|
|
},
|
|
});
|
|
|
|
if (linkTags?.length) {
|
|
await insert(tx.tagItem, linkTags.map((tagId: string) => ({
|
|
tagId,
|
|
itemType: "LINK_TAG",
|
|
linkId: createdLink.id,
|
|
taggedBy: (session.user as CustomUser).id,
|
|
})));
|
|
}
|
|
|
|
const tags = linkTags?.length
|
|
? await tx.tag.findMany({
|
|
where: { id: { in: linkTags } },
|
|
select: { id: true, name: true, color: true, description: true },
|
|
})
|
|
: [];
|
|
|
|
return { ...createdLink, tags };
|
|
});
|
|
const linkWithView = {
|
|
...newLink,
|
|
_count: { views: 0 },
|
|
views: [],
|
|
};
|
|
|
|
after(
|
|
sendLinkCreatedWebhook({
|
|
teamId,
|
|
data: {
|
|
link_id: newLink.id,
|
|
document_id: newLink.documentId,
|
|
dataroom_id: newLink.dataroomId,
|
|
},
|
|
}),
|
|
);
|
|
|
|
return res.status(201).json(linkWithView);
|
|
} catch (error) {
|
|
errorhandler(error, res);
|
|
}
|
|
}
|
|
|
|
// We only allow PUT requests
|
|
res.setHeader("Allow", ["POST"]);
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|