From 4ed752b19e8054a19cb7079dff10e58c5a90b9e6 Mon Sep 17 00:00:00 2001 From: bsiggel Date: Sun, 8 Mar 2026 18:59:56 +0000 Subject: [PATCH] feat(document-sync): enhance metadata update to reset file status after preview generation --- services/document_sync_utils.py | 14 +++++++++----- steps/vmh/document_sync_event_step.py | 12 ++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/services/document_sync_utils.py b/services/document_sync_utils.py index 5e083e6..951d2c7 100644 --- a/services/document_sync_utils.py +++ b/services/document_sync_utils.py @@ -484,10 +484,11 @@ class DocumentSync(BaseSyncUtils): collection_ids: Optional[List[str]] = None, file_hash: Optional[str] = None, preview_data: Optional[bytes] = None, + reset_file_status: bool = False, entity_type: str = 'CDokumente' ) -> None: """ - Updated Document-Metadaten nach erfolgreichem xAI-Sync + Updated Document-Metadaten nach erfolgreichem xAI-Sync oder Preview-Generierung Args: document_id: EspoCRM Document ID @@ -495,6 +496,7 @@ class DocumentSync(BaseSyncUtils): collection_ids: Liste der xAI Collection IDs (optional) file_hash: MD5/SHA Hash des gesyncten Files preview_data: Vorschaubild (WebP) als bytes + reset_file_status: Ob fileStatus/dateiStatus zurückgesetzt werden soll entity_type: Entity-Type (CDokumente oder Document) """ try: @@ -511,13 +513,15 @@ class DocumentSync(BaseSyncUtils): if collection_ids is not None: update_data['xaiCollections'] = collection_ids - # Nur Status auf "Gesynct" setzen wenn xAI-File-ID vorhanden - if xai_file_id: + # Status zurücksetzen wenn xAI-Sync erfolgt ist ODER explizit angefordert + if xai_file_id or reset_file_status: # CDokumente verwendet fileStatus, Document verwendet dateiStatus if entity_type == 'CDokumente': - update_data['fileStatus'] = 'synced' + # Bei xAI-Sync: "synced", bei nur Preview: "processed" + update_data['fileStatus'] = 'synced' if xai_file_id else 'processed' else: - update_data['dateiStatus'] = 'Gesynct' + # Bei xAI-Sync: "Gesynct", bei nur Preview: "Verarbeitet" + update_data['dateiStatus'] = 'Gesynct' if xai_file_id else 'Verarbeitet' # Hash speichern für zukünftige Change Detection if file_hash: diff --git a/steps/vmh/document_sync_event_step.py b/steps/vmh/document_sync_event_step.py index 6d7a2f3..eb8bdfc 100644 --- a/steps/vmh/document_sync_event_step.py +++ b/steps/vmh/document_sync_event_step.py @@ -214,17 +214,23 @@ async def handle_create_or_update(entity_id: str, document: Dict[str, Any], sync if preview_data: ctx.logger.info(f"✅ Preview generated: {len(preview_data)} bytes WebP") - # 5. Upload Preview zu EspoCRM + # 5. Upload Preview zu EspoCRM und reset file status ctx.logger.info(f"📤 Uploading preview to EspoCRM...") await sync_utils.update_sync_metadata( entity_id, preview_data=preview_data, + reset_file_status=True, # Reset status nach Preview-Generierung entity_type=entity_type - # Keine xaiFileId/collections - nur Preview update ) ctx.logger.info(f"✅ Preview uploaded successfully") else: ctx.logger.warn("⚠️ Preview-Generierung lieferte keine Daten") + # Auch bei fehlgeschlagener Preview-Generierung Status zurücksetzen + await sync_utils.update_sync_metadata( + entity_id, + reset_file_status=True, + entity_type=entity_type + ) finally: # Cleanup temp file @@ -250,6 +256,8 @@ async def handle_create_or_update(entity_id: str, document: Dict[str, Any], sync if not needs_sync: ctx.logger.info("✅ Kein xAI-Sync erforderlich, Lock wird released") + # Wenn Preview generiert wurde aber kein xAI sync nötig, + # wurde Status bereits in Preview-Schritt zurückgesetzt await sync_utils.release_sync_lock(entity_id, success=True, entity_type=entity_type) return