mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-22 04:53:09 +00:00
Word-start on name, municipality count on the row, shared HomeSearchHitRow; POST /campanha/home-search aggregates advisors alongside municipalities.
116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
import 'server-only'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import { homeSearchQueryIsActive, normalizeHomeSearchRaw } from '@/lib/campaignHomeSearchContract'
|
|
import {
|
|
toHomeSearchMunicipalityHit,
|
|
type HomeSearchMunicipalityHit,
|
|
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 { loadMunicipalityScope } from '@/utilities/municipality/campaignMunicipalityScope'
|
|
import { loadTerritoryOverview } from '@/utilities/territory/loadTerritoryOverview'
|
|
|
|
const emptyResponse = (): HomeSearchSuccessResponse => ({
|
|
status: 'success',
|
|
resultKind: 'search',
|
|
municipalities: [],
|
|
territories: [],
|
|
advisors: [],
|
|
})
|
|
|
|
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 { municipalities } = await loadMunicipalityScope(payload, user, {})
|
|
|
|
const municipalityHits: HomeSearchMunicipalityHit[] = municipalities
|
|
.map((doc) => {
|
|
const normalizedName = normalizeHomeSearchName(doc.name)
|
|
if (!matchesNormalizedAtWordStart(normalizedName, normalizedQuery)) {
|
|
return null
|
|
}
|
|
return {
|
|
normalizedName,
|
|
hit: toHomeSearchMunicipalityHit(doc),
|
|
}
|
|
})
|
|
.filter((row): row is NonNullable<typeof row> => row !== null)
|
|
.sort((left, right) =>
|
|
compareHomeSearchNameRelevance(
|
|
{
|
|
normalizedName: left.normalizedName,
|
|
tieBreakDesc: left.hit.votePosition2022?.votes ?? 0,
|
|
},
|
|
{
|
|
normalizedName: right.normalizedName,
|
|
tieBreakDesc: right.hit.votePosition2022?.votes ?? 0,
|
|
},
|
|
normalizedQuery,
|
|
),
|
|
)
|
|
.map((row) => row.hit)
|
|
|
|
const territoryRows = await loadTerritoryOverview(payload, user)
|
|
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: [],
|
|
}
|
|
}
|