feat: Integrate NotificationManager for handling notifications in sync operations
This commit is contained in:
@@ -16,6 +16,7 @@ import logging
|
||||
import redis
|
||||
from config import Config
|
||||
from services.espocrm import EspoCRMAPI
|
||||
from services.notification_utils import NotificationManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -35,6 +36,7 @@ class BeteiligteSync:
|
||||
self.espocrm = espocrm_api
|
||||
self.context = context
|
||||
self.redis = redis_client or self._init_redis()
|
||||
self.notification_manager = NotificationManager(espocrm_api=self.espocrm, context=context)
|
||||
|
||||
def _init_redis(self) -> redis.Redis:
|
||||
"""Initialize Redis client for distributed locking"""
|
||||
@@ -395,11 +397,11 @@ class BeteiligteSync:
|
||||
extra_data: Optional[Dict[str, Any]] = None
|
||||
) -> None:
|
||||
"""
|
||||
Sendet EspoCRM In-App Notification
|
||||
Sendet EspoCRM Notification via NotificationManager
|
||||
|
||||
Args:
|
||||
entity_id: CBeteiligte Entity ID
|
||||
notification_type: "conflict" oder "deleted"
|
||||
notification_type: "conflict", "deleted" oder "error"
|
||||
extra_data: Zusätzliche Daten für Nachricht
|
||||
"""
|
||||
try:
|
||||
@@ -407,45 +409,58 @@ class BeteiligteSync:
|
||||
entity = await self.espocrm.get_entity('CBeteiligte', entity_id)
|
||||
name = entity.get('name', 'Unbekannt')
|
||||
betnr = entity.get('betnr')
|
||||
assigned_user = entity.get('assignedUserId')
|
||||
|
||||
# Erstelle Nachricht basierend auf Typ
|
||||
# Map notification_type zu action_type
|
||||
if notification_type == "conflict":
|
||||
message = (
|
||||
f"⚠️ Sync-Konflikt bei Beteiligten '{name}' (betNr: {betnr}). "
|
||||
f"EspoCRM hat Vorrang - Änderungen wurden nach Advoware übertragen. "
|
||||
f"Bitte prüfen Sie die Details."
|
||||
)
|
||||
action_type = 'sync_conflict'
|
||||
details = {
|
||||
'message': f"Sync-Konflikt bei Beteiligten '{name}' (betNr: {betnr})",
|
||||
'description': (
|
||||
f"EspoCRM hat Vorrang - Änderungen wurden nach Advoware übertragen.\n\n"
|
||||
f"Bitte prüfen Sie die Details und stellen Sie sicher, dass die Daten korrekt sind."
|
||||
),
|
||||
'entity_name': name,
|
||||
'betnr': betnr,
|
||||
'priority': 'Normal'
|
||||
}
|
||||
elif notification_type == "deleted":
|
||||
deleted_at = entity.get('advowareDeletedAt', 'unbekannt')
|
||||
message = (
|
||||
f"🗑️ Beteiligter '{name}' (betNr: {betnr}) wurde in Advoware gelöscht "
|
||||
f"(am {deleted_at}). Der Datensatz wurde in EspoCRM markiert, aber nicht gelöscht. "
|
||||
f"Bitte prüfen Sie, ob dies beabsichtigt war."
|
||||
)
|
||||
action_type = 'entity_deleted_in_source'
|
||||
details = {
|
||||
'message': f"Beteiligter '{name}' wurde in Advoware gelöscht",
|
||||
'description': (
|
||||
f"Der Beteiligte '{name}' (betNr: {betnr}) wurde am {deleted_at} "
|
||||
f"in Advoware gelöscht.\n\n"
|
||||
f"Der Datensatz wurde in EspoCRM markiert, aber nicht gelöscht. "
|
||||
f"Bitte prüfen Sie, ob dies beabsichtigt war."
|
||||
),
|
||||
'entity_name': name,
|
||||
'betnr': betnr,
|
||||
'deleted_at': deleted_at,
|
||||
'priority': 'High'
|
||||
}
|
||||
else:
|
||||
message = f"Benachrichtigung für Beteiligten '{name}'"
|
||||
action_type = 'general_manual_action'
|
||||
details = {
|
||||
'message': f"Benachrichtigung für Beteiligten '{name}'",
|
||||
'entity_name': name,
|
||||
'betnr': betnr
|
||||
}
|
||||
|
||||
# Erstelle Notification in EspoCRM
|
||||
notification_data = {
|
||||
'type': 'message',
|
||||
'message': message,
|
||||
'relatedType': 'CBeteiligte',
|
||||
'relatedId': entity_id,
|
||||
}
|
||||
# Merge extra_data if provided
|
||||
if extra_data:
|
||||
details.update(extra_data)
|
||||
|
||||
# Wenn assigned user vorhanden, sende an diesen
|
||||
if assigned_user:
|
||||
notification_data['userId'] = assigned_user
|
||||
|
||||
# Sende via API
|
||||
result = await self.espocrm.api_call(
|
||||
'Notification',
|
||||
method='POST',
|
||||
data=notification_data
|
||||
# Sende via NotificationManager
|
||||
await self.notification_manager.notify_manual_action_required(
|
||||
entity_type='CBeteiligte',
|
||||
entity_id=entity_id,
|
||||
action_type=action_type,
|
||||
details=details,
|
||||
create_task=True
|
||||
)
|
||||
|
||||
self._log(f"Notification gesendet für {entity_id}: {notification_type}")
|
||||
self._log(f"Notification via NotificationManager gesendet: {notification_type} für {entity_id}")
|
||||
|
||||
except Exception as e:
|
||||
self._log(f"Fehler beim Senden der Notification: {e}", level='error')
|
||||
|
||||
Reference in New Issue
Block a user