diff --git a/services/advoware_document_sync_utils.py b/services/advoware_document_sync_utils.py index 069e380..83e9ed7 100644 --- a/services/advoware_document_sync_utils.py +++ b/services/advoware_document_sync_utils.py @@ -289,7 +289,8 @@ class AdvowareDocumentSyncUtils: """ Check if metadata needs update in EspoCRM. - Compares History metadata (text, art, dat) with EspoCRM fields. + Compares History metadata (text, art, hNr) with EspoCRM fields. + Always syncs metadata changes even if file content hasn't changed. Args: espo_doc: Document from EspoCRM @@ -300,24 +301,28 @@ class AdvowareDocumentSyncUtils: """ updates = {} - # Map History fields to EspoCRM fields + # Map History fields to correct EspoCRM field names history_text = advo_history.get('text', '') history_art = advo_history.get('art', '') - history_dat = advo_history.get('dat', '') + history_hnr = advo_history.get('hNr') - espo_description = espo_doc.get('description', '') - espo_type = espo_doc.get('type', '') - espo_date = espo_doc.get('dateUploaded', '') + espo_bemerkung = espo_doc.get('advowareBemerkung', '') + espo_art = espo_doc.get('advowareArt', '') + espo_hnr = espo_doc.get('hnr') - # Check if different - if history_text and history_text != espo_description: - updates['description'] = history_text + # Check if different - sync metadata independently of file changes + if history_text != espo_bemerkung: + updates['advowareBemerkung'] = history_text - if history_art and history_art != espo_type: - updates['type'] = history_art + if history_art != espo_art: + updates['advowareArt'] = history_art - if history_dat and history_dat != espo_date: - updates['dateUploaded'] = history_dat + if history_hnr is not None and history_hnr != espo_hnr: + updates['hnr'] = history_hnr + + # Always update lastSyncTimestamp when metadata changes + if len(updates) > 0: + updates['lastSyncTimestamp'] = datetime.now().isoformat() needs_update = len(updates) > 0 diff --git a/src/steps/advoware_docs/document_sync_event_step.py b/src/steps/advoware_docs/document_sync_event_step.py index f649fd7..2a49da2 100644 --- a/src/steps/advoware_docs/document_sync_event_step.py +++ b/src/steps/advoware_docs/document_sync_event_step.py @@ -240,6 +240,9 @@ async def handler(event_data: Dict[str, Any], ctx: FlowContext) -> None: # Extract full Windows path from watcher data full_path = windows_file.get('path', '') + # Current timestamp for sync tracking + now_iso = datetime.now().isoformat() + new_doc = await espocrm.create_entity('CDokumente', { 'name': filename, 'dokumentId': attachment.get('id'), # Link to attachment @@ -252,7 +255,8 @@ async def handler(event_data: Dict[str, Any], ctx: FlowContext) -> None: 'blake3hash': blake3_hash, 'syncedHash': blake3_hash, 'usn': windows_file.get('usn', 0), - 'syncStatus': 'synced' + 'syncStatus': 'synced', + 'lastSyncTimestamp': now_iso }) doc_id = new_doc.get('id') @@ -281,12 +285,15 @@ async def handler(event_data: Dict[str, Any], ctx: FlowContext) -> None: # Update document in EspoCRM with correct field names ctx.logger.info(f"💾 Updating document in EspoCRM...") + now_iso = datetime.now().isoformat() + update_data = { 'blake3hash': blake3_hash, 'syncedHash': blake3_hash, 'usn': windows_file.get('usn', 0), 'dateipfad': full_path, - 'syncStatus': 'synced' + 'syncStatus': 'synced', + 'lastSyncTimestamp': now_iso } # Also update History fields if available