Skip to content

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

api: delete team

Aashish-Upadhyay-10156eeb95ef6a3
diff --git a/pages/api/teams/[teamId]/index.ts b/pages/api/teams/[teamId]/index.ts
index 05eb23ce..35ee2d1d 100644
--- a/pages/api/teams/[teamId]/index.ts
+++ b/pages/api/teams/[teamId]/index.ts
@@ -65,5 +65,56 @@ export default async function handle(
} catch (error) {
errorHanlder(error, res);
}
+ } else if (req.method === "DELETE") {
+ // DELETE /api/teams/:teamId
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ res.status(401).end("Unauthorized");
+ return;
+ }
+
+ const { teamId } = req.query as { teamId: string };
+
+ try {
+ // check if the team exists
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ include: {
+ users: true,
+ },
+ });
+ if (!team) {
+ return res.status(400).json("Team doesn't exists");
+ }
+
+ // check if current user is admin of the team
+ const isUserAdmin = team.users.some(
+ (user) =>
+ user.role === "ADMIN" &&
+ user.userId === (session.user as CustomUser).id
+ );
+ if (!isUserAdmin) {
+ return res
+ .status(403)
+ .json("You are not permitted to perform this action");
+ }
+
+ // delete team
+ await prisma.team.delete({
+ where: {
+ id: teamId,
+ },
+ });
+
+ return res.status(204).end();
+ } catch (error) {
+ return res.status(500).json({ message: (error as Error).message });
+ }
+ } else {
+ // We only allow GET and DELETE requests
+ res.setHeader("Allow", ["GET", "DELETE"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}
diff --git a/pages/api/teams/[teamId]/update-name.ts b/pages/api/teams/[teamId]/update-name.ts
index 296eb8f5..eec02a27 100644
--- a/pages/api/teams/[teamId]/update-name.ts
+++ b/pages/api/teams/[teamId]/update-name.ts
@@ -56,7 +56,7 @@ export default async function handle(
return res.status(200).json("Team name updated!");
} catch (error) {
- errorHanlder(error, res);
+ return res.status(500).json({ message: (error as Error).message });
}
} else {
// We only allow POST requests
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 83cd84b5..89a947b8 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -105,7 +105,7 @@ model Document {
numPages Int? // This should be a reference to the number of pages in the document
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
teamId String?
- team Team? @relation(fields: [teamId], references: [id])
+ team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
ownerId String // This field holds the foreign key.
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -172,7 +172,7 @@ model Domain {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
teamId String?
- Team Team? @relation(fields: [teamId], references: [id])
+ Team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
verified Boolean @default(false) // Whether the domain has been verified
lastChecked DateTime @default(now())
createdAt DateTime @default(now())