feat(scanner): region-preferred product matching in api_record_scan (#926)#938
Merged
ericsocrat merged 1 commit intomainfrom Mar 17, 2026
Merged
Conversation
…926) - Prefer same-country product when EAN exists in multiple countries - Exclude deprecated products from scan lookup - Add is_cross_country flag to found response - Fix COMMENT ambiguity on api_admin_get_submissions overload (#925) - Fix pre-existing pgTAP bugs: NULL user_id trigger crash, invalid session_replication_role - 8 new pgTAP tests (plan 72→80): dual-EAN preference, cross-country flag, deprecated exclusion
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Implements smarter barcode scan lookup in the Supabase scanner RPC by preferring same-country matches when multiple products share an EAN, excluding deprecated products, and exposing cross-country context to clients.
Changes:
- Update
public.api_record_scan()to (a) prefer same-country matches, (b) exclude deprecated products, and (c) returnis_cross_country. - Fix
api_admin_get_submissionsoverload/comment ambiguity by dropping the stale 3-arg overload and qualifying the function signature inCOMMENT ON. - Extend pgTAP coverage for cross-country behavior and document the updated response contract.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| supabase/tests/scanner_functions.test.sql | Expands pgTAP coverage for is_cross_country, region-preferred matching, deprecated exclusion, and adjusts plan/replication-role usage. |
| supabase/migrations/20260321000200_region_preferred_scan_matching.sql | Updates api_record_scan lookup/response, excludes deprecated products, adds is_cross_country, and removes stale api_admin_get_submissions overload. |
| supabase/migrations/20260321000100_admin_submissions_country_context.sql | Qualifies the COMMENT ON FUNCTION signature to avoid overload ambiguity. |
| docs/API_CONTRACTS.md | Documents region-preferred matching behavior and the new is_cross_country response key. |
Comment on lines
+57
to
+63
| -- Validate EAN format | ||
| IF p_ean IS NULL OR LENGTH(TRIM(p_ean)) NOT IN (8, 13) THEN | ||
| RETURN jsonb_build_object( | ||
| 'api_version', '1.0', | ||
| 'error', 'EAN must be 8 or 13 digits' | ||
| ); | ||
| END IF; |
Comment on lines
+81
to
+83
| v_scan_country := p_scan_country; | ||
| IF v_scan_country IS NULL AND v_user_id IS NOT NULL THEN | ||
| SELECT up.country INTO v_scan_country |
Comment on lines
+92
to
+103
| -- When v_scan_country IS NULL, (p.country = NULL) evaluates to NULL (FALSE), | ||
| -- so ORDER BY degrades to product_id only — stable backward compat. | ||
| SELECT p.product_id, p.product_name, p.product_name_en, p.name_translations, | ||
| p.brand, p.category, p.country, p.unhealthiness_score, p.nutri_score_label | ||
| INTO v_product | ||
| FROM public.products p | ||
| WHERE p.ean = TRIM(p_ean) | ||
| AND p.is_deprecated IS NOT TRUE | ||
| ORDER BY (p.country = v_scan_country) DESC, | ||
| p.product_id | ||
| LIMIT 1; | ||
|
|
Comment on lines
+634
to
+639
| -- Cross-country fallback: user in XX scans PL-only EAN | ||
| SELECT is( | ||
| (public.api_record_scan('5901234123457', 'PL'))->>'found', | ||
| 'true', | ||
| 'cross-country fallback: PL user still finds XX-only product (#926)' | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements issue #926 — region-preferred product matching in
api_record_scan.When a barcode exists in multiple countries (e.g., both PL and DE), the function now prefers the product whose
countrymatches the user's resolvedscan_country. Deprecated products are excluded from lookup.Changes
Migration:
20260321000200_region_preferred_scan_matching.sqlThree changes to
api_record_scan:AND p.is_deprecated IS NOT TRUEin EAN lookup WHERE clauseORDER BY (p.country = v_scan_country) DESC, p.product_id— same-country products sort first, with deterministic tiebreakis_cross_country(boolean) added to found response —truewhen matched product's country differs from scan_country and scan_country is not NULLAlso fixes stale 3-param
api_admin_get_submissionsoverload from #925 (DROP + re-COMMENT with qualified signature).Bug fix:
20260321000100_admin_submissions_country_context.sqlFixed
COMMENT ON FUNCTIONambiguity (SQLSTATE 42725) — qualified with parameter types(text, integer, integer, text).pgTAP tests:
scanner_functions.test.sqlis_cross_countrykey exists, cross-country flag values, DE/PL preference with dual-EAN, cross-country fallback, deprecated exclusionsession_replication_rolein section 14Docs:
API_CONTRACTS.mdis_cross_countryto found response schemaAcceptance Criteria (from #926)
api_record_scanreturns same-region product when EAN exists in both PL and DEis_cross_countryboolean flag in found responseproduct_idwhen no country preferenceVerification
File Impact
4 files changed, +274 / -7 lines
is_cross_countryadded)Closes #926