Enhance KommunikationSyncManager and Sync Event Step
- Improved bidirectional synchronization logic in KommunikationSyncManager: - Added initial sync handling to prevent duplicates. - Optimized hash calculation to only write changes when necessary. - Enhanced conflict resolution with clearer logging and handling of various scenarios. - Refactored diff computation for better clarity and maintainability. - Updated beteiligte_sync_event_step to ensure proper lock management: - Added error handling for entity fetching and retry logic. - Improved logging for better traceability of sync actions. - Ensured lock release in case of unexpected errors.
This commit is contained in:
@@ -44,9 +44,12 @@ class KommunikationSyncManager:
|
||||
Bidirektionale Synchronisation mit intelligentem Diffing
|
||||
|
||||
Optimiert:
|
||||
- Lädt Daten nur 1x von jeder Seite
|
||||
- Lädt Daten nur 1x von jeder Seite (kein doppelter API-Call)
|
||||
- Echtes 3-Way Diffing (Advoware, EspoCRM, Marker)
|
||||
- Handhabt alle 6 Szenarien korrekt
|
||||
- Handhabt alle 6 Szenarien korrekt (Var1-6)
|
||||
- Initial Sync: Value-Matching verhindert Duplikate (BUG-3 Fix)
|
||||
- Hash nur bei Änderung schreiben (Performance)
|
||||
- Lock-Release garantiert via try/finally
|
||||
|
||||
Args:
|
||||
direction: 'both', 'to_espocrm', 'to_advoware'
|
||||
@@ -60,6 +63,9 @@ class KommunikationSyncManager:
|
||||
'summary': {'total_changes': 0}
|
||||
}
|
||||
|
||||
# NOTE: Lock-Management erfolgt außerhalb dieser Methode (in Event/Cron Handler)
|
||||
# Diese Methode ist für die reine Sync-Logik zuständig
|
||||
|
||||
try:
|
||||
# ========== LADE DATEN NUR 1X ==========
|
||||
self.logger.info(f"[KOMM] Bidirectional Sync: betnr={betnr}, bet_id={beteiligte_id}")
|
||||
@@ -106,63 +112,65 @@ class KommunikationSyncManager:
|
||||
|
||||
# ========== APPLY CHANGES ==========
|
||||
|
||||
# Bestimme Sync-Richtungen und Konflikt-Handling
|
||||
sync_to_espocrm = direction in ['both', 'to_espocrm']
|
||||
sync_to_advoware = direction in ['both', 'to_advoware']
|
||||
should_revert_advoware_changes = (sync_to_espocrm and espo_wins) or (direction == 'to_advoware')
|
||||
|
||||
# 1. Advoware → EspoCRM (Var4: Neu in Advoware, Var6: Geändert in Advoware)
|
||||
# WICHTIG: Bei Konflikt (espo_wins=true) KEINE Advoware-Änderungen übernehmen!
|
||||
if direction in ['both', 'to_espocrm'] and not espo_wins:
|
||||
if sync_to_espocrm and not espo_wins:
|
||||
self.logger.info(f"[KOMM] ✅ Applying Advoware→EspoCRM changes...")
|
||||
espo_result = await self._apply_advoware_to_espocrm(
|
||||
beteiligte_id, diff, advo_bet
|
||||
)
|
||||
result['advoware_to_espocrm'] = espo_result
|
||||
elif direction in ['both', 'to_espocrm'] and espo_wins:
|
||||
self.logger.info(f"[KOMM] ⚠️ CONFLICT: EspoCRM wins - skipping Advoware→EspoCRM sync")
|
||||
|
||||
# Bei Konflikt oder direction='to_advoware': Revert Advoware-Änderungen
|
||||
if should_revert_advoware_changes:
|
||||
if espo_wins:
|
||||
self.logger.info(f"[KOMM] ⚠️ CONFLICT: EspoCRM wins - reverting Advoware changes")
|
||||
else:
|
||||
self.logger.info(f"[KOMM] ℹ️ Direction={direction}: reverting Advoware changes")
|
||||
|
||||
# FIX: Bei Konflikt müssen Var6-Änderungen (Advoware changed) revertiert werden!
|
||||
# Var6: Revert Änderungen
|
||||
if len(diff['advo_changed']) > 0:
|
||||
self.logger.info(f"[KOMM] 🔄 Reverting {len(diff['advo_changed'])} Var6 entries to EspoCRM values (EspoCRM wins)...")
|
||||
self.logger.info(f"[KOMM] 🔄 Reverting {len(diff['advo_changed'])} Var6 entries to EspoCRM values...")
|
||||
for komm, old_value, new_value in diff['advo_changed']:
|
||||
await self._revert_advoware_change(betnr, komm, old_value, new_value, advo_bet)
|
||||
result['espocrm_to_advoware']['updated'] += 1
|
||||
|
||||
# FIX: Bei Konflikt müssen Var4-Einträge (neu in Advoware) zu Empty Slots gemacht werden!
|
||||
# Sonst bleiben sie in Advoware aber nicht in EspoCRM → Nicht synchron!
|
||||
# Var4: Convert to Empty Slots
|
||||
if len(diff['advo_new']) > 0:
|
||||
self.logger.info(f"[KOMM] 🔄 Converting {len(diff['advo_new'])} Var4 entries to Empty Slots (EspoCRM wins)...")
|
||||
for komm in diff['advo_new']:
|
||||
await self._create_empty_slot(betnr, komm)
|
||||
result['espocrm_to_advoware']['deleted'] += 1
|
||||
|
||||
else:
|
||||
self.logger.info(f"[KOMM] ℹ️ Skipping Advoware→EspoCRM (direction={direction})")
|
||||
|
||||
# FIX: Bei direction='to_advoware' müssen Var6-Änderungen (Advoware changed)
|
||||
# zurückgesetzt werden auf EspoCRM-Wert!
|
||||
if direction == 'to_advoware' and len(diff['advo_changed']) > 0:
|
||||
self.logger.info(f"[KOMM] 🔄 Reverting {len(diff['advo_changed'])} Var6 entries to EspoCRM values (EspoCRM wins)...")
|
||||
for komm, old_value, new_value in diff['advo_changed']:
|
||||
# Revert: new_value (Advoware) → old_value (EspoCRM synced value)
|
||||
await self._revert_advoware_change(betnr, komm, old_value, new_value, advo_bet)
|
||||
result['espocrm_to_advoware']['updated'] += 1
|
||||
|
||||
# Bei direction='to_advoware' müssen auch Var4-Einträge (neu in Advoware)
|
||||
# zu Empty Slots gemacht werden!
|
||||
if direction == 'to_advoware' and len(diff['advo_new']) > 0:
|
||||
self.logger.info(f"[KOMM] 🔄 Converting {len(diff['advo_new'])} Var4 entries to Empty Slots (EspoCRM wins)...")
|
||||
self.logger.info(f"[KOMM] 🔄 Converting {len(diff['advo_new'])} Var4 entries to Empty Slots...")
|
||||
for komm in diff['advo_new']:
|
||||
await self._create_empty_slot(betnr, komm)
|
||||
result['espocrm_to_advoware']['deleted'] += 1
|
||||
|
||||
# 2. EspoCRM → Advoware (Var1: Neu in EspoCRM, Var2: Gelöscht in EspoCRM, Var5: Geändert in EspoCRM)
|
||||
if direction in ['both', 'to_advoware']:
|
||||
if sync_to_advoware:
|
||||
advo_result = await self._apply_espocrm_to_advoware(
|
||||
betnr, diff, advo_bet
|
||||
)
|
||||
# FIX: Merge statt überschreiben (Var6/Var4 Counts aus else-Block behalten)
|
||||
# Merge results (Var6/Var4 Counts aus Konflikt-Handling behalten)
|
||||
result['espocrm_to_advoware']['created'] += advo_result['created']
|
||||
result['espocrm_to_advoware']['updated'] += advo_result['updated']
|
||||
result['espocrm_to_advoware']['deleted'] += advo_result['deleted']
|
||||
result['espocrm_to_advoware']['errors'].extend(advo_result['errors'])
|
||||
|
||||
# 3. Initial Sync Matches: Nur Marker setzen (keine CREATE/UPDATE)
|
||||
if is_initial_sync and 'initial_sync_matches' in diff:
|
||||
self.logger.info(f"[KOMM] ✓ Processing {len(diff['initial_sync_matches'])} initial sync matches...")
|
||||
for value, matched_komm, espo_item in diff['initial_sync_matches']:
|
||||
# Erkenne kommKz
|
||||
espo_type = espo_item.get('type', 'email' if '@' in value else None)
|
||||
kommkz = detect_kommkz(value, advo_bet, espo_type=espo_type)
|
||||
# Setze Marker in Advoware
|
||||
await self.advoware.update_kommunikation(betnr, matched_komm['id'], {
|
||||
'bemerkung': create_marker(value, kommkz),
|
||||
'online': espo_item.get('primary', False)
|
||||
})
|
||||
result['espocrm_to_advoware']['updated'] += 1
|
||||
|
||||
total_changes = (
|
||||
result['advoware_to_espocrm']['emails_synced'] +
|
||||
result['advoware_to_espocrm']['phones_synced'] +
|
||||
@@ -172,38 +180,44 @@ class KommunikationSyncManager:
|
||||
)
|
||||
result['summary']['total_changes'] = total_changes
|
||||
|
||||
# Speichere neuen Kommunikations-Hash in EspoCRM (für nächsten Sync)
|
||||
# WICHTIG: Auch beim initialen Sync oder wenn keine Änderungen
|
||||
if total_changes > 0 or is_initial_sync:
|
||||
# Re-berechne Hash nach allen Änderungen
|
||||
# Hash-Update: Immer berechnen, aber nur schreiben wenn geändert
|
||||
import hashlib
|
||||
|
||||
# FIX: Nur neu laden wenn Änderungen gemacht wurden
|
||||
if total_changes > 0:
|
||||
advo_result_final = await self.advoware.get_beteiligter(betnr)
|
||||
if isinstance(advo_result_final, list):
|
||||
advo_bet_final = advo_result_final[0]
|
||||
else:
|
||||
advo_bet_final = advo_result_final
|
||||
|
||||
import hashlib
|
||||
final_kommunikationen = advo_bet_final.get('kommunikation', [])
|
||||
|
||||
# FIX #3: Nur sync-relevante Kommunikationen für Hash verwenden
|
||||
# (nicht leere Slots oder nicht-sync-relevante Einträge)
|
||||
sync_relevant_komm = [
|
||||
k for k in final_kommunikationen
|
||||
if should_sync_to_espocrm(k)
|
||||
]
|
||||
|
||||
komm_rowids = sorted([k.get('rowId', '') for k in sync_relevant_komm if k.get('rowId')])
|
||||
new_komm_hash = hashlib.md5(''.join(komm_rowids).encode()).hexdigest()[:16]
|
||||
|
||||
else:
|
||||
# Keine Änderungen: Verwende cached data (keine doppelte API-Call)
|
||||
final_kommunikationen = advo_bet.get('kommunikation', [])
|
||||
|
||||
# Berechne neuen Hash
|
||||
sync_relevant_komm = [
|
||||
k for k in final_kommunikationen
|
||||
if should_sync_to_espocrm(k)
|
||||
]
|
||||
komm_rowids = sorted([k.get('rowId', '') for k in sync_relevant_komm if k.get('rowId')])
|
||||
new_komm_hash = hashlib.md5(''.join(komm_rowids).encode()).hexdigest()[:16]
|
||||
|
||||
# Nur schreiben wenn Hash sich geändert hat oder Initial Sync
|
||||
if new_komm_hash != stored_komm_hash:
|
||||
await self.espocrm.update_entity('CBeteiligte', beteiligte_id, {
|
||||
'kommunikationHash': new_komm_hash
|
||||
})
|
||||
self.logger.info(f"[KOMM] ✅ Updated kommunikationHash: {new_komm_hash} (based on {len(sync_relevant_komm)} sync-relevant of {len(final_kommunikationen)} total)")
|
||||
self.logger.info(f"[KOMM] ✅ Updated kommunikationHash: {stored_komm_hash} → {new_komm_hash} (based on {len(sync_relevant_komm)} sync-relevant of {len(final_kommunikationen)} total)")
|
||||
else:
|
||||
self.logger.info(f"[KOMM] ℹ️ Hash unchanged: {new_komm_hash} - no EspoCRM update needed")
|
||||
|
||||
self.logger.info(f"[KOMM] ✅ Bidirectional Sync complete: {total_changes} total changes")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler bei Bidirectional Sync: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler bei Bidirectional Sync: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
result['advoware_to_espocrm']['errors'].append(str(e))
|
||||
result['espocrm_to_advoware']['errors'].append(str(e))
|
||||
|
||||
@@ -214,170 +228,242 @@ class KommunikationSyncManager:
|
||||
def _compute_diff(self, advo_kommunikationen: List[Dict], espo_emails: List[Dict],
|
||||
espo_phones: List[Dict], advo_bet: Dict, espo_bet: Dict) -> Dict[str, List]:
|
||||
"""
|
||||
Berechnet Diff zwischen Advoware und EspoCRM mit Kommunikations-Hash-basierter Konflikt-Erkennung
|
||||
|
||||
Da die Beteiligte-rowId sich NICHT bei Kommunikations-Änderungen ändert,
|
||||
nutzen wir einen Hash aus allen Kommunikations-rowIds + EspoCRM modifiedAt.
|
||||
Berechnet Diff zwischen Advoware und EspoCRM mit Hash-basierter Konflikt-Erkennung
|
||||
|
||||
Returns:
|
||||
{
|
||||
'advo_changed': [(komm, old_value, new_value)], # Var6: In Advoware geändert
|
||||
'advo_new': [komm], # Var4: Neu in Advoware (ohne Marker)
|
||||
'advo_deleted': [(value, item)], # Var3: In Advoware gelöscht (via Hash)
|
||||
'espo_changed': [(value, advo_komm)], # Var5: In EspoCRM geändert
|
||||
'espo_new': [(value, item)], # Var1: Neu in EspoCRM (via Hash)
|
||||
'espo_deleted': [advo_komm], # Var2: In EspoCRM gelöscht
|
||||
'no_change': [(value, komm, item)] # Keine Änderung
|
||||
}
|
||||
Dict mit Var1-6 Änderungen und Konflikt-Status
|
||||
"""
|
||||
diff = {
|
||||
'advo_changed': [],
|
||||
'advo_new': [],
|
||||
'advo_deleted': [], # NEU: Var3
|
||||
'espo_changed': [],
|
||||
'espo_new': [],
|
||||
'espo_deleted': [],
|
||||
'advo_changed': [], # Var6
|
||||
'advo_new': [], # Var4
|
||||
'advo_deleted': [], # Var3
|
||||
'espo_changed': [], # Var5
|
||||
'espo_new': [], # Var1
|
||||
'espo_deleted': [], # Var2
|
||||
'no_change': [],
|
||||
'espo_wins': False # Default
|
||||
'espo_wins': False
|
||||
}
|
||||
|
||||
# Hole Sync-Metadaten für Konflikt-Erkennung
|
||||
# 1. Konflikt-Erkennung
|
||||
is_initial_sync, espo_wins, espo_changed_since_sync, advo_changed_since_sync = \
|
||||
self._detect_conflict(advo_kommunikationen, espo_bet)
|
||||
diff['espo_wins'] = espo_wins
|
||||
|
||||
# 2. Baue Value-Maps
|
||||
espo_values = self._build_espocrm_value_map(espo_emails, espo_phones)
|
||||
advo_with_marker, advo_without_marker = self._build_advoware_maps(advo_kommunikationen)
|
||||
|
||||
# 3. Analysiere Advoware-Einträge MIT Marker
|
||||
self._analyze_advoware_with_marker(advo_with_marker, espo_values, diff)
|
||||
|
||||
# 4. Analysiere Advoware-Einträge OHNE Marker (Var4) + Initial Sync Matching
|
||||
self._analyze_advoware_without_marker(
|
||||
advo_without_marker, espo_values, is_initial_sync, advo_bet, diff
|
||||
)
|
||||
|
||||
# 5. Analysiere EspoCRM-Einträge die nicht in Advoware sind (Var1/Var3)
|
||||
self._analyze_espocrm_only(
|
||||
espo_values, advo_with_marker, espo_wins,
|
||||
espo_changed_since_sync, advo_changed_since_sync, diff
|
||||
)
|
||||
|
||||
return diff
|
||||
|
||||
def _detect_conflict(self, advo_kommunikationen: List[Dict], espo_bet: Dict) -> Tuple[bool, bool, bool, bool]:
|
||||
"""
|
||||
Erkennt Konflikte via Hash-Vergleich
|
||||
|
||||
Returns:
|
||||
(is_initial_sync, espo_wins, espo_changed_since_sync, advo_changed_since_sync)
|
||||
"""
|
||||
espo_modified = espo_bet.get('modifiedAt')
|
||||
last_sync = espo_bet.get('advowareLastSync')
|
||||
stored_komm_hash = espo_bet.get('kommunikationHash')
|
||||
|
||||
# Berechne Hash aus Kommunikations-rowIds
|
||||
# FIX #3: Nur sync-relevante Kommunikationen für Hash verwenden
|
||||
# Berechne aktuellen Hash
|
||||
import hashlib
|
||||
sync_relevant_komm = [
|
||||
k for k in advo_kommunikationen
|
||||
if should_sync_to_espocrm(k)
|
||||
]
|
||||
sync_relevant_komm = [k for k in advo_kommunikationen if should_sync_to_espocrm(k)]
|
||||
komm_rowids = sorted([k.get('rowId', '') for k in sync_relevant_komm if k.get('rowId')])
|
||||
current_advo_hash = hashlib.md5(''.join(komm_rowids).encode()).hexdigest()[:16]
|
||||
stored_komm_hash = espo_bet.get('kommunikationHash')
|
||||
|
||||
# Parse Timestamps
|
||||
from services.beteiligte_sync_utils import BeteiligteSync
|
||||
espo_modified_ts = BeteiligteSync.parse_timestamp(espo_modified)
|
||||
last_sync_ts = BeteiligteSync.parse_timestamp(last_sync)
|
||||
|
||||
# Bestimme wer geändert hat
|
||||
# Bestimme Änderungen
|
||||
espo_changed_since_sync = espo_modified_ts and last_sync_ts and espo_modified_ts > last_sync_ts
|
||||
advo_changed_since_sync = stored_komm_hash and current_advo_hash != stored_komm_hash
|
||||
|
||||
# Initial Sync: Wenn kein Hash gespeichert ist, behandle als "keine Änderung in Advoware"
|
||||
is_initial_sync = not stored_komm_hash
|
||||
|
||||
# Konflikt-Logik: Beide geändert → EspoCRM wins
|
||||
espo_wins = espo_changed_since_sync and advo_changed_since_sync
|
||||
|
||||
self.logger.info(f"[KOMM] 🔍 Konflikt-Check:")
|
||||
self.logger.info(f"[KOMM] - EspoCRM changed: {espo_changed_since_sync} (modified={espo_modified}, lastSync={last_sync})")
|
||||
self.logger.info(f"[KOMM] - Advoware changed: {advo_changed_since_sync} (stored_hash={stored_komm_hash}, current_hash={current_advo_hash})")
|
||||
self.logger.info(f"[KOMM] - Initial sync: {is_initial_sync}")
|
||||
self.logger.info(f"[KOMM] - Kommunikation rowIds count: {len(komm_rowids)}")
|
||||
self.logger.info(f"[KOMM] - EspoCRM changed: {espo_changed_since_sync}, Advoware changed: {advo_changed_since_sync}")
|
||||
self.logger.info(f"[KOMM] - Initial sync: {is_initial_sync}, Conflict: {espo_wins}")
|
||||
self.logger.info(f"[KOMM] - Hash: stored={stored_komm_hash}, current={current_advo_hash}")
|
||||
|
||||
if espo_changed_since_sync and advo_changed_since_sync:
|
||||
self.logger.warn(f"[KOMM] ⚠️ KONFLIKT: Beide Seiten geändert seit letztem Sync - EspoCRM WINS")
|
||||
espo_wins = True
|
||||
else:
|
||||
espo_wins = False
|
||||
|
||||
# Speichere espo_wins im diff für spätere Verwendung
|
||||
diff['espo_wins'] = espo_wins
|
||||
|
||||
# Baue EspoCRM Value Map
|
||||
return is_initial_sync, espo_wins, espo_changed_since_sync, advo_changed_since_sync
|
||||
|
||||
def _build_espocrm_value_map(self, espo_emails: List[Dict], espo_phones: List[Dict]) -> Dict[str, Dict]:
|
||||
"""Baut Map: value → {value, is_email, primary, type}"""
|
||||
espo_values = {}
|
||||
|
||||
for email in espo_emails:
|
||||
val = email.get('emailAddress', '').strip()
|
||||
if val:
|
||||
espo_values[val] = {'value': val, 'is_email': True, 'primary': email.get('primary', False), 'type': 'email'}
|
||||
espo_values[val] = {
|
||||
'value': val,
|
||||
'is_email': True,
|
||||
'primary': email.get('primary', False),
|
||||
'type': 'email'
|
||||
}
|
||||
|
||||
for phone in espo_phones:
|
||||
val = phone.get('phoneNumber', '').strip()
|
||||
if val:
|
||||
espo_values[val] = {'value': val, 'is_email': False, 'primary': phone.get('primary', False), 'type': phone.get('type', 'Office')}
|
||||
espo_values[val] = {
|
||||
'value': val,
|
||||
'is_email': False,
|
||||
'primary': phone.get('primary', False),
|
||||
'type': phone.get('type', 'Office')
|
||||
}
|
||||
|
||||
# Baue Advoware Maps
|
||||
advo_with_marker = {} # synced_value -> (komm, current_value)
|
||||
advo_without_marker = [] # Einträge ohne Marker (von Advoware angelegt)
|
||||
return espo_values
|
||||
|
||||
def _build_advoware_maps(self, advo_kommunikationen: List[Dict]) -> Tuple[Dict, List]:
|
||||
"""
|
||||
Trennt Advoware-Einträge in MIT Marker und OHNE Marker
|
||||
|
||||
Returns:
|
||||
(advo_with_marker: {synced_value: (komm, current_value)}, advo_without_marker: [komm])
|
||||
"""
|
||||
advo_with_marker = {}
|
||||
advo_without_marker = []
|
||||
|
||||
for komm in advo_kommunikationen:
|
||||
if not should_sync_to_espocrm(komm):
|
||||
continue
|
||||
|
||||
tlf = (komm.get('tlf') or '').strip()
|
||||
if not tlf: # Leere Einträge ignorieren
|
||||
if not tlf:
|
||||
continue
|
||||
|
||||
bemerkung = komm.get('bemerkung') or ''
|
||||
marker = parse_marker(bemerkung)
|
||||
marker = parse_marker(komm.get('bemerkung', ''))
|
||||
|
||||
if marker and not marker['is_slot']:
|
||||
# Hat Marker → Von EspoCRM synchronisiert
|
||||
synced_value = marker['synced_value']
|
||||
advo_with_marker[synced_value] = (komm, tlf)
|
||||
advo_with_marker[marker['synced_value']] = (komm, tlf)
|
||||
else:
|
||||
# Kein Marker → Von Advoware angelegt (Var4)
|
||||
advo_without_marker.append(komm)
|
||||
|
||||
# ========== ANALYSE ==========
|
||||
|
||||
# 1. Prüfe Advoware-Einträge MIT Marker
|
||||
return advo_with_marker, advo_without_marker
|
||||
|
||||
def _analyze_advoware_with_marker(self, advo_with_marker: Dict, espo_values: Dict, diff: Dict) -> None:
|
||||
"""Analysiert Advoware-Einträge MIT Marker für Var6, Var5, Var2"""
|
||||
for synced_value, (komm, current_value) in advo_with_marker.items():
|
||||
|
||||
if synced_value != current_value:
|
||||
# Var6: In Advoware geändert
|
||||
self.logger.info(f"[KOMM] ✏️ Var6: Changed in Advoware - synced='{synced_value[:30]}...', current='{current_value[:30]}...'")
|
||||
self.logger.info(f"[KOMM] ✏️ Var6: Changed in Advoware")
|
||||
diff['advo_changed'].append((komm, synced_value, current_value))
|
||||
|
||||
elif synced_value in espo_values:
|
||||
espo_item = espo_values[synced_value]
|
||||
|
||||
# Prüfe ob primary geändert wurde (Var5 könnte auch sein)
|
||||
current_online = komm.get('online', False)
|
||||
espo_primary = espo_item['primary']
|
||||
|
||||
if current_online != espo_primary:
|
||||
# Var5: EspoCRM hat primary geändert
|
||||
self.logger.info(f"[KOMM] 🔄 Var5: Primary changed in EspoCRM - value='{synced_value}', advo_online={current_online}, espo_primary={espo_primary}")
|
||||
self.logger.info(f"[KOMM] 🔄 Var5: Primary changed in EspoCRM")
|
||||
diff['espo_changed'].append((synced_value, komm, espo_item))
|
||||
else:
|
||||
# Keine Änderung
|
||||
self.logger.info(f"[KOMM] ✓ No change: '{synced_value[:30]}...'")
|
||||
diff['no_change'].append((synced_value, komm, espo_item))
|
||||
|
||||
else:
|
||||
# Eintrag war mal in EspoCRM (hat Marker), ist jetzt aber nicht mehr da
|
||||
# → Var2: In EspoCRM gelöscht
|
||||
self.logger.info(f"[KOMM] 🗑️ Var2: Deleted in EspoCRM - synced_value='{synced_value}', komm_id={komm.get('id')}")
|
||||
# Var2: In EspoCRM gelöscht
|
||||
self.logger.info(f"[KOMM] 🗑️ Var2: Deleted in EspoCRM")
|
||||
diff['espo_deleted'].append(komm)
|
||||
|
||||
def _analyze_advoware_without_marker(
|
||||
self, advo_without_marker: List[Dict], espo_values: Dict,
|
||||
is_initial_sync: bool, advo_bet: Dict, diff: Dict
|
||||
) -> None:
|
||||
"""Analysiert Advoware-Einträge OHNE Marker für Var4 + Initial Sync Matching"""
|
||||
|
||||
# 2. Prüfe Advoware-Einträge OHNE Marker
|
||||
# FIX BUG-3: Bei Initial Sync Value-Map erstellen
|
||||
advo_values_without_marker = {}
|
||||
if is_initial_sync:
|
||||
advo_values_without_marker = {
|
||||
(k.get('tlf') or '').strip(): k
|
||||
for k in advo_without_marker
|
||||
if (k.get('tlf') or '').strip()
|
||||
}
|
||||
|
||||
# Sammle matched values für Initial Sync
|
||||
matched_komm_ids = set()
|
||||
|
||||
# Prüfe ob EspoCRM-Werte bereits in Advoware existieren (Initial Sync)
|
||||
if is_initial_sync:
|
||||
for value in espo_values.keys():
|
||||
if value in advo_values_without_marker:
|
||||
matched_komm = advo_values_without_marker[value]
|
||||
espo_item = espo_values[value]
|
||||
|
||||
# Match gefunden - setze nur Marker, kein Var1/Var4
|
||||
if 'initial_sync_matches' not in diff:
|
||||
diff['initial_sync_matches'] = []
|
||||
diff['initial_sync_matches'].append((value, matched_komm, espo_item))
|
||||
matched_komm_ids.add(matched_komm['id'])
|
||||
|
||||
self.logger.info(f"[KOMM] ✓ Initial Sync Match: '{value[:30]}...'")
|
||||
|
||||
# Var4: Neu in Advoware (nicht matched im Initial Sync)
|
||||
for komm in advo_without_marker:
|
||||
# Var4: Neu in Advoware angelegt
|
||||
tlf = (komm.get('tlf') or '').strip()
|
||||
self.logger.info(f"[KOMM] ➕ Var4: New in Advoware - value='{tlf[:30]}...', komm_id={komm.get('id')}")
|
||||
diff['advo_new'].append(komm)
|
||||
if komm['id'] not in matched_komm_ids:
|
||||
tlf = (komm.get('tlf') or '').strip()
|
||||
self.logger.info(f"[KOMM] ➕ Var4: New in Advoware - '{tlf[:30]}...'")
|
||||
diff['advo_new'].append(komm)
|
||||
|
||||
def _analyze_espocrm_only(
|
||||
self, espo_values: Dict, advo_with_marker: Dict,
|
||||
espo_wins: bool, espo_changed_since_sync: bool,
|
||||
advo_changed_since_sync: bool, diff: Dict
|
||||
) -> None:
|
||||
"""Analysiert EspoCRM-Einträge die nicht in Advoware sind für Var1/Var3"""
|
||||
|
||||
# Sammle bereits gematchte values aus Initial Sync
|
||||
matched_values = set()
|
||||
if 'initial_sync_matches' in diff:
|
||||
matched_values = {v for v, k, e in diff['initial_sync_matches']}
|
||||
|
||||
# 3. Prüfe EspoCRM-Einträge die NICHT in Advoware sind (oder nur mit altem Marker)
|
||||
for value, espo_item in espo_values.items():
|
||||
if value not in advo_with_marker:
|
||||
# HASH-BASIERTE KONFLIKT-LOGIK: Unterscheide Var1 von Var3
|
||||
|
||||
if espo_wins or (espo_changed_since_sync and not advo_changed_since_sync):
|
||||
# Var1: Neu in EspoCRM (EspoCRM geändert, Advoware nicht)
|
||||
self.logger.info(f"[KOMM] Var1: New in EspoCRM '{value}' (espo changed, advo unchanged)")
|
||||
diff['espo_new'].append((value, espo_item))
|
||||
|
||||
elif advo_changed_since_sync and not espo_changed_since_sync:
|
||||
# Var3: In Advoware gelöscht (Advoware geändert, EspoCRM nicht)
|
||||
self.logger.info(f"[KOMM] Var3: Deleted in Advoware '{value}' (advo changed, espo unchanged)")
|
||||
diff['advo_deleted'].append((value, espo_item))
|
||||
|
||||
else:
|
||||
# Kein klarer Hinweis - Default: Behandle als Var1 (neu in EspoCRM)
|
||||
self.logger.info(f"[KOMM] Var1 (default): '{value}' - no clear indication, treating as new in EspoCRM")
|
||||
diff['espo_new'].append((value, espo_item))
|
||||
|
||||
return diff
|
||||
# Skip wenn bereits im Initial Sync gematched
|
||||
if value in matched_values:
|
||||
continue
|
||||
|
||||
# Skip wenn in Advoware mit Marker
|
||||
if value in advo_with_marker:
|
||||
continue
|
||||
|
||||
# Hash-basierte Logik: Var1 vs Var3
|
||||
if espo_wins or (espo_changed_since_sync and not advo_changed_since_sync):
|
||||
# Var1: Neu in EspoCRM
|
||||
self.logger.info(f"[KOMM] ➕ Var1: New in EspoCRM '{value[:30]}...'")
|
||||
diff['espo_new'].append((value, espo_item))
|
||||
|
||||
elif advo_changed_since_sync and not espo_changed_since_sync:
|
||||
# Var3: In Advoware gelöscht
|
||||
self.logger.info(f"[KOMM] 🗑️ Var3: Deleted in Advoware '{value[:30]}...'")
|
||||
diff['advo_deleted'].append((value, espo_item))
|
||||
|
||||
else:
|
||||
# Default: Var1 (neu in EspoCRM)
|
||||
self.logger.info(f"[KOMM] ➕ Var1 (default): '{value[:30]}...'")
|
||||
diff['espo_new'].append((value, espo_item))
|
||||
|
||||
# ========== APPLY CHANGES ==========
|
||||
|
||||
@@ -491,7 +577,9 @@ class KommunikationSyncManager:
|
||||
self.logger.info(f"[KOMM] ✅ Updated EspoCRM: {result['emails_synced']} emails, {result['phones_synced']} phones")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler bei Advoware→EspoCRM Apply: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler bei Advoware→EspoCRM Apply: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
result['errors'].append(str(e))
|
||||
|
||||
return result
|
||||
@@ -576,7 +664,9 @@ class KommunikationSyncManager:
|
||||
result['created'] += 1
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler bei EspoCRM→Advoware Apply: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler bei EspoCRM→Advoware Apply: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
result['errors'].append(str(e))
|
||||
|
||||
return result
|
||||
@@ -619,7 +709,9 @@ class KommunikationSyncManager:
|
||||
self.logger.info(f"[KOMM] ✅ Created empty slot: komm_id={komm_id}, kommKz={kommkz}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler beim Erstellen von Empty Slot: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler beim Erstellen von Empty Slot: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
|
||||
async def _revert_advoware_change(
|
||||
self,
|
||||
@@ -667,7 +759,9 @@ class KommunikationSyncManager:
|
||||
self.logger.info(f"[KOMM] ✅ Reverted Var6: '{advo_current_value[:30]}...' → '{espo_synced_value[:30]}...' (komm_id={komm_id})")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler beim Revert von Var6: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler beim Revert von Var6: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
|
||||
def _needs_update(self, advo_komm: Dict, espo_item: Dict) -> bool:
|
||||
"""Prüft ob Update nötig ist"""
|
||||
@@ -704,7 +798,9 @@ class KommunikationSyncManager:
|
||||
self.logger.info(f"[KOMM] ✅ Updated: komm_id={komm_id}, value={value[:30]}...")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler beim Update: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler beim Update: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
|
||||
async def _create_or_reuse_kommunikation(self, betnr: int, espo_item: Dict,
|
||||
advo_kommunikationen: List[Dict]) -> bool:
|
||||
@@ -756,7 +852,9 @@ class KommunikationSyncManager:
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[KOMM] Fehler beim Erstellen/Reuse: {e}", exc_info=True)
|
||||
import traceback
|
||||
self.logger.error(f"[KOMM] Fehler beim Erstellen/Reuse: {e}")
|
||||
self.logger.error(traceback.format_exc())
|
||||
return False
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user