fix(sync): backfill address_mismatch for orders missing blue dot
Orders synced before address_mismatch was deployed had stale 0 values, causing missing blue dots in the dashboard. Adds startup backfill from stored address JSON + recomputes on each sync for ALREADY_IMPORTED orders. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import oracledb
|
||||
import aiosqlite
|
||||
import sqlite3
|
||||
import json
|
||||
import re
|
||||
import unicodedata
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -359,12 +362,69 @@ def init_sqlite():
|
||||
logger.info(f"Migrated orders: added column {col}")
|
||||
|
||||
conn.commit()
|
||||
|
||||
# Backfill address_mismatch from stored address JSON
|
||||
_backfill_address_mismatch(conn)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Migration check failed: {e}")
|
||||
|
||||
conn.close()
|
||||
logger.info(f"SQLite initialized: {_sqlite_db_path}")
|
||||
|
||||
|
||||
def _backfill_address_mismatch(conn):
|
||||
"""Recompute address_mismatch from stored address JSON for all orders."""
|
||||
_ADDR_WORDS = re.compile(
|
||||
r'\b(STR|STRADA|NR|NUMAR|NUMARUL|BL|BLOC|SC|SCARA|AP|APART|APARTAMENT|'
|
||||
r'ET|ETAJ|COM|COMUNA|SAT|MUN|MUNICIPIUL|JUD|JUDETUL|CARTIER|PARTER|SECTOR|ORAS)\b'
|
||||
)
|
||||
|
||||
def norm(s):
|
||||
s = unicodedata.normalize('NFD', s or '')
|
||||
s = re.sub(r'[\u0300-\u036f]', '', s).upper()
|
||||
s = _ADDR_WORDS.sub('', s)
|
||||
return re.sub(r'[^A-Z0-9]', '', s)
|
||||
|
||||
def addr_match(gomag_json, roa_json):
|
||||
if not gomag_json or not roa_json:
|
||||
return True
|
||||
try:
|
||||
g = json.loads(gomag_json) if isinstance(gomag_json, str) else gomag_json
|
||||
r = json.loads(roa_json) if isinstance(roa_json, str) else roa_json
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return True
|
||||
g_street = norm(g.get('address') or g.get('strada') or '')
|
||||
r_street = norm((r.get('strada') or '') + (r.get('numar') or ''))
|
||||
g_city = norm(g.get('city') or g.get('localitate') or '')
|
||||
r_city = norm(r.get('localitate') or '')
|
||||
g_region = norm(g.get('region') or g.get('judet') or '')
|
||||
r_region = norm(r.get('judet') or '')
|
||||
return g_street == r_street and g_city == r_city and g_region == r_region
|
||||
|
||||
try:
|
||||
rows = conn.execute("""
|
||||
SELECT order_number, adresa_livrare_gomag, adresa_livrare_roa,
|
||||
adresa_facturare_gomag, adresa_facturare_roa
|
||||
FROM orders
|
||||
WHERE adresa_livrare_roa IS NOT NULL OR adresa_facturare_roa IS NOT NULL
|
||||
""").fetchall()
|
||||
updated = 0
|
||||
for r in rows:
|
||||
livr_ok = addr_match(r[1], r[2])
|
||||
fact_ok = addr_match(r[3], r[4])
|
||||
new_val = 1 if (not livr_ok or not fact_ok) else 0
|
||||
conn.execute(
|
||||
"UPDATE orders SET address_mismatch = ? WHERE order_number = ?",
|
||||
(new_val, r[0])
|
||||
)
|
||||
updated += 1
|
||||
if updated:
|
||||
conn.commit()
|
||||
logger.info(f"Backfill address_mismatch: {updated} orders recomputed")
|
||||
except Exception as e:
|
||||
logger.warning(f"Backfill address_mismatch failed: {e}")
|
||||
|
||||
async def get_sqlite():
|
||||
"""Get async SQLite connection."""
|
||||
if _sqlite_db_path is None:
|
||||
|
||||
Reference in New Issue
Block a user