- Translated comments and docstrings from German to English for better clarity. - Improved logging consistency across various webhook handlers for create, delete, and update operations. - Centralized logging functionality by utilizing a dedicated logger utility. - Added new enums for file and XAI sync statuses in models. - Updated Redis client factory to use a centralized logger and improved error handling. - Enhanced API responses to include more descriptive messages and status codes.
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
"""VMH Webhook - Beteiligte Update"""
|
|
import json
|
|
import datetime
|
|
from typing import Any
|
|
from motia import FlowContext, http, ApiRequest, ApiResponse
|
|
|
|
|
|
config = {
|
|
"name": "VMH Webhook Beteiligte Update",
|
|
"description": "Receives update webhooks from EspoCRM for Beteiligte",
|
|
"flows": ["vmh-beteiligte"],
|
|
"triggers": [
|
|
http("POST", "/vmh/webhook/beteiligte/update")
|
|
],
|
|
"enqueues": ["vmh.beteiligte.update"],
|
|
}
|
|
|
|
|
|
async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
|
"""
|
|
Webhook handler for Beteiligte updates in EspoCRM.
|
|
|
|
Note: Loop prevention is implemented on EspoCRM side.
|
|
rowId updates no longer trigger webhooks, so no filtering needed.
|
|
"""
|
|
try:
|
|
payload = request.body or []
|
|
|
|
ctx.logger.info("=" * 80)
|
|
ctx.logger.info("📥 VMH WEBHOOK: BETEILIGTE UPDATE")
|
|
ctx.logger.info("=" * 80)
|
|
ctx.logger.info(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}")
|
|
ctx.logger.info("=" * 80)
|
|
|
|
# Collect all IDs from 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 found for update sync")
|
|
|
|
# Emit events for queue processing
|
|
for entity_id in entity_ids:
|
|
await ctx.enqueue({
|
|
'topic': 'vmh.beteiligte.update',
|
|
'data': {
|
|
'entity_id': entity_id,
|
|
'action': 'update',
|
|
'source': 'webhook',
|
|
'timestamp': datetime.datetime.now().isoformat()
|
|
}
|
|
})
|
|
|
|
ctx.logger.info("✅ VMH Update Webhook processed: "
|
|
f"{len(entity_ids)} events emitted")
|
|
|
|
return ApiResponse(
|
|
status_code=200,
|
|
body={
|
|
'status': 'received',
|
|
'action': 'update',
|
|
'ids_count': len(entity_ids)
|
|
}
|
|
)
|
|
|
|
except Exception as e:
|
|
ctx.logger.error("=" * 80)
|
|
ctx.logger.error("❌ ERROR: VMH UPDATE WEBHOOK")
|
|
ctx.logger.error(f"Error: {e}")
|
|
ctx.logger.error("=" * 80)
|
|
return ApiResponse(
|
|
status_code=500,
|
|
body={
|
|
'error': 'Internal server error',
|
|
'details': str(e)
|
|
}
|
|
)
|