amati/src/modules/role/actions/getAllRoles.ts
2024-02-14 14:48:40 +07:00

47 lines
1.3 KiB
TypeScript

"use server"
import prisma from "@/db";
import checkPermission from "@/modules/dashboard/services/checkPermission";
import unauthorized from "@/modules/dashboard/utils/unauthorized";
import "server-only";
/**
* Retrieves all roles along with the count of associated permissions and users.
* Authorization check is performed for the operation.
*
* @returns An array of role objects each including details and counts of related permissions and users.
*/
export default async function getAllRoles() {
// Authorization check
if (!await checkPermission("roles.getAll")) {
unauthorized();
}
try {
// Fetch roles from the database
const roles = await prisma.role.findMany({
include: {
_count: {
select: {
permissions: true,
users: true
}
}
}
});
// Transform the data into the desired format
return roles.map(({ id, code, name, description, isActive, _count }) => ({
id,
code,
name,
description,
isActive,
permissionCount: _count.permissions,
userCount: _count.users
}));
} catch (error) {
console.error('Error retrieving roles', error);
throw error;
}
}