teqo-backup/src/utilities/supporterViewModels.ts
Francisco Ignácio Fontoura Solla e2724c10da Remodel the campaign vertical from Praça to Município
Rename the operational unit "Praça"/"Núcleo" to "Município" (code identifier
`municipality`) and land the 2026-07-23 course changes:

- New `candidate` role with unrestricted visibility (`isCampaignStaff` now
  includes it) alongside coordinator/advisor; leader is locked down to the
  supporter contact tool only.
- Demandas become staff-only; dobradinhas modeled via a `stateDeputy` entity.
- Camaçari consolidated to a single município (ZE 170/171 aggregated);
  Salvador keeps its 19 zone municípios.
- Map moves to the staff dashboard; `/campanha/municipios` is list/overview.
- Vote pledges keep the declared vs estimated (scenario) asymmetry; leaders
  never see estimates.

Destructive migration for the campaign vertical (drops nucleus/plaza-era
tables, wipes campaign rows, seeds 435 municípios). Public-site data untouched.

Includes the post-delivery /simplify cleanups: shared reloadStaffActor /
reloadCoordinatorActor helpers, req.context cache for getFreshCampaignUser,
parallelized dashboard loads, candidate dashboard-copy fix, and dead
leader-dashboard / roleKind code removal.
2026-07-23 21:36:20 -03:00

97 lines
3.3 KiB
TypeScript

import { territoryForCity } from '@/lib/bahiaTerritories'
import type { SupporterVoteIntention } from '@/lib/schemas/supporter'
import type { CampaignUser, Contact, Municipality, Supporter } from '@/payload-types'
import { formatBrazilianPhoneInput } from '@/utilities/phone'
import { isPopulatedRelationship } from '@/utilities/relationship'
import { supporterSourceLabels } from '@/utilities/supporterUi'
export type SupporterListItemViewModel = {
id: number
name: string
phone: string
city: string | null
municipalityName: string | null
municipalitySlug: string | null
voteIntention: SupporterVoteIntention | null
}
export type SupporterListOverviewViewModel = {
total: number
certoAndTende: number
indeciso: number
}
export type SupporterDetailViewModel = {
id: number
name: string
phone: string
phoneDisplay: string
email: string | null
city: string | null
territory: string | null
municipalityName: string | null
municipalitySlug: string | null
voteIntention: SupporterVoteIntention | null
sourceLabel: string
consentedAt: string | null
createdByName: string | null
hasVoteIntentionConsent: boolean
createdAt: string
}
const contactFromSupporter = (supporter: { contact: Supporter['contact'] }): Contact | null =>
isPopulatedRelationship<Contact>(supporter.contact) ? supporter.contact : null
const municipalityFromSupporter = (supporter: { municipality?: Supporter['municipality'] }): Municipality | null =>
isPopulatedRelationship<Municipality>(supporter.municipality) ? supporter.municipality : null
export const toSupporterListItemViewModel = (
supporter: Pick<Supporter, 'id' | 'voteIntention' | 'contact' | 'municipality'>,
): SupporterListItemViewModel => {
const contact = contactFromSupporter(supporter)
const municipality = municipalityFromSupporter(supporter)
return {
id: supporter.id,
name: contact?.name ?? 'Contato sem nome',
phone: contact?.phone ?? '',
city: contact?.city ?? null,
municipalityName: municipality?.name ?? null,
municipalitySlug: municipality?.slug ?? null,
voteIntention: supporter.voteIntention ?? null,
}
}
export const toSupporterDetailViewModel = (supporter: Supporter): SupporterDetailViewModel => {
const contact = contactFromSupporter(supporter)
const municipality = municipalityFromSupporter(supporter)
const creator = isPopulatedRelationship<CampaignUser>(supporter.createdBy)
? supporter.createdBy
: null
const city = contact?.city ?? null
const phone = contact?.phone ?? ''
return {
id: supporter.id,
name: contact?.name ?? 'Contato sem nome',
phone,
phoneDisplay: phone ? formatBrazilianPhoneInput(phone) : '',
email: contact?.email ?? null,
city,
territory: city ? (territoryForCity(city) ?? null) : null,
municipalityName: municipality?.name ?? null,
municipalitySlug: municipality?.slug ?? null,
voteIntention: supporter.voteIntention ?? null,
sourceLabel: supporterSourceLabels[supporter.source],
consentedAt: supporter.consentedAt ?? null,
createdByName: creator?.name ?? null,
hasVoteIntentionConsent: Boolean(supporter.voteIntentionConsentedAt),
createdAt: supporter.createdAt,
}
}
export const parseSupporterId = (value: string): number | null => {
if (!/^[1-9]\d*$/.test(value)) return null
const id = Number(value)
return Number.isSafeInteger(id) ? id : null
}