Threshold Evaluation Service
Component Detail
Description
Shared Dart utility implementing the threshold decision logic used both client-side (immediate UX feedback) and server-side (via Supabase Edge Function for authoritative validation). Computes the combined total of all expense lines on a claim and compares against the configured distance and amount thresholds to determine the approval path.
threshold-evaluation-service
Summaries
The Threshold Evaluation Service ensures that expense claim approval policies are applied consistently, accurately, and in real time — on the user's device before submission and authoritatively on the server during processing. By running identical logic in both contexts, it eliminates discrepancies between what a peer mentor sees on screen and the actual approval decision made by the system, building trust and reducing surprise rejections. Organisations can configure their own distance and amount thresholds, giving administrators direct control over which claims require human review versus automatic approval. This flexibility allows the platform to serve organisations of different sizes and policies without custom development.
As a shared component running across mobile, backend, and shared execution contexts, this service requires careful coordination to ensure the Dart implementation compiles and behaves identically in both the Flutter mobile app and the Supabase Edge Function (Dart runtime). It has no external dependencies, making it safe to develop and test early. The primary delivery risk is divergence between client-side and server-side behaviour — thorough parity testing with identical inputs is essential. Test cases must cover boundary conditions for both distance and amount thresholds, including edge cases where both limits are reached simultaneously.
Any change to threshold logic must be regression-tested across all three execution contexts simultaneously.
This shared Dart utility is designed for execution context portability — the same class runs in the Flutter mobile app and the Supabase Edge Function (Dart isolate). The primary method evaluateClaim(claim, thresholds) orchestrates computeCombinedTotal(expenseLines) and computeCombinedDistance(expenseLines) to aggregate all expense line values, then compares against organisation-specific thresholds via isAutoApprovable(). Threshold configuration is fetched via getThresholdConfig(organisationId), which must be injected or cached appropriately per execution context. The class must remain stateless and free of platform-specific imports to ensure portability.
Data model dependencies are expense_claim and expense_line. The shared execution context designation means any interface change requires coordinated updates in both Flutter and Edge Function call sites.
Responsibilities
- Sum all expense line amounts and distances for a given claim
- Compare combined totals against organisation-configured thresholds
- Return approval path decision (auto-approve vs manual review)
- Expose identical logic for both client and Edge Function execution contexts
Interfaces
evaluateClaim(claim, thresholds)
computeCombinedTotal(expenseLines)
computeCombinedDistance(expenseLines)
isAutoApprovable(claim, thresholds)
getThresholdConfig(organisationId)
Relationships
Dependents (2)
Components that depend on this component
Related Data Entities (3)
Data entities managed by this component