Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add visitor groups API routes and SWR hook
Cursor Agentf624275681ca
diff --git a/lib/swr/use-visitor-groups.ts b/lib/swr/use-visitor-groups.tsnew file mode 100644index 00000000..763d0920--- /dev/null+++ b/lib/swr/use-visitor-groups.ts@@ -0,0 +1,33 @@+import { useTeam } from "@/context/team-context";+import { VisitorGroup } from "@prisma/client";+import useSWR from "swr";++import { fetcher } from "@/lib/utils";++export type VisitorGroupWithCount = VisitorGroup & {+ _count: {+ links: number;+ };+};++export default function useVisitorGroups() {+ const teamInfo = useTeam();+ const teamId = teamInfo?.currentTeam?.id;++ const {+ data: visitorGroups,+ error,+ mutate,+ } = useSWR<VisitorGroupWithCount[]>(+ teamId ? `/api/teams/${teamId}/visitor-groups` : null,+ fetcher,+ { dedupingInterval: 30000 },+ );++ return {+ visitorGroups,+ loading: !visitorGroups && !error,+ error,+ mutate,+ };+}diff --git a/pages/api/teams/[teamId]/visitor-groups/[groupId]/index.ts b/pages/api/teams/[teamId]/visitor-groups/[groupId]/index.tsnew file mode 100644index 00000000..eaa51c4b--- /dev/null+++ b/pages/api/teams/[teamId]/visitor-groups/[groupId]/index.ts@@ -0,0 +1,157 @@+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,+) {+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }++ const { teamId, groupId } = req.query as {+ teamId: string;+ groupId: string;+ };+ const userId = (session.user as CustomUser).id;++ // Verify team access+ const teamAccess = await prisma.userTeam.findUnique({+ where: {+ userId_teamId: {+ userId,+ teamId,+ },+ },+ });++ if (!teamAccess) {+ return res.status(401).end("Unauthorized");+ }++ if (req.method === "GET") {+ // GET /api/teams/:teamId/visitor-groups/:groupId+ try {+ const visitorGroup = await prisma.visitorGroup.findUnique({+ where: {+ id: groupId,+ teamId,+ },+ include: {+ links: {+ include: {+ link: {+ select: {+ id: true,+ name: true,+ linkType: true,+ deletedAt: true,+ isArchived: true,+ documentId: true,+ dataroomId: true,+ },+ },+ },+ },+ _count: {+ select: { links: true },+ },+ },+ });++ if (!visitorGroup) {+ return res.status(404).json({ error: "Visitor group not found." });+ }++ return res.status(200).json(visitorGroup);+ } catch (error) {+ errorhandler(error, res);+ }+ } else if (req.method === "PUT") {+ // PUT /api/teams/:teamId/visitor-groups/:groupId+ 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 visitorGroup = await prisma.visitorGroup.update({+ where: {+ id: groupId,+ teamId,+ },+ data: {+ name: name.trim(),+ emails: emails || [],+ },+ include: {+ _count: {+ select: { links: true },+ },+ },+ });++ return res.status(200).json(visitorGroup);+ } catch (error) {+ errorhandler(error, res);+ }+ } else if (req.method === "DELETE") {+ // DELETE /api/teams/:teamId/visitor-groups/:groupId+ try {+ // Check if the group is attached to any active (non-deleted) links+ const activeLinks = await prisma.linkVisitorGroup.findMany({+ where: {+ visitorGroupId: groupId,+ link: {+ deletedAt: null,+ },+ },+ include: {+ link: {+ select: {+ id: true,+ name: true,+ linkType: true,+ documentId: true,+ dataroomId: true,+ },+ },+ },+ });++ if (activeLinks.length > 0) {+ return res.status(400).json({+ error:+ "Cannot delete this visitor group because it is used by active links. Remove the group from those links first.",+ activeLinks: activeLinks.map((al) => ({+ linkId: al.link.id,+ linkName: al.link.name,+ linkType: al.link.linkType,+ })),+ });+ }++ await prisma.visitorGroup.delete({+ where: {+ id: groupId,+ teamId,+ },+ });++ return res.status(204).end();+ } catch (error) {+ errorhandler(error, res);+ }+ } else {+ res.setHeader("Allow", ["GET", "PUT", "DELETE"]);+ return res.status(405).end(`Method ${req.method} Not Allowed`);+ }+}diff --git a/pages/api/teams/[teamId]/visitor-groups/index.ts b/pages/api/teams/[teamId]/visitor-groups/index.tsnew file mode 100644index 00000000..7f911d42--- /dev/null+++ b/pages/api/teams/[teamId]/visitor-groups/index.ts@@ -0,0 +1,102 @@+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`);+ }+}