Fixed most stuff. Need to be very careful before actually deploying the changes
This commit is contained in:
@@ -38,6 +38,7 @@ from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.exc import MultipleResultsFound
|
||||
|
||||
from .customer_service import CustomerService
|
||||
from .db import Customer, Reservation
|
||||
@@ -391,7 +392,7 @@ class CSVImporter:
|
||||
age = int(float(age_val))
|
||||
if 0 <= age <= 17:
|
||||
children_ages.append(age)
|
||||
except (ValueError, TypeError):
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
# Debug: log extraction details
|
||||
@@ -404,24 +405,18 @@ class CSVImporter:
|
||||
)
|
||||
|
||||
# If we extracted ages but num_children says there are different number,
|
||||
# use the actual extracted ages count
|
||||
if len(children_ages) > 0 and len(children_ages) != num_children:
|
||||
_LOGGER.info(
|
||||
"Row %d: Correcting num_children from %d to %d (actual ages found)",
|
||||
row_num,
|
||||
num_children,
|
||||
len(children_ages),
|
||||
)
|
||||
num_children = len(children_ages)
|
||||
elif num_children > 0 and len(children_ages) == 0:
|
||||
# num_children says there should be children but we found none
|
||||
# Set to 0 to avoid validation error
|
||||
_LOGGER.warning(
|
||||
"Row %d: num_children=%d but no ages found. Setting num_children=0",
|
||||
row_num,
|
||||
num_children,
|
||||
)
|
||||
num_children = 0
|
||||
# compact the list to match num_children. Remove ages "0" first
|
||||
if len(children_ages) > num_children:
|
||||
# Remove ages "0" first, but only as many as needed
|
||||
num_to_remove = len(children_ages) - num_children
|
||||
|
||||
for _ in range(num_to_remove):
|
||||
if 0 in children_ages:
|
||||
children_ages.remove(0)
|
||||
else:
|
||||
# If no "0" ages left, just remove the last one
|
||||
children_ages.pop()
|
||||
|
||||
|
||||
# Generate unique ID (use submission timestamp if available, else row number)
|
||||
submission_ts = str(row.get("submission_timestamp", "")).strip()
|
||||
@@ -545,13 +540,28 @@ class CSVImporter:
|
||||
|
||||
query = query.where(or_(*filters))
|
||||
result = await self.db_session.execute(query)
|
||||
existing = result.scalar_one_or_none()
|
||||
try:
|
||||
existing = result.scalar()
|
||||
except MultipleResultsFound:
|
||||
compiled_query = query.compile(compile_kwargs={"literal_binds": True})
|
||||
_LOGGER.error(compiled_query)
|
||||
|
||||
if existing:
|
||||
# Update customer data if needed
|
||||
return await self.customer_service.update_customer(
|
||||
existing, customer_data
|
||||
)
|
||||
try:
|
||||
existing_customer = await self.customer_service.update_customer(
|
||||
existing, customer_data
|
||||
)
|
||||
except Exception as e:
|
||||
|
||||
print(customer_data)
|
||||
print("---")
|
||||
print(existing)
|
||||
|
||||
|
||||
raise
|
||||
|
||||
return existing_customer
|
||||
|
||||
# Create new customer
|
||||
return await self.customer_service.create_customer(customer_data)
|
||||
|
||||
@@ -253,6 +253,9 @@ class Customer(Base):
|
||||
name_title = Column(String) # Added for XML
|
||||
reservations = relationship("Reservation", back_populates="customer")
|
||||
|
||||
def __repr__(self):
|
||||
return f"Customer (id={self.id}, contact_id={self.contact_id}, email={self.email_address}), given_name={self.given_name} surname={self.surname}), phone={self.phone}, city={self.city_name}), postal_code={self.postal_code}, country_code={self.country_code})"
|
||||
|
||||
@staticmethod
|
||||
def _normalize_and_hash(value):
|
||||
"""Normalize and hash a value according to Meta Conversion API requirements."""
|
||||
|
||||
Reference in New Issue
Block a user