Substitui o modelo de MunicipalityUpdate (kind/semanal/urgente/nota/sinal + signalType) por um modelo unificado: body obrigatório + polarity (boa/neutra/ruim) + urgent + adversarySignal. - Schema: polarity enum obrigatório, urgent boolean default false, adversarySignal boolean - Wizard: novo WizardUpdateBodyStep com campo de texto + polaridade + urgente - Lista: controle unificado de atualização com polaridade inline - Feed: exibe polaridade como badge colorido + indicador de urgência - V2 status strip: atualização rápida de polaridade - E11: queries atualizadas para polarity + adversarySignal - Migração: 20260807_030000_unify_municipality_update (backfill polarity=neutra, urgent de kind, adversarySignal de kind+signalType) - Testes: 18/18 integração C87, 593/593 total, 1399/1399 unitários - Fix: testes de state deputy usavam nomes fixos que colidiam entre execuções
135 lines
4.3 KiB
TypeScript
135 lines
4.3 KiB
TypeScript
import type { CampaignWizardActionId } from '@/lib/campaignActionRoutes'
|
|
import { WIZARD_CHAIN_SKIP_LABEL } from '@/lib/campaignWizardCopy'
|
|
import { politicalTrendStatuses, type PoliticalTrendStatusValue } from '@/lib/schemas/municipality'
|
|
import { resolveWizardChainEntry, wizardChainContinueHref } from '@/lib/wizardActionChain'
|
|
|
|
const politicalTrendDisplayLabels: Record<PoliticalTrendStatusValue, string> = {
|
|
favoravel: 'Favorável',
|
|
neutra: 'Neutra',
|
|
desfavoravel: 'Desfavorável',
|
|
}
|
|
|
|
export const WIZARD_TREND_SAVE_LABEL = 'Salvar' as const
|
|
|
|
export const WIZARD_TREND_CLEAR_LABEL = 'Limpar' as const
|
|
|
|
export const WIZARD_TREND_SAVED_MESSAGE = 'Tendência política registrada.' as const
|
|
|
|
export const WIZARD_TREND_UNREGISTERED_TITLE = 'Tendência não registrada' as const
|
|
|
|
export type WizardTrendSkipAction = {
|
|
label: string
|
|
href: string
|
|
}
|
|
|
|
export const wizardTrendChoiceStepTitle = (
|
|
currentStatus: PoliticalTrendStatusValue | null,
|
|
): string =>
|
|
currentStatus
|
|
? `Tendência ${politicalTrendDisplayLabels[currentStatus]}`
|
|
: WIZARD_TREND_UNREGISTERED_TITLE
|
|
|
|
export const selectablePoliticalTrendStatuses = (
|
|
currentStatus: PoliticalTrendStatusValue | null,
|
|
): PoliticalTrendStatusValue[] =>
|
|
currentStatus
|
|
? politicalTrendStatuses.filter((status) => status !== currentStatus)
|
|
: [...politicalTrendStatuses]
|
|
|
|
/** Where the change-trend wizard should land when `tendencia=` is present. */
|
|
export type WizardTrendNoteDestination = 'note' | 'choice' | 'home'
|
|
|
|
/**
|
|
* `home` when the query trend already matches the persisted status — covers
|
|
* post-save RSC refresh (B97) and stale deep-links; never loops back to choice.
|
|
*/
|
|
export const resolveWizardTrendNoteDestination = (
|
|
trendStatus: PoliticalTrendStatusValue,
|
|
currentStatus: PoliticalTrendStatusValue | null,
|
|
): WizardTrendNoteDestination => {
|
|
if (trendStatus === currentStatus) {
|
|
return 'home'
|
|
}
|
|
|
|
if (!selectablePoliticalTrendStatuses(currentStatus).includes(trendStatus)) {
|
|
return 'choice'
|
|
}
|
|
|
|
return 'note'
|
|
}
|
|
|
|
export const shouldShowWizardTrendSkip = (
|
|
entryAction: CampaignWizardActionId | undefined,
|
|
): boolean => entryAction != null && entryAction !== 'change-trend'
|
|
|
|
export const resolveWizardTrendSkip = (
|
|
entryAction: CampaignWizardActionId | undefined,
|
|
municipalitySlug: string,
|
|
returnPath?: string,
|
|
): WizardTrendSkipAction | undefined =>
|
|
shouldShowWizardTrendSkip(entryAction)
|
|
? {
|
|
label: WIZARD_CHAIN_SKIP_LABEL,
|
|
href: wizardChainContinueHref(
|
|
resolveWizardChainEntry(entryAction, 'change-trend'),
|
|
'change-trend',
|
|
municipalitySlug,
|
|
returnPath,
|
|
),
|
|
}
|
|
: undefined
|
|
|
|
export type PoliticalTrendNotePrefillSource =
|
|
| { kind: 'none' }
|
|
| { kind: 'voteAdjustment'; previousValue: number; newValue: number }
|
|
| { kind: 'custom'; text: string }
|
|
|
|
export const buildPoliticalTrendNotePrefill = (source: PoliticalTrendNotePrefillSource): string => {
|
|
switch (source.kind) {
|
|
case 'none':
|
|
return ''
|
|
case 'voteAdjustment':
|
|
return `Ajuste de votos: ${source.previousValue} → ${source.newValue}`
|
|
case 'custom':
|
|
return source.text
|
|
default: {
|
|
const exhaustive: never = source
|
|
return exhaustive
|
|
}
|
|
}
|
|
}
|
|
|
|
const parseOptionalIntParam = (value: string | string[] | undefined): number | undefined => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
if (!trimmed) return undefined
|
|
const parsed = Number.parseInt(trimmed, 10)
|
|
return Number.isFinite(parsed) ? parsed : undefined
|
|
}
|
|
|
|
export const resolvePoliticalTrendNotePrefillSource = ({
|
|
entryAction,
|
|
notePrefill,
|
|
voteFrom,
|
|
voteTo,
|
|
}: {
|
|
entryAction?: CampaignWizardActionId
|
|
notePrefill?: string | string[] | undefined
|
|
voteFrom?: string | string[] | undefined
|
|
voteTo?: string | string[] | undefined
|
|
}): PoliticalTrendNotePrefillSource => {
|
|
const customText = Array.isArray(notePrefill) ? notePrefill[0] : notePrefill
|
|
if (customText?.trim()) {
|
|
return { kind: 'custom', text: customText.trim() }
|
|
}
|
|
|
|
if (entryAction === 'update-votes') {
|
|
const previousValue = parseOptionalIntParam(voteFrom)
|
|
const newValue = parseOptionalIntParam(voteTo)
|
|
if (previousValue !== undefined && newValue !== undefined) {
|
|
return { kind: 'voteAdjustment', previousValue, newValue }
|
|
}
|
|
}
|
|
|
|
return { kind: 'none' }
|
|
}
|