teqo/src/utilities/campaignMunicipalityScope.ts
Francisco Ignácio Fontoura Solla c01467547d Pass 2 W4b: zero exports mortos; knip exports/types/enumMembers em ERROR no CI
- ~190 findings zerados: knip --fix (grafo corrigido: tests como entries, migrations como consumidores) + delecao guardada dos orfaos (git grep -w antes de deletar) + reparos manuais dos corpos arrow pendurados
- pecas nao usadas do kit ui deletadas (PaginationLink/Next/Previous, familias Sidebar/Combobox/Sheet/Drawer/Dialog etc.) - shadcn add recoloca se precisar
- acoes mortas removidas (updateCampaignDemandDetails*, *Record orfaos), schemas/tipos mortos (campaignDemandDetailsUpdateSchema, Contact do zod), helpers mortos
- tipos de eleicao derivam direto das *_OPTIONS (consts intermediarias eliminadas)
- knip.json: rules exports/types/enumMembers=error, duplicates=off (unicos hits: 6 aliases deliberados canUpdateX=canReadX); hints aplicados (payload.config entry redundante, ignores de tsx/sharp/playwright/payload-types desnecessarios)
- ledger: linha P2 dos unused exports fechada
2026-07-25 15:51:52 -03:00

81 lines
2.2 KiB
TypeScript

import 'server-only'
import { cache } from 'react'
import type { Payload, Where } from 'payload'
import type { CampaignUser, Municipality } from '@/payload-types'
import { aggregatePledgesByMunicipality } from '@/utilities/votePledgeData'
import { type MunicipalityPledgeAggregate } from '@/utilities/votePledgeViews'
/** Superset of the fields the dashboard, map, and list overview consume. */
type ScopedMunicipalityDoc = Pick<
Municipality,
| 'id'
| 'name'
| 'slug'
| 'kind'
| 'region'
| 'ibgeCode'
| 'advisors'
| 'priority'
| 'expectedVotes'
>
export type MunicipalityScope = {
municipalities: ScopedMunicipalityDoc[]
pledgeAggregates: Map<number, MunicipalityPledgeAggregate>
}
const loadScope = async (
payload: Payload,
user: CampaignUser,
where: Where,
): Promise<MunicipalityScope> => {
const result = await payload.find({
collection: 'municipality',
depth: 0,
limit: 0,
pagination: false,
select: {
name: true,
slug: true,
kind: true,
region: true,
ibgeCode: true,
advisors: true,
priority: true,
expectedVotes: true,
},
where,
user,
overrideAccess: false,
})
const municipalities = result.docs as ScopedMunicipalityDoc[]
const pledgeAggregates = await aggregatePledgesByMunicipality(
payload,
municipalities.map((municipality) => municipality.id),
)
return { municipalities, pledgeAggregates }
}
/**
* Request-scoped municipality scope (accessible docs + pledge aggregates),
* deduplicated with React `cache()`: the staff dashboard and the map bundle
* both need the same scope on one request and used to load it twice.
*
* `payload` (module singleton) and `user` (from the request-cached
* `getCampaignUser`) are reference-stable within a request; the `where` clause
* participates via its canonical JSON string.
*/
const loadScopeCached = cache((payload: Payload, user: CampaignUser, whereKey: string) =>
loadScope(payload, user, JSON.parse(whereKey) as Where),
)
export const loadMunicipalityScope = (
payload: Payload,
user: CampaignUser,
where: Where,
): Promise<MunicipalityScope> => loadScopeCached(payload, user, JSON.stringify(where))