diff --git a/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py b/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py index 47f52c38..a0267497 100644 --- a/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py +++ b/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py @@ -481,17 +481,40 @@ async def handler(event, context): elif not adv_exists: # Missing in Advoware strategy = row['sync_strategy'] - if strategy == 'source_system_wins' and row['source_system'] == 'advoware': - # Recreate in Google - try: - new_event_id = await create_google_event(service, calendar_id, standardize_appointment_data(google_map[event_id], 'google')) - async with conn.transaction(): - await conn.execute("UPDATE calendar_sync SET google_event_id = $1, sync_status = 'synced' WHERE sync_id = $2;", new_event_id, row['sync_id']) - logger.info(f"Phase 3: Recreated Google event {new_event_id} for sync_id {row['sync_id']}") - except Exception as e: - logger.warning(f"Phase 3: Failed to recreate 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']) + if strategy == 'source_system_wins': + if row['source_system'] == 'advoware': + # 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']) + 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: # Propagate delete to Google try: