Request/response transformation middleware
epic-external-system-integration-configuration-backend-infrastructure-task-004 — Build the transformation middleware layer within the Edge Function that converts internal data models into external API formats before dispatch, and normalizes external API responses into a standardized integration result schema. This includes serialization for JSON and CSV payloads, date format normalization, and currency/amount precision handling required by Xledger and Dynamics.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Handles integration between different epics or system components. Requires coordination across multiple development streams.
Implementation Notes
Implement as a dedicated transformers/ directory inside the Edge Function with one file per adapter (xledger-transformer.ts, dynamics-transformer.ts, bufdir-transformer.ts) plus a shared types.ts. Use discriminated union types for IntegrationResult to make error handling exhaustive at compile time. For date handling, always convert to UTC before serialization β do not assume server timezone. For Bufdir CSV, read column order dynamically from bufdir_column_schema at runtime rather than hardcoding, since schema versions can change.
Currency: use integer arithmetic (store as ΓΈre/cents internally) to avoid floating-point drift, only convert to decimal at serialization boundary. Keep transformers stateless and free of I/O so they can be tested without Supabase dependencies.
Testing Requirements
Write unit tests in Deno test framework covering: (1) Xledger transformer β valid activity record produces correct JSON structure; (2) Dynamics transformer β expense claim maps OData type annotations correctly; (3) Bufdir CSV transformer β column_mappings from bufdir_column_schema drive column order; (4) Date normalizer β input in Norway timezone (Europe/Oslo) serializes to correct UTC ISO string; (5) Currency precision β 0.005 rounds to 0.01, 0.015 rounds to 0.02 (banker's rounding); (6) TransformationError thrown for missing required field. Integration tests: invoke Edge Function locally with Supabase CLI and assert output payload matches snapshot. Target 90% line coverage on transformer module.
Supabase Edge Functions have cold start latency that can cause the first sync invocation after idle periods to fail or timeout when the external API has a short connection window, leading to missed scheduled syncs that go undetected.
Mitigation & Contingency
Mitigation: Configure Edge Function memory and implement a warm-up ping mechanism before heavy sync invocations. Set generous timeout values on the external API calls. Log all cold-start incidents for monitoring.
Contingency: If cold starts cause consistent sync failures, migrate the sync scheduler to a persistent Supabase cron job that pre-warms the function 30 seconds before the scheduled sync time.
The sync scheduler must execute jobs at predictable times for financial reporting accuracy. Drift in cron execution timing (due to Supabase infrastructure delays) could cause syncs to run at wrong times, leading to missing data in accounting exports or duplicate exports across reporting periods.
Mitigation & Contingency
Mitigation: Implement idempotency keys based on integration ID + scheduled period, so re-runs of a delayed sync cannot create duplicate exports. Log actual execution timestamps vs scheduled timestamps and alert on drift exceeding 5 minutes.
Contingency: If scheduler reliability is insufficient, integrate with a dedicated cron service (e.g., pg_cron on Supabase) for millisecond-precise scheduling, replacing the application-level scheduler.
Aggressive health monitoring ping frequency could trigger rate limiting on external APIs (especially Xledger and Dynamics), causing legitimate export calls to fail after the monitor exhausts the API's request quota.
Mitigation & Contingency
Mitigation: Use lightweight health check endpoints (HEAD requests or vendor-specific ping/status endpoints) rather than data requests. Set health check frequency to once per 15 minutes minimum. Implement exponential backoff after consecutive failures.
Contingency: If rate limiting occurs, disable active health monitoring for the affected integration type and switch to passive health detection (mark unhealthy only when a scheduled sync fails).