Fix Phase 3 delete logic for source_system_wins: propagate delete to Google when Advoware appointment is missing and source is Advoware, instead of recreating.

This commit is contained in:
root
2025-10-23 10:42:49 +00:00
parent 9ab90fef5a
commit ed669f8561

View File

@@ -481,17 +481,40 @@ async def handler(event, context):
elif not adv_exists: elif not adv_exists:
# Missing in Advoware # Missing in Advoware
strategy = row['sync_strategy'] strategy = row['sync_strategy']
if strategy == 'source_system_wins' and row['source_system'] == 'advoware': if strategy == 'source_system_wins':
# Recreate in Google if row['source_system'] == 'advoware':
try: # Propagate delete to Google
new_event_id = await create_google_event(service, calendar_id, standardize_appointment_data(google_map[event_id], 'google')) try:
async with conn.transaction(): await delete_google_event(service, calendar_id, event_id)
await conn.execute("UPDATE calendar_sync SET google_event_id = $1, sync_status = 'synced' WHERE sync_id = $2;", new_event_id, row['sync_id']) async with conn.transaction():
logger.info(f"Phase 3: Recreated Google event {new_event_id} for sync_id {row['sync_id']}") await conn.execute("UPDATE calendar_sync SET deleted = TRUE, sync_status = 'synced' WHERE sync_id = $1;", row['sync_id'])
except Exception as e: logger.info(f"Phase 3: Propagated delete to Google for sync_id {row['sync_id']}")
logger.warning(f"Phase 3: Failed to recreate Google for sync_id {row['sync_id']}: {e}") except Exception as e:
async with conn.transaction(): logger.warning(f"Phase 3: Failed to delete Google for sync_id {row['sync_id']}: {e}")
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id']) async with conn.transaction():
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
elif row['source_system'] == 'google' and row['advoware_write_allowed']:
# Recreate in Advoware
try:
new_frnr = await safe_create_advoware_appointment(advoware, standardize_appointment_data(google_map[event_id], 'google'), employee_kuerzel, row['advoware_write_allowed'])
async with conn.transaction():
await conn.execute("UPDATE calendar_sync SET advoware_frnr = $1, sync_status = 'synced' WHERE sync_id = $2;", int(new_frnr), row['sync_id'])
logger.info(f"Phase 3: Recreated Advoware appointment {new_frnr} for sync_id {row['sync_id']}")
except Exception as e:
logger.warning(f"Phase 3: Failed to recreate Advoware for sync_id {row['sync_id']}: {e}")
async with conn.transaction():
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
else:
# For other cases, propagate delete to Google
try:
await delete_google_event(service, calendar_id, event_id)
async with conn.transaction():
await conn.execute("UPDATE calendar_sync SET deleted = TRUE, sync_status = 'synced' WHERE sync_id = $1;", row['sync_id'])
logger.info(f"Phase 3: Propagated delete to Google for sync_id {row['sync_id']}")
except Exception as e:
logger.warning(f"Phase 3: Failed to delete Google for sync_id {row['sync_id']}: {e}")
async with conn.transaction():
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
else: else:
# Propagate delete to Google # Propagate delete to Google
try: try: