import { CatalogError, foundationErrors, requirePermission } from "@hivegrid/foundation"; import { tenantHasModule } from "~/utils/connector/tenantConnector.server"; import { getPermissionsByUser } from "~/utils/db/permissions/userRoles.db.server"; import { withTenantRLS } from "~/utils/db/.server/tenantRls.server"; import { requireAuth } from "~/utils/loaders.middleware"; import { getUserInfo } from "~/utils/session.server"; import { getTenantIdFromUrl } from "~/utils/services/.server/urlService"; import { TELEPHONY_MODULE_KEY } from "./telephonyRoutePaths"; export const TELEPHONY_PERMISSIONS = { readOwn: "telephony.read_own", readAll: "telephony.read_all", call: "telephony.call", manage: "telephony.manage", } as const; export type TelephonyPermission = (typeof TELEPHONY_PERMISSIONS)[keyof typeof TELEPHONY_PERMISSIONS]; export type TelephonyRouteContext = { tenantId: string; userId: string; tenantUserId: string; permissions: readonly string[]; hasModule: boolean; }; export async function loadTelephonyContext({ request, params, requireModule = false, }: { request: Request; params: Record; requireModule?: boolean; }): Promise { await requireAuth({ request, params }); const [tenantId, userInfo] = await Promise.all([getTenantIdFromUrl(params), getUserInfo(request)]); if (!tenantId || !userInfo.userId) { throw new CatalogError(foundationErrors.notFound); } const [hasModule, permissions, membership] = await Promise.all([ tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: TELEPHONY_MODULE_KEY }), getPermissionsByUser(userInfo.userId, tenantId), withTenantRLS(tenantId, (tx) => tx.tenantUser.findFirst({ where: { tenantId, userId: userInfo.userId }, select: { id: true } }), ), ]); if (!membership) { throw new CatalogError(foundationErrors.notFound); } if (requireModule && !hasModule) { throw new CatalogError(foundationErrors.notFound, { module: TELEPHONY_MODULE_KEY }); } return { tenantId, userId: userInfo.userId, tenantUserId: membership.id, permissions, hasModule }; } export function telephonyHasPermission(ctx: TelephonyRouteContext, permission: TelephonyPermission): boolean { if (ctx.permissions.includes(TELEPHONY_PERMISSIONS.manage)) return true; if (permission === TELEPHONY_PERMISSIONS.readOwn && ctx.permissions.includes(TELEPHONY_PERMISSIONS.readAll)) return true; return ctx.permissions.includes(permission); } export function requireTelephonyPermission(ctx: TelephonyRouteContext, permission: TelephonyPermission): TelephonyRouteContext { if (!telephonyHasPermission(ctx, permission)) { throw new CatalogError(foundationErrors.notFound, { permission }); } if (ctx.permissions.includes(permission)) { requirePermission({ tenantId: ctx.tenantId, userId: ctx.userId, permissions: ctx.permissions }, permission); } return ctx; } export function canReadAllTelephonyCalls(ctx: TelephonyRouteContext): boolean { return telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readAll); } export function requireTelephonyCallVisibility(ctx: TelephonyRouteContext, boundUserId: string | null): TelephonyRouteContext { if (canReadAllTelephonyCalls(ctx)) return ctx; if (boundUserId === ctx.tenantUserId && telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readOwn)) return ctx; throw new CatalogError(foundationErrors.notFound, { call: "not-visible" }); }