feat: Integrate NotificationManager for handling notifications in sync operations

This commit is contained in:
2026-02-08 14:42:33 +00:00
parent b4e41e7381
commit da9a962858
2 changed files with 112 additions and 85 deletions

View File

@@ -2,6 +2,7 @@ from services.advoware import AdvowareAPI
from services.espocrm import EspoCRMAPI
from services.bankverbindungen_mapper import BankverbindungenMapper
from services.beteiligte_sync_utils import BeteiligteSync
from services.notification_utils import NotificationManager
import json
import redis
from config import Config
@@ -53,6 +54,7 @@ async def handler(event_data, context):
advoware = AdvowareAPI(context)
sync_utils = BeteiligteSync(espocrm, redis_client, context) # Reuse utils
mapper = BankverbindungenMapper()
notification_mgr = NotificationManager(espocrm_api=espocrm, context=context)
try:
# 1. ACQUIRE LOCK
@@ -98,11 +100,11 @@ async def handler(event_data, context):
# FALL B: Existiert (hat advowareId) → UPDATE oder CHECK
elif advoware_id:
await handle_update(entity_id, betnr, advoware_id, espo_entity, espocrm, advoware, mapper, context, redis_client, lock_key)
await handle_update(entity_id, betnr, advoware_id, espo_entity, espocrm, advoware, mapper, notification_mgr, context, redis_client, lock_key)
# FALL C: DELETE
elif action == 'delete':
await handle_delete(entity_id, betnr, advoware_id, espocrm, advoware, context, redis_client, lock_key)
await handle_delete(entity_id, betnr, advoware_id, espo_entity, espocrm, advoware, notification_mgr, context, redis_client, lock_key)
else:
context.logger.warn(f"⚠️ Unbekannte Kombination: action={action}, advowareId={advoware_id}")
@@ -165,35 +167,44 @@ async def handle_create(entity_id, betnr, espo_entity, espocrm, advoware, mapper
redis_client.delete(lock_key)
async def handle_update(entity_id, betnr, advoware_id, espo_entity, espocrm, advoware, mapper, context, redis_client, lock_key):
"""Update nicht möglich - Sendet Notification an User"""
async def handle_update(entity_id, betnr, advoware_id, espo_entity, espocrm, advoware, mapper, notification_mgr, context, redis_client, lock_key):
"""Update nicht möglich - Sendet Notification an User via NotificationManager"""
try:
context.logger.warn(f"⚠️ UPDATE: Advoware API unterstützt kein PUT für Bankverbindungen")
# Erstelle Notification für User in EspoCRM
iban = espo_entity.get('iban', 'N/A')
bank = espo_entity.get('bank', 'N/A')
name = espo_entity.get('name', 'Unbenannt')
notification_message = (
f"Bankverbindung wurde in EspoCRM geändert, aber die Advoware API unterstützt keine Updates.\n\n"
f"**Bitte manuell in Advoware aktualisieren:**\n"
f"- Bank: {bank}\n"
f"- IBAN: {iban}\n"
f"- Beteiligter betNr: {betnr}\n"
f"- Advoware ID: {advoware_id}\n\n"
f"**Workaround:** Löschen und neu erstellen in EspoCRM, dann wird neue Bankverbindung in Advoware angelegt."
# Sende via NotificationManager
await notification_mgr.notify_manual_action_required(
entity_type='CBankverbindungen',
entity_id=entity_id,
action_type='api_limitation',
details={
'message': f'UPDATE nicht möglich für Bankverbindung: {name}',
'description': (
f"Die Advoware API unterstützt keine Updates für Bankverbindungen.\n\n"
f"**Details:**\n"
f"- Bank: {bank}\n"
f"- IBAN: {iban}\n"
f"- Beteiligter betNr: {betnr}\n"
f"- Advoware ID: {advoware_id}\n\n"
f"**Workaround:**\n"
f"Löschen Sie die Bankverbindung in EspoCRM und erstellen Sie sie neu. "
f"Die neue Bankverbindung wird dann automatisch in Advoware angelegt."
),
'entity_name': name,
'iban': iban,
'bank': bank,
'betnr': betnr,
'advoware_id': advoware_id,
'priority': 'Normal'
},
create_task=True
)
# Sende Notification via EspoCRM API
await espocrm.api_call('/Notification', method='POST', json_data={
'type': 'message',
'message': notification_message,
'userId': espo_entity.get('createdById') or espo_entity.get('modifiedById'),
'relatedType': 'CBankverbindungen',
'relatedId': entity_id
})
context.logger.info(f"📧 Notification an User gesendet: Manuelle Aktualisierung erforderlich")
context.logger.info(f"📧 Notification via NotificationManager gesendet: Update-Limitation")
redis_client.delete(lock_key)
except Exception as e:
@@ -203,8 +214,8 @@ async def handle_update(entity_id, betnr, advoware_id, espo_entity, espocrm, adv
redis_client.delete(lock_key)
async def handle_delete(entity_id, betnr, advoware_id, espocrm, advoware, context, redis_client, lock_key):
"""Delete nicht möglich - Sendet Notification an User"""
async def handle_delete(entity_id, betnr, advoware_id, espo_entity, espocrm, advoware, notification_mgr, context, redis_client, lock_key):
"""Delete nicht möglich - Sendet Notification an User via NotificationManager"""
try:
context.logger.warn(f"⚠️ DELETE: Advoware API unterstützt kein DELETE für Bankverbindungen")
@@ -213,37 +224,38 @@ async def handle_delete(entity_id, betnr, advoware_id, espocrm, advoware, contex
redis_client.delete(lock_key)
return
# Hole Entity-Details für Notification (vor dem Delete)
try:
espo_entity = await espocrm.get_entity('CBankverbindungen', entity_id)
iban = espo_entity.get('iban', 'N/A')
bank = espo_entity.get('bank', 'N/A')
user_id = espo_entity.get('createdById') or espo_entity.get('modifiedById')
except:
iban = 'N/A'
bank = 'N/A'
user_id = None
iban = espo_entity.get('iban', 'N/A')
bank = espo_entity.get('bank', 'N/A')
name = espo_entity.get('name', 'Unbenannt')
# Erstelle Notification für User in EspoCRM
notification_message = (
f"Bankverbindung wurde in EspoCRM gelöscht, aber die Advoware API unterstützt keine Löschungen.\n\n"
f"**Bitte manuell in Advoware löschen:**\n"
f"- Bank: {bank}\n"
f"- IBAN: {iban}\n"
f"- Beteiligter betNr: {betnr}\n"
f"- Advoware ID: {advoware_id}\n\n"
f"Die Bankverbindung bleibt in Advoware bestehen bis zur manuellen Löschung."
# Sende via NotificationManager
await notification_mgr.notify_manual_action_required(
entity_type='CBankverbindungen',
entity_id=entity_id,
action_type='delete_not_supported',
details={
'message': f'DELETE erforderlich für Bankverbindung: {name}',
'description': (
f"Die Advoware API unterstützt keine Löschungen für Bankverbindungen.\n\n"
f"**Bitte manuell in Advoware löschen:**\n"
f"- Bank: {bank}\n"
f"- IBAN: {iban}\n"
f"- Beteiligter betNr: {betnr}\n"
f"- Advoware ID: {advoware_id}\n\n"
f"Die Bankverbindung wurde in EspoCRM gelöscht, bleibt aber in Advoware "
f"bestehen bis zur manuellen Löschung."
),
'entity_name': name,
'iban': iban,
'bank': bank,
'betnr': betnr,
'advoware_id': advoware_id,
'priority': 'Normal'
},
create_task=True
)
# Sende Notification
if user_id:
await espocrm.api_call('/Notification', method='POST', json_data={
'type': 'message',
'message': notification_message,
'userId': user_id
})
context.logger.info(f"📧 Notification an User gesendet: Manuelle Löschung erforderlich")
context.logger.info(f"📧 Notification via NotificationManager gesendet: Delete erforderlich")
redis_client.delete(lock_key)
except Exception as e: