Fix: Tilføjet pagination til kontakter + forbedret relation linking
This commit is contained in:
parent
62fc3cb4dd
commit
64935b5808
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user