feat(webhooks): Implement Akte webhooks for create, delete, and update operations
This commit is contained in:
56
src/steps/crm/akte/webhooks/akte_create_api_step.py
Normal file
56
src/steps/crm/akte/webhooks/akte_create_api_step.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""Akte Webhook - Create"""
|
||||
import json
|
||||
import time
|
||||
import datetime
|
||||
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 queued Entity-IDs für den Sync",
|
||||
"flows": ["akte-sync"],
|
||||
"triggers": [http("POST", "/crm/akte/webhook/create")],
|
||||
"enqueues": [],
|
||||
}
|
||||
|
||||
PENDING_KEY = "akte:pending_entity_ids"
|
||||
|
||||
|
||||
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_code=400, body={"error": "No entity ID found in payload"})
|
||||
|
||||
from services.redis_client import get_redis_client
|
||||
redis_client = get_redis_client(strict=False)
|
||||
if not redis_client:
|
||||
ctx.logger.error("❌ Redis unavailable")
|
||||
return ApiResponse(status_code=503, body={"error": "Service unavailable"})
|
||||
|
||||
ts = time.time()
|
||||
redis_client.zadd(PENDING_KEY, {eid: ts for eid in entity_ids})
|
||||
|
||||
ctx.logger.info(f"✅ Queued {len(entity_ids)} entity ID(s): {entity_ids}")
|
||||
ctx.logger.info("=" * 60)
|
||||
|
||||
return ApiResponse(status_code=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_code=500, body={"error": str(e)})
|
||||
Reference in New Issue
Block a user