fix(barcode): look up product info before checking auto_add_to_inventory
Previously, get_or_create_product was only called when auto_add was true, so scan responses with auto_add=false returned no product details. Now the DB lookup always runs when product_info is available; inventory insertion is still conditional on auto_add_to_inventory. Fixes preview-only barcode scans returning empty product fields.
This commit is contained in:
parent
e57f46f4b6
commit
8c765b7da2
1 changed files with 25 additions and 22 deletions
|
|
@ -478,7 +478,8 @@ async def scan_barcode_image(
|
||||||
from app.services.openfoodfacts import OpenFoodFactsService
|
from app.services.openfoodfacts import OpenFoodFactsService
|
||||||
from app.services.expiration_predictor import ExpirationPredictor
|
from app.services.expiration_predictor import ExpirationPredictor
|
||||||
|
|
||||||
barcodes = await asyncio.to_thread(BarcodeScanner().scan_image, temp_file)
|
image_bytes = temp_file.read_bytes()
|
||||||
|
barcodes = await asyncio.to_thread(BarcodeScanner().scan_from_bytes, image_bytes)
|
||||||
if not barcodes:
|
if not barcodes:
|
||||||
return BarcodeScanResponse(
|
return BarcodeScanResponse(
|
||||||
success=False, barcodes_found=0, results=[],
|
success=False, barcodes_found=0, results=[],
|
||||||
|
|
@ -500,9 +501,10 @@ async def scan_barcode_image(
|
||||||
product_info = await off.lookup_product(code)
|
product_info = await off.lookup_product(code)
|
||||||
product_source = "openfoodfacts"
|
product_source = "openfoodfacts"
|
||||||
|
|
||||||
|
db_product = None
|
||||||
inventory_item = None
|
inventory_item = None
|
||||||
if product_info and auto_add_to_inventory:
|
if product_info:
|
||||||
product, _ = await asyncio.to_thread(
|
db_product, _ = await asyncio.to_thread(
|
||||||
store.get_or_create_product,
|
store.get_or_create_product,
|
||||||
product_info.get("name", code),
|
product_info.get("name", code),
|
||||||
code,
|
code,
|
||||||
|
|
@ -512,29 +514,30 @@ async def scan_barcode_image(
|
||||||
source=product_source,
|
source=product_source,
|
||||||
source_data=product_info,
|
source_data=product_info,
|
||||||
)
|
)
|
||||||
exp = predictor.predict_expiration(
|
if auto_add_to_inventory:
|
||||||
product_info.get("category", ""),
|
exp = predictor.predict_expiration(
|
||||||
location,
|
product_info.get("category", ""),
|
||||||
product_name=product_info.get("name", code),
|
location,
|
||||||
tier=session.tier,
|
product_name=product_info.get("name", code),
|
||||||
has_byok=session.has_byok,
|
tier=session.tier,
|
||||||
)
|
has_byok=session.has_byok,
|
||||||
resolved_qty = product_info.get("pack_quantity") or quantity
|
)
|
||||||
resolved_unit = product_info.get("pack_unit") or "count"
|
resolved_qty = product_info.get("pack_quantity") or quantity
|
||||||
inventory_item = await asyncio.to_thread(
|
resolved_unit = product_info.get("pack_unit") or "count"
|
||||||
store.add_inventory_item,
|
inventory_item = await asyncio.to_thread(
|
||||||
product["id"], location,
|
store.add_inventory_item,
|
||||||
quantity=resolved_qty,
|
db_product["id"], location,
|
||||||
unit=resolved_unit,
|
quantity=resolved_qty,
|
||||||
expiration_date=str(exp) if exp else None,
|
unit=resolved_unit,
|
||||||
source="barcode_scan",
|
expiration_date=str(exp) if exp else None,
|
||||||
)
|
source="barcode_scan",
|
||||||
product_found = product_info is not None
|
)
|
||||||
|
product_found = db_product is not None
|
||||||
needs_capture = not product_found and has_visual_capture
|
needs_capture = not product_found and has_visual_capture
|
||||||
results.append({
|
results.append({
|
||||||
"barcode": code,
|
"barcode": code,
|
||||||
"barcode_type": bc.get("type", "unknown"),
|
"barcode_type": bc.get("type", "unknown"),
|
||||||
"product": ProductResponse.model_validate(product_info) if product_info else None,
|
"product": ProductResponse.model_validate(db_product) if db_product else None,
|
||||||
"inventory_item": InventoryItemResponse.model_validate(inventory_item) if inventory_item else None,
|
"inventory_item": InventoryItemResponse.model_validate(inventory_item) if inventory_item else None,
|
||||||
"added_to_inventory": inventory_item is not None,
|
"added_to_inventory": inventory_item is not None,
|
||||||
"needs_manual_entry": not product_found and not needs_capture,
|
"needs_manual_entry": not product_found and not needs_capture,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue