Skip to content

Commit 4de56e6

Browse files
authored
fixes#307 require atleast one admin (#313)
* Add admins count check in API * Show error message on client
1 parent f525bd6 commit 4de56e6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

apps/api/src/controllers/auth.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,19 @@ export function authRoutes(fastify: FastifyInstance) {
753753
const token = checkToken(bearer);
754754
if (token) {
755755
const { id, role } = request.body as { id: string; role: boolean };
756-
756+
// check for atleast one admin on role downgrade
757+
if (role === false) {
758+
const admins = await prisma.user.findMany({
759+
where: { isAdmin: true },
760+
});
761+
if (admins.length === 1) {
762+
reply.code(400).send({
763+
message: "Atleast one admin is required",
764+
success: false
765+
});
766+
return;
767+
}
768+
}
757769
await prisma.user.update({
758770
where: { id },
759771
data: {

apps/client/components/UpdateUserModal/index.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Dialog, Transition } from "@headlessui/react";
22
import { XMarkIcon } from "@heroicons/react/24/outline";
3+
import { notifications } from "@mantine/notifications";
34
import { getCookie } from "cookies-next";
45
import { useRouter } from "next/router";
56
import { Fragment, useState } from "react";
@@ -22,6 +23,24 @@ export default function UpdateUserModal({ user }) {
2223
role: admin,
2324
id: user.id,
2425
}),
26+
})
27+
.then((res) => res.json())
28+
.then((res) => {
29+
if (res.success === true) {
30+
notifications.show({
31+
title: "User Updated",
32+
message: "User updated succesfully",
33+
color: "green",
34+
autoClose: 5000,
35+
});
36+
} else {
37+
notifications.show({
38+
title: "Error",
39+
message: res.message,
40+
color: "red",
41+
autoClose: 5000,
42+
});
43+
}
2544
});
2645
// .then(() => router.reload());
2746
}

0 commit comments

Comments
 (0)