Build Field Mapping Resolver JSONB loader
epic-external-system-integration-configuration-core-services-task-003 — Implement the FieldMappingResolver service responsible for loading per-organization JSONB field mapping configurations from the database. Must support lazy loading, caching with TTL, and cache invalidation on config update. Resolves org-specific field name aliases (e.g., Blindeforbundet Xledger field names vs HLF Dynamics field schema).
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Handles integration between different epics or system components. Requires coordination across multiple development streams.
Implementation Notes
Implement the cache as a Map
Keep the parsing logic in a separate static method for easy unit testing. Expose via Riverpod as an AsyncNotifierProvider or a simple Provider wrapping the class instance. Document clearly that this resolver only loads mappings — applying them to payloads is handled in task-004.
Testing Requirements
Unit tests using flutter_test with a mocked Supabase client. Tests: (1) first load triggers one database fetch, (2) second load within TTL returns cached result with zero database calls, (3) load after TTL expiry triggers a new database fetch, (4) invalidate() causes next load() to re-fetch, (5) missing mapping returns empty FieldMapping, (6) malformed JSONB returns empty FieldMapping and logs warning, (7) concurrent load() calls for same key result in exactly one database fetch (no thundering herd). Use a FakeAsync or manual clock to control TTL expiry in tests without real delays.
Each of the five external systems (Xledger, Dynamics, Cornerstone, Consio, Bufdir) has a different authentication flow, field schema, and error format. Forcing them into a uniform adapter interface may require compromises that result in leaky abstractions or make the adapter contract too complex to maintain.
Mitigation & Contingency
Mitigation: Design the IntegrationAdapter interface with a loose invoke() payload rather than a typed one, allowing each adapter to declare its own input/output schema. Use integration type metadata in the registry to document per-adapter quirks. Build Xledger first as the most documented API, then adapt the interface based on learnings.
Contingency: If the uniform interface cannot accommodate all five systems, split into two interface tiers: a simple polling/export adapter and a richer bidirectional adapter, with the registry declaring which tier each system implements.
Development and testing of the Cornerstone and Consio adapters depends on NHF providing sandbox API access. If credentials or documentation are delayed, these adapters cannot be validated, blocking the epic's acceptance criteria.
Mitigation & Contingency
Mitigation: Implement Xledger and Dynamics adapters first (better-documented, sandbox available). Create a mock adapter for Cornerstone/Consio using recorded API responses for CI testing. Proactively request sandbox access from NHF at project kickoff.
Contingency: Ship the epic with Cornerstone/Consio adapters in a 'stub' state (connectivity test returns a simulated success, invoke() is not production-wired) and gate the NHF integration behind a feature flag until real API access is obtained.
Real-world field mappings may include nested transformations, conditional logic, and data type coercions (e.g., Norwegian date formats, currency rounding rules) that the Field Mapping Resolver's initial design does not accommodate, requiring scope expansion mid-epic.
Mitigation & Contingency
Mitigation: Gather actual field mapping examples from Blindeforbundet (Xledger) and HLF (Dynamics) before designing the resolver. Identify the most complex transformation required and ensure the resolver design handles it. Limit Phase 1 to direct field renaming and format conversion only.
Contingency: If complex transformations are required, implement a simple expression evaluator (e.g., JSONata or a custom mini-DSL) as an extension point in the resolver, delivering basic mappings first and complex ones in a follow-up task.