teqo-backup/src/app/(campaign)/campanha/(app)/municipios/municipalityStaffFormActions.ts
Francisco Solla 7343f15053 feat(C89): página Atualizações — feed de cards + filtro omnibox + criar (modal/bottom sheet)
Substitui o placeholder B164 por um feed operacional da carteira (staff):
- Cards com avatar do autor, polaridade/urgente/adversário, município e data
- Filtro omnibox (município via dotted-path, polaridade, urgente, autor, busca)
  no padrão das listas (URL canônica + adapter puro)
- Criação in-place: modal desktop / bottom sheet mobile, reusando o registro
  unificado (C87) com prefill do município do filtro ativo
- Leader lockdown intacto (gate staff + canReadMunicipalityUpdate fail-closed)
- parseMunicipalityUpdateFormData único para os 3 forms de atualização
- Loader cruzando a carteira sem cláusula de município (access por ator)
- Int/unit specs (19 unit + 5 int) + convenções

Fix real: o modal de criação não pode ser filho React do <form> de filtros —
React 19 não liga progressive enhancement em action-form aninhado.
2026-08-08 23:07:23 -03:00

178 lines
7 KiB
TypeScript

'use server'
import {
assignMunicipalityAdvisors,
setMunicipalityExpectedVotes,
setMunicipalityPoliticalTrend,
} from '@/app/(campaign)/campanha/actions/municipality'
import { createMunicipalityUpdate } from '@/app/(campaign)/campanha/actions/municipalityUpdate'
import {
createMunicipalityStateDeputy,
setStateDeputyMunicipalitiesBatch,
type MunicipalityStateDeputyCreateResult,
} from '@/app/(campaign)/campanha/actions/stateDeputy'
import {
nullableFormText,
optionalFormText,
optionalMunicipalitySlugFromForm,
repeatedRelationshipFormValues,
requiredFormBoolean,
requiredFormText,
requiredRelationshipFormValue,
voteEstimateScenarioFromForm,
} from '@/lib/formData'
import { WIZARD_TREND_SAVED_MESSAGE } from '@/lib/politicalTrendWizardUi'
import {
MUNICIPALITY_ADVISOR_MEMBERSHIP_UNRESTRICTED_MESSAGE,
MUNICIPALITY_STATE_DEPUTIES_CAP_MESSAGE,
parsePoliticalTrendStatusFormValue,
} from '@/lib/schemas/municipality'
import {
STATE_DEPUTY_CONFLICT_MESSAGE,
STATE_DEPUTY_MUNICIPALITIES_SAFE_MESSAGES,
STATE_DEPUTY_NAME_REQUIRED_MESSAGE,
STATE_DEPUTY_STAFF_MESSAGE,
} from '@/lib/schemas/stateDeputy'
import {
runCampaignFormAction,
type CampaignFormActionState,
} from '@/utilities/campaignFormActionError'
import { revalidateMunicipalityListPaths } from '@/utilities/municipality/municipalityRevalidation'
import { parseMunicipalityUpdateFormData } from '@/utilities/municipality/municipalityUpdateFormData'
import { municipalityStaffEditSafeMessages } from './municipalityStaffEditMessages'
export const setMunicipalityExpectedVotesFormAction = async (
_state: CampaignFormActionState,
formData: FormData,
): Promise<CampaignFormActionState> =>
runCampaignFormAction({
execute: async () => {
const municipality = requiredRelationshipFormValue(formData, 'municipalityId')
const expectedVotes = voteEstimateScenarioFromForm(formData, 'expectedVotes')
await setMunicipalityExpectedVotes({ municipality, expectedVotes })
revalidateMunicipalityListPaths({ slug: optionalMunicipalitySlugFromForm(formData) })
return { message: 'Votos estimados atualizados.' }
},
safeMessages: municipalityStaffEditSafeMessages,
genericMessage:
'Não foi possível salvar os votos estimados. Verifique seu acesso e tente novamente.',
})
export const setMunicipalityPoliticalTrendFormAction = async (
_state: CampaignFormActionState,
formData: FormData,
): Promise<CampaignFormActionState> =>
runCampaignFormAction({
execute: async () => {
const municipality = requiredRelationshipFormValue(formData, 'municipalityId')
const status = parsePoliticalTrendStatusFormValue(optionalFormText(formData, 'trendStatus'))
await setMunicipalityPoliticalTrend({
municipality,
status,
note: nullableFormText(formData, 'trendNote'),
})
revalidateMunicipalityListPaths({ slug: optionalMunicipalitySlugFromForm(formData) })
return { message: WIZARD_TREND_SAVED_MESSAGE }
},
safeMessages: municipalityStaffEditSafeMessages,
genericMessage:
'Não foi possível registrar a tendência. Verifique seu acesso e tente novamente.',
})
export const assignMunicipalityAdvisorsFormAction = async (
_state: CampaignFormActionState,
formData: FormData,
): Promise<CampaignFormActionState> =>
runCampaignFormAction({
execute: async () => {
const municipality = requiredRelationshipFormValue(formData, 'municipalityId')
const advisors = repeatedRelationshipFormValues(formData, 'advisors')
await assignMunicipalityAdvisors({ municipality, advisors })
revalidateMunicipalityListPaths({ slug: optionalMunicipalitySlugFromForm(formData) })
return { message: 'Assessores atualizados.' }
},
safeMessages: [MUNICIPALITY_ADVISOR_MEMBERSHIP_UNRESTRICTED_MESSAGE],
genericMessage:
'Não foi possível atualizar os assessores. Verifique seu acesso e tente novamente.',
})
export const createMunicipalityListUpdateFormAction = async (
_state: CampaignFormActionState,
formData: FormData,
): Promise<CampaignFormActionState> =>
runCampaignFormAction({
execute: async () => {
await createMunicipalityUpdate(parseMunicipalityUpdateFormData(formData))
revalidateMunicipalityListPaths({ slug: optionalMunicipalitySlugFromForm(formData) })
return { message: 'Atualização registrada.' }
},
genericMessage:
'Não foi possível registrar a atualização. Verifique seu acesso e tente novamente.',
})
/**
* B157 — one chip toggle in the "Dobradinhas" column of `/campanha/municipios`.
* Deliberately NOT a new record: the B37 batch already owns the inverted write
* (`stateDeputyId` + `municipalityIds[]`), so this wrapper maps the owner-shaped
* delta of the cell (`municipalityId` + `stateDeputyId`) onto it — the lock,
* the advisor-scope check, the cap and the revalidation all stay where B37 put
* them (and `setStateDeputyMunicipalitiesBatch` already revalidates the list,
* the touched detail and the deputy's ficha).
*/
export const setMunicipalityStateDeputiesFormAction = async (
_state: CampaignFormActionState,
formData: FormData,
): Promise<CampaignFormActionState> =>
runCampaignFormAction({
execute: async () => {
const municipalityId = requiredRelationshipFormValue(formData, 'municipalityId')
const stateDeputyId = requiredRelationshipFormValue(formData, 'stateDeputyId')
const assigned = requiredFormBoolean(formData, 'assigned')
await setStateDeputyMunicipalitiesBatch({
stateDeputyId,
municipalityIds: [municipalityId],
assigned,
})
return { message: assigned ? 'Dobradinha vinculada.' : 'Dobradinha removida.' }
},
safeMessages: STATE_DEPUTY_MUNICIPALITIES_SAFE_MESSAGES,
genericMessage:
'Não foi possível atualizar as dobradinhas. Verifique seu acesso e tente novamente.',
})
/**
* B157 — inline create from the "Dobradinhas" column: the raw search text
* (`Nome (PARTIDO)` syntax) is parsed and persisted with the automatic link to
* the município. The success state carries the created deputy so the cell can
* swap its optimistic chip without waiting for the RSC refresh.
*/
export const createMunicipalityStateDeputyFormAction = async (
_state: CampaignFormActionState,
formData: FormData,
): Promise<
CampaignFormActionState & { stateDeputy?: MunicipalityStateDeputyCreateResult['stateDeputy'] }
> =>
runCampaignFormAction({
execute: async () => {
const stateDeputy = await createMunicipalityStateDeputy({
municipalityId: requiredRelationshipFormValue(formData, 'municipalityId'),
rawName: requiredFormText(formData, 'rawName'),
})
return {
message: 'Dobradinha criada e vinculada.',
stateDeputy: stateDeputy.stateDeputy,
}
},
safeMessages: [
STATE_DEPUTY_CONFLICT_MESSAGE,
STATE_DEPUTY_STAFF_MESSAGE,
STATE_DEPUTY_NAME_REQUIRED_MESSAGE,
MUNICIPALITY_STATE_DEPUTIES_CAP_MESSAGE,
],
genericMessage: 'Não foi possível criar a dobradinha. Tente novamente.',
})