77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
"""VMH Webhook - Bankverbindungen Create"""
|
|
import json
|
|
import datetime
|
|
from typing import Any
|
|
from motia import FlowContext, http, ApiRequest, ApiResponse
|
|
|
|
|
|
config = {
|
|
"name": "VMH Webhook Bankverbindungen Create",
|
|
"description": "Empfängt Create-Webhooks von EspoCRM für Bankverbindungen",
|
|
"flows": ["vmh-bankverbindungen"],
|
|
"triggers": [
|
|
http("POST", "/vmh/webhook/bankverbindungen/create")
|
|
],
|
|
"enqueues": ["vmh.bankverbindungen.create"],
|
|
}
|
|
|
|
|
|
async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
|
"""
|
|
Webhook handler for Bankverbindungen creation in EspoCRM.
|
|
"""
|
|
try:
|
|
payload = request.body or []
|
|
|
|
ctx.logger.info("=" * 80)
|
|
ctx.logger.info("📥 VMH WEBHOOK: BANKVERBINDUNGEN CREATE")
|
|
ctx.logger.info("=" * 80)
|
|
ctx.logger.info(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}")
|
|
ctx.logger.info("=" * 80)
|
|
|
|
# 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_ids.add(entity['id'])
|
|
elif isinstance(payload, dict) and 'id' in payload:
|
|
entity_ids.add(payload['id'])
|
|
|
|
ctx.logger.info(f"{len(entity_ids)} IDs zum Create-Sync gefunden")
|
|
|
|
# Emit events
|
|
for entity_id in entity_ids:
|
|
await ctx.enqueue({
|
|
'topic': 'vmh.bankverbindungen.create',
|
|
'data': {
|
|
'entity_id': entity_id,
|
|
'action': 'create',
|
|
'source': 'webhook',
|
|
'timestamp': datetime.datetime.now().isoformat()
|
|
}
|
|
})
|
|
|
|
ctx.logger.info("✅ VMH Create Webhook verarbeitet: "
|
|
f"{len(entity_ids)} Events emittiert")
|
|
|
|
return ApiResponse(
|
|
status=200,
|
|
body={
|
|
'status': 'received',
|
|
'action': 'create',
|
|
'ids_count': len(entity_ids)
|
|
}
|
|
)
|
|
|
|
except Exception as e:
|
|
ctx.logger.error("=" * 80)
|
|
ctx.logger.error("❌ FEHLER: BANKVERBINDUNGEN CREATE WEBHOOK")
|
|
ctx.logger.error(f"Error: {e}")
|
|
ctx.logger.error("=" * 80)
|
|
return ApiResponse(
|
|
status=500,
|
|
body={'error': 'Internal server error', 'details': str(e)}
|
|
)
|