- Advoware API Proxy für alle HTTP-Methoden (GET/POST/PUT/DELETE) - EspoCRM Webhook-Receiver für Beteiligte CRUD-Operationen - Redis-basierte Deduplikation für Webhook-Events - Event-driven Synchronisations-Handler (Placeholder) - Detaillierte README.md mit Setup und Verwendungsanleitung - Fehlerbehebungen für Context-Attribute und Redis-Verbindungen
97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
from typing import Any, Dict, Set
|
|
import json
|
|
import redis
|
|
from config import Config
|
|
import datetime
|
|
|
|
config = {
|
|
'type': 'api',
|
|
'name': 'VMH Webhook Beteiligte Update',
|
|
'description': 'Empfängt Update-Webhooks von EspoCRM für Beteiligte',
|
|
'path': '/vmh/webhook/beteiligte/update',
|
|
'method': 'POST',
|
|
'flows': ['vmh'],
|
|
'emits': ['vmh.beteiligte.update']
|
|
}
|
|
|
|
async def handler(req, context):
|
|
try:
|
|
# Payload aus dem Request-Body holen
|
|
payload = req.get('body', [])
|
|
|
|
# Detailliertes Logging
|
|
context.logger.info("VMH Webhook Beteiligte Update empfangen")
|
|
context.logger.info(f"Headers: {json.dumps(dict(req.get('headers', {})), indent=2)}")
|
|
context.logger.info(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}")
|
|
|
|
# Sammle alle IDs aus dem Batch
|
|
ids_to_sync: Set[str] = set()
|
|
|
|
if isinstance(payload, list):
|
|
for entity in payload:
|
|
if isinstance(entity, dict) and 'id' in entity:
|
|
entity_id = entity['id']
|
|
ids_to_sync.add(entity_id)
|
|
context.logger.info(f"Update Entity ID gefunden: {entity_id}")
|
|
elif isinstance(payload, dict) and 'id' in payload:
|
|
ids_to_sync.add(payload['id'])
|
|
context.logger.info(f"Update Single Entity ID gefunden: {payload['id']}")
|
|
|
|
context.logger.info(f"Insgesamt {len(ids_to_sync)} eindeutige IDs zum Update-Sync gefunden")
|
|
|
|
# Redis Verbindung für Deduplizierung
|
|
redis_client = redis.Redis(
|
|
host=Config.REDIS_HOST,
|
|
port=int(Config.REDIS_PORT),
|
|
db=int(Config.REDIS_DB_ADVOWARE_CACHE),
|
|
decode_responses=True
|
|
)
|
|
|
|
# Deduplizierung: Prüfe welche IDs schon in der Queue sind
|
|
pending_key = 'vmh:beteiligte:update_pending'
|
|
existing_ids = redis_client.smembers(pending_key)
|
|
new_ids = ids_to_sync - set(existing_ids)
|
|
|
|
if new_ids:
|
|
# Füge neue IDs zur Pending-Queue hinzu
|
|
redis_client.sadd(pending_key, *new_ids)
|
|
context.logger.info(f"{len(new_ids)} neue IDs zur Update-Sync-Queue hinzugefügt: {list(new_ids)}")
|
|
|
|
# Emittiere Events für neue IDs
|
|
for entity_id in new_ids:
|
|
await context.emit({
|
|
'topic': 'vmh.beteiligte.update',
|
|
'data': {
|
|
'entity_id': entity_id,
|
|
'action': 'update',
|
|
'source': 'webhook',
|
|
'timestamp': req.get('timestamp') or datetime.datetime.now().isoformat()
|
|
}
|
|
})
|
|
context.logger.info(f"Update-Event emittiert für ID: {entity_id}")
|
|
else:
|
|
context.logger.info("Keine neuen IDs zum Update-Sync gefunden")
|
|
|
|
context.logger.info("VMH Update Webhook erfolgreich verarbeitet")
|
|
|
|
return {
|
|
'status': 200,
|
|
'body': {
|
|
'status': 'received',
|
|
'action': 'update',
|
|
'new_ids_count': len(new_ids) if 'new_ids' in locals() else 0,
|
|
'total_ids_in_batch': len(ids_to_sync)
|
|
}
|
|
}
|
|
|
|
except Exception as e:
|
|
context.logger.error(f"Fehler beim Verarbeiten des VMH Update Webhooks: {e}")
|
|
context.logger.error(f"Request: {req}")
|
|
return {
|
|
'status': 500,
|
|
'body': {
|
|
'error': 'Internal server error',
|
|
'details': str(e)
|
|
}
|
|
}
|