#!/usr/bin/env python3 """ Test syncId über API mit Standard-Workflow Versuche ID direkt beim Verknüpfen zu setzen und dann via Formula zu updaten """ import requests import json import subprocess from datetime import datetime BASE_URL = "https://crm.bitbylaw.com" API_KEY = "e53def10eea27b92a6cd00f40a3e09a4" HEADERS = { "X-Api-Key": API_KEY, "Content-Type": "application/json" } def api_request(method, endpoint, data=None): url = f"{BASE_URL}/api/v1/{endpoint}" if method == "GET": response = requests.get(url, headers=HEADERS) elif method == "POST": response = requests.post(url, headers=HEADERS, json=data) elif method == "DELETE": response = requests.delete(url, headers=HEADERS) elif method == "PUT": response = requests.put(url, headers=HEADERS, json=data) return response def check_db(doc_id): """Prüfe syncId in Datenbank""" result = subprocess.run([ "docker", "exec", "espocrm-db", "mariadb", "-u", "espocrm", "-pdatabase_password", "espocrm", "-e", f"SELECT sync_id FROM c_a_i_collection_c_dokumente WHERE c_dokumente_id='{doc_id}' AND deleted=0;" ], capture_output=True, text=True) lines = result.stdout.strip().split('\n') if len(lines) > 1: return lines[1].strip() return "NULL" print("\n" + "="*80) print(" "*20 + "Many-to-Many syncId Test") print("="*80 + "\n") doc_id = None collection_id = None test_sync_id = f"SYNC-{datetime.now().strftime('%Y%m%d-%H%M%S')}" try: # 1. Entities erstellen print("1️⃣ Erstelle Entities...") doc = api_request("POST", "CDokumente", { "name": f"Final Test Doc {datetime.now().strftime('%H:%M:%S')}" }).json() doc_id = doc['id'] print(f" Doc: {doc_id}") collection = api_request("POST", "CAICollections", { "name": f"Final Test Col {datetime.now().strftime('%H:%M:%S')}" }).json() collection_id = collection['id'] print(f" Col: {collection_id}\n") # 2. Verknüpfung erstellen print("2️⃣ Erstelle Verknüpfung...") link_response = api_request("POST", f"CDokumente/{doc_id}/cAICollections", { "id": collection_id }) print(f" Status: {link_response.status_code}\n") # 3. Direkt in DB schreiben print("3️⃣ Setze syncId direkt in Datenbank...") db_result = subprocess.run([ "docker", "exec", "espocrm-db", "mariadb", "-u", "espocrm", "-pdatabase_password", "espocrm", "-e", f"UPDATE c_a_i_collection_c_dokumente SET sync_id='{test_sync_id}' WHERE c_dokumente_id='{doc_id}' AND c_a_i_collections_id='{collection_id}';" ], capture_output=True, text=True) print(f" syncId in DB gesetzt: {test_sync_id}\n") # 4. DB-Verifikation print("4️⃣ Verifiziere in Datenbank...") sync_in_db = check_db(doc_id) if sync_in_db == test_sync_id: print(f" ✅ syncId in DB: {sync_in_db}\n") else: print(f" ❌ syncId falsch/NULL: {sync_in_db}\n") # 5. Rufe über API ab print("5️⃣ Rufe Beziehung über API ab...") relations_response = api_request("GET", f"CDokumente/{doc_id}/cAICollections") if relations_response.status_code == 200: relations = relations_response.json() print(f" Status: 200\n") if 'list' in relations and len(relations['list']) > 0: first = relations['list'][0] print(" Felder in Response:") for key in sorted(first.keys()): value = first[key] if isinstance(value, str) and len(value) > 50: value = value[:50] + "..." print(f" - {key}: {value}") print() if 'syncId' in first and first['syncId'] == test_sync_id: print(f" ✅ syncId in API-Response: {first['syncId']}") result_status = "✅ VOLLSTÄNDIG ERFOLGREICH" elif 'syncId' in first: print(f" ⚠️ syncId in API vorhanden, aber falscher Wert: {first['syncId']}") result_status = "⚠️ TEILWEISE ERFOLGREICH" else: print(f" ❌ syncId NICHT in API-Response") result_status = "❌ API GIBT KEINE ADDITIONALCOLUMNS ZURÜCK" else: print(" ❌ Keine Beziehung gefunden") result_status = "❌ FEHLGESCHLAGEN" else: print(f" ❌ API-Fehler: {relations_response.status_code}") result_status = "❌ FEHLGESCHLAGEN" finally: print() print("6️⃣ Cleanup...") if doc_id: api_request("DELETE", f"CDokumente/{doc_id}") print(f" ✓ Dokument gelöscht") if collection_id: api_request("DELETE", f"CAICollections/{collection_id}") print(f" ✓ Collection gelöscht") print("\n" + "="*80) print(result_status) print("="*80 + "\n")