mirror of
https://github.com/fsolla/teqo.git
synced 2026-12-01 12:03:06 +00:00
Pass 2 W4d: runCampaignFormAction (stay-on-page) fecha o C8 F4
- novo wrapper ao lado de runCampaignRedirectFormAction: execute -> success spread (message + extras tipo advisorId/supporterId); erro mapeado por mapCampaignFormActionError com echo de values/revision - 13 acoes migradas em 7 arquivos: dobradinhas/[slug], organizacoes/[slug], municipios editar, liderancas/[id], assessores (5), demandas/[slug] (3), apoiadores/novo, convite autofill - early-returns viram throws: mensagens seguras (Campo invalido, Selecione ao menos um municipio, Status invalido) e FormDataBoundaryError p/ erros de campo (cost, receipt) - estados visiveis identicos - excecoes documentadas em comentario: planos (mapper custom + fallback async de titulo duplicado), apoiadores/[id] (flatten p/ controles message-only), convite login (duas fases + redirect) - pin unitario do wrapper: campaignFormActions.unit.spec.ts (spread, boundary, zod, safe/generic, echo)
This commit is contained in:
parent
c52e0e7957
commit
267218b05a
12 changed files with 450 additions and 352 deletions
|
|
@ -1,5 +1,9 @@
|
||||||
'use server'
|
'use server'
|
||||||
|
|
||||||
|
// Documented exception to `runCampaignFormAction` (Pass 2 W4d): these actions
|
||||||
|
// feed message-only inline controls, so every mapped state (field errors
|
||||||
|
// included) is flattened to a single message via `toMessageOnlyState`.
|
||||||
|
|
||||||
import { revalidatePath } from 'next/cache'
|
import { revalidatePath } from 'next/cache'
|
||||||
import { redirect } from 'next/navigation'
|
import { redirect } from 'next/navigation'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import {
|
||||||
optionalFormText,
|
optionalFormText,
|
||||||
} from '@/lib/formData'
|
} from '@/lib/formData'
|
||||||
import { supporterCreateSchema } from '@/lib/schemas/supporter'
|
import { supporterCreateSchema } from '@/lib/schemas/supporter'
|
||||||
import { mapCampaignFormActionError } from '@/utilities/campaignFormActionError'
|
import { runCampaignFormAction } from '@/utilities/campaignFormActionError'
|
||||||
import { sanitizeBrazilianPhoneInput } from '@/lib/phone'
|
import { sanitizeBrazilianPhoneInput } from '@/lib/phone'
|
||||||
|
|
||||||
export type SupporterFormState = {
|
export type SupporterFormState = {
|
||||||
|
|
@ -39,64 +39,52 @@ const safeActionMessages = [
|
||||||
'Consentimento de intenção de voto ainda não configurado.',
|
'Consentimento de intenção de voto ainda não configurado.',
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
const getSupporterFormError = (
|
|
||||||
error: unknown,
|
|
||||||
values?: SupporterFormValues,
|
|
||||||
revision?: number,
|
|
||||||
): SupporterFormState =>
|
|
||||||
mapCampaignFormActionError({
|
|
||||||
error,
|
|
||||||
safeMessages: safeActionMessages,
|
|
||||||
genericMessage: 'Não foi possível cadastrar o apoiador. Verifique os dados e tente novamente.',
|
|
||||||
values,
|
|
||||||
revision,
|
|
||||||
})
|
|
||||||
|
|
||||||
export const createSupporterFormAction = async (
|
export const createSupporterFormAction = async (
|
||||||
state: SupporterFormState,
|
state: SupporterFormState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<SupporterFormState> => {
|
): Promise<SupporterFormState> => {
|
||||||
let values: SupporterFormValues | undefined
|
// Parsed before the ladder (non-throwing readers) so failures echo them back.
|
||||||
const revision = (state.revision ?? 0) + 1
|
const values: SupporterFormValues = {
|
||||||
|
name: optionalFormText(formData, 'name'),
|
||||||
try {
|
phone: optionalFormText(formData, 'phone'),
|
||||||
values = {
|
email: optionalFormText(formData, 'email'),
|
||||||
name: optionalFormText(formData, 'name'),
|
city: optionalFormText(formData, 'city'),
|
||||||
phone: optionalFormText(formData, 'phone'),
|
municipality: optionalFormText(formData, 'municipality'),
|
||||||
email: optionalFormText(formData, 'email'),
|
voteIntention: optionalFormText(formData, 'voteIntention'),
|
||||||
city: optionalFormText(formData, 'city'),
|
|
||||||
municipality: optionalFormText(formData, 'municipality'),
|
|
||||||
voteIntention: optionalFormText(formData, 'voteIntention'),
|
|
||||||
}
|
|
||||||
|
|
||||||
const phone = sanitizeBrazilianPhoneInput(values.phone ?? '')
|
|
||||||
const voteIntention = values.voteIntention?.trim() || undefined
|
|
||||||
|
|
||||||
const municipality = nullableRelationshipFormValue(formData, 'municipality')
|
|
||||||
const input = supporterCreateSchema.parse({
|
|
||||||
name: values.name ?? '',
|
|
||||||
phone,
|
|
||||||
email: values.email,
|
|
||||||
city: values.city,
|
|
||||||
...(municipality ? { municipality } : {}),
|
|
||||||
...(voteIntention ? { voteIntention } : {}),
|
|
||||||
consentAccepted: checkboxFormValue(formData, 'consentAccepted') ? true : undefined,
|
|
||||||
voteIntentionConsentAccepted: voteIntention
|
|
||||||
? checkboxFormValue(formData, 'voteIntentionConsentAccepted')
|
|
||||||
: undefined,
|
|
||||||
})
|
|
||||||
|
|
||||||
const supporter = await createSupporter(input)
|
|
||||||
revalidatePath('/campanha/apoiadores')
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: 'success',
|
|
||||||
message: supporter.contactReused
|
|
||||||
? 'Apoiador cadastrado. O contato existente com este celular foi reutilizado.'
|
|
||||||
: 'Apoiador cadastrado com sucesso.',
|
|
||||||
supporterId: supporter.id,
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return getSupporterFormError(error, values, revision)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return runCampaignFormAction({
|
||||||
|
execute: async () => {
|
||||||
|
const phone = sanitizeBrazilianPhoneInput(values.phone ?? '')
|
||||||
|
const voteIntention = values.voteIntention?.trim() || undefined
|
||||||
|
|
||||||
|
const municipality = nullableRelationshipFormValue(formData, 'municipality')
|
||||||
|
const input = supporterCreateSchema.parse({
|
||||||
|
name: values.name ?? '',
|
||||||
|
phone,
|
||||||
|
email: values.email,
|
||||||
|
city: values.city,
|
||||||
|
...(municipality ? { municipality } : {}),
|
||||||
|
...(voteIntention ? { voteIntention } : {}),
|
||||||
|
consentAccepted: checkboxFormValue(formData, 'consentAccepted') ? true : undefined,
|
||||||
|
voteIntentionConsentAccepted: voteIntention
|
||||||
|
? checkboxFormValue(formData, 'voteIntentionConsentAccepted')
|
||||||
|
: undefined,
|
||||||
|
})
|
||||||
|
|
||||||
|
const supporter = await createSupporter(input)
|
||||||
|
revalidatePath('/campanha/apoiadores')
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: supporter.contactReused
|
||||||
|
? 'Apoiador cadastrado. O contato existente com este celular foi reutilizado.'
|
||||||
|
: 'Apoiador cadastrado com sucesso.',
|
||||||
|
supporterId: supporter.id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
safeMessages: safeActionMessages,
|
||||||
|
genericMessage: 'Não foi possível cadastrar o apoiador. Verifique os dados e tente novamente.',
|
||||||
|
values,
|
||||||
|
revision: (state.revision ?? 0) + 1,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,19 @@ import {
|
||||||
} from '@/lib/formData'
|
} from '@/lib/formData'
|
||||||
import { ADVISOR_ACTION_SAFE_MESSAGES } from '@/lib/schemas/advisor'
|
import { ADVISOR_ACTION_SAFE_MESSAGES } from '@/lib/schemas/advisor'
|
||||||
import {
|
import {
|
||||||
mapCampaignFormActionError,
|
runCampaignFormAction,
|
||||||
type CampaignFormActionState,
|
type CampaignFormActionState,
|
||||||
} from '@/utilities/campaignFormActionError'
|
} from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
|
const INVALID_FIELD_MESSAGE = 'Campo inválido.'
|
||||||
|
const EMPTY_BATCH_MESSAGE = 'Selecione ao menos um município.'
|
||||||
|
|
||||||
|
const advisorSafeMessages = [
|
||||||
|
...ADVISOR_ACTION_SAFE_MESSAGES,
|
||||||
|
INVALID_FIELD_MESSAGE,
|
||||||
|
EMPTY_BATCH_MESSAGE,
|
||||||
|
] as const
|
||||||
|
|
||||||
const parseMunicipalityIds = (formData: FormData): number[] => {
|
const parseMunicipalityIds = (formData: FormData): number[] => {
|
||||||
const raw = formData.getAll('municipalityIds')
|
const raw = formData.getAll('municipalityIds')
|
||||||
const ids = raw
|
const ids = raw
|
||||||
|
|
@ -29,131 +38,109 @@ const parseMunicipalityIds = (formData: FormData): number[] => {
|
||||||
export const updateAdvisorProfileFormAction = async (
|
export const updateAdvisorProfileFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const id = requiredRelationshipFormValue(formData, 'advisorId')
|
execute: async () => {
|
||||||
const field = requiredFormText(formData, 'field')
|
const id = requiredRelationshipFormValue(formData, 'advisorId')
|
||||||
|
const field = requiredFormText(formData, 'field')
|
||||||
|
|
||||||
if (field === 'name') {
|
if (field === 'name') {
|
||||||
await updateAdvisorProfile({ id, name: requiredFormText(formData, 'name') })
|
await updateAdvisorProfile({ id, name: requiredFormText(formData, 'name') })
|
||||||
} else if (field === 'email') {
|
} else if (field === 'email') {
|
||||||
await updateAdvisorProfile({ id, email: requiredFormText(formData, 'email') })
|
await updateAdvisorProfile({ id, email: requiredFormText(formData, 'email') })
|
||||||
} else if (field === 'phone') {
|
} else if (field === 'phone') {
|
||||||
await updateAdvisorProfile({
|
await updateAdvisorProfile({
|
||||||
id,
|
id,
|
||||||
phone: nullableFormText(formData, 'phone'),
|
phone: nullableFormText(formData, 'phone'),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return { message: 'Campo inválido.' }
|
throw new Error(INVALID_FIELD_MESSAGE)
|
||||||
}
|
}
|
||||||
|
|
||||||
return { status: 'success', message: 'Salvo.' }
|
return { message: 'Salvo.' }
|
||||||
} catch (error) {
|
},
|
||||||
return mapCampaignFormActionError({
|
safeMessages: advisorSafeMessages,
|
||||||
error,
|
genericMessage: 'Não foi possível salvar. Verifique os dados e tente novamente.',
|
||||||
safeMessages: ADVISOR_ACTION_SAFE_MESSAGES,
|
})
|
||||||
genericMessage: 'Não foi possível salvar. Verifique os dados e tente novamente.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const setAdvisorMunicipalityMembershipFormAction = async (
|
export const setAdvisorMunicipalityMembershipFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const assignedRaw = requiredFormText(formData, 'assigned')
|
execute: async () => {
|
||||||
await setAdvisorMunicipalityMembership({
|
const assignedRaw = requiredFormText(formData, 'assigned')
|
||||||
advisorId: requiredRelationshipFormValue(formData, 'advisorId'),
|
await setAdvisorMunicipalityMembership({
|
||||||
municipalityId: requiredRelationshipFormValue(formData, 'municipalityId'),
|
advisorId: requiredRelationshipFormValue(formData, 'advisorId'),
|
||||||
assigned: assignedRaw === 'true',
|
municipalityId: requiredRelationshipFormValue(formData, 'municipalityId'),
|
||||||
})
|
assigned: assignedRaw === 'true',
|
||||||
return { status: 'success', message: 'Carteira atualizada.' }
|
})
|
||||||
} catch (error) {
|
return { message: 'Carteira atualizada.' }
|
||||||
return mapCampaignFormActionError({
|
},
|
||||||
error,
|
safeMessages: advisorSafeMessages,
|
||||||
safeMessages: ADVISOR_ACTION_SAFE_MESSAGES,
|
genericMessage: 'Não foi possível atualizar a carteira do assessor.',
|
||||||
genericMessage: 'Não foi possível atualizar a carteira do assessor.',
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const setAdvisorMunicipalitiesBatchFormAction = async (
|
export const setAdvisorMunicipalitiesBatchFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const municipalityIds = parseMunicipalityIds(formData)
|
execute: async () => {
|
||||||
if (municipalityIds.length === 0) {
|
const municipalityIds = parseMunicipalityIds(formData)
|
||||||
return { message: 'Selecione ao menos um município.' }
|
if (municipalityIds.length === 0) {
|
||||||
}
|
throw new Error(EMPTY_BATCH_MESSAGE)
|
||||||
const assignedRaw = requiredFormText(formData, 'assigned')
|
}
|
||||||
await setAdvisorMunicipalitiesBatch({
|
const assignedRaw = requiredFormText(formData, 'assigned')
|
||||||
advisorId: requiredRelationshipFormValue(formData, 'advisorId'),
|
await setAdvisorMunicipalitiesBatch({
|
||||||
municipalityIds,
|
advisorId: requiredRelationshipFormValue(formData, 'advisorId'),
|
||||||
assigned: assignedRaw === 'true',
|
municipalityIds,
|
||||||
})
|
assigned: assignedRaw === 'true',
|
||||||
return { status: 'success', message: 'Carteira atualizada.' }
|
})
|
||||||
} catch (error) {
|
return { message: 'Carteira atualizada.' }
|
||||||
return mapCampaignFormActionError({
|
},
|
||||||
error,
|
safeMessages: advisorSafeMessages,
|
||||||
safeMessages: ADVISOR_ACTION_SAFE_MESSAGES,
|
genericMessage: 'Não foi possível atualizar a carteira do assessor.',
|
||||||
genericMessage: 'Não foi possível atualizar a carteira do assessor.',
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const sendAdvisorPasswordResetFormAction = async (
|
export const sendAdvisorPasswordResetFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const result = await sendAdvisorPasswordReset({
|
execute: async () => {
|
||||||
advisorId: requiredRelationshipFormValue(formData, 'advisorId'),
|
const result = await sendAdvisorPasswordReset({
|
||||||
})
|
advisorId: requiredRelationshipFormValue(formData, 'advisorId'),
|
||||||
return {
|
})
|
||||||
status: 'success',
|
return { message: `Link de redefinição enviado para ${result.email}.` }
|
||||||
message: `Link de redefinição enviado para ${result.email}.`,
|
},
|
||||||
}
|
safeMessages: advisorSafeMessages,
|
||||||
} catch (error) {
|
genericMessage: 'Não foi possível enviar o link de redefinição de senha.',
|
||||||
return mapCampaignFormActionError({
|
})
|
||||||
error,
|
|
||||||
safeMessages: ADVISOR_ACTION_SAFE_MESSAGES,
|
|
||||||
genericMessage: 'Não foi possível enviar o link de redefinição de senha.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createAdvisorFormAction = async (
|
export const createAdvisorFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState & { advisorId?: number }> => {
|
): Promise<CampaignFormActionState & { advisorId?: number }> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const created = await createAdvisor({
|
execute: async () => {
|
||||||
name: requiredFormText(formData, 'name'),
|
const created = await createAdvisor({
|
||||||
email: requiredFormText(formData, 'email'),
|
name: requiredFormText(formData, 'name'),
|
||||||
phone: nullableFormText(formData, 'phone') ?? undefined,
|
email: requiredFormText(formData, 'email'),
|
||||||
})
|
phone: nullableFormText(formData, 'phone') ?? undefined,
|
||||||
|
|
||||||
const municipalityIds = parseMunicipalityIds(formData)
|
|
||||||
if (municipalityIds.length > 0) {
|
|
||||||
await setAdvisorMunicipalitiesBatch({
|
|
||||||
advisorId: created.id,
|
|
||||||
municipalityIds,
|
|
||||||
assigned: true,
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
const municipalityIds = parseMunicipalityIds(formData)
|
||||||
status: 'success',
|
if (municipalityIds.length > 0) {
|
||||||
message: 'Assessor criado.',
|
await setAdvisorMunicipalitiesBatch({
|
||||||
advisorId: created.id,
|
advisorId: created.id,
|
||||||
}
|
municipalityIds,
|
||||||
} catch (error) {
|
assigned: true,
|
||||||
return mapCampaignFormActionError({
|
})
|
||||||
error,
|
}
|
||||||
safeMessages: ADVISOR_ACTION_SAFE_MESSAGES,
|
|
||||||
genericMessage: 'Não foi possível criar o assessor. Verifique os dados e tente novamente.',
|
return { message: 'Assessor criado.', advisorId: created.id }
|
||||||
})
|
},
|
||||||
}
|
safeMessages: advisorSafeMessages,
|
||||||
}
|
genericMessage: 'Não foi possível criar o assessor. Verifique os dados e tente novamente.',
|
||||||
|
})
|
||||||
|
|
|
||||||
|
|
@ -7,100 +7,99 @@ import {
|
||||||
setCampaignDemandCost,
|
setCampaignDemandCost,
|
||||||
transitionCampaignDemand,
|
transitionCampaignDemand,
|
||||||
} from '@/app/(campaign)/campanha/actions/demand'
|
} from '@/app/(campaign)/campanha/actions/demand'
|
||||||
import { nullableFormText, requiredFormText, requiredRelationshipFormValue } from '@/lib/formData'
|
import {
|
||||||
|
FormDataBoundaryError,
|
||||||
|
nullableFormText,
|
||||||
|
requiredFormText,
|
||||||
|
requiredRelationshipFormValue,
|
||||||
|
} from '@/lib/formData'
|
||||||
import { campaignDemandStatuses, type CampaignDemandStatus } from '@/lib/schemas/campaignDemand'
|
import { campaignDemandStatuses, type CampaignDemandStatus } from '@/lib/schemas/campaignDemand'
|
||||||
import { getCampaignActionContext } from '@/utilities/campaignActionContext'
|
import { getCampaignActionContext } from '@/utilities/campaignActionContext'
|
||||||
import {
|
import {
|
||||||
mapCampaignFormActionError,
|
runCampaignFormAction,
|
||||||
type CampaignFormActionState,
|
type CampaignFormActionState,
|
||||||
} from '@/utilities/campaignFormActionError'
|
} from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
|
const INVALID_STATUS_MESSAGE = 'Status de demanda inválido.'
|
||||||
|
|
||||||
const transitionSafeMessages = [
|
const transitionSafeMessages = [
|
||||||
'Somente a coordenação e a assessoria movem demandas.',
|
'Somente a coordenação e a assessoria movem demandas.',
|
||||||
'Demandas escaladas são decididas pelo Coordenador Geral.',
|
'Demandas escaladas são decididas pelo Coordenador Geral.',
|
||||||
|
INVALID_STATUS_MESSAGE,
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
export const transitionDemandFormAction = async (
|
export const transitionDemandFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const rawStatus = requiredFormText(formData, 'status')
|
execute: async () => {
|
||||||
if (!campaignDemandStatuses.includes(rawStatus as CampaignDemandStatus)) {
|
const rawStatus = requiredFormText(formData, 'status')
|
||||||
return { message: 'Status de demanda inválido.' }
|
if (!campaignDemandStatuses.includes(rawStatus as CampaignDemandStatus)) {
|
||||||
}
|
throw new Error(INVALID_STATUS_MESSAGE)
|
||||||
|
}
|
||||||
|
|
||||||
await transitionCampaignDemand({
|
await transitionCampaignDemand({
|
||||||
id: requiredRelationshipFormValue(formData, 'demandId'),
|
id: requiredRelationshipFormValue(formData, 'demandId'),
|
||||||
status: rawStatus as CampaignDemandStatus,
|
status: rawStatus as CampaignDemandStatus,
|
||||||
decisionNote: nullableFormText(formData, 'decisionNote'),
|
decisionNote: nullableFormText(formData, 'decisionNote'),
|
||||||
})
|
})
|
||||||
revalidatePath('/campanha/demandas/[slug]', 'page')
|
revalidatePath('/campanha/demandas/[slug]', 'page')
|
||||||
return { status: 'success', message: 'Demanda atualizada.' }
|
return { message: 'Demanda atualizada.' }
|
||||||
} catch (error) {
|
},
|
||||||
return mapCampaignFormActionError({
|
safeMessages: transitionSafeMessages,
|
||||||
error,
|
genericMessage: 'Não foi possível mover a demanda. Verifique seu acesso e tente novamente.',
|
||||||
safeMessages: transitionSafeMessages,
|
})
|
||||||
genericMessage: 'Não foi possível mover a demanda. Verifique seu acesso e tente novamente.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const setDemandCostFormAction = async (
|
export const setDemandCostFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const rawCost = formData.get('cost')
|
execute: async () => {
|
||||||
let cost: number | null = null
|
const rawCost = formData.get('cost')
|
||||||
if (typeof rawCost === 'string' && rawCost.trim()) {
|
let cost: number | null = null
|
||||||
const parsed = Number(rawCost.replace(',', '.'))
|
if (typeof rawCost === 'string' && rawCost.trim()) {
|
||||||
if (!Number.isFinite(parsed) || parsed < 0) {
|
const parsed = Number(rawCost.replace(',', '.'))
|
||||||
return { fieldErrors: { cost: ['Informe um valor válido.'] } }
|
if (!Number.isFinite(parsed) || parsed < 0) {
|
||||||
|
throw new FormDataBoundaryError('cost', 'Informe um valor válido.')
|
||||||
|
}
|
||||||
|
cost = Math.round(parsed * 100) / 100
|
||||||
}
|
}
|
||||||
cost = Math.round(parsed * 100) / 100
|
|
||||||
}
|
|
||||||
|
|
||||||
await setCampaignDemandCost({
|
await setCampaignDemandCost({
|
||||||
id: requiredRelationshipFormValue(formData, 'demandId'),
|
id: requiredRelationshipFormValue(formData, 'demandId'),
|
||||||
cost,
|
cost,
|
||||||
})
|
})
|
||||||
revalidatePath('/campanha/demandas/[slug]', 'page')
|
revalidatePath('/campanha/demandas/[slug]', 'page')
|
||||||
return { status: 'success', message: 'Custo registrado.' }
|
return { message: 'Custo registrado.' }
|
||||||
} catch (error) {
|
},
|
||||||
return mapCampaignFormActionError({
|
safeMessages: ['Somente a coordenação e a assessoria registram custos.'],
|
||||||
error,
|
genericMessage: 'Não foi possível registrar o custo. Tente novamente.',
|
||||||
safeMessages: ['Somente a coordenação e a assessoria registram custos.'],
|
})
|
||||||
genericMessage: 'Não foi possível registrar o custo. Tente novamente.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const attachDemandReceiptFormAction = async (
|
export const attachDemandReceiptFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const demandID = requiredRelationshipFormValue(formData, 'demandId')
|
execute: async () => {
|
||||||
const file = formData.get('receipt')
|
const demandID = requiredRelationshipFormValue(formData, 'demandId')
|
||||||
if (!(file instanceof File)) {
|
const file = formData.get('receipt')
|
||||||
return { fieldErrors: { receipt: ['Selecione um arquivo.'] } }
|
if (!(file instanceof File)) {
|
||||||
}
|
throw new FormDataBoundaryError('receipt', 'Selecione um arquivo.')
|
||||||
|
}
|
||||||
|
|
||||||
const { payload, actor } = await getCampaignActionContext()
|
const { payload, actor } = await getCampaignActionContext()
|
||||||
await attachCampaignDemandReceiptRecord(payload, actor, demandID, file)
|
await attachCampaignDemandReceiptRecord(payload, actor, demandID, file)
|
||||||
revalidatePath('/campanha/demandas/[slug]', 'page')
|
revalidatePath('/campanha/demandas/[slug]', 'page')
|
||||||
return { status: 'success', message: 'Comprovante anexado.' }
|
return { message: 'Comprovante anexado.' }
|
||||||
} catch (error) {
|
},
|
||||||
return mapCampaignFormActionError({
|
safeMessages: [
|
||||||
error,
|
'Envie uma imagem (JPEG, PNG, WebP) ou PDF.',
|
||||||
safeMessages: [
|
'O arquivo enviado está vazio.',
|
||||||
'Envie uma imagem (JPEG, PNG, WebP) ou PDF.',
|
'O comprovante deve ter no máximo 10 MB.',
|
||||||
'O arquivo enviado está vazio.',
|
'Somente a coordenação e a assessoria anexam comprovantes.',
|
||||||
'O comprovante deve ter no máximo 10 MB.',
|
],
|
||||||
'Somente a coordenação e a assessoria anexam comprovantes.',
|
genericMessage: 'Não foi possível anexar o comprovante. Tente novamente.',
|
||||||
],
|
})
|
||||||
genericMessage: 'Não foi possível anexar o comprovante. Tente novamente.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -5,28 +5,25 @@ import { revalidatePath } from 'next/cache'
|
||||||
import { updateStateDeputy } from '@/app/(campaign)/campanha/actions/stateDeputy'
|
import { updateStateDeputy } from '@/app/(campaign)/campanha/actions/stateDeputy'
|
||||||
import { nullableFormText, requiredRelationshipFormValue } from '@/lib/formData'
|
import { nullableFormText, requiredRelationshipFormValue } from '@/lib/formData'
|
||||||
import {
|
import {
|
||||||
mapCampaignFormActionError,
|
runCampaignFormAction,
|
||||||
type CampaignFormActionState,
|
type CampaignFormActionState,
|
||||||
} from '@/utilities/campaignFormActionError'
|
} from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
export const updateStateDeputyFormAction = async (
|
export const updateStateDeputyFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
await updateStateDeputy({
|
execute: async () => {
|
||||||
id: requiredRelationshipFormValue(formData, 'stateDeputyId'),
|
await updateStateDeputy({
|
||||||
party: nullableFormText(formData, 'party'),
|
id: requiredRelationshipFormValue(formData, 'stateDeputyId'),
|
||||||
notes: nullableFormText(formData, 'notes'),
|
party: nullableFormText(formData, 'party'),
|
||||||
})
|
notes: nullableFormText(formData, 'notes'),
|
||||||
revalidatePath('/campanha/dobradinhas/[slug]', 'page')
|
})
|
||||||
return { status: 'success', message: 'Dobradinha atualizada.' }
|
revalidatePath('/campanha/dobradinhas/[slug]', 'page')
|
||||||
} catch (error) {
|
return { message: 'Dobradinha atualizada.' }
|
||||||
return mapCampaignFormActionError({
|
},
|
||||||
error,
|
safeMessages: ['Somente a coordenação e a assessoria gerenciam dobradinhas.'],
|
||||||
safeMessages: ['Somente a coordenação e a assessoria gerenciam dobradinhas.'],
|
genericMessage:
|
||||||
genericMessage:
|
'Não foi possível salvar a dobradinha. Verifique seu acesso e tente novamente.',
|
||||||
'Não foi possível salvar a dobradinha. Verifique seu acesso e tente novamente.',
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import {
|
||||||
} from '@/lib/formData'
|
} from '@/lib/formData'
|
||||||
import { leadershipSectors, leadershipSupportStatuses } from '@/lib/schemas/leadership'
|
import { leadershipSectors, leadershipSupportStatuses } from '@/lib/schemas/leadership'
|
||||||
import {
|
import {
|
||||||
mapCampaignFormActionError,
|
runCampaignFormAction,
|
||||||
type CampaignFormActionState,
|
type CampaignFormActionState,
|
||||||
} from '@/utilities/campaignFormActionError'
|
} from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
|
|
@ -23,34 +23,31 @@ const safeMessages = [
|
||||||
export const updateLeadershipInternalFormAction = async (
|
export const updateLeadershipInternalFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const sector = optionalFormText(formData, 'sector')
|
execute: async () => {
|
||||||
const supportStatus = optionalFormText(formData, 'supportStatus')
|
const sector = optionalFormText(formData, 'sector')
|
||||||
|
const supportStatus = optionalFormText(formData, 'supportStatus')
|
||||||
|
|
||||||
await updateLeadershipInternal({
|
await updateLeadershipInternal({
|
||||||
id: requiredRelationshipFormValue(formData, 'leadershipId'),
|
id: requiredRelationshipFormValue(formData, 'leadershipId'),
|
||||||
municipalities: repeatedRelationshipFormValues(formData, 'municipalities'),
|
municipalities: repeatedRelationshipFormValues(formData, 'municipalities'),
|
||||||
organizations: repeatedRelationshipFormValues(formData, 'organizations'),
|
organizations: repeatedRelationshipFormValues(formData, 'organizations'),
|
||||||
stateDeputies: repeatedRelationshipFormValues(formData, 'stateDeputies'),
|
stateDeputies: repeatedRelationshipFormValues(formData, 'stateDeputies'),
|
||||||
sector: leadershipSectors.includes(sector as (typeof leadershipSectors)[number])
|
sector: leadershipSectors.includes(sector as (typeof leadershipSectors)[number])
|
||||||
? (sector as (typeof leadershipSectors)[number])
|
? (sector as (typeof leadershipSectors)[number])
|
||||||
: null,
|
: null,
|
||||||
supportStatus: leadershipSupportStatuses.includes(
|
supportStatus: leadershipSupportStatuses.includes(
|
||||||
supportStatus as (typeof leadershipSupportStatuses)[number],
|
supportStatus as (typeof leadershipSupportStatuses)[number],
|
||||||
)
|
)
|
||||||
? (supportStatus as (typeof leadershipSupportStatuses)[number])
|
? (supportStatus as (typeof leadershipSupportStatuses)[number])
|
||||||
: undefined,
|
: undefined,
|
||||||
notes: nullableFormText(formData, 'notes'),
|
notes: nullableFormText(formData, 'notes'),
|
||||||
consentNote: nullableFormText(formData, 'consentNote'),
|
consentNote: nullableFormText(formData, 'consentNote'),
|
||||||
})
|
})
|
||||||
revalidatePath('/campanha/liderancas/[id]', 'page')
|
revalidatePath('/campanha/liderancas/[id]', 'page')
|
||||||
return { status: 'success', message: 'Ficha da liderança atualizada.' }
|
return { message: 'Ficha da liderança atualizada.' }
|
||||||
} catch (error) {
|
},
|
||||||
return mapCampaignFormActionError({
|
safeMessages,
|
||||||
error,
|
genericMessage: 'Não foi possível salvar a ficha. Verifique seu acesso e tente novamente.',
|
||||||
safeMessages,
|
})
|
||||||
genericMessage: 'Não foi possível salvar a ficha. Verifique seu acesso e tente novamente.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import {
|
||||||
requiredRelationshipFormValue,
|
requiredRelationshipFormValue,
|
||||||
} from '@/lib/formData'
|
} from '@/lib/formData'
|
||||||
import {
|
import {
|
||||||
mapCampaignFormActionError,
|
runCampaignFormAction,
|
||||||
type CampaignFormActionState,
|
type CampaignFormActionState,
|
||||||
} from '@/utilities/campaignFormActionError'
|
} from '@/utilities/campaignFormActionError'
|
||||||
import { revalidateMunicipalityListPaths } from '@/utilities/municipalityRevalidation'
|
import { revalidateMunicipalityListPaths } from '@/utilities/municipalityRevalidation'
|
||||||
|
|
@ -19,29 +19,26 @@ import { municipalityStaffEditSafeMessages } from '../../municipalityStaffEditMe
|
||||||
export const updateMunicipalityStrategyFormAction = async (
|
export const updateMunicipalityStrategyFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const municipality = requiredRelationshipFormValue(formData, 'municipalityId')
|
execute: async () => {
|
||||||
const priority = optionalFormText(formData, 'priority')
|
const municipality = requiredRelationshipFormValue(formData, 'municipalityId')
|
||||||
|
const priority = optionalFormText(formData, 'priority')
|
||||||
|
|
||||||
await updateMunicipalityStrategy({
|
await updateMunicipalityStrategy({
|
||||||
municipality,
|
municipality,
|
||||||
priority: priority === 'alta' ? 'alta' : 'normal',
|
priority: priority === 'alta' ? 'alta' : 'normal',
|
||||||
strengths: repeatedFormTexts(formData, 'strengths'),
|
strengths: repeatedFormTexts(formData, 'strengths'),
|
||||||
risks: repeatedFormTexts(formData, 'risks'),
|
risks: repeatedFormTexts(formData, 'risks'),
|
||||||
stateDeputies: repeatedRelationshipFormValues(formData, 'stateDeputies'),
|
stateDeputies: repeatedRelationshipFormValues(formData, 'stateDeputies'),
|
||||||
dobradinhaNotes: nullableFormText(formData, 'dobradinhaNotes'),
|
dobradinhaNotes: nullableFormText(formData, 'dobradinhaNotes'),
|
||||||
nextSteps: nullableFormText(formData, 'nextSteps'),
|
nextSteps: nullableFormText(formData, 'nextSteps'),
|
||||||
budgetNotes: nullableFormText(formData, 'budgetNotes'),
|
budgetNotes: nullableFormText(formData, 'budgetNotes'),
|
||||||
})
|
})
|
||||||
revalidateMunicipalityListPaths({ slug: optionalMunicipalitySlugFromForm(formData) })
|
revalidateMunicipalityListPaths({ slug: optionalMunicipalitySlugFromForm(formData) })
|
||||||
return { status: 'success', message: 'Estratégia atualizada.' }
|
return { message: 'Estratégia atualizada.' }
|
||||||
} catch (error) {
|
},
|
||||||
return mapCampaignFormActionError({
|
safeMessages: municipalityStaffEditSafeMessages,
|
||||||
error,
|
genericMessage:
|
||||||
safeMessages: municipalityStaffEditSafeMessages,
|
'Não foi possível salvar a estratégia. Verifique seu acesso e tente novamente.',
|
||||||
genericMessage:
|
})
|
||||||
'Não foi possível salvar a estratégia. Verifique seu acesso e tente novamente.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -11,32 +11,29 @@ import {
|
||||||
} from '@/lib/formData'
|
} from '@/lib/formData'
|
||||||
import { organizationKinds, type OrganizationKind } from '@/lib/schemas/organization'
|
import { organizationKinds, type OrganizationKind } from '@/lib/schemas/organization'
|
||||||
import {
|
import {
|
||||||
mapCampaignFormActionError,
|
runCampaignFormAction,
|
||||||
type CampaignFormActionState,
|
type CampaignFormActionState,
|
||||||
} from '@/utilities/campaignFormActionError'
|
} from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
export const updateOrganizationFormAction = async (
|
export const updateOrganizationFormAction = async (
|
||||||
_state: CampaignFormActionState,
|
_state: CampaignFormActionState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignFormActionState> => {
|
): Promise<CampaignFormActionState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const rawKind = optionalFormText(formData, 'kind')
|
execute: async () => {
|
||||||
await updateOrganization({
|
const rawKind = optionalFormText(formData, 'kind')
|
||||||
id: requiredRelationshipFormValue(formData, 'organizationId'),
|
await updateOrganization({
|
||||||
kind: organizationKinds.includes(rawKind as OrganizationKind)
|
id: requiredRelationshipFormValue(formData, 'organizationId'),
|
||||||
? (rawKind as OrganizationKind)
|
kind: organizationKinds.includes(rawKind as OrganizationKind)
|
||||||
: undefined,
|
? (rawKind as OrganizationKind)
|
||||||
notes: nullableFormText(formData, 'notes'),
|
: undefined,
|
||||||
municipalities: repeatedRelationshipFormValues(formData, 'municipalities'),
|
notes: nullableFormText(formData, 'notes'),
|
||||||
})
|
municipalities: repeatedRelationshipFormValues(formData, 'municipalities'),
|
||||||
revalidatePath('/campanha/organizacoes/[slug]', 'page')
|
})
|
||||||
return { status: 'success', message: 'Organização atualizada.' }
|
revalidatePath('/campanha/organizacoes/[slug]', 'page')
|
||||||
} catch (error) {
|
return { message: 'Organização atualizada.' }
|
||||||
return mapCampaignFormActionError({
|
},
|
||||||
error,
|
safeMessages: ['Somente a coordenação e a assessoria gerenciam organizações.'],
|
||||||
safeMessages: ['Somente a coordenação e a assessoria gerenciam organizações.'],
|
genericMessage:
|
||||||
genericMessage:
|
'Não foi possível salvar a organização. Verifique seu acesso e tente novamente.',
|
||||||
'Não foi possível salvar a organização. Verifique seu acesso e tente novamente.',
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
'use server'
|
'use server'
|
||||||
|
|
||||||
|
// Documented exception to `runCampaignRedirectFormAction`/`runCampaignFormAction`
|
||||||
|
// (Pass 2 W4d): these ladders carry a custom error mapper (DB unique-violation
|
||||||
|
// regexes surfacing as field errors) plus an async duplicate-title fallback
|
||||||
|
// that links to the existing plan — policy the shared wrappers deliberately
|
||||||
|
// don't grow parameters for.
|
||||||
|
|
||||||
import config from '@payload-config'
|
import config from '@payload-config'
|
||||||
import { getPayload } from 'payload'
|
import { getPayload } from 'payload'
|
||||||
import { redirect } from 'next/navigation'
|
import { redirect } from 'next/navigation'
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,10 @@ import {
|
||||||
requiredFormSecret,
|
requiredFormSecret,
|
||||||
requiredFormText,
|
requiredFormText,
|
||||||
} from '@/lib/formData'
|
} from '@/lib/formData'
|
||||||
import { mapCampaignFormActionError } from '@/utilities/campaignFormActionError'
|
import {
|
||||||
|
mapCampaignFormActionError,
|
||||||
|
runCampaignFormAction,
|
||||||
|
} from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
export type CampaignInviteFormState = {
|
export type CampaignInviteFormState = {
|
||||||
status?: 'success'
|
status?: 'success'
|
||||||
|
|
@ -46,22 +49,20 @@ export const redeemCampaignInviteAutofillFormAction = async (
|
||||||
token: string,
|
token: string,
|
||||||
_state: CampaignInviteFormState,
|
_state: CampaignInviteFormState,
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
): Promise<CampaignInviteFormState> => {
|
): Promise<CampaignInviteFormState> =>
|
||||||
try {
|
runCampaignFormAction({
|
||||||
const input = campaignInviteAutofillSchema.parse({
|
execute: async () => {
|
||||||
token,
|
const input = campaignInviteAutofillSchema.parse({
|
||||||
...profileFromForm(formData),
|
token,
|
||||||
consentAccepted: checkboxFormValue(formData, 'consentAccepted'),
|
...profileFromForm(formData),
|
||||||
})
|
consentAccepted: checkboxFormValue(formData, 'consentAccepted'),
|
||||||
await redeemCampaignInviteAutofill(input)
|
})
|
||||||
return {
|
await redeemCampaignInviteAutofill(input)
|
||||||
status: 'success',
|
return { message: 'Seus dados foram confirmados com sucesso.' }
|
||||||
message: 'Seus dados foram confirmados com sucesso.',
|
},
|
||||||
}
|
genericMessage:
|
||||||
} catch (error) {
|
'Este convite não está disponível. Peça um novo convite à pessoa que falou com você.',
|
||||||
return inviteFormError(error)
|
})
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const redeemCampaignInviteLoginFormAction = async (
|
export const redeemCampaignInviteLoginFormAction = async (
|
||||||
token: string,
|
token: string,
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,52 @@ export const mapCampaignFormActionError = <Values>(
|
||||||
return { message: genericMessage, values, revision }
|
return { message: genericMessage, values, revision }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The shared stay-on-page form-action ladder: parse + mutate inside
|
||||||
|
* `execute` (thrown `FormDataBoundaryError`/`ZodError`/safe messages map to
|
||||||
|
* the standard error state), spread whatever `execute` resolves into the
|
||||||
|
* success state (`message` plus extras like created ids). Pre-parsed `values`
|
||||||
|
* and `revision` are echoed on failure so forms can repopulate.
|
||||||
|
*
|
||||||
|
* Documented exceptions that deliberately stay hand-rolled: the planos
|
||||||
|
* ladders (custom unique-violation mapping + async duplicate-title fallback)
|
||||||
|
* and apoiadores/[id] (flattens field errors into message-only states for
|
||||||
|
* its inline controls).
|
||||||
|
*/
|
||||||
|
export const runCampaignFormAction = async <
|
||||||
|
Success extends { message: string },
|
||||||
|
Values = undefined,
|
||||||
|
>({
|
||||||
|
execute,
|
||||||
|
safeMessages,
|
||||||
|
genericMessage,
|
||||||
|
values,
|
||||||
|
revision,
|
||||||
|
resolveBoundaryMessage,
|
||||||
|
}: {
|
||||||
|
execute: () => Promise<Success>
|
||||||
|
safeMessages?: readonly string[]
|
||||||
|
genericMessage: string
|
||||||
|
/** Echoed back on failure so the form can repopulate. */
|
||||||
|
values?: Values
|
||||||
|
revision?: number
|
||||||
|
resolveBoundaryMessage?: (error: FormDataBoundaryError) => string
|
||||||
|
}): Promise<({ status: 'success' } & Success) | CampaignFormErrorState<Values>> => {
|
||||||
|
try {
|
||||||
|
const success = await execute()
|
||||||
|
return { ...success, status: 'success' as const }
|
||||||
|
} catch (error) {
|
||||||
|
return mapCampaignFormActionError({
|
||||||
|
error,
|
||||||
|
safeMessages,
|
||||||
|
genericMessage,
|
||||||
|
values,
|
||||||
|
revision,
|
||||||
|
resolveBoundaryMessage,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The shared create→redirect form-action ladder: run the mutation, map any
|
* The shared create→redirect form-action ladder: run the mutation, map any
|
||||||
* failure through `mapCampaignFormActionError`, redirect on success. The
|
* failure through `mapCampaignFormActionError`, redirect on success. The
|
||||||
|
|
|
||||||
79
tests/unit/campaignFormActions.unit.spec.ts
Normal file
79
tests/unit/campaignFormActions.unit.spec.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
// @vitest-environment node
|
||||||
|
|
||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { FormDataBoundaryError } from '@/lib/formData'
|
||||||
|
import { runCampaignFormAction } from '@/utilities/campaignFormActionError'
|
||||||
|
|
||||||
|
const GENERIC = 'Não foi possível concluir a ação.'
|
||||||
|
|
||||||
|
describe('runCampaignFormAction (shared stay-on-page ladder)', () => {
|
||||||
|
it('spreads the execute result into the success state', async () => {
|
||||||
|
const state = await runCampaignFormAction({
|
||||||
|
execute: async () => ({ message: 'Salvo.', advisorId: 42 }),
|
||||||
|
genericMessage: GENERIC,
|
||||||
|
})
|
||||||
|
expect(state).toEqual({ status: 'success', message: 'Salvo.', advisorId: 42 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('maps FormDataBoundaryError to a field error', async () => {
|
||||||
|
const state = await runCampaignFormAction({
|
||||||
|
execute: async () => {
|
||||||
|
throw new FormDataBoundaryError('cost', 'Informe um valor válido.')
|
||||||
|
},
|
||||||
|
genericMessage: GENERIC,
|
||||||
|
})
|
||||||
|
expect(state).toEqual({
|
||||||
|
fieldErrors: { cost: ['Informe um valor válido.'] },
|
||||||
|
values: undefined,
|
||||||
|
revision: undefined,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('maps ZodError to per-field validation errors', async () => {
|
||||||
|
const schema = z.object({ name: z.string().min(2, 'Informe o nome.') })
|
||||||
|
const state = await runCampaignFormAction({
|
||||||
|
execute: async () => {
|
||||||
|
schema.parse({ name: '' })
|
||||||
|
return { message: 'nunca chega' }
|
||||||
|
},
|
||||||
|
genericMessage: GENERIC,
|
||||||
|
})
|
||||||
|
expect(state).toMatchObject({ fieldErrors: { name: ['Informe o nome.'] } })
|
||||||
|
expect(state).not.toHaveProperty('status')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('echoes safe-listed error messages and hides everything else behind the generic one', async () => {
|
||||||
|
const safe = await runCampaignFormAction({
|
||||||
|
execute: async () => {
|
||||||
|
throw new Error('Somente a coordenação gerencia isto.')
|
||||||
|
},
|
||||||
|
safeMessages: ['Somente a coordenação gerencia isto.'],
|
||||||
|
genericMessage: GENERIC,
|
||||||
|
})
|
||||||
|
expect(safe.message).toBe('Somente a coordenação gerencia isto.')
|
||||||
|
|
||||||
|
const leaked = await runCampaignFormAction({
|
||||||
|
execute: async () => {
|
||||||
|
throw new Error('duplicate key value violates unique constraint "contact_phone_idx"')
|
||||||
|
},
|
||||||
|
safeMessages: ['Somente a coordenação gerencia isto.'],
|
||||||
|
genericMessage: GENERIC,
|
||||||
|
})
|
||||||
|
expect(leaked.message).toBe(GENERIC)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('echoes pre-parsed values and the revision on failure so forms repopulate', async () => {
|
||||||
|
const values = { name: 'Ana', phone: '71 9 9999-0000' }
|
||||||
|
const state = await runCampaignFormAction({
|
||||||
|
execute: async () => {
|
||||||
|
throw new Error('boom')
|
||||||
|
},
|
||||||
|
genericMessage: GENERIC,
|
||||||
|
values,
|
||||||
|
revision: 3,
|
||||||
|
})
|
||||||
|
expect(state).toEqual({ message: GENERIC, values, revision: 3 })
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Reference in a new issue