Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
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`);
}
}