teqo-backup/src/utilities/documents.ts
Francisco Ignácio Fontoura Solla a32ba3b4f0 Cache the campaign hot path: committed TSE artifact + request-scoped dedup
- committed artifact src/lib/electionAggregates/bahia-federal-baseline.json
  (Solla nominal + federal T1 valid votes per catalog municipality × TSE year,
  Salvador split by zone) generated by pnpm build:election-aggregates from the
  locally seeded TSE data; typed loader src/lib/bahiaElectionAggregates.ts;
  snapshot spec pins artifact ↔ catalog ↔ baseline ticket
- map bundle: historical years now read the artifact (was 6 statewide
  city×zone scans per dashboard visit); compare mode keeps only the OTHER
  candidate on the DB path (Solla side from the artifact)
- loadMunicipalityScope (React cache()): one municipality find + pledge
  aggregate per request shared by dashboard KPIs, map bundle, and the
  municípios list overview (was 2-3 duplicate loads)
- unstable_cache + election-tse tag on the immutable elections-tab loaders
  (baseline, candidate comparison, candidate options); tag added to the
  /api/revalidate allowlist — runbook: re-seed then bust the tag
- municipalityMapContract.ts: client-safe map vocabulary/bundle types so the
  client panel no longer imports the server data module (build had pulled
  @payload-config into the client graph); municipalityMapData is server-only
  and re-exports the contract; PLAZA_MAP_* renamed to MUNICIPALITY_MAP_*
- petition generateStaticParams enumerates bare ids (getPetitionIds)
2026-07-23 23:06:28 -03:00

56 lines
1.8 KiB
TypeScript

import type { Config } from '@/payload-types'
import configPromise from '@payload-config'
import { revalidateTag, unstable_cache } from 'next/cache'
import { getPayload } from 'payload'
type Collection = keyof Config['collections']
export const getDocuments = <Slug extends Collection>(collection: Slug, depth?: number) =>
getPayload({ config: configPromise }).then((payload) => payload.find({ collection, depth }))
/** Bare petition ids (no relationship population) — for `generateStaticParams`. */
export const getPetitionIds = async (): Promise<string[]> => {
const payload = await getPayload({ config: configPromise })
const result = await payload.find({
collection: 'petition',
depth: 0,
limit: 0,
pagination: false,
select: {},
})
return result.docs.map((doc) => String(doc.id))
}
export const getDocumentById = <Slug extends Collection>(
collection: Slug,
id: string | number,
depth?: number,
) =>
getPayload({ config: configPromise }).then((payload) =>
payload.findByID({ collection, id, depth }),
)
const getTag = <Slug extends Collection>(collection: Slug, id: string | number) =>
`document_${collection}:${id}`
export const getCachedDocumentById = <Slug extends Collection>(
collection: Slug,
id: string | number,
depth?: number,
) =>
unstable_cache(() => getDocumentById(collection, id, depth), [getTag(collection, id)], {
tags: [getTag(collection, id)],
})
export const revalidateDocumentById = <Slug extends Collection>(
collection: Slug,
id: string | number,
) => revalidateTag(getTag(collection, id))
const getListingTag = <Slug extends Collection>(collection: Slug) => `${collection}s`
export const revalidateCollectionListing = <Slug extends Collection>(collection: Slug) =>
revalidateTag(getListingTag(collection))
export const revalidatePostsListing = () => revalidateCollectionListing('post')