Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: get all team pending team invitations

Marc Seitz73a5484a813c
diff --git a/lib/swr/use-invitations.ts b/lib/swr/use-invitations.ts
new file mode 100644
index 00000000..70b16bcf
--- /dev/null
+++ b/lib/swr/use-invitations.ts
@@ -0,0 +1,24 @@
+import useSWR from "swr";
+import { fetcher } from "@/lib/utils";
+import { useTeam } from "@/context/team-context";
+import { Invitation } from "@prisma/client";
+
+export function useInvitations() {
+ const teamInfo = useTeam();
+
+ // only fetch data once when linkId is present
+ const { data: invitations, error } = useSWR<Invitation[]>(
+ teamInfo?.currentTeam &&
+ `/api/teams/${teamInfo.currentTeam.id}/invitations`,
+ fetcher,
+ {
+ dedupingInterval: 10000,
+ },
+ );
+
+ return {
+ invitations,
+ loading: !error && !invitations,
+ error,
+ };
+}
diff --git a/pages/api/teams/[teamId]/invitations/index.ts b/pages/api/teams/[teamId]/invitations/index.ts
new file mode 100644
index 00000000..25d7fd82
--- /dev/null
+++ b/pages/api/teams/[teamId]/invitations/index.ts
@@ -0,0 +1,55 @@
+import { getServerSession } from "next-auth";
+import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+import { errorhandler } from "@/lib/errorHandler";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "GET") {
+ // GET /api/teams/:teamId/invitations
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauhorized");
+ }
+
+ const { teamId } = req.query as { teamId: string };
+
+ try {
+ // check if currentUser is part of the team with the teamId
+ const userTeam = await prisma.userTeam.findFirst({
+ where: {
+ teamId,
+ userId: (session.user as CustomUser).id,
+ },
+ });
+
+ if (!userTeam) {
+ return res.status(403).json("You are not part of this team");
+ }
+
+ // get current invitations for the team
+ const invitations = await prisma.invitation.findMany({
+ where: {
+ teamId: teamId,
+ },
+ select: {
+ email: true,
+ expires: true,
+ },
+ });
+
+ if (!invitations) {
+ return res.status(404).json("No invitations found for this team");
+ }
+
+ res.status(200).json(invitations);
+ return;
+ } catch (error) {
+ errorhandler(error, res);
+ }
+ }
+}