mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
- GET/POST /api/teams/:teamId/visitor-groups - list & create - GET/PUT/DELETE /api/teams/:teamId/visitor-groups/:groupId - CRUD - Deletion protection: prevents deletion if group is used by active links - SWR hook for consuming visitor groups data in UI Co-authored-by: Marc Seitz <[email protected]>
103 lines
2.6 KiB
TypeScript
103 lines
2.6 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]";
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { errorhandler } from "@/lib/errorHandler";
|
|
import prisma from "@/lib/prisma";
|
|
import { CustomUser } from "@/lib/types";
|
|
|
|
export default async function handle(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
if (req.method === "GET") {
|
|
// GET /api/teams/:teamId/visitor-groups
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const { teamId } = req.query 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 visitorGroups = await prisma.visitorGroup.findMany({
|
|
where: { teamId },
|
|
orderBy: { createdAt: "desc" },
|
|
include: {
|
|
_count: {
|
|
select: { links: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
return res.status(200).json(visitorGroups);
|
|
} catch (error) {
|
|
errorhandler(error, res);
|
|
}
|
|
} else if (req.method === "POST") {
|
|
// POST /api/teams/:teamId/visitor-groups
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const { teamId } = req.query as { teamId: string };
|
|
const userId = (session.user as CustomUser).id;
|
|
const { name, emails } = req.body as { name: string; emails: string[] };
|
|
|
|
if (!name || name.trim() === "") {
|
|
return res.status(400).json({ error: "Group name is required." });
|
|
}
|
|
|
|
try {
|
|
const teamAccess = await prisma.userTeam.findUnique({
|
|
where: {
|
|
userId_teamId: {
|
|
userId,
|
|
teamId,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!teamAccess) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const visitorGroup = await prisma.visitorGroup.create({
|
|
data: {
|
|
name: name.trim(),
|
|
emails: emails || [],
|
|
teamId,
|
|
},
|
|
include: {
|
|
_count: {
|
|
select: { links: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
return res.status(201).json(visitorGroup);
|
|
} catch (error) {
|
|
errorhandler(error, res);
|
|
}
|
|
} else {
|
|
res.setHeader("Allow", ["GET", "POST"]);
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
}
|