import { db } from "../../db.server"; import { Permission, Role, RolePermission, UserRole } from "@toolctrl/db/prisma-browser"; import { FiltersDto } from "~/application/dtos/data/FiltersDto"; import RowFiltersHelper from "~/utils/helpers/RowFiltersHelper"; import { UserSimple } from "../users.db.server"; import UserModelHelper from "~/utils/helpers/models/UserModelHelper"; import { cachified, clearCacheKey } from "~/utils/cache.server"; export type RoleWithPermissions = Role & { permissions: (RolePermission & { permission: Permission })[]; }; export type RoleWithPermissionsAndUsers = RoleWithPermissions & { users: (UserRole & { user: UserSimple })[]; }; export async function getAllRoles(type?: "admin" | "app"): Promise { let where = {}; if (type !== undefined) { where = { type, tenantId: null, }; } return await db.role.findMany({ where, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, }, orderBy: [ { type: "asc", }, { order: "asc", }, ], }); } export async function getAllRolesNames(): Promise<{ id: string; name: string }[]> { return await db.role.findMany({ select: { id: true, name: true }, orderBy: [{ type: "asc" }, { order: "asc" }], }); } export async function getAllRolesWithoutPermissions(type?: "admin" | "app"): Promise { let where = {}; if (type !== undefined) { where = { type, tenantId: null, }; } return await cachified({ key: `roles:${type}`, ttl: 1000 * 60 * 60 * 24, getFreshValue: () => db.role.findMany({ where, orderBy: [ { type: "asc", }, { order: "asc", }, ], }), }); } export async function getAllRolesWithUsers(type?: "admin" | "app", filters?: FiltersDto, tenantId?: string | null): Promise { const scopeWhere: any = { ...(type !== undefined ? { type } : {}), ...(tenantId !== undefined ? { OR: [{ tenantId: null }, { tenantId }] } : { tenantId: null }), }; let where: any = scopeWhere; if (filters !== undefined) { const filterWhere = RowFiltersHelper.getFiltersCondition(filters); const permissionId = filters?.properties.find((f) => f.name === "permissionId")?.value; const permissionWhere = permissionId ? { OR: [filterWhere, { permissions: { some: { permissionId } } }] } : filterWhere; where = { AND: [scopeWhere, permissionWhere], }; } return await db.role.findMany({ where, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, users: { ...(tenantId !== undefined ? { where: { tenantId } } : {}), include: { ...UserModelHelper.includeSimpleUser, }, }, }, orderBy: [ { type: "asc", }, { order: "asc", }, ], }); } export async function getRoles(ids: string[]): Promise { return await db.role.findMany({ where: { id: { in: ids, }, }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, users: { include: { ...UserModelHelper.includeSimpleUser, }, }, }, }); } export async function getRolesByName(names: string[]): Promise { return await db.role.findMany({ where: { name: { in: names, }, }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, users: { include: { ...UserModelHelper.includeSimpleUser, }, }, }, }); } export async function getRole(id: string): Promise { return await db.role.findUnique({ where: { id, }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, }, }); } export async function getTenantAssignableAppRole(id: string, tenantId: string): Promise { return await db.role.findFirst({ where: { id, type: "app", OR: [{ isDefault: true, tenantId: null }, { tenantId }], }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, }, }); } export async function getTenantMutableAppRole(id: string, tenantId: string): Promise { return await db.role.findFirst({ where: { id, type: "app", isDefault: false, tenantId, }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, }, }); } export async function getRoleByName(name: string): Promise { return await db.role.findFirst({ where: { name, tenantId: null, }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, }, }); } export async function getTenantAppRoleByName(name: string, tenantId: string): Promise { return await db.role.findFirst({ where: { name, tenantId, type: "app", }, include: { permissions: { include: { permission: true, }, orderBy: { permission: { name: "asc" } }, }, }, }); } export async function getNextRolesOrder(type?: "admin" | "app", tenantId?: string | null): Promise { let where = {}; if (type !== undefined) { where = { type, tenantId: tenantId ?? null, }; } return ( (( await db.role.aggregate({ where, _max: { order: true, }, }) )._max.order ?? 0) + 1 ); } export async function createRole(data: { order: number; name: string; description: string; type: "admin" | "app"; assignToNewUsers: boolean; isDefault: boolean; tenantId?: string | null; }) { return await db.role .create({ data, }) .then((item) => { clearCacheKey(`roles:${data.type}`); return item; }); } export async function updateRole(id: string, data: { name: string; description: string; type: "admin" | "app"; assignToNewUsers: boolean }) { return await db.role .update({ where: { id }, data, }) .then((item) => { clearCacheKey(`roles:${item.type}`); return item; }); } export async function deleteRole(id: string) { return await db.role .delete({ where: { id }, }) .then((item) => { clearCacheKey(`roles:${item.type}`); return item; }); }