Private/Export-EntityData.ps1

function Export-EntityData {
    <#
    .SYNOPSIS
        Exports entity data to a file in CSV or JSON format.
 
    .DESCRIPTION
        A private helper function that handles common export operations including
        format detection, directory creation, and file writing. This eliminates
        duplicated logic across Export-Account, Export-Biller, Export-Earning, etc.
 
    .PARAMETER Data
        The data to export (array of objects).
 
    .PARAMETER OutputPath
        The file path to export to.
 
    .PARAMETER Format
        The export format (CSV or JSON). If not specified, detected from file extension.
 
    .PARAMETER Properties
        Optional array of property names to select for export. If not specified, all properties are exported.
 
    .PARAMETER EntityType
        The type of entity being exported (for verbose messaging).
 
    .EXAMPLE
        Export-EntityData -Data $accounts -OutputPath "accounts.csv" -EntityType "account"
 
    .EXAMPLE
        Export-EntityData -Data $billers -OutputPath "billers.json" -Format JSON -Properties @('Name', 'Amount', 'Frequency')
 
    .OUTPUTS
        [string] The resolved path of the exported file, or $null on failure
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory)]
        [AllowNull()]
        [AllowEmptyCollection()]
        [object[]]$Data,

        [Parameter(Mandatory)]
        [string]$OutputPath,

        [Parameter()]
        [ValidateSet('CSV', 'JSON')]
        [string]$Format,

        [Parameter()]
        [string[]]$Properties,

        [Parameter()]
        [string]$EntityType = 'entity'
    )

    # Check for empty data
    if (-not $Data -or $Data.Count -eq 0) {
        Write-Warning "No ${EntityType}s found to export."
        return $null
    }

    # Determine format from file extension if not specified
    if (-not $Format) {
        $extension = [System.IO.Path]::GetExtension($OutputPath).ToLower()
        $Format = switch ($extension) {
            '.json' { 'JSON' }
            '.csv' { 'CSV' }
            default { 'CSV' }
        }
    }

    # Ensure directory exists
    $directory = Split-Path -Path $OutputPath -Parent
    if ($directory -and -not (Test-Path $directory)) {
        New-Item -Path $directory -ItemType Directory -Force | Out-Null
    }

    # Select properties if specified
    $exportData = if ($Properties) {
        $Data | Select-Object -Property $Properties
    } else {
        $Data
    }

    # Export based on format
    try {
        switch ($Format) {
            'CSV' {
                $exportData | Export-Csv -Path $OutputPath -NoTypeInformation -ErrorAction Stop
                Write-Verbose "Exported $($Data.Count) ${EntityType}(s) to CSV: $OutputPath"
            }
            'JSON' {
                $exportData | ConvertTo-Json -Depth 10 |
                    Set-Content -Path $OutputPath -ErrorAction Stop
                Write-Verbose "Exported $($Data.Count) ${EntityType}(s) to JSON: $OutputPath"
            }
        }

        return (Resolve-Path $OutputPath).Path
    }
    catch {
        Write-Error "Failed to export ${EntityType}s: $_"
        return $null
    }
}