teqo/src/utilities/municipality/municipalityElectionGeography.ts
Francisco Ignácio Fontoura Solla 4806c417c8 Engineering Pass 3: consolidate patterns and lock them with guards.
Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
2026-07-29 06:11:29 -03:00

36 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { Where } from 'payload'
import {
getMunicipalityCatalogEntry,
type MunicipalityCatalogEntry,
} from '@/lib/municipalityCatalog'
/**
* Election-data geography of a Município: exactly one municipality (TSE cityCode)
* and either all of its official zones (whole-municipality unit) or the single
* zone (zona unit). Election collections are keyed by cityCode × zoneNumber,
* so the cut at the municipal boundary is exact.
*/
export type MunicipalityElectionGeography = {
cityCode: string
zones: number[]
}
export const municipalityElectionGeography = (
entry: Pick<MunicipalityCatalogEntry, 'tseCityCode' | 'tseZones'>,
): MunicipalityElectionGeography => ({
cityCode: entry.tseCityCode,
zones: [...entry.tseZones],
})
export const municipalityElectionGeographyForSlug = (
slug: string,
): MunicipalityElectionGeography | null => {
const entry = getMunicipalityCatalogEntry(slug)
return entry ? municipalityElectionGeography(entry) : null
}
/** Payload Where clause matching the municipality's city×zone cells. */
export const municipalityGeographyWhere = (geography: MunicipalityElectionGeography): Where => ({
and: [{ cityCode: { equals: geography.cityCode } }, { zoneNumber: { in: geography.zones } }],
})