teqo-backup/src/utilities/homeSearch/loadHomeSearchSuggestions.ts
Cursor Agent 7e6bc9d984
B125 — nearest município first in search suggest + wizard
- Restore B117 home search: scopeMunicipalities, merge prefix, session prompt
- Shared useNearestMunicipalitySlug (B14 key) for busca geral + wizard
- Wizard merge uses geoSlug only — no Perto de você / km copy on rows
- Impl plan: docs/plans/municipio-geo-topo-sugestoes-busca-impl.md

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-02 18:21:52 +00:00

95 lines
2.9 KiB
TypeScript

import 'server-only'
import type { Payload } from 'payload'
import {
toHomeSearchMunicipalityHit,
type HomeSearchSuccessResponse,
} from '@/lib/campaignHomeSearchHits'
import { HOME_SEARCH_STAFF_ONLY_MESSAGE } from '@/lib/campaignHomeSearchMessages'
import { isStaffCampaignRole, isUnrestrictedCampaignRole } from '@/lib/campaignRoles'
import {
rankHomeSearchSuggestMunicipalities,
type HomeSearchSuggestMunicipalityInput,
} from '@/lib/homeSearchSuggest'
import type { CampaignUser } from '@/payload-types'
import { loadMunicipalityScope } from '@/utilities/municipality/campaignMunicipalityScope'
import { centralDeficitSortValue } from '@/utilities/municipality/goalCoverage'
import { loadMunicipalityGoalCoverageBundle } from '@/utilities/municipality/municipalityGoalAccount'
import { resolveMunicipalityLastSignalAt } from '@/utilities/municipality/municipalitySignal'
export const loadHomeSearchSuggestions = async (
payload: Payload,
user: CampaignUser,
): Promise<HomeSearchSuccessResponse> => {
if (!isStaffCampaignRole(user.role)) {
throw new Error(HOME_SEARCH_STAFF_ONLY_MESSAGE)
}
const { municipalities, pledgeAggregates } = await loadMunicipalityScope(
payload,
user,
{},
{
extraSelect: { lastUpdateAt: true },
},
)
let coverageByMunicipalityID:
| Awaited<ReturnType<typeof loadMunicipalityGoalCoverageBundle>>['coverageByMunicipalityID']
| null = null
if (isUnrestrictedCampaignRole(user.role)) {
const altaMunicipalities = municipalities.filter(
(municipality) => municipality.priority === 'alta',
)
const bundle = await loadMunicipalityGoalCoverageBundle(
payload,
user,
altaMunicipalities,
pledgeAggregates,
)
coverageByMunicipalityID = bundle.coverageByMunicipalityID
}
const inputs: HomeSearchSuggestMunicipalityInput[] = municipalities.map((doc) => {
const aggregate = pledgeAggregates.get(doc.id)
const lastSignalAt = resolveMunicipalityLastSignalAt(
doc.lastUpdateAt ?? null,
aggregate?.lastPledgeAt ?? null,
)
const centralCoverage = coverageByMunicipalityID?.get(doc.id)?.central
return {
slug: doc.slug,
name: doc.name,
region: doc.region,
priority: doc.priority ?? null,
lastSignalAt,
centralDeficitSortValue: centralDeficitSortValue(centralCoverage),
}
})
const ranked = rankHomeSearchSuggestMunicipalities(user.role, inputs)
const municipalityHits = ranked.map(toHomeSearchMunicipalityHit)
return {
status: 'success',
resultKind: 'suggest',
municipalities: municipalityHits,
scopeMunicipalities: municipalities.map((doc) => ({
slug: doc.slug,
name: doc.name,
region: doc.region,
priority: doc.priority ?? null,
ibgeCode: doc.ibgeCode,
})),
territories: [],
advisors: [],
leaderships: [],
stateDeputies: [],
activities: [],
demands: [],
}
}