Skip to content

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

feat: add resend invitation function

Marc Seitzdd976d2222a2
diff --git a/pages/api/teams/[teamId]/invitations/resend.ts b/pages/api/teams/[teamId]/invitations/resend.ts
new file mode 100644
index 00000000..ce22ccce
--- /dev/null
+++ b/pages/api/teams/[teamId]/invitations/resend.ts
@@ -0,0 +1,91 @@
+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]";
+import { sendTeammateInviteEmail } from "@/lib/emails/send-teammate-invite";
+import { newId } from "@/lib/id-helper";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "PUT") {
+ // PUT /api/teams/:teamId/invitations/resend
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ res.status(401).end("Unauhorized");
+ return;
+ }
+
+ const { teamId } = req.query as { teamId: string };
+
+ const { email } = req.body as { email: 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) {
+ res.status(403).json("You are not part of this team");
+ return;
+ }
+
+ const isUserAdmin = userTeam.role === "ADMIN";
+ if (!isUserAdmin) {
+ res.status(403).json("Only admins can resend the invitation!");
+ return;
+ }
+
+ // get current team
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ select: {
+ name: true,
+ },
+ });
+
+ const expiresAt = new Date();
+ expiresAt.setHours(expiresAt.getHours() + 24); // invitation expires in 24 hour
+
+ // update invitation
+ const invitation = await prisma.invitation.update({
+ where: {
+ email: email,
+ teamId: teamId,
+ },
+ data: {
+ expires: expiresAt,
+ },
+ select: {
+ token: true,
+ },
+ });
+
+ // send invite email
+ const sender = session.user as CustomUser;
+
+ sendTeammateInviteEmail({
+ senderName: sender.name || "",
+ senderEmail: sender.email || "",
+ teamName: team?.name || "",
+ teamId: teamId || "",
+ token: invitation?.token || "",
+ to: email,
+ });
+
+ res.status(200).json("Invitation sent again!");
+ return;
+ } catch (error) {
+ errorhandler(error, res);
+ }
+ }
+}
diff --git a/pages/settings/people.tsx b/pages/settings/people.tsx
index 8677008c..6bb5fe97 100644
--- a/pages/settings/people.tsx
+++ b/pages/settings/people.tsx
@@ -23,6 +23,7 @@ import { useTeam } from "@/context/team-context";
import { useTeams } from "@/lib/swr/use-teams";
import Link from "next/link";
import { usePlan } from "@/lib/swr/use-billing";
+import { useInvitations } from "@/lib/swr/use-invitations";
export default function Billing() {
const [isTeamMemberInviteModalOpen, setTeamMemberInviteModalOpen] =
@@ -35,11 +36,13 @@ export default function Billing() {
const { plan: userPlan } = usePlan();
const { teams } = useTeams();
+ const { invitations } = useInvitations();
+
const router = useRouter();
const getUserDocumentCount = (userId: string) => {
const documents = team?.documents.filter(
- (document) => document.owner.id === userId
+ (document) => document.owner.id === userId,
);
return documents?.length;
};
@@ -55,7 +58,7 @@ export default function Billing() {
return team?.users.some(
(user) =>
user.role === "ADMIN" &&
- user.userId === (session?.user as CustomUser)?.id
+ user.userId === (session?.user as CustomUser)?.id,
);
};
@@ -92,6 +95,30 @@ export default function Billing() {
toast.success("Teammate removed successfully!");
};
+ // resend invitation function
+ const resendInvitation = async (invitation: { email: string } & any) => {
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/invitations/resend`,
+ {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: invitation.email as string,
+ }),
+ },
+ );
+
+ if (response.status !== 200) {
+ const error = await response.json();
+ toast.error(error);
+ return;
+ }
+
+ toast.success("Invitation resent successfully!");
+ };
+
return (
<AppLayout>
<Navbar current="People" />
@@ -218,6 +245,54 @@ export default function Billing() {
</div>
</li>
))}
+ {invitations &&
+ invitations.map((invitation, index) => (
+ <li
+ className="flex py-4 px-10 justify-between items-center"
+ key={index}
+ >
+ <div className="flex items-center gap-12">
+ <div className="space-y-1">
+ <h4 className="font-semibold text-sm">
+ {invitation.email}
+ </h4>
+ <p className="text-xs text-muted-foreground">
+ {invitation.email}
+ </p>
+ </div>
+ </div>
+ <div className="flex items-center gap-12">
+ <span
+ className="text-sm text-foreground"
+ title={`Expires on ${new Date(
+ invitation.expires,
+ ).toLocaleString()}`}
+ >
+ {new Date(invitation.expires) >= new Date(Date.now())
+ ? "Pending"
+ : "Expired"}
+ </span>
+
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button variant="ghost" className="h-8 w-8 p-0">
+ <span className="sr-only">Open menu</span>
+ <MoreVertical className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuLabel>Actions</DropdownMenuLabel>
+ <DropdownMenuItem
+ onClick={() => resendInvitation(invitation)}
+ className="text-red-500 focus:bg-destructive focus:text-destructive-foreground hover:cursor-pointer"
+ >
+ Resend
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ </li>
+ ))}
</ul>
</div>
</AppLayout>