Implement Dynamics Exporter service
epic-accounting-system-export-foundation-task-011 — Build the DynamicsExporter service that transforms ApprovedClaim objects into Microsoft Dynamics 365-compatible accounting records using ChartOfAccountsMapper and CsvJsonFileGenerator. Map fields to Dynamics journal line format: transactionDate, ledgerAccount, amount, currency, description, workerDimension, and projectDimension. Support the HLF Dynamics portal intake schema. Validate all required fields before file generation. Return a typed ExportResult with file URL and record count.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Mirror the XledgerExporter architecture but with a DynamicsRecordMapper producing DynamicsJournalLine objects. The key difference from Xledger: Dynamics uses a single-row-per-claim format (no double-entry pair) with amount as a signed decimal (positive for expenses). Define DynamicsJournalLine as an immutable Dart class with the 7 required fields. Use the same pipeline pattern: map → validate → serialize → upload → record → return.
The ChartOfAccountsMapper should be capable of resolving both Xledger account codes and Dynamics ledger account / dimension codes from the same org configuration — confirm this is in scope when ChartOfAccountsMapper is implemented (task-004). Reuse CsvJsonFileGenerator from task-005 with a DynamicsColumnSchema configuration object. Keep the two exporters as sibling classes implementing a shared abstract AccountingExporter interface — this allows the Accounting Exporter Service to call them polymorphically. The interface: Future
Store Dynamics-specific field length constants (60 char description limit) in a DynamicsExportConstants class rather than magic numbers.
Testing Requirements
Unit tests (flutter_test with mocked dependencies): test DynamicsRecordMapper.toDynamicsJournalLine(claim) for mileage and toll claim types; assert currency is 'NOK'; assert description truncation at 60 chars; assert missing ledger account moves claim to unresolvable list; assert zero-amount claim triggers validation failure. Integration test: invoke full export() with 5 mock ApprovedClaims against test Supabase Storage; verify file created and export_runs record has status 'completed'. Test file: test/services/dynamics_exporter_test.dart. Mock ChartOfAccountsMapper and CsvJsonFileGenerator.
Confirm DynamicsExporter and XledgerExporter share no logic beyond the injected ChartOfAccountsMapper and CsvJsonFileGenerator — they must remain independently testable.
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.