1
0
Fork 0
polaris-task-force/src/utils/access-control/hasLogisticsQualification.ts
Z8MB1E a0f767cc13 perf: cut queries per render and refreshes per realtime event
Under 10-12 concurrent users the app queued behind itself: every SSE
game-event/profile-update made each client run router.refresh(), i.e. a
full layout + page render at ~15-25 queries, and a mission completion
emitted one XP event per attendee row. Measured against production, no
single query is slow and the DB is small, so this targets render count
and per-render query count instead of indexes.

Fewer renders per event:
- GameTickRealtime: 250ms coalescing window -> 1s, hidden tabs skip the
  render and refresh once when shown again.
- Delete OperationsRealtimeRefresher (5 routes) and the duplicate
  useGameTick refreshers in ProductionPanel/SlotsPanel; the global
  subscriber already refreshes those routes.
- Presence heartbeats no longer broadcast (was O(N^2) per channel every
  15s for a lastSeen no surface renders).
- NavCore links opt out of prefetch: the layout is force-dynamic with no
  loading.tsx, so ~35 prefetched links per tab were 35 server renders.

Fewer queries per render:
- Qualification flags go through one cached, single-flight profile read
  instead of one query per auth check; invalidated on profile writes.
- Dedupe DIVISION_ADMIN_QUALIFICATIONS (5 identical lookups -> 1).
- Anchor the permission cache on globalThis so route bundles share it.
- Minigame registry: real TTL cache plus invalidation on write.
- New cachedConfig TTL cache for the shims/game-rules globals, with
  invalidation hooks; bank-account existence check stops hydrating.
- pg pool max 10 -> 20 (node-postgres default throttled concurrent
  renders).

Hot-path per-item work:
- Contribution grant sweep: one status write per batch, one bucket write
  and one name lookup per user, one existence query per source
  collection instead of ~5 queries per entry.
- Notifications bell: coalesced refetch, hidden-tab skip; mark-all-read
  filters by the actor instead of scanning 100 foreign unread rows.
- Polling loops (shipments, ledger, mission reminders, chip bank) pause
  while the tab is hidden.
2026-11-22 22:39:10 -05:00

69 lines
1.9 KiB
TypeScript

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<boolean> {
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;
}