import { db } from "../../db.server"; import { Permission, Role, RolePermission } from "@hivegrid/db/prisma-browser"; import { getRoleByName } from "./roles.db.server"; import { createRolePermission, getAllRolePermissions } from "./rolePermissions.db.server"; import RowFiltersHelper from "~/utils/helpers/RowFiltersHelper"; import { FiltersDto } from "~/application/dtos/data/FiltersDto"; import { cachified, clearCacheKey } from "~/utils/cache.server"; import { createScopedLogger } from "~/utils/logger.server"; const permissionsLogger = createScopedLogger("db.permissions"); export type PermissionWithRoles = Permission & { inRoles: (RolePermission & { role: Role })[]; }; export async function getAllPermissions(type?: string, filters?: FiltersDto): Promise { let where: any = { type, }; if (filters) { where = RowFiltersHelper.getFiltersCondition(filters); const roleId = filters?.properties.find((f) => f.name === "roleId")?.value; if (roleId) { where = { OR: [where, { inRoles: { some: { roleId } } }], }; } if (type !== undefined) { where = { AND: [type, where], }; } } return await db.permission.findMany({ where, include: { inRoles: { include: { role: true, }, }, }, orderBy: [{ type: "asc" }, { name: "asc" }], }); } export async function getAllPermissionsIdsAndNames(): Promise<{ id: string; name: string }[]> { return await db.permission.findMany({ select: { id: true, name: true }, orderBy: { name: "asc" }, }); } export async function getPermission(id: string): Promise { return await db.permission.findUnique({ where: { id, }, include: { inRoles: { include: { role: true, }, }, }, }); } export async function getPermissionByName(name: string): Promise { return await db.permission.findUnique({ where: { name, }, include: { inRoles: { include: { role: true, }, }, }, }); } export async function getPermissionName(name: string): Promise<{ id: string; name: string; description: string } | null> { return await cachified({ key: `permission:${name}`, ttl: 1000 * 60 * 60 * 24, getFreshValue: () => db.permission.findUnique({ where: { name, }, select: { id: true, name: true, description: true }, }), }); } export async function getPermissionByNames(names: string[]): Promise { return await db.permission.findMany({ where: { name: { in: names, }, }, include: { inRoles: { include: { role: true, }, }, }, }); } export async function getNextPermissionsOrder(type?: string): Promise { let where = {}; if (type !== undefined) { where = { type, }; } return ( (( await db.permission.aggregate({ where, _max: { order: true, }, }) )._max.order ?? 0) + 1 ); } export async function createPermissions( permissions: { inRoles: string[]; name: string; description: string; type: string }[], fromOrder: number = 0 ) { const allRolePermissions = await getAllRolePermissions(); const allPermissions = await getAllPermissions(); const permissionByName = new Map(allPermissions.map((permission) => [permission.name, permission])); const createdPermissionNames = new Set(); for (const [idx, data] of permissions.entries()) { const existing = permissionByName.get(data.name); if (createdPermissionNames.has(data.name)) { permissionsLogger.info({ permission: data.name }, "Permission already exists"); continue; } const permission = existing ?? (await createPermission({ order: fromOrder + idx + 1, name: data.name, description: data.description, type: data.type, isDefault: true, })); createdPermissionNames.add(permission.name); if (existing) { permissionsLogger.info({ permission: data.name }, "Permission already exists"); } else { permissionByName.set(permission.name, permission); clearCacheKey(`permission:${permission.name}`); } await Promise.all( data.inRoles.map(async (inRole) => { const role = await getRoleByName(inRole); if (!role) { throw new Error("Role required: " + inRole); } const existing = allRolePermissions.find((p) => p.roleId === role.id && p.permission.name === permission.name); if (existing) { return existing; } return await createRolePermission({ permissionId: permission.id, roleId: role.id, }); }) ); } } export async function createPermission(data: { order: number; name: string; description: string; type: string; isDefault: boolean }) { return await db.permission.create({ data, }); } export async function updatePermission( id: string, data: { name?: string; description?: string; type?: string; order?: number; } ) { return await db.permission .update({ where: { id }, data, }) .then((item) => { clearCacheKey(`permission:${item.name}`); return item; }); } export async function deletePermission(id: string) { return await db.permission .delete({ where: { id }, }) .then((item) => { clearCacheKey(`permission:${item.name}`); return item; }); }