Skip to content

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

team handlers

Aashish-Upadhyay-1011538b39b8de6
diff --git a/components/emails/team-invitation.tsx b/components/emails/team-invitation.tsx
new file mode 100644
index 00000000..ff0a11c1
--- /dev/null
+++ b/components/emails/team-invitation.tsx
@@ -0,0 +1,47 @@
+import React from "react";
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Preview,
+ Text,
+ Tailwind,
+} from "@react-email/components";
+
+export default function TeamInvitation({
+ senderName,
+ senderEmail,
+ teamName,
+ teamId,
+}: {
+ senderName: string;
+ senderEmail: string;
+ teamName: string;
+ teamId: string;
+}) {
+ return (
+ <Html>
+ <Head />
+ <Preview>Team Invitation</Preview>
+ <Tailwind>
+ <Body className="bg-white my-auto mx-auto font-sans">
+ <Container className="my-10 mx-auto p-5 w-[465px]">
+ <Heading className="text-2xl font-normal text-center p-0 mt-4 mb-8 mx-0">
+ <span className="font-bold tracking-tighter">Papermark</span>
+ </Heading>
+ <Heading className="text-xl font-seminbold text-center p-0 mt-4 mb-8 mx-0">
+ Team Invitation
+ </Heading>
+ <Text className="text-sm leading-6 text-black">
+ You are invited by ${senderName}-(${senderEmail} to join $
+ {teamName}.) please click the link below to accept the invitation.
+ \n\n{process.env.NEXTAUTH_URL}/api/teams/${teamId}/invite
+ </Text>
+ </Container>
+ </Body>
+ </Tailwind>
+ </Html>
+ );
+}
diff --git a/pages/api/teams/[teamId]/index.ts b/pages/api/teams/[teamId]/index.ts
new file mode 100644
index 00000000..3f7bba17
--- /dev/null
+++ b/pages/api/teams/[teamId]/index.ts
@@ -0,0 +1,63 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import { getServerSession } from "next-auth";
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+import { authOptions } from "../../auth/[...nextauth]";
+import { errorHanlder } from "@/lib/errorHandler";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "GET") {
+ // GET /api/teams/:teamId
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const { teamId } = req.query as { teamId: string };
+
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: {
+ include: {
+ user: true,
+ },
+ },
+ documents: {
+ include: {
+ owner: {
+ select: {
+ name: true,
+ },
+ },
+ views: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ // check that the user is member of the team, otherwise return 403
+ const teamUsers = team?.users;
+ const isUserPartOfTeam = teamUsers?.some(
+ (team) => team.userId === (session.user as CustomUser).id
+ );
+ if (!isUserPartOfTeam) {
+ return res.status(403).end("Unauthorized to access this team");
+ }
+
+ return res.status(200).json(team);
+ } catch (error) {
+ errorHanlder(error, res);
+ }
+ }
+}
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
new file mode 100644
index 00000000..1ded564b
--- /dev/null
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -0,0 +1,129 @@
+import { sendEmail } from "@/lib/resend";
+import { getServerSession } from "next-auth";
+import { authOptions } from "../../auth/[...nextauth]";
+import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+import { errorHanlder } from "@/lib/errorHandler";
+import TeamInvitation from "@/components/emails/team-invitation";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "POST") {
+ // POST /api/teams/:teamId/invite
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauhorized");
+ }
+
+ const { teamId } = req.query as { teamId: string };
+
+ const { email } = req.body;
+
+ if (!email) {
+ return res.status(400).end("Email is missing in request body");
+ }
+
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: {
+ select: {
+ userId: true,
+ role: true,
+ },
+ },
+ },
+ });
+
+ // check that the user is admin of the team, otherwise return 403
+ const teamUsers = team?.users;
+ const isUserAdmin = teamUsers?.some(
+ (user) =>
+ user.role === "ADMIN" &&
+ user.userId === (session.user as CustomUser).id
+ );
+ if (!isUserAdmin) {
+ return res.status(403).end("Unauthorized to access this team");
+ }
+
+ // send invite email
+ const sender = session.user as CustomUser;
+
+ sendTeamInviteEmail({
+ to: email,
+ senderName: sender.name || "",
+ senderEmail: sender.email || "",
+ teamName: team?.name || "",
+ teamId: team?.id || "",
+ });
+
+ return res.status(200).json("Invitation send!");
+ } catch (error) {
+ errorHanlder(error, res);
+ }
+ } else if (req.method === "GET") {
+ // GET /api/teams/:teamId/invite
+ const session = await getServerSession(req, res, authOptions);
+
+ const { teamId } = req.query as { teamId: string };
+
+ if (!session) {
+ res.redirect(`/login?next=/api/teams/${teamId}/invite`);
+ }
+
+ try {
+ const userTeam = await prisma.userTeam.findFirst({
+ where: {
+ teamId,
+ userId: (session?.user as CustomUser).id,
+ },
+ });
+
+ if (userTeam) {
+ return res.status(400).end("User is already in the team");
+ }
+
+ const team = await prisma.team.update({
+ where: {
+ id: teamId,
+ },
+ data: {
+ users: {
+ create: {
+ userId: (session?.user as CustomUser).id,
+ },
+ },
+ },
+ });
+ return res.redirect("/documents");
+ } catch (error) {
+ errorHanlder(error, res);
+ }
+ }
+}
+
+export const sendTeamInviteEmail = async ({
+ to,
+ senderName,
+ senderEmail,
+ teamName,
+ teamId,
+}: {
+ to: string;
+ senderName: string;
+ senderEmail: string;
+ teamName: string;
+ teamId: string;
+}) => {
+ await sendEmail({
+ to: to,
+ subject: "You are invited to join a Team",
+ react: TeamInvitation({ senderName, senderEmail, teamName, teamId }),
+ });
+};
diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.ts
new file mode 100644
index 00000000..e2318c86
--- /dev/null
+++ b/pages/api/teams/index.ts
@@ -0,0 +1,84 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import { getServerSession } from "next-auth";
+import { authOptions } from "../auth/[...nextauth]";
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+import { log } from "@/lib/utils";
+import { errorHanlder } from "@/lib/errorHandler";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "GET") {
+ // GET /api/teams
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ try {
+ const userTeams = await prisma.userTeam.findMany({
+ where: {
+ userId: (session.user as CustomUser).id,
+ },
+ include: {
+ team: {
+ include: {
+ users: {
+ select: {
+ userId: true,
+ role: true,
+ },
+ },
+ documents: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ const teams = userTeams.map((userTeam) => userTeam.team);
+ return res.status(200).json(teams);
+ } catch (error) {
+ log(`Failed to add domain. Error: \n\n ${error}`);
+ errorHanlder(error, res);
+ }
+ } else if (req.method === "POST") {
+ // POST /api/teams
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const { team } = req.body;
+
+ try {
+ const newTeam = await prisma.team.create({
+ data: {
+ name: team,
+ users: {
+ create: {
+ userId: (session.user as CustomUser).id,
+ role: "ADMIN",
+ },
+ },
+ },
+ include: {
+ users: true,
+ },
+ });
+
+ return res.status(201).json(newTeam);
+ } catch (error) {
+ log(`Failed to add domain. Error: \n\n ${error}`);
+ errorHanlder(error, res);
+ }
+ } else {
+ res.setHeader("Allow", ["GET", "POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}