mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-15 02:23:07 +00:00
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.
46 lines
1.3 KiB
TypeScript
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
|
|
}
|