feat: Refactor webhook handlers for Beteiligte to improve ID processing and logging, and enhance update filtering logic
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
from typing import Any, Dict, Set
|
||||
import json
|
||||
import redis
|
||||
from config import Config
|
||||
import datetime
|
||||
|
||||
config = {
|
||||
@@ -21,51 +18,38 @@ async def handler(req, context):
|
||||
context.logger.info("VMH Webhook Beteiligte Delete empfangen")
|
||||
context.logger.info(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}")
|
||||
|
||||
ids_to_sync: Set[str] = set()
|
||||
# Sammle alle IDs aus dem Batch
|
||||
entity_ids = 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)
|
||||
entity_ids.add(entity['id'])
|
||||
elif isinstance(payload, dict) and 'id' in payload:
|
||||
ids_to_sync.add(payload['id'])
|
||||
entity_ids.add(payload['id'])
|
||||
|
||||
context.logger.info(f"{len(ids_to_sync)} IDs zum Delete-Sync gefunden")
|
||||
context.logger.info(f"{len(entity_ids)} IDs zum Delete-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
|
||||
)
|
||||
# Emittiere Events direkt (Deduplizierung erfolgt im Event-Handler via Lock)
|
||||
for entity_id in entity_ids:
|
||||
await context.emit({
|
||||
'topic': 'vmh.beteiligte.delete',
|
||||
'data': {
|
||||
'entity_id': entity_id,
|
||||
'action': 'delete',
|
||||
'source': 'webhook',
|
||||
'timestamp': req.get('timestamp') or datetime.datetime.now().isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
pending_key = 'vmh:beteiligte:pending_delete'
|
||||
existing_ids = redis_client.smembers(pending_key)
|
||||
new_ids = ids_to_sync - set(existing_ids)
|
||||
|
||||
if new_ids:
|
||||
redis_client.sadd(pending_key, *new_ids)
|
||||
context.logger.info(f"{len(new_ids)} neue IDs zur Delete-Queue hinzugefügt")
|
||||
|
||||
for entity_id in new_ids:
|
||||
await context.emit({
|
||||
'topic': 'vmh.beteiligte.delete',
|
||||
'data': {
|
||||
'entity_id': entity_id,
|
||||
'action': 'delete',
|
||||
'source': 'webhook',
|
||||
'timestamp': req.get('timestamp') or datetime.datetime.now().isoformat()
|
||||
}
|
||||
})
|
||||
context.logger.info(f"VMH Delete Webhook verarbeitet: {len(entity_ids)} Events emittiert")
|
||||
|
||||
return {
|
||||
'status': 200,
|
||||
'body': {
|
||||
'status': 'received',
|
||||
'action': 'delete',
|
||||
'new_ids_count': len(new_ids) if 'new_ids' in locals() else 0
|
||||
'ids_count': len(entity_ids)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user