Fix wizard suggest signal tie-break and route typing
Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
This commit is contained in:
parent
1ed65d0d81
commit
4ed2787d36
3 changed files with 16 additions and 11 deletions
|
|
@ -23,28 +23,32 @@ import { searchStaffMunicipalityHits } from '@/utilities/homeSearch/searchStaffM
|
|||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
type HomeSearchRouteSuccessBody =
|
||||
| HomeSearchSuccessResponse
|
||||
| WizardMunicipalitySearchSuccessResponse
|
||||
|
||||
export const POST = campaignJsonMutationRoute(
|
||||
{
|
||||
bodySchema: homeSearchBodySchema,
|
||||
safeMessages: [HOME_SEARCH_STAFF_ONLY_MESSAGE],
|
||||
genericMessage: HOME_SEARCH_GENERIC_ERROR_MESSAGE,
|
||||
},
|
||||
async (body) => {
|
||||
async (body): Promise<NextResponse<HomeSearchRouteSuccessBody>> => {
|
||||
const { payload, actor } = await getCampaignActionContext()
|
||||
|
||||
if (body.mode === 'suggest') {
|
||||
const result = await loadHomeSearchSuggestions(payload, actor)
|
||||
return NextResponse.json<HomeSearchSuccessResponse>(result)
|
||||
return NextResponse.json(result)
|
||||
}
|
||||
|
||||
if (body.mode === 'wizard-municipality-suggest') {
|
||||
const result = await loadWizardMunicipalitySuggestions(payload, actor)
|
||||
return NextResponse.json<WizardMunicipalitySearchSuccessResponse>(result)
|
||||
return NextResponse.json(result)
|
||||
}
|
||||
|
||||
if (body.mode === 'wizard-municipality') {
|
||||
const municipalities = await searchStaffMunicipalityHits(payload, actor, body.query)
|
||||
return NextResponse.json<WizardMunicipalitySearchSuccessResponse>({
|
||||
return NextResponse.json({
|
||||
status: 'success',
|
||||
municipalities,
|
||||
})
|
||||
|
|
@ -59,7 +63,7 @@ export const POST = campaignJsonMutationRoute(
|
|||
searchHomeStateDeputies(payload, actor, body.query),
|
||||
searchHomeDemands(payload, actor, body.query),
|
||||
])
|
||||
return NextResponse.json<HomeSearchSuccessResponse>({
|
||||
return NextResponse.json({
|
||||
...municipalityResult,
|
||||
advisors,
|
||||
leaderships,
|
||||
|
|
|
|||
|
|
@ -76,19 +76,17 @@ const compareVotes2022 = (
|
|||
return 0
|
||||
}
|
||||
|
||||
/** Older signal first; null = coldest. */
|
||||
/** Older signal first; null = coldest; equal timestamps defer to later tie-breakers. */
|
||||
const compareSignalOldestFirst = (
|
||||
left: WizardMunicipalitySuggestInput,
|
||||
right: WizardMunicipalitySuggestInput,
|
||||
): number => {
|
||||
if (left.lastSignalAt === null && right.lastSignalAt === null) {
|
||||
return municipalityNameCompare(left, right)
|
||||
}
|
||||
if (left.lastSignalAt === null && right.lastSignalAt === null) return 0
|
||||
if (left.lastSignalAt === null) return -1
|
||||
if (right.lastSignalAt === null) return 1
|
||||
const byTime = left.lastSignalAt.localeCompare(right.lastSignalAt)
|
||||
if (byTime !== 0) return byTime
|
||||
return municipalityNameCompare(left, right)
|
||||
return 0
|
||||
}
|
||||
|
||||
const compareWizardMunicipalitySuggest = (
|
||||
|
|
|
|||
|
|
@ -59,10 +59,13 @@ export const loadWizardMunicipalitySuggestions = async (
|
|||
})
|
||||
|
||||
const ranked = rankWizardMunicipalitySuggestions(inputs)
|
||||
const docBySlug = new Map(municipalities.map((doc) => [doc.slug, doc]))
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
resultKind: 'wizard-suggest',
|
||||
municipalities: ranked.map(toHomeSearchMunicipalityHit),
|
||||
municipalities: ranked.map((input) =>
|
||||
toHomeSearchMunicipalityHit(docBySlug.get(input.slug)!),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue