mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-21 11:43:11 +00:00
- Colunas responsivas ao painel via @container/territory-list (P0→P3), sem scroll horizontal
- Contagem de municípios no nome ("Irecê (20)"); % e votos absorvidos na célula 2022 (hover = válidos)
- Célula 2026 no detalhe de Municípios (valor central + %, faixa de cenários no hover, leitura)
- Colunas de rede só leitura: Assessor, Liderança, Dobradinha (para todo o staff)
- Cobertura oculta por padrão no seletor; labels curtos (2022/2026/Captura/Cobertura)
- loadTerritoryOverview devolve { rows, references }; rollup puro com soma por cenário, hasEstimate e uniões de rede
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import 'server-only'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import { homeSearchQueryIsActive, normalizeHomeSearchRaw } from '@/lib/campaignHomeSearchContract'
|
|
import {
|
|
type HomeSearchSuccessResponse,
|
|
type HomeSearchTerritoryHit,
|
|
} from '@/lib/campaignHomeSearchHits'
|
|
import { HOME_SEARCH_STAFF_ONLY_MESSAGE } from '@/lib/campaignHomeSearchMessages'
|
|
import { isStaffCampaignRole } from '@/lib/campaignRoles'
|
|
import {
|
|
compareHomeSearchNameRelevance,
|
|
normalizeHomeSearchName,
|
|
} from '@/lib/homeSearchMunicipalityMatch'
|
|
import { DEFAULT_VOTE_RANK_YEAR } from '@/lib/municipalityVoteRank'
|
|
import { matchesNormalizedAtWordStart } from '@/lib/wordStartFilter'
|
|
import type { CampaignUser } from '@/payload-types'
|
|
import { loadTerritoryOverview } from '@/utilities/territory/loadTerritoryOverview'
|
|
|
|
import { searchStaffMunicipalityHits } from './searchStaffMunicipalityHits'
|
|
|
|
const emptyResponse = (): HomeSearchSuccessResponse => ({
|
|
status: 'success',
|
|
resultKind: 'search',
|
|
municipalities: [],
|
|
territories: [],
|
|
advisors: [],
|
|
leaderships: [],
|
|
stateDeputies: [],
|
|
activities: [],
|
|
demands: [],
|
|
})
|
|
|
|
export const searchHomeMunicipalities = async (
|
|
payload: Payload,
|
|
user: CampaignUser,
|
|
rawQuery: string,
|
|
): Promise<HomeSearchSuccessResponse> => {
|
|
if (!isStaffCampaignRole(user.role)) {
|
|
throw new Error(HOME_SEARCH_STAFF_ONLY_MESSAGE)
|
|
}
|
|
|
|
const query = normalizeHomeSearchRaw(rawQuery)
|
|
if (!homeSearchQueryIsActive(query)) {
|
|
return emptyResponse()
|
|
}
|
|
|
|
const normalizedQuery = normalizeHomeSearchName(query)
|
|
|
|
const [municipalityHits, territoryOverview] = await Promise.all([
|
|
searchStaffMunicipalityHits(payload, user, query),
|
|
loadTerritoryOverview(payload, user),
|
|
])
|
|
const territoryRows = territoryOverview.rows
|
|
const territoryHits: HomeSearchTerritoryHit[] = territoryRows
|
|
.map((row) => {
|
|
const normalizedName = normalizeHomeSearchName(row.region)
|
|
if (!matchesNormalizedAtWordStart(normalizedName, normalizedQuery)) {
|
|
return null
|
|
}
|
|
return {
|
|
normalizedName,
|
|
hit: {
|
|
kind: 'territory' as const,
|
|
region: row.region,
|
|
votes2022: row.votesByYear[DEFAULT_VOTE_RANK_YEAR] ?? 0,
|
|
},
|
|
}
|
|
})
|
|
.filter((row): row is NonNullable<typeof row> => row !== null)
|
|
.sort((left, right) =>
|
|
compareHomeSearchNameRelevance(
|
|
{
|
|
normalizedName: left.normalizedName,
|
|
tieBreakDesc: left.hit.votes2022,
|
|
},
|
|
{
|
|
normalizedName: right.normalizedName,
|
|
tieBreakDesc: right.hit.votes2022,
|
|
},
|
|
normalizedQuery,
|
|
),
|
|
)
|
|
.map((row) => row.hit)
|
|
|
|
return {
|
|
status: 'success',
|
|
resultKind: 'search',
|
|
municipalities: municipalityHits,
|
|
territories: territoryHits,
|
|
advisors: [],
|
|
leaderships: [],
|
|
stateDeputies: [],
|
|
activities: [],
|
|
demands: [],
|
|
}
|
|
}
|