Implement Chart of Accounts Mapper
epic-accounting-system-export-foundation-task-004 — Build the ChartOfAccountsMapper that resolves org-specific mappings from expense_type and activity_type to accounting account codes. Load mapping configuration from a Supabase org_account_mappings table (or a JSON config per org stored in Supabase Storage). Expose a mapExpenseTypeToAccountCode(orgId, expenseType) method. Support separate mapping profiles per target system (Xledger vs Dynamics). Include a fallback to a default account code when no explicit mapping exists.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Place in lib/features/accounting/data/services/chart_of_accounts_mapper.dart. The in-memory cache can be a simple Map
JSON file in Storage: prefer a table for editability by coordinators; use Storage JSON only if account code management is done entirely by Norse Digital Products engineers. Coordinate with HLF and Blindeforbundet accounting teams to gather the initial account code mappings for Xledger and Dynamics before seeding data.
Testing Requirements
Unit tests: Test loadMappings populates internal cache correctly from mocked Supabase response. Test mapExpenseTypeToAccountCode with explicit match — returns correct code. Test with no explicit match but default exists — returns default code. Test with no match and no default — returns fallback constant and emits warning log.
Test that calling mapExpenseTypeToAccountCode before loadMappings throws a StateError with a clear message. Test Xledger vs Dynamics return different codes for the same expense_type. Aim for 100% branch coverage on the mapping resolution logic.
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.