Implement coordinator approve and reject transitions
epic-expense-approval-workflow-core-logic-task-004 — Implement approveClaim() and rejectClaim() methods on ApprovalWorkflowService for the manual approval path. Each method must validate that the claim is in the submitted state, verify the acting user holds the coordinator role, persist the decision with actor identity and comment via ClaimApprovalDecisionRepository, and update claim status via ExpenseClaimStatusRepository.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Implement approveClaim() and rejectClaim() as separate methods on ApprovalWorkflowServiceImpl; they share common validation logic (state check + role check) that should be extracted into a private _validateCoordinatorTransition(claimId, coordinatorUserId) method returning Either
Add dartdoc to both methods explicitly stating the preconditions (claim must be in submitted state, actor must be coordinator) so future callers understand the contract.
Testing Requirements
Unit tests (flutter_test): mock UserRoleRepository, ClaimApprovalDecisionRepository, ExpenseClaimStatusRepository, ClaimAuditEventRepository. For approveClaim(): test (1) valid coordinator + submitted claim → returns approved state; (2) non-coordinator user → returns insufficientPermissions; (3) non-submitted claim → returns invalidState; (4) repository failure → returns repositoryError; (5) audit event recorded with coordinatorApproved type; (6) decision record has correct actor and optional comment. For rejectClaim(): mirror the same cases plus (7) justification text is persisted correctly; (8) justification over 1000 chars is rejected. Integration test: create a submitted claim in Supabase test environment, call approveClaim(), assert DB state is approved with correct coordinator ID and a matching audit event row.
The ThresholdEvaluationService is described as shared Dart logic used both client-side and in the Edge Function. Supabase Edge Functions run Deno/TypeScript, not Dart, meaning the threshold logic must be maintained in two languages and can diverge, causing the server to reject legitimate client submissions.
Mitigation & Contingency
Mitigation: Implement the threshold logic as a single TypeScript module in the Edge Function and call it via a thin Dart HTTP client wrapper for client-side preview feedback only. The server is always authoritative; the client version is purely for UX (showing the user whether their claim will auto-approve before they submit).
Contingency: If dual-language maintenance is unavoidable, create a shared golden test file (JSON fixtures with inputs and expected outputs) that is run against both implementations in CI to detect divergence immediately.
A peer mentor could double-tap the submit button or a network retry could trigger a duplicate submission, causing the ApprovalWorkflowService to attempt two concurrent state transitions from draft→submitted for the same claim, potentially resulting in two audit events or conflicting statuses.
Mitigation & Contingency
Mitigation: Implement idempotency in the ApprovalWorkflowService using a database-level unique constraint on (claim_id, from_status, to_status) per transition, combined with a UI-level submission lock (disable button after first tap until response returns).
Contingency: Add a deduplication check at the start of every state transition method that returns the existing state if an identical transition is already in progress or completed within the last 10 seconds.
Claims with multiple expense lines (e.g., mileage + parking) must have their combined total evaluated against the threshold. If individual lines are added asynchronously or the evaluation runs before all lines are persisted, the auto-approval decision may be computed on an incomplete set of expense lines.
Mitigation & Contingency
Mitigation: The Edge Function always fetches all expense lines from the database (not from the client payload) before computing the threshold decision. Define a clear claim submission contract that requires all expense lines to be persisted before the submit action is called.
Contingency: Add a validation step in ApprovalWorkflowService that counts expected vs. persisted expense lines before allowing the transition, returning a validation error if lines are missing.