Fixed most stuff. Need to be very careful before actually deploying the changes

This commit is contained in:
Jonas Linter
2025-11-17 19:25:43 +01:00
parent c91290f1b0
commit bb20000031
4 changed files with 1453617 additions and 357795 deletions

File diff suppressed because one or more lines are too long

34
sql_analysis.md Normal file
View File

@@ -0,0 +1,34 @@
```
select sum(room.total_revenue::float)
from alpinebits.conversions as con
join alpinebits.room_reservations as room on room.conversion_id = con.id
join alpinebits.reservations as res on res.id = con.reservation_id
where con.reservation_id is not null and room.total_revenue is not null
and res.start_date <= room.arrival_date + INTERVAL '7 days'
;
```
```
select res.created_at, con.reservation_date, res.start_date, room.arrival_date,res.end_date,
room.departure_date, reservation_type, booking_channel, advertising_medium,
guest_first_name,guest_last_name, total_revenue,
room.room_status
from alpinebits.conversions as con
join alpinebits.room_reservations as room on room.conversion_id = con.id
join alpinebits.reservations as res on res.id = con.reservation_id
where con.reservation_id is not null and room.total_revenue is not null
and res.start_date <= room.arrival_date + INTERVAL '7 days'
order by reservation_date;
```

View File

@@ -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
@@ -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)

View File

@@ -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."""