From 64935b5808176bf840a6b431a3da584054171961 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 22 Dec 2025 13:24:41 +0100 Subject: [PATCH] =?UTF-8?q?Fix:=20Tilf=C3=B8jet=20pagination=20til=20konta?= =?UTF-8?q?kter=20+=20forbedret=20relation=20linking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/system/backend/sync_router.py | 41 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/app/system/backend/sync_router.py b/app/system/backend/sync_router.py index ff0fe62..3893ddf 100644 --- a/app/system/backend/sync_router.py +++ b/app/system/backend/sync_router.py @@ -153,15 +153,37 @@ async def sync_vtiger_contacts() -> Dict[str, Any]: vtiger = get_vtiger_service() - # Query vTiger for all contacts (no limit) - query = "SELECT id, firstname, lastname, email, phone, mobile, title, department, account_id FROM Contacts;" + # Fetch ALL contacts with pagination (same as accounts) + all_contacts = [] + last_id = None + batch_size = 100 - contacts = await vtiger.query(query) - logger.info(f"📥 Fetched {len(contacts)} contacts from vTiger") + while True: + # Build query with ID filter to paginate + if last_id is None: + query = f"SELECT id, firstname, lastname, email, phone, mobile, title, department, account_id FROM Contacts ORDER BY id LIMIT {batch_size};" + else: + query = f"SELECT id, firstname, lastname, email, phone, mobile, title, department, account_id FROM Contacts WHERE id > '{last_id}' ORDER BY id LIMIT {batch_size};" + + batch = await vtiger.query(query) + + if not batch or len(batch) == 0: + break + + all_contacts.extend(batch) + last_id = batch[-1].get('id') + logger.info(f"📥 Fetched batch: {len(batch)} contacts (last ID: {last_id}, total: {len(all_contacts)})") + + if len(batch) < batch_size: + break + + logger.info(f"📥 Fetched total of {len(all_contacts)} contacts from vTiger") + contacts = all_contacts created_count = 0 updated_count = 0 skipped_count = 0 + linked_count = 0 for contact in contacts: vtiger_contact_id = contact.get('id') @@ -246,13 +268,12 @@ async def sync_vtiger_contacts() -> Dict[str, Any]: if account_id and contact_id: # Find customer by vTiger account ID customer = execute_query( - "SELECT id FROM customers WHERE vtiger_id = %s", + "SELECT id, name FROM customers WHERE vtiger_id = %s", (account_id,) ) if customer: - customer_name_result = execute_query("SELECT name FROM customers WHERE id = %s", (customer[0]['id'],)) - customer_name = customer_name_result[0]['name'] if customer_name_result else 'ukendt' + customer_name = customer[0]['name'] # Check if relationship exists existing_rel = execute_query( "SELECT id FROM contact_companies WHERE contact_id = %s AND customer_id = %s", @@ -265,14 +286,18 @@ async def sync_vtiger_contacts() -> Dict[str, Any]: "INSERT INTO contact_companies (contact_id, customer_id, is_primary) VALUES (%s, %s, false)", (contact_id, customer[0]['id']) ) + linked_count += 1 logger.info(f"🔗 Linket kontakt {first_name} {last_name} til firma: {customer_name}") + else: + logger.debug(f"⚠️ Kunde ikke fundet for account_id={account_id} (kontakt: {first_name} {last_name})") - logger.info(f"✅ vTiger kontakt sync fuldført: {created_count} oprettet, {updated_count} opdateret, {skipped_count} sprunget over af {len(contacts)} totalt") + logger.info(f"✅ vTiger kontakt sync fuldført: {created_count} oprettet, {updated_count} opdateret, {linked_count} linket, {skipped_count} sprunget over af {len(contacts)} totalt") return { "status": "success", "created": created_count, "updated": updated_count, + "linked": linked_count, "skipped": skipped_count, "total_processed": len(contacts) }