feat: Implement entity comparison logic for improved sync detection between EspoCRM and Advoware

This commit is contained in:
2026-02-07 19:41:20 +00:00
parent 9076688f58
commit 101f290293
3 changed files with 121 additions and 16 deletions

View File

@@ -184,14 +184,10 @@ async def handle_update(entity_id, betnr, espo_entity, espocrm, advoware, sync_u
context.logger.info(f"📥 Von Advoware geladen: {advo_entity.get('name')}")
# TIMESTAMP-VERGLEICH
comparison = sync_utils.compare_timestamps(
espo_entity.get('modifiedAt'),
advo_entity.get('geaendertAm'),
espo_entity.get('advowareLastSync')
)
# ÄNDERUNGSERKENNUNG (Primary: rowId, Fallback: Timestamps)
comparison = sync_utils.compare_entities(espo_entity, advo_entity)
context.logger.info(f"⏱️ Timestamp-Vergleich: {comparison}")
context.logger.info(f"⏱️ Vergleich: {comparison}")
# SPECIAL: Wenn LastSync null → immer von EspoCRM syncen (initial sync)
if not espo_entity.get('advowareLastSync'):
@@ -206,7 +202,12 @@ async def handle_update(entity_id, betnr, espo_entity, espocrm, advoware, sync_u
data=merged_data
)
await sync_utils.release_sync_lock(entity_id, 'clean')
# Speichere rowId für zukünftige Vergleiche
await sync_utils.release_sync_lock(
entity_id,
'clean',
extra_fields={'advowareRowId': advo_entity.get('rowId')}
)
context.logger.info(f"✅ Advoware aktualisiert (initial sync)")
return
@@ -229,7 +230,16 @@ async def handle_update(entity_id, betnr, espo_entity, espocrm, advoware, sync_u
data=merged_data
)
await sync_utils.release_sync_lock(entity_id, 'clean')
# Hole aktualisierte Entity um neue rowId zu bekommen
updated_advo = await advoware.api_call(f'api/v1/advonet/Beteiligte/{betnr}', method='GET')
if isinstance(updated_advo, list):
updated_advo = updated_advo[0]
await sync_utils.release_sync_lock(
entity_id,
'clean',
extra_fields={'advowareRowId': updated_advo.get('rowId')}
)
context.logger.info(f"✅ Advoware aktualisiert")
# ADVOWARE NEUER → Update EspoCRM
@@ -239,7 +249,11 @@ async def handle_update(entity_id, betnr, espo_entity, espocrm, advoware, sync_u
espo_data = mapper.map_advoware_to_cbeteiligte(advo_entity)
await espocrm.update_entity('CBeteiligte', entity_id, espo_data)
await sync_utils.release_sync_lock(entity_id, 'clean')
await sync_utils.release_sync_lock(
entity_id,
'clean',
extra_fields={'advowareRowId': advo_entity.get('rowId')}
)
context.logger.info(f"✅ EspoCRM aktualisiert")
# KONFLIKT → EspoCRM WINS
@@ -255,6 +269,11 @@ async def handle_update(entity_id, betnr, espo_entity, espocrm, advoware, sync_u
data=merged_data
)
# Hole aktualisierte Entity um neue rowId zu bekommen
updated_advo = await advoware.api_call(f'api/v1/advonet/Beteiligte/{betnr}', method='GET')
if isinstance(updated_advo, list):
updated_advo = updated_advo[0]
conflict_msg = (
f"EspoCRM: {espo_entity.get('modifiedAt')}, "
f"Advoware: {advo_entity.get('geaendertAm')}. "
@@ -265,7 +284,8 @@ async def handle_update(entity_id, betnr, espo_entity, espocrm, advoware, sync_u
entity_id,
espo_entity,
advo_entity,
conflict_msg
conflict_msg,
extra_fields={'advowareRowId': updated_advo.get('rowId')}
)
context.logger.info(f"✅ Konflikt gelöst: EspoCRM → Advoware")