- Implemented `test_thumbnail_generation.py` to validate the complete flow of document thumbnail generation in EspoCRM, including document creation, file upload, webhook triggering, and preview verification. - Created `test_xai_collections_api.py` to test critical operations of the xAI Collections API, covering file uploads, collection CRUD operations, document management, and response validation. - Both scripts include detailed logging for success and error states, ensuring robust testing and easier debugging.
77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
"""VMH Webhook - Document Update"""
|
|
import json
|
|
from typing import Any
|
|
from motia import FlowContext, http, ApiRequest, ApiResponse
|
|
|
|
|
|
config = {
|
|
"name": "VMH Webhook Document Update",
|
|
"description": "Empfängt Update-Webhooks von EspoCRM für Documents",
|
|
"flows": ["vmh-documents"],
|
|
"triggers": [
|
|
http("POST", "/vmh/webhook/document/update")
|
|
],
|
|
"enqueues": ["vmh.document.update"],
|
|
}
|
|
|
|
|
|
async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
|
"""
|
|
Webhook handler for Document updates in EspoCRM.
|
|
|
|
Receives batch or single entity notifications and emits queue events
|
|
for each entity ID to be synced to xAI.
|
|
"""
|
|
try:
|
|
payload = request.body or []
|
|
|
|
ctx.logger.info("VMH Webhook Document Update empfangen")
|
|
ctx.logger.debug(f"Payload: {json.dumps(payload, indent=2, ensure_ascii=False)}")
|
|
|
|
# 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'])
|
|
entity_type = entity.get('entityType', 'CDokumente')
|
|
elif isinstance(payload, dict) and 'id' in payload:
|
|
entity_ids.add(payload['id'])
|
|
entity_type = payload.get('entityType', 'CDokumente')
|
|
|
|
ctx.logger.info(f"{len(entity_ids)} Document IDs zum Update-Sync gefunden")
|
|
|
|
# Emit events für Queue-Processing
|
|
for entity_id in entity_ids:
|
|
await ctx.enqueue({
|
|
'topic': 'vmh.document.update',
|
|
'data': {
|
|
'entity_id': entity_id,
|
|
'entity_type': entity_type if 'entity_type' in locals() else 'CDokumente',
|
|
'action': 'update',
|
|
'timestamp': payload[0].get('modifiedAt') if isinstance(payload, list) and payload else None
|
|
}
|
|
})
|
|
|
|
return ApiResponse(
|
|
status_code=200,
|
|
body={
|
|
'success': True,
|
|
'message': f'{len(entity_ids)} Document(s) zum Sync enqueued',
|
|
'entity_ids': list(entity_ids)
|
|
}
|
|
)
|
|
|
|
except Exception as e:
|
|
ctx.logger.error(f"Fehler im Document Update Webhook: {e}")
|
|
ctx.logger.error(f"Payload: {request.body}")
|
|
|
|
return ApiResponse(
|
|
status_code=500,
|
|
body={
|
|
'success': False,
|
|
'error': str(e)
|
|
}
|
|
)
|