fix(partner-mismatch): fix 3 infinite-loop bugs in mismatch detection cycle
Three root causes caused partner_mismatch=1 to loop indefinitely: 1. No-CUI company orders (is_pj=1, no cod_fiscal): old code flagged as mismatch every cycle. Fixed by requiring new_cf to be non-null for PF→PJ detection. Stale flags from old code cleared via new clear_stale_partner_mismatches_no_cui() for out-of-window orders. 2. same_partner resync path did not update cod_fiscal_gomag in SQLite. On next cycle GoMag returned a CUI but stored_cf was still NULL → re-detected as mismatch. Fixed by also calling update_partner_resync_data (not just update_partner_mismatch_batch) in the same_partner branch. 3. GoMag sends CUI with space: 'RO 17922480'. The _strip_ro() regex ^RO left the space → ' 17922480' != '17922480' → false mismatch. Fixed: changed regex to ^RO\s* and added .strip(). Also adds diagnostic logger.info lines for mismatch detection/resync counts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1440,6 +1440,36 @@ async def update_partner_mismatch_batch(updates: list) -> None:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def clear_stale_partner_mismatches_no_cui(exclude_numbers: set) -> int:
|
||||
"""Clear partner_mismatch=1 for orders with cod_fiscal_gomag=NULL that are NOT in the
|
||||
current sync batch. These were flagged by old code (before the no-CUI fix) and will
|
||||
never self-correct because they fall outside the active sync window.
|
||||
Returns number of rows cleared.
|
||||
"""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
if exclude_numbers:
|
||||
placeholders = ",".join("?" * len(exclude_numbers))
|
||||
sql = f"""
|
||||
UPDATE orders SET partner_mismatch = 0, updated_at = datetime('now')
|
||||
WHERE partner_mismatch = 1
|
||||
AND cod_fiscal_gomag IS NULL
|
||||
AND order_number NOT IN ({placeholders})
|
||||
"""
|
||||
await db.execute(sql, list(exclude_numbers))
|
||||
else:
|
||||
await db.execute("""
|
||||
UPDATE orders SET partner_mismatch = 0, updated_at = datetime('now')
|
||||
WHERE partner_mismatch = 1 AND cod_fiscal_gomag IS NULL
|
||||
""")
|
||||
await db.commit()
|
||||
cursor = await db.execute("SELECT changes()")
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row else 0
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def update_partner_resync_data(order_number: str, data: dict) -> None:
|
||||
"""Update partner fields + clear partner_mismatch after a successful resync."""
|
||||
db = await get_sqlite()
|
||||
|
||||
Reference in New Issue
Block a user