Implement CSV and JSON File Generator utility
epic-accounting-system-export-foundation-task-005 — Build the CsvJsonFileGenerator infrastructure utility that serializes a list of structured accounting records into both CSV and JSON formats. For CSV: support configurable column headers and delimiters per target system. For JSON: emit a schema-valid document matching the intake format expected by Xledger and Dynamics. Upload generated files to Supabase Storage under a scoped path (exports/{orgId}/{runId}.csv and .json). Return signed download URLs with 24-hour expiry.
Acceptance Criteria
Technical Requirements
Implementation Notes
Place in lib/features/accounting/infrastructure/csv_json_file_generator.dart. For CSV generation, consider the csv pub.dev package (ListToCsvConverter) rather than manual string concatenation to correctly handle RFC 4180 escaping. For JSON schemas: obtain the exact intake format spec from Xledger and Dynamics API documentation before implementing — do not guess schema field names. If specs are unavailable, use a configurable Map
Run CPU-intensive serialization in a Flutter Isolate using compute() to keep the export UI responsive. The Supabase Storage bucket name ('exports') must be created in the Supabase dashboard with appropriate policies before upload calls will succeed.
Testing Requirements
Unit tests: Test generateCsv with 3 records — verify header row, data rows, correct delimiter, correct date format. Test CSV special character escaping: claimantName with commas and double-quotes. Test generateJson with Xledger config — verify JSON structure matches expected schema. Test generateJson with Dynamics config — verify different structure.
Test empty list — verify valid empty CSV and JSON output. Integration test (mocked Storage): Test uploadToStorage returns a string URL. Test generateAndUploadBoth calls upload twice and returns both URLs. Test upload failure on second file rolls back gracefully (or documents that it does not and cleanup is manual).
Adding exported_at and export_run_id columns to expense_claims requires a live migration on a table shared with the approval workflow. A poorly timed migration could lock the table and block claim submissions or approvals.
Mitigation & Contingency
Mitigation: Use non-blocking ADD COLUMN with a DEFAULT of NULL (no backfill needed) executed during a low-traffic window. Test migration rollback on a staging replica before production deployment.
Contingency: If migration causes table lock contention, roll back and reschedule for a maintenance window. Use a feature flag to gate the export UI until the migration completes successfully.
Chart of accounts mapping configurations for Xledger and Dynamics may not be fully specified by stakeholders at development time, leaving the mapper with incomplete data and causing validation failures for unmapped expense categories.
Mitigation & Contingency
Mitigation: Implement the mapper to return a structured validation error (not a crash) for any unmapped field, and surface these errors clearly in the export confirmation dialog. Request full mapping tables from Blindeforbundet and HLF stakeholders as a pre-condition for this epic.
Contingency: If mappings arrive incomplete, ship the mapper with the available subset and mark unmapped categories as excluded (skipped with reason). Coordinators see which categories are skipped and can manually submit those records.
Supabase Vault configuration for storing per-org accounting credentials may require infra permissions or environment secrets not yet provisioned in staging or production, blocking development and testing of credential retrieval.
Mitigation & Contingency
Mitigation: Provision Vault secrets and environment configuration in staging as the first task of this epic. Document the exact secret naming convention and rotation procedure before implementation begins.
Contingency: If Vault is unavailable, use environment variables scoped to the Edge Function as a temporary fallback for development. Block production deployment until Vault-based storage is confirmed operational.