Bufdir Export Edge Function
Component Detail
Description
Supabase Edge Function that accepts org_id, scope_id, scope_level, and date range parameters and returns either a preview payload or a finalised export. Runs server-side to handle large dataset aggregation without client-side memory issues.
bufdir-export-edge-function
Summaries
The Bufdir Export Edge Function is the operational core that transforms months of activity data into a compliant government report at the press of a button. By running server-side, it handles the heavy computational work that would otherwise crash or timeout on a mobile device, ensuring coordinators and administrators can reliably produce complete reports regardless of their internet speed or device capability. This directly reduces the time staff spend on reporting from hours to minutes, and eliminates the manual errors that come from preparing reports in spreadsheets. It also creates an automatic audit record with every export, providing the organisation with a defensible paper trail for all submissions.
This is the most integration-intensive component in the export feature, with five direct dependencies: the aggregation service, format serializer, PDF generator, CSV generator, and reports repository. It cannot be fully tested until all five dependencies are complete, so it should be scheduled at the end of the export pipeline sprint sequence. Plan for an integration testing phase of at least three days with real Supabase edge function deployment — local emulation often misses environment configuration issues. Key risks include cold-start latency for large datasets, edge function timeout limits (default 150s in Supabase), and format-specific failures in PDF/CSV generation.
Add a preview mode to the acceptance criteria to allow stakeholder sign-off before finalised exports are enabled.
Implemented as a Deno-based Supabase Edge Function. invoke() is the entry point — deserialise the JSON body, call validateRequest() with a Zod schema, then branch on the preview flag. For preview: call aggregation service + serializer + buildPreviewResponse() and return immediately without writing to storage. For full export: call aggregation, serializer, then the appropriate file generator based on format param, upload the result to Supabase Storage, call generated-reports-repository to persist the file URL and mark status as complete.
Wrap the entire handler in try/catch and call handleError() which writes a failed status to the repository and returns a structured error response. Set max execution time via the function's deploy config. Pass the Supabase service-role client via environment variables, never from the request.
Responsibilities
- Receive and validate export request parameters
- Invoke aggregation and mapping services
- Return structured JSON preview or trigger file generation
- Write audit record to generated_reports
Interfaces
invoke(orgId, scopeId, scopeLevel, dateRange, format, preview)
validateRequest(params)
buildPreviewResponse(aggregatedData)
buildExportResponse(file, reportId)
handleError(error)
Relationships
Dependencies (5)
Components this component depends on
Used Integrations (2)
External integrations and APIs this component relies on