47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
"""Akte Webhook - Create"""
|
|
import json
|
|
from typing import Any
|
|
from motia import FlowContext, http, ApiRequest, ApiResponse
|
|
|
|
|
|
config = {
|
|
"name": "Akte Webhook - Create",
|
|
"description": "Empfängt EspoCRM-Create-Webhooks für CAkten und triggert sofort den Sync",
|
|
"flows": ["akte-sync"],
|
|
"triggers": [http("POST", "/crm/akte/webhook/create")],
|
|
"enqueues": ["akte.sync"],
|
|
}
|
|
|
|
|
|
async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
|
try:
|
|
payload = request.body or {}
|
|
|
|
ctx.logger.info("=" * 60)
|
|
ctx.logger.info("📥 AKTE WEBHOOK: CREATE")
|
|
ctx.logger.info(f" Payload: {json.dumps(payload, ensure_ascii=False)[:200]}")
|
|
|
|
entity_ids: set[str] = set()
|
|
if isinstance(payload, list):
|
|
for item in payload:
|
|
if isinstance(item, dict) and 'id' in item:
|
|
entity_ids.add(item['id'])
|
|
elif isinstance(payload, dict) and 'id' in payload:
|
|
entity_ids.add(payload['id'])
|
|
|
|
if not entity_ids:
|
|
ctx.logger.warn("⚠️ No entity IDs in payload")
|
|
return ApiResponse(status=400, body={"error": "No entity ID found in payload"})
|
|
|
|
for eid in entity_ids:
|
|
await ctx.enqueue({'topic': 'akte.sync', 'data': {'akte_id': eid, 'aktennummer': None}})
|
|
|
|
ctx.logger.info(f"✅ Emitted akte.sync for {len(entity_ids)} ID(s): {entity_ids}")
|
|
ctx.logger.info("=" * 60)
|
|
|
|
return ApiResponse(status=200, body={"status": "received", "action": "create", "ids_count": len(entity_ids)})
|
|
|
|
except Exception as e:
|
|
ctx.logger.error(f"❌ Webhook error: {e}")
|
|
return ApiResponse(status=500, body={"error": str(e)})
|