teqo-backup/src/utilities/campaignGoals.ts
Francisco Ignácio Fontoura Solla 31fcc20ffd E9 /simplify: consolidate duplicated helpers, dead passthrough, comment drift
Cross-checked by parallel code-quality/performance/reuse reviewers against
the E9 commit. Behavior-preserving:

- laterTimestamp/resolveMunicipalityLastSignalAt were the same "latest of
  two ISO timestamps" written twice; both now delegate to a new
  latestIsoTimestamp in campaignTime.ts, which documents the fixed-width
  UTC invariant that makes the string compare sound.
- Hoisted the frescor comparator's Date.now() out of the per-comparison
  accessor so the sort key stops drifting mid-sort.
- Deleted the unread baseTotal/growthFactor/belowBase passthrough from the
  statewide goal entry point and fixed a comment citing a symbol
  (goalDecompositionBelowBase) that never existed; renamed
  computeStatewideGoalDecomposition -> computeStatewideSuggestedGoals now
  that nothing is proportionally decomposed.
- Collapsed the advisor badge's two copies (desktop table vs mobile card)
  into one AdvisorStatusBadge; they had quietly diverged on which icon
  renders for the "no advisor" state.
- Renamed the sort-menu's stale "Cobertura" label for advisor coverage to
  "Assessoria" (E8 already renamed the list column for the same reason);
  derived the sort-key set from the label record instead of a third
  parallel list.
- Consolidated three zeroed-scenario-record literals into one
  zeroByVoteEstimateScenario() factory in voteEstimate.ts.
- Routed three separate votesByYear[2022] reads through one votesInYear
  helper in municipalityPotential.ts.
- Gated the goal card's empty-state copy on !usesMesaEstimate so it stops
  telling a município with an existing mesa estimate to enter one.
- Swapped a test assertion pinned to a raw Tailwind token for a stable
  data-signal="cold" hook, and tightened the "desert" test's bound to
  check against the actual growth factor (the old bound was true for
  every slug regardless of the formula).

Left alone (roadmap decision, not cleanup): sanityCheckSuggestedGoalsByTerritory
has no production caller yet (deferred to E11/E12); comment shortened
instead of deleting the dead-but-documented code.

Verified: tsc --noEmit, pnpm lint (0 warnings), 337 unit + 366 integration
tests, pnpm build all green.
2026-07-25 02:24:18 -03:00

37 lines
1.4 KiB
TypeScript

import 'server-only'
import { cache } from 'react'
import type { Payload } from 'payload'
import type { CampaignGoal, CampaignUser, User } from '@/payload-types'
type CampaignGoalsReader = CampaignUser | User
/**
* Request-scoped read of the `campaignGoals` global (E8 "conta da cadeira" —
* state-level vote goal + margin, turned into a per-municipality suggested
* goal by `municipalityPotential.ts`). Degrau 1 da caching ladder
* (React `cache()` per-request dedup): the global changes rarely and every
* page that shows coverage needs the same value once per request.
*
* No `unstable_cache`/tag layer on top: `/campanha` is dynamic with
* per-request auth, so a cross-request cache would need its own
* invalidation path for no read-latency benefit (see the global's own
* comment on why it skips the `revalidateGlobal` hook).
*
* Access is enforced explicitly (not delegated to `payload.findGlobal`'s
* default admin bypass): pass `user` + `overrideAccess: false` so leaders and
* unauthenticated CLI-less callers hit the global's own `access.read` and get
* denied, matching the Local API security pattern used everywhere else in
* this codebase.
*/
export const loadCampaignGoals = cache(
async (payload: Payload, user: CampaignGoalsReader): Promise<CampaignGoal> =>
payload.findGlobal({
slug: 'campaignGoals',
depth: 0,
user,
overrideAccess: false,
}),
)