import type { Payload } from "payload"; import { hasAnyPermission } from "@/utils/access-control/hasPermission"; import { loadQualificationFlags } from "@/utils/access-control/qualificationCache"; const LOGISTICS_PERMISSIONS = [ "logistics:manage", "assets:read", "assets:create", "assets:update", "assets:delete", "resources:read", "resources:create", "resources:update", "resources:delete", "vehicles:read", "vehicles:create", "vehicles:update", "vehicles:delete", "structures:read", "structures:create", "structures:update", "structures:delete", "shipments:read", "shipments:create", "shipments:update", "shipments:delete", "bank-accounts:read", "bank-accounts:create", "bank-accounts:update", "bank-accounts:delete", "banking:manage", "market-listings:read", "market-listings:create", "market-listings:update", "market-listings:delete", "game-structures:read", "game-structures:create", "game-structures:update", "game-structures:delete", "game-vehicles:read", "game-vehicles:create", "game-vehicles:update", "game-vehicles:delete", "game-npcs:read", "game-npcs:create", "game-npcs:update", "game-npcs:delete", ] as const; /** * Checks if a user has the Logistics qualification. * Queries the user's profile for qualifications matching "Logistics" (case-insensitive). * Users with ANY logistics-domain RBAC permission always pass. */ export async function hasLogisticsQualification( payload: Payload, user: { id: number | string; roles?: unknown } | null | undefined, ): Promise { if (!user) return false; if (await hasAnyPermission(payload, user, ...LOGISTICS_PERMISSIONS)) return true; // Cached + single-flight; see qualificationCache.ts. const userId = Number(user.id); if (!Number.isFinite(userId)) return false; const flags = await loadQualificationFlags(payload, userId); return flags.logistics; }