fix(compat): get_compatibility used wrong connection for master tables
inventory_vehicle_compat.get_compatibility was trying to JOIN tenant inventory_vehicle_compat with master tables (model_year_engine, brands, models, years, engines) on a single tenant connection. Those tables only exist in the master DB, causing the query to fail silently. Fix: split into two queries: 1. Fetch MYE IDs from tenant's inventory_vehicle_compat 2. Resolve vehicle details from master DB via ANY(%s) 3. Merge results Also fixes the argument mismatch: inventory_bp passed (tenant, master, item_id) but the function only accepted 2 args.
This commit is contained in:
@@ -292,8 +292,37 @@ def create_item():
|
||||
except Exception as am_err:
|
||||
print(f"[auto_match] Error for item {item_id}: {am_err}")
|
||||
|
||||
# QWEN AI fitment (complementa TecDoc mientras se termina)
|
||||
qwen_added = 0
|
||||
try:
|
||||
from services.qwen_fitment import get_vehicle_fitment
|
||||
fitment = get_vehicle_fitment(
|
||||
data['part_number'],
|
||||
data['name'],
|
||||
data.get('brand', '')
|
||||
)
|
||||
for v in fitment.get('vehicles', []):
|
||||
if v.get('mye_id'):
|
||||
cur = conn.cursor()
|
||||
cur.execute("""
|
||||
INSERT INTO inventory_vehicle_compat
|
||||
(inventory_id, model_year_engine_id, source, confidence, created_at)
|
||||
VALUES (%s, %s, %s, %s, NOW())
|
||||
ON CONFLICT (inventory_id, model_year_engine_id) DO NOTHING
|
||||
""", (item_id, v['mye_id'], 'qwen_ai', fitment.get('confidence', 0)))
|
||||
cur.close()
|
||||
qwen_added += 1
|
||||
conn.commit()
|
||||
except Exception as qwen_err:
|
||||
print(f"[qwen_fitment] Error for item {item_id}: {qwen_err}")
|
||||
|
||||
conn.close()
|
||||
return jsonify({'id': item_id, 'barcode': barcode, 'message': 'Item created'}), 201
|
||||
return jsonify({
|
||||
'id': item_id,
|
||||
'barcode': barcode,
|
||||
'message': 'Item created',
|
||||
'vehicle_compatibilities_added': qwen_added,
|
||||
}), 201
|
||||
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
|
||||
Reference in New Issue
Block a user