Barcode Matching
Purpose
Resolve barcode → company; persist barcode meta
Inputs
barcode, device_id (JWT)
Outputs
company id + isBoycott flag
Tables
barcodes
tip
For the end‑to‑end request/response, start with Flows → Scan Barcode.
Versioning Context
This document explains how a barcode scanned by the Flutter app is processed and matched to a boycott (or alternative) company.
- v3 implements barcode storage and matching in Turso (tables under
src/lib/dbs/schema/schema.ts). - Legacy tables like
upc_barcodeand whitelist data are used as migration sources.
Summary
- Entry point: user scan route receives a numeric barcode.
- Normalization/validation: barcode is cleaned to digits and validated (EAN-13/8, UPC-A/E logic).
- Lookup tiers:
- Whitelist barcode lookup (barcode → whitelist → company name match).
- External enrichment (Go-UPC, Open Food Facts) persisted to
barcodestable.
- On match: return
{ id, isBoycott }.
Entry Point (Scan)
- Route:
GET /v3/users/:device_id/scan/:barcode- Validates the barcode via
ScanParams(numeric string, min 4 digits). - Calls
lookupBarcode(barcode)which queries the whitelist tables. - Schedules
cacheBarcodeFromProvidersto enrich from external sources asynchronously. - Files:
src/routes/users.tssrc/schemas/user.schema.tssrc/services/barcode.service.ts
- Validates the barcode via
Normalization & Validation
- Non-digit characters are stripped before any lookup.
- Supported barcodes and check digits:
- EAN-13 and EAN-8
- UPC-A (12 digits)
- UPC-E (6 or 8 digits) via expansion to UPC-A
- Unsupported lengths throw an error during validation.
- File:
src/utils/barcode.ts
Primary Lookup
- Whitelist-based lookup via
whitelist_barcodes+whiteliststables.- Finds barcode in
whitelist_barcodes, joins towhitelistsforparentCompanyName. - Matches
parentCompanyNameagainstboycott_companiesoralternative_companies. - Returns
{ id: number, isBoycott: boolean }.
- Finds barcode in
- External providers (Go-UPC, Open Food Facts) run asynchronously via
cacheBarcodeFromProviders.- Results cached in single
barcodestable for future lookups.
- Results cached in single
- File:
src/services/barcode.service.ts(lookupBarcode,cacheBarcodeFromProviders)
External Enrichment (Async)
- After initial lookup,
cacheBarcodeFromProviders(barcode, env)runs asynchronously. - Queries configured providers (Go-UPC, Open Food Facts) for product/brand data.
- On hit, caches result in the unified
barcodestable withproductName,brandName. - File:
src/services/barcode.service.ts(cacheBarcodeFromProviders,resolveBarcode)
Open Tasks (WIP)
- Add Elasticsearch-based enrichment fallback for unmatched barcodes.
- Implement user reward point adjustment on successful scan.
Code references
- Validation utils:
src/utils/barcode.ts - Service:
src/services/barcode.service.ts(lookupBarcode,cacheBarcodeFromProviders) - Tables:
src/lib/dbs/schema/schema.ts(barcodes table) - Routing:
src/routes/users.ts(scan route)