Define ReportHistoryRecord domain model and freezed types
epic-bufdir-report-history-foundation-task-005 — Define the ReportHistoryRecord immutable data class using freezed in Dart. Include all fields matching the database schema: id, organizationId, reportPeriodStart, reportPeriodEnd, submittedByUserId, submittedAt, status (enum: draft, submitted, acknowledged), filePath, fileSizeBytes, checksum, metadata Map, createdAt, updatedAt. Add fromJson/toJson serialization. Define ReportStatus enum. Export from a single barrel file.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Use `@freezed` annotation with `part 'report_history_record.freezed.dart'` and `part 'report_history_record.g.dart'`. Use `@JsonKey(name: 'organization_id')` annotations on all fields to map camelCase Dart names to snake_case database column names — this is critical for Supabase compatibility. For DateTime parsing, use `@JsonKey(fromJson: _dateTimeFromJson, toJson: _dateTimeToJson)` with helper functions that handle ISO 8601 strings. For ReportStatus, define a custom `@JsonKey(unknownEnumValue: ReportStatus.draft)` to handle unexpected values gracefully.
The metadata field should use `@Default({})` in the freezed factory to avoid null. Add the model to the `pubspec.yaml` build_runner watch list. Do not add UI-specific logic (display names, color mappings) to domain models — those belong in presentation layer extension methods.
Testing Requirements
Write unit tests with flutter_test: (1) fromJson parses a complete valid JSON map correctly for all fields; (2) fromJson parses a JSON map with all nullable fields set to null without throwing; (3) fromJson correctly parses ReportStatus from each valid string ('draft', 'submitted', 'acknowledged'); (4) fromJson throws or returns a safe default when status is an unrecognized string — document and test the chosen behavior; (5) toJson round-trip: `ReportHistoryRecord.fromJson(record.toJson()) == record`; (6) copyWith correctly overrides individual fields while preserving others; (7) Two records with identical field values are equal (== returns true). Run with `flutter test test/features/bufdir_report/domain/models/`.
Incorrectly authored RLS policies could silently allow cross-organization data reads, exposing sensitive report history of one organization to coordinators of another in a multi-tenant environment.
Mitigation & Contingency
Mitigation: Write integration tests that explicitly authenticate as a user from organization A and assert zero rows are returned for organization B's history records. Use Supabase's built-in RLS testing utilities and review policies with a second developer.
Contingency: If a cross-tenant leak is discovered post-deployment, immediately revoke all active sessions for affected organizations, audit query logs for unauthorized access, and patch the RLS policy in a hotfix migration before re-enabling access.
The 5-year retention policy for report files may conflict with Supabase Storage's lack of native lifecycle rules, requiring a custom pg_cron job that could fail silently and either delete files prematurely or never clean up.
Mitigation & Contingency
Mitigation: Implement the retention cleanup as a documented pg_cron job with explicit logging to a separate audit_jobs table. Add a Supabase Edge Function health check that verifies the cron job ran within the last 25 hours.
Contingency: If the cron job fails, files accumulate in storage (non-critical for compliance — over-retention is safer than under-retention). Alert the ops team via monitoring and manually trigger the cleanup function once the cron issue is resolved.
Signed URL generation depends on the requesting user's Supabase session being valid at the time of the call. If sessions expire during a long screen interaction, URL generation will fail with an authorization error and confuse the coordinator.
Mitigation & Contingency
Mitigation: Wrap signed URL generation in the service layer with a session-refresh check before calling Supabase Storage. Generate URLs on demand (tap-to-download) rather than pre-generating them for all list items on screen load.
Contingency: If a URL generation fails due to session expiry, surface a clear error message prompting the coordinator to re-authenticate, then automatically retry URL generation after session refresh completes.