Implement batch-mode duplicate detection
epic-proxy-activity-registration-core-services-task-003 — Extend proxy-duplicate-detection-service with a batch variant that accepts a list of composite keys and returns a map of key to conflict results. The batch implementation must use a single Supabase RPC call or IN-clause query to avoid N+1 queries. This supports the bulk registration flow where coordinators submit multiple peer mentor activities simultaneously.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Two viable implementation strategies: (1) Use Supabase RPC with a PostgreSQL function that accepts a JSON array of composite keys and returns matching records — preferred for complex multi-column IN matching. (2) Use PostgREST OR filter chains if Supabase client supports it. Strategy 1 is recommended because multi-column IN-clause is not natively supported in PostgREST without RPC. The PostgreSQL function signature should be: check_duplicate_batch(keys jsonb) RETURNS TABLE(...).
After fetching results, group rows in Dart by their composite key using a Map
Testing Requirements
Unit tests with mocked Supabase client: empty input, single item batch, large batch (20 items), batch with mix of conflicting and clean keys, batch where all keys conflict, batch size limit exceeded, network error mid-batch. Verify exactly one query is issued per call using mock call count assertions. Integration test against Supabase test project with pre-seeded conflicting records. Performance test: simulate 20-key batch and assert completion under 1 second.
Use flutter_test with mocktail.
Overly strict duplicate matching (exact date + type) may flag legitimate back-to-back sessions of the same activity type on the same day as duplicates, frustrating coordinators and undermining trust in the feature.
Mitigation & Contingency
Mitigation: Confirm with product owners whether the matching key should be (mentor_id, date, activity_type_id) only or should also consider duration and time-of-day. Document the chosen threshold in the service and surface it in the duplicate warning dialog for transparency.
Contingency: If false-positive rates are high in user testing, add a duration-window tolerance parameter to the detection query that can be tuned without code changes.
If the current session token is invalidated between the coordinator starting the proxy form and submitting it, the activity-attribution-service may fail to resolve the coordinator's user ID, causing a silent attribution error.
Mitigation & Contingency
Mitigation: Read the coordinator's user ID from the session at service call time rather than at form-open time. Validate the session is still active before committing the insert, and surface a clear re-authentication prompt if it has expired.
Contingency: If a mis-attributed record is detected post-submission, the audit log retains the original session metadata, allowing a corrective record to be created with accurate attribution.