teqo-backup/src/app/(campaign)/campanha/actions/municipality.ts
Francisco Ignácio Fontoura Solla c7001b75f0 E14: engagement levels N0-N4 and the first allocationDecision writer.
`municipality.engagementLevel` (staff-only, no default) records what the
campaign decided to invest in a município, next to the E8/E10 metrics that
describe what it knows. Moving it is a reallocation decision, so only
unrestricted staff may, every move carries a rationale plus the signals that
would reverse it, and the write files an `allocationDecision` in the same
transaction under an advisory lock taken before the current level is read —
otherwise the snapshot's `from` is a guess. The enum gains `movimento`: a level
move is neither `aceita` nor `descarta`.

No backfill. Null is "Sem nível", and filtering by it is the triage queue.
That is also why the list sorts `nivel` in memory rather than in the query:
Postgres puts NULLs first on DESC, so a native sort would open "N4 primeiro"
on a page of municípios nobody has decided about yet. The filter stays native.

The hysteresis rules from research §6.8 are pure and client-safe, so the
Popover explains a block before submitting and the server re-runs them anyway;
a blocked move answers 409 with typed violations and the override is recorded
with exactly the rules it contradicted.
2026-07-27 21:19:33 -03:00

340 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use server'
import type { Payload } from 'payload'
import {
ENGAGEMENT_LEVEL_PATTERN_ID,
EngagementLevelBlockedError,
getEngagementLevelViolations,
} from '@/lib/engagementLevel'
import { nextAdvisorIdsAfterMembership } from '@/lib/municipalityAdvisorMembership'
import {
MUNICIPALITY_ADVISOR_MEMBERSHIP_UNRESTRICTED_MESSAGE,
MUNICIPALITY_ENGAGEMENT_LEVEL_UNRESTRICTED_MESSAGE,
municipalityAdvisorMembershipSchema,
municipalityAdvisorsAssignmentSchema,
municipalityEngagementLevelSchema,
municipalityExpectedVotesSchema,
municipalityPoliticalTrendSchema,
municipalityStrategyUpdateSchema,
type MunicipalityAdvisorMembershipInput,
type MunicipalityAdvisorsAssignmentInput,
type MunicipalityEngagementLevelInput,
type MunicipalityExpectedVotesInput,
type MunicipalityPoliticalTrendInput,
type MunicipalityStrategyUpdateInput,
} from '@/lib/schemas/municipality'
import { normalizeVoteEstimateOnSave, toVoteEstimateScenarioViewModel } from '@/lib/voteEstimate'
import type { CampaignUser } from '@/payload-types'
import {
getCampaignActionContext,
reloadStaffActor,
reloadUnrestrictedActor,
} from '@/utilities/campaignActionContext'
import type { PayloadTransactionRequest } from '@/utilities/payloadTransaction'
import { withPayloadTransaction } from '@/utilities/payloadTransaction'
import { acquireTextAdvisoryLocks } from '@/utilities/postgresTransactionLocks'
import { uniqueRelationshipIds } from '@/utilities/relationship'
const getFreshStaffActor = (
payload: Payload,
actor: CampaignUser,
req?: PayloadTransactionRequest,
): Promise<CampaignUser> =>
reloadStaffActor(
payload,
actor,
'Somente a coordenação e a assessoria podem editar o município.',
req,
)
/** Staff strategy fields: priority and intel notes. Access enforced by row scope. */
export const updateMunicipalityStrategyRecord = async (
payload: Payload,
actor: CampaignUser,
input: MunicipalityStrategyUpdateInput,
) => {
const { municipality, strengths, risks, stateDeputies, ...fields } =
municipalityStrategyUpdateSchema.parse(input)
const currentActor = await getFreshStaffActor(payload, actor)
return payload.update({
collection: 'municipality',
id: municipality,
data: {
...fields,
...(strengths === undefined ? {} : { strengths: strengths.map((text) => ({ text })) }),
...(risks === undefined ? {} : { risks: risks.map((text) => ({ text })) }),
...(stateDeputies === undefined ? {} : { stateDeputies }),
},
depth: 0,
user: currentActor,
overrideAccess: false,
})
}
export const updateMunicipalityStrategy = async (input: MunicipalityStrategyUpdateInput) => {
const { payload, actor } = await getCampaignActionContext()
return updateMunicipalityStrategyRecord(payload, actor, input)
}
/** Political trend: manual conjuncture reading by staff (author/date derived by hook). */
export const setMunicipalityPoliticalTrendRecord = async (
payload: Payload,
actor: CampaignUser,
input: MunicipalityPoliticalTrendInput,
) => {
const { municipality, status, note } = municipalityPoliticalTrendSchema.parse(input)
const currentActor = await getFreshStaffActor(payload, actor)
return payload.update({
collection: 'municipality',
id: municipality,
data: {
politicalTrend: { status, note },
},
depth: 0,
user: currentActor,
overrideAccess: false,
})
}
export const setMunicipalityPoliticalTrend = async (input: MunicipalityPoliticalTrendInput) => {
const { payload, actor } = await getCampaignActionContext()
return setMunicipalityPoliticalTrendRecord(payload, actor, input)
}
/** Staff-only total expected votes for the municipality (distinct from pledge aggregates). */
export const setMunicipalityExpectedVotesRecord = async (
payload: Payload,
actor: CampaignUser,
input: MunicipalityExpectedVotesInput,
) => {
const normalizedExpectedVotes = normalizeVoteEstimateOnSave(
toVoteEstimateScenarioViewModel(input.expectedVotes),
)
const { municipality, expectedVotes } = municipalityExpectedVotesSchema.parse({
municipality: input.municipality,
expectedVotes: normalizedExpectedVotes,
})
const currentActor = await getFreshStaffActor(payload, actor)
return payload.update({
collection: 'municipality',
id: municipality,
data: { expectedVotes },
depth: 0,
user: currentActor,
overrideAccess: false,
})
}
export const setMunicipalityExpectedVotes = async (input: MunicipalityExpectedVotesInput) => {
const { payload, actor } = await getCampaignActionContext()
return setMunicipalityExpectedVotesRecord(payload, actor, input)
}
/**
* E14 — moving a município up or down the N0–N4 ladder. Two writes in one
* transaction: the município carries the current level, and `allocationDecision`
* carries the movement forever (C12's first production writer), so the history
* survives the next movement instead of being overwritten by it.
*
* The lock is what makes the recorded "nível anterior" true: without it two
* coordinators moving the same município would each read the same `from` and
* write two decisions claiming to start from it.
*/
export const setMunicipalityEngagementLevelRecord = async (
payload: Payload,
actor: CampaignUser,
input: MunicipalityEngagementLevelInput,
) => {
const { municipality, level, note, reversalSignals, triangulatedShock, override } =
municipalityEngagementLevelSchema.parse(input)
return withPayloadTransaction(
payload,
async ({ req }) => {
const currentActor = await reloadUnrestrictedActor(
payload,
actor,
MUNICIPALITY_ENGAGEMENT_LEVEL_UNRESTRICTED_MESSAGE,
req,
)
await acquireTextAdvisoryLocks(payload, req, [
`municipality-engagement-level:${municipality}`,
])
const current = await payload.findByID({
collection: 'municipality',
id: municipality,
depth: 0,
select: { engagementLevel: true, levelChangedAt: true },
user: currentActor,
overrideAccess: false,
req,
})
const from = current.engagementLevel ?? null
const violations = getEngagementLevelViolations({
from,
to: level,
levelChangedAt: current.levelChangedAt ?? null,
now: new Date(),
triangulatedShock,
})
if (violations.length > 0 && !override) throw new EngagementLevelBlockedError(violations)
const updated = await payload.update({
collection: 'municipality',
id: municipality,
data: { engagementLevel: level, levelNote: note },
depth: 0,
user: currentActor,
overrideAccess: false,
req,
})
await payload.create({
collection: 'allocationDecision',
data: {
municipality,
patternId: ENGAGEMENT_LEVEL_PATTERN_ID,
outcome: 'movimento',
rationale: note,
// Only the values the decision was taken on — no document dumps
// (the collection caps the serialized snapshot at 16 KB).
snapshot: {
from,
to: level,
reversalSignals,
triangulatedShock,
violations: violations.map((violation) => violation.id),
overridden: violations.length > 0,
previousLevelChangedAt: current.levelChangedAt ?? null,
},
},
depth: 0,
user: currentActor,
overrideAccess: false,
req,
})
return updated
},
{ beginFailureMessage: 'Não foi possível iniciar o movimento de nível.' },
)
}
export const setMunicipalityEngagementLevel = async (input: MunicipalityEngagementLevelInput) => {
const { payload, actor } = await getCampaignActionContext()
return setMunicipalityEngagementLevelRecord(payload, actor, input)
}
/** Advisor assignment is unrestricted staff (coordinator + candidate); the hook validates eligibility. */
export const assignMunicipalityAdvisorsRecord = async (
payload: Payload,
actor: CampaignUser,
input: MunicipalityAdvisorsAssignmentInput,
) => {
const { municipality, advisors } = municipalityAdvisorsAssignmentSchema.parse(input)
return withPayloadTransaction(
payload,
async ({ req }) => {
await reloadUnrestrictedActor(
payload,
actor,
MUNICIPALITY_ADVISOR_MEMBERSHIP_UNRESTRICTED_MESSAGE,
req,
)
await acquireTextAdvisoryLocks(payload, req, [`municipality-advisors:${municipality}`])
// Intentional admin bypass: unrestricted role was freshly verified above;
// the advisors field is admin-only by field access.
return payload.update({
collection: 'municipality',
id: municipality,
data: { advisors },
depth: 0,
overrideAccess: true,
req,
})
},
{ beginFailureMessage: 'Não foi possível iniciar a designação de assessores.' },
)
}
export const assignMunicipalityAdvisors = async (input: MunicipalityAdvisorsAssignmentInput) => {
const { payload, actor } = await getCampaignActionContext()
return assignMunicipalityAdvisorsRecord(payload, actor, input)
}
/**
* Single-advisor delta from the list popover — writes one toggle instead of
* replacing the whole `advisors` array, so a concurrent edit from another
* tab/actor is never clobbered. Same lock and eligibility hook as
* `assignMunicipalityAdvisorsRecord`; unlike it, the target can be coordinator
* or candidate too (the popover lists every unrestricted staff member as
* eligible, not just `advisor`-role accounts), so it does not reuse
* `assertTargetAdvisor` from `actions/advisor.ts`. Deliberately does not
* revalidate — the list's facet/sort by advisor reconciles on next navigation,
* same choice as the votos estimados popover.
*/
export const setMunicipalityAdvisorMembershipRecord = async (
payload: Payload,
actor: CampaignUser,
input: MunicipalityAdvisorMembershipInput,
) => {
const { municipality, advisor, assigned } = municipalityAdvisorMembershipSchema.parse(input)
return withPayloadTransaction(
payload,
async ({ req }) => {
await reloadUnrestrictedActor(
payload,
actor,
MUNICIPALITY_ADVISOR_MEMBERSHIP_UNRESTRICTED_MESSAGE,
req,
)
await acquireTextAdvisoryLocks(payload, req, [`municipality-advisors:${municipality}`])
const current = await payload.findByID({
collection: 'municipality',
id: municipality,
depth: 0,
select: { advisors: true },
overrideAccess: true,
req,
})
const currentAdvisorIDs = uniqueRelationshipIds(current.advisors)
const nextAdvisorIDs = nextAdvisorIdsAfterMembership(currentAdvisorIDs, advisor, assigned)
if (nextAdvisorIDs === null) return current
// Intentional admin bypass: unrestricted role was freshly verified above;
// the advisors field is admin-only by field access, and eligibility is
// reconfirmed by the `validateMunicipalityAdvisors` beforeValidate hook.
return payload.update({
collection: 'municipality',
id: municipality,
data: { advisors: nextAdvisorIDs },
depth: 0,
overrideAccess: true,
req,
})
},
{ beginFailureMessage: 'Não foi possível iniciar a atualização de assessores.' },
)
}
export const setMunicipalityAdvisorMembership = async (
input: MunicipalityAdvisorMembershipInput,
) => {
const { payload, actor } = await getCampaignActionContext()
return setMunicipalityAdvisorMembershipRecord(payload, actor, input)
}