Fix: Simply-CRM sync opretter nu manglende customers i Hub (ikke kun opdaterer eksisterende)
This commit is contained in:
parent
808a8bb2ee
commit
fa55e6b98e
@ -547,7 +547,32 @@ async def sync_economic_numbers_from_simplycrm():
|
||||
hub_customers = execute_query("SELECT id, name FROM customers")
|
||||
logger.info(f"📊 Found {len(hub_customers)} customers in Hub")
|
||||
|
||||
# Match og opdater
|
||||
# Build set af eksisterende kunde-navne (normalized)
|
||||
existing_customer_names = {c['name'].strip().lower() for c in hub_customers}
|
||||
|
||||
# Find customers der mangler i Hub
|
||||
missing_customers = []
|
||||
for name_key, data in name_to_economic.items():
|
||||
if name_key not in existing_customer_names:
|
||||
missing_customers.append(data)
|
||||
|
||||
logger.info(f"📊 Found {len(missing_customers)} customers missing in Hub")
|
||||
|
||||
# Opret manglende customers
|
||||
for missing in missing_customers:
|
||||
try:
|
||||
execute_query(
|
||||
"""INSERT INTO customers (name, economic_customer_number, created_at, updated_at)
|
||||
VALUES (%s, %s, NOW(), NOW())""",
|
||||
(missing['original_name'], missing['economic_customer_number'])
|
||||
)
|
||||
logger.info(f"➕ Created customer: {missing['original_name']} → e-conomic #{missing['economic_customer_number']}")
|
||||
stats["hub_customers_created"] = stats.get("hub_customers_created", 0) + 1
|
||||
except Exception as create_error:
|
||||
logger.error(f"❌ Failed to create customer {missing['original_name']}: {create_error}")
|
||||
stats["errors"] += 1
|
||||
|
||||
# Match og opdater eksisterende
|
||||
for customer in hub_customers:
|
||||
customer_name_key = customer['name'].strip().lower()
|
||||
|
||||
@ -586,7 +611,7 @@ async def sync_economic_numbers_from_simplycrm():
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Synced {stats['hub_customers_updated']} customers with e-conomic numbers from Simply-CRM",
|
||||
"message": f"Synced {stats['hub_customers_updated']} existing customers and created {stats.get('hub_customers_created', 0)} new customers from Simply-CRM",
|
||||
"stats": stats
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user