import type { Payload } from "payload"; import { hasAnyPermission } from "@/utils/access-control/hasPermission"; import { loadQualificationFlags } from "@/utils/access-control/qualificationCache"; // Membership signals only. Read permissions (missions:read, campaigns:read, // factions:read, technologies:read) must NOT appear here: the standard "user" // role grants all four, so including them would make every regular player pass // this qualification (and with it, wiki moderation and intel admin surfaces). // Navigation visibility (sidebar, palette, dashboard) uses canViewIntelligence. const INTELLIGENCE_PERMISSIONS = [ "intelligence:manage", "missions:create", "missions:update", "missions:delete", "missions:read-sensitive", "campaigns:create", "campaigns:update", "campaigns:delete", "factions:create", "factions:update", "factions:delete", "technologies:create", "technologies:update", "technologies:delete", ] as const; export async function hasIntelligenceQualification( payload: Payload, user: { id: number | string; roles?: unknown } | null | undefined, ): Promise { if (!user) return false; if (await hasAnyPermission(payload, user, ...INTELLIGENCE_PERMISSIONS)) return true; // Cached + single-flight: `payload.auth()` calls this many times per request // (once per division-gated collection operation), so the raw profile read // must not run once per call. See qualificationCache.ts. const userId = Number(user.id); if (!Number.isFinite(userId)) return false; const flags = await loadQualificationFlags(payload, userId); return flags.intelligence; }