feat(scanner): pass user region through api_record_scan and api_submit_product (#923)#935
Merged
ericsocrat merged 1 commit intomainfrom Mar 17, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Adds country context capture to the scanner + submission RPCs by introducing optional country parameters, persisting resolved country into the new schema columns, and documenting/testing the updated contracts as part of the “Country-Aware Scanner” epic.
Changes:
- Add
scan_country/suggested_countrycolumns (with FKs + indexes) toscan_historyandproduct_submissions. - Update
api_record_scanandapi_submit_productRPCs to accept country params, resolve fromuser_preferenceswhen absent, persist to tables, and return country keys in JSON responses. - Expand pgTAP coverage and update API contracts documentation for the new signatures/response fields.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
supabase/migrations/20260320000100_scan_history_country.sql |
Adds scan_history.scan_country (FK to country_ref) + partial index and column comment. |
supabase/migrations/20260320000200_submissions_country.sql |
Adds product_submissions.scan_country + suggested_country (FKs), partial indexes, and comments. |
supabase/migrations/20260320000300_country_aware_scanner_rpcs.sql |
Updates api_record_scan / api_submit_product signatures + response shape and persists resolved country values. |
supabase/tests/schema_contracts.test.sql |
Adds schema + function signature contract assertions and adjusts plan count. |
supabase/tests/scanner_functions.test.sql |
Adds tests for new scan_country / product_country response keys and explicit param behavior. |
docs/API_CONTRACTS.md |
Documents new RPC params, country resolution, and response examples. |
Comment on lines
+62
to
+68
| 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 | ||
| FROM public.user_preferences up | ||
| WHERE up.user_id = v_user_id; | ||
| END IF; | ||
|
|
Comment on lines
+251
to
+261
| -- Resolve scan_country: explicit param → user_preferences → NULL | ||
| v_scan_country := p_scan_country; | ||
| IF v_scan_country IS NULL THEN | ||
| SELECT up.country INTO v_scan_country | ||
| FROM public.user_preferences up | ||
| WHERE up.user_id = v_uid; | ||
| END IF; | ||
|
|
||
| -- Resolve suggested_country: explicit param → scan_country → NULL | ||
| v_suggested_country := COALESCE(p_suggested_country, v_scan_country); | ||
|
|
|
|
||
| | Name | Type | Default | Description | | ||
| | ---------------- | ---- | ------- | --------------------------------------------------------------------------------------------------------------- | | ||
| | `p_ean` | text | — | Barcode (EAN-8 or EAN-13). Validated via `is_valid_ean()`. | |
| } | ||
| ``` | ||
|
|
||
| **Error cases:** invalid EAN checksum, rate limit (100/24h per user). |
Comment on lines
195
to
+218
| -- ─── 7. Whitespace trimming ──────────────────────────────────────────────── | ||
|
|
||
| -- ─── 6b. Explicit scan_country parameter (#923) ─────────────────────────── | ||
|
|
||
| SELECT is( | ||
| (public.api_record_scan('5901234123457', 'PL'))->>'scan_country', | ||
| 'PL', | ||
| 'explicit p_scan_country=PL is returned in response (#923)' | ||
| ); | ||
|
|
||
| SELECT is( | ||
| (public.api_record_scan('5901234123457', 'PL'))->>'product_country', | ||
| 'XX', | ||
| 'product_country reflects fixture product country XX (#923)' | ||
| ); | ||
|
|
||
| SELECT is( | ||
| (public.api_record_scan('5901234123457'))->>'scan_country', | ||
| NULL, | ||
| 'scan_country is NULL when no param and no auth (#923)' | ||
| ); | ||
|
|
||
| -- ─── 7 (cont). Whitespace trimming ──────────────────────────────────────── | ||
|
|
…t_product (#923) - api_record_scan: new p_scan_country text DEFAULT NULL param Country resolution: p_scan_country > user_preferences.country Response adds: scan_country, product_country keys - api_submit_product: new p_scan_country, p_suggested_country params Country resolution: scan_country from param or prefs suggested_country from param or defaults to resolved scan_country Response adds: scan_country, suggested_country keys - DROP old 1-param api_record_scan and 6-param api_submit_product - All new params DEFAULT NULL for backward compatibility - 6 new pgTAP tests in scanner_functions.test.sql (plan 64->70) - 2 new schema contract assertions (plan 303->305) - API_CONTRACTS.md: document both RPC signatures under §8
342f19f to
ac0de76
Compare
This was referenced Mar 17, 2026
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 #923 — the third issue in epic #920 (Country-Aware Scanner).
Modifies
api_record_scanandapi_submit_productRPC functions to accept explicit country parameters, resolve country from user preferences when not provided, and return country information in their responses.Depends on: PR #933 (issue #921) and PR #934 (issue #922) — both merged into this branch.
Changes
Migration:
20260320000300_country_aware_scanner_rpcs.sqlapi_record_scan(p_ean text, p_scan_country text DEFAULT NULL)(text)— 1 param(text, text)— 2 paramsp_scan_country DEFAULT NULLCOALESCE(p_scan_country, user_preferences.country)scan_countrycolumnscan_country,product_countryscan_countryCountry resolution order:
p_scan_countryparameter (if provided)user_preferences.country(if user is authenticated and has preferences)NULL(anonymous users or no preference set)api_submit_product(p_ean, p_product_name, p_brand, p_category, p_photo_url, p_notes, p_scan_country, p_suggested_country)(text ×6)— 6 params(text ×8)— 8 paramsp_scan_country,p_suggested_countryDEFAULT NULLscan_country,suggested_countryscan_country,suggested_countryOld function signatures are dropped via
DROP FUNCTION IF EXISTS.Tests
scanner_functions.test.sql— plan64→70(+6 tests):scan_countryandproduct_countrykeys exist in found responsescan_countrykey exists in not-found responseschema_contracts.test.sql— plan303→305(+2 assertions):has_functionforapi_record_scan(text, text)— 2-param signaturehas_functionforapi_submit_product(text ×8)— 8-param signatureDocumentation
docs/API_CONTRACTS.md— new subsections under §8 (Barcode Scanner):api_record_scan— PostgREST call, parameters table, country resolution, response examples (found/not-found/error)api_submit_product— 8-param signature, parameters table, country resolution, response examples (success/error)Backward Compatibility
All new parameters have
DEFAULT NULL. Existing callers that pass only the original parameters continue to work identically — country fields returnnullwhen not provided and user has no preferences.Response keys are additive only. No existing keys were removed or renamed.
Verification
File Impact
4 files changed, +428 / -2 lines:
Acceptance Criteria (from issue #923)
api_record_scan(p_ean, p_scan_country DEFAULT NULL)— new signatureapi_submit_product(…, p_scan_country, p_suggested_country DEFAULT NULL)— new signature