teqo-backup/src/utilities/supporter/supporterImport.ts
Francisco Ignácio Fontoura Solla 4806c417c8 Engineering Pass 3: consolidate patterns and lock them with guards.
Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
2026-07-29 06:11:29 -03:00

54 lines
1.5 KiB
TypeScript

import type { SupporterVoteIntention } from '@/lib/schemas/supporter'
type SupporterImportRowStatus =
| 'ok'
| 'duplicado_pelo_telefone'
| 'telefone_invalido'
| 'municipio_nao_reconhecido'
| 'nome_invalido'
| 'intencao_invalida'
export type SupporterImportPreviewRowBase = {
line: number
nome: string
telefone: string
municipio: string
intencao: string
}
export type SupporterImportOkRow = SupporterImportPreviewRowBase & {
status: 'ok'
normalizedPhone: string
canonicalCity?: string
voteIntention?: SupporterVoteIntention
}
export type SupporterImportPreviewRow =
| SupporterImportOkRow
| (SupporterImportPreviewRowBase & {
status: Exclude<SupporterImportRowStatus, 'ok'>
normalizedPhone?: string
canonicalCity?: string
voteIntention?: SupporterVoteIntention
})
export type SupporterImportPreviewResult = {
counts: {
ok: number
duplicate: number
error: number
}
/** First N rows for the on-screen preview table (the full set stays server-side). */
sampleRows: SupporterImportPreviewRow[]
/** Precomputed CSV of every error row, for the "Baixar relatório de erros" button. */
errorReportCsv: string
/** HMAC-signed token that `confirmSupporterImport` exchanges for the staged batch. */
importToken: string
}
export const isSupporterImportOkRow = (
row: SupporterImportPreviewRow,
): row is SupporterImportOkRow => row.status === 'ok'
export const isPreviewErrorRow = (row: SupporterImportPreviewRow): boolean =>
row.status !== 'ok' && row.status !== 'duplicado_pelo_telefone'