Integrate approved claims query and chart-of-accounts field mapping
epic-accounting-system-export-orchestration-task-003 — Wire the ApprovedClaimsQueryService and ChartOfAccountsMapper calls into the AccountingExporterService orchestration sequence. The service must query all approved claims within the requested date range for the given org and pass them through the mapper before forwarding to the concrete exporter. Ensure proper typing and null-safety throughout.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Define MappedClaim as a distinct Dart class that extends or wraps ApprovedClaim — not a type alias — so the type system prevents passing unmapped claims to the exporter. Use Dart's extension types (Dart 3.3+) for zero-overhead wrapping if available, otherwise use a simple immutable wrapper class. The ChartOfAccountsMapper preloads the chart as a Map
Document the Supabase query used in ApprovedClaimsQueryService as a code comment (the exact .from().select().eq().gte().lte().is_() chain) to make it auditable. Use const constructors for MappedClaim fields that are known at compile time to improve performance in test suites with large fixture sets.
Testing Requirements
Unit tests (dart test) with mocked ApprovedClaimsQueryService and ChartOfAccountsMapper: (1) Normal path — 10 claims returned, all mapped, passed to exporter. (2) Empty claims — fetchApprovedClaims returns [], assert ExportResult.empty() and mapper/exporter never called. (3) Mapping failure for one claim — assert ExportResult.failure() with errorCode='CHART_OF_ACCOUNTS_MAPPING_ERROR' and failing claim ID in errorDetail. (4) Verify typed constraint — attempt to pass an ApprovedClaim directly to the exporter method and confirm compilation fails (type test).
Integration test with Supabase local emulator: seed expense_claims with mixed statuses, assert only approved+unexported claims are returned. Mapper integration test: seed chart_of_accounts table, verify mapping resolves correct accountCode per activity type.
The Edge Function may exceed Supabase's execution time limit (default 150 seconds, but effectively constrained by the 10-second client SLA) when processing large batches of claims with complex chart-of-accounts mapping, causing the export to fail after partial processing.
Mitigation & Contingency
Mitigation: Implement the export pipeline with early termination on timeout and an in-progress export run status. Add a benchmark test in CI that runs the full pipeline against 500 claims and fails if it exceeds 8 seconds. Optimize the approved claims query with indexes on status, org_id, and date fields.
Contingency: If performance targets cannot be met synchronously, convert the Edge Function to an async job pattern: the function queues the export and returns a job ID immediately; the client polls a status endpoint and downloads the file when ready. This requires a job queue table and a polling UI state.
Supabase Vault access from the Edge Function may require specific service role key configuration that differs between staging and production environments, causing credential retrieval to fail silently and producing export runs that appear successful but have no valid accounting system target.
Mitigation & Contingency
Mitigation: Test Vault read access in the Edge Function in staging before implementing any business logic. Add an explicit credential validation step at Edge Function startup that fails fast with a clear error if Vault is unreachable or the secret is missing.
Contingency: If Vault access fails in production, fall back to environment variable-based credentials temporarily (never returned to client) while the Vault configuration is corrected. Alert on-call via a monitoring rule that fires if credential retrieval fails.
AccountingExporter Service may become tightly coupled to specific exporter implementations if the factory pattern is not implemented cleanly, making it difficult to add a third exporter in the future without modifying the orchestrator.
Mitigation & Contingency
Mitigation: Define an AccountingExporter abstract class with a strict interface contract before implementing any concrete class. Use a registry pattern (Map<orgType, AccountingExporter>) in the factory rather than conditionals. Code review should verify no concrete class is imported directly in the orchestrator.
Contingency: If tight coupling is discovered after implementation, refactor the factory before the Edge Function epic ships so the interface is stable before any external callers are wired in.