teqo/src/utilities/signatureExport.ts
Francisco Ignácio Fontoura Solla d93e2aa72f Add admin CSV export for signatures and contacts.
Enables @payloadcms/plugin-import-export on signature (flattened contact/petition columns) and contact (native fields) so operations can download filtered lists for external ESP/SMS workflows.
2026-07-23 00:53:04 -03:00

46 lines
1.3 KiB
TypeScript

import type { Contact, Petition } from '@/payload-types'
import { isPopulatedRelationship } from '@/utilities/relationship'
import type { ToCSVFunction } from '@payloadcms/plugin-import-export/types'
type ExportRow = Record<string, unknown>
const emptyContactColumns = (row: ExportRow): void => {
row.contact_name = ''
row.contact_email = ''
row.contact_phone = ''
row.contact_state = ''
row.contact_city = ''
}
const isPopulatedPetition = (value: unknown): value is Pick<Petition, 'id' | 'title'> => {
if (value === null || typeof value !== 'object') return false
const record = value as Record<string, unknown>
return typeof record.id === 'string' && typeof record.title === 'string'
}
export const signatureContactToCSV: ToCSVFunction = ({ value, row }) => {
if (!isPopulatedRelationship<Contact>(value)) {
emptyContactColumns(row)
return undefined
}
row.contact_name = value.name
row.contact_email = value.email ?? ''
row.contact_phone = value.phone ?? ''
row.contact_state = value.state ?? ''
row.contact_city = value.city ?? ''
return undefined
}
export const signaturePetitionToCSV: ToCSVFunction = ({ value, row }) => {
if (!isPopulatedPetition(value)) {
row.petition_id = ''
row.petition_title = ''
return undefined
}
row.petition_id = value.id
row.petition_title = value.title
return undefined
}