Implement Generate Export Supabase Edge Function
epic-accounting-system-export-foundation-task-014 — Build the generate-export Supabase Edge Function that serves as the secure server-side entry point for all export operations. Accept a POST request with { orgId, dateRangeStart, dateRangeEnd, targetSystem } and a valid coordinator JWT. Retrieve org credentials from AccountingCredentialsVault, invoke the AccountingExporterService pipeline, and return the ExportRun result including file URL. Ensure the function is never callable from the Flutter client without a valid auth token. Add integration test covering the happy path and the double-export rejection path.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Structure the Edge Function handler as: (1) parse and validate Authorization header, (2) decode and verify JWT, (3) extract role and orgId from claims, (4) validate request body with a typed schema (Zod or manual Deno validation), (5) fetch credentials from vault, (6) instantiate and invoke AccountingExporterService, (7) map ExportResult to HTTP response. Keep each step in its own function for testability. Use Deno's built-in crypto for JWT verification rather than an external library to minimise cold-start time. The Edge Function should import AccountingExporterService as a shared module (placed in supabase/functions/_shared/) to avoid duplicating pipeline logic.
Ensure the function's Deno permissions are scoped minimally (--allow-net for Supabase, --allow-env for secrets). Deploy with supabase functions deploy generate-export and document the required environment variables in a .env.example file committed to the repo (with placeholder values only).
Testing Requirements
Integration tests using Supabase local development stack (supabase start): (1) Happy path — mint a valid coordinator JWT, POST valid body, assert HTTP 200 and ExportRun row in DB. (2) Double-export — repeat identical request, assert HTTP 409 and no new ExportRun. (3) Auth failure — POST without Authorization header, assert HTTP 401. (4) Role failure — use a peer mentor JWT, assert HTTP 403.
(5) Org mismatch — use a coordinator JWT for org A but request orgId of org B, assert HTTP 403. (6) Invalid body — missing dateRangeStart, assert HTTP 422. (7) Vault credential retrieval failure — mock vault to throw, assert HTTP 500 with errorCode but no credential content. All tests must be automated and run in CI on every PR touching the Edge Function.
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.