Fixed most stuff. Need to be very careful before actually deploying the changes
This commit is contained in:
1811315
config/alpinebits.log
1811315
config/alpinebits.log
File diff suppressed because one or more lines are too long
34
sql_analysis.md
Normal file
34
sql_analysis.md
Normal 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;
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
@@ -38,6 +38,7 @@ from pathlib import Path
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.exc import MultipleResultsFound
|
||||||
|
|
||||||
from .customer_service import CustomerService
|
from .customer_service import CustomerService
|
||||||
from .db import Customer, Reservation
|
from .db import Customer, Reservation
|
||||||
@@ -404,24 +405,18 @@ class CSVImporter:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# If we extracted ages but num_children says there are different number,
|
# If we extracted ages but num_children says there are different number,
|
||||||
# use the actual extracted ages count
|
# compact the list to match num_children. Remove ages "0" first
|
||||||
if len(children_ages) > 0 and len(children_ages) != num_children:
|
if len(children_ages) > num_children:
|
||||||
_LOGGER.info(
|
# Remove ages "0" first, but only as many as needed
|
||||||
"Row %d: Correcting num_children from %d to %d (actual ages found)",
|
num_to_remove = len(children_ages) - num_children
|
||||||
row_num,
|
|
||||||
num_children,
|
for _ in range(num_to_remove):
|
||||||
len(children_ages),
|
if 0 in children_ages:
|
||||||
)
|
children_ages.remove(0)
|
||||||
num_children = len(children_ages)
|
else:
|
||||||
elif num_children > 0 and len(children_ages) == 0:
|
# If no "0" ages left, just remove the last one
|
||||||
# num_children says there should be children but we found none
|
children_ages.pop()
|
||||||
# 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
|
|
||||||
|
|
||||||
# Generate unique ID (use submission timestamp if available, else row number)
|
# Generate unique ID (use submission timestamp if available, else row number)
|
||||||
submission_ts = str(row.get("submission_timestamp", "")).strip()
|
submission_ts = str(row.get("submission_timestamp", "")).strip()
|
||||||
@@ -545,13 +540,28 @@ class CSVImporter:
|
|||||||
|
|
||||||
query = query.where(or_(*filters))
|
query = query.where(or_(*filters))
|
||||||
result = await self.db_session.execute(query)
|
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:
|
if existing:
|
||||||
# Update customer data if needed
|
# Update customer data if needed
|
||||||
return await self.customer_service.update_customer(
|
try:
|
||||||
|
existing_customer = await self.customer_service.update_customer(
|
||||||
existing, customer_data
|
existing, customer_data
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print(customer_data)
|
||||||
|
print("---")
|
||||||
|
print(existing)
|
||||||
|
|
||||||
|
|
||||||
|
raise
|
||||||
|
|
||||||
|
return existing_customer
|
||||||
|
|
||||||
# Create new customer
|
# Create new customer
|
||||||
return await self.customer_service.create_customer(customer_data)
|
return await self.customer_service.create_customer(customer_data)
|
||||||
|
|||||||
@@ -253,6 +253,9 @@ class Customer(Base):
|
|||||||
name_title = Column(String) # Added for XML
|
name_title = Column(String) # Added for XML
|
||||||
reservations = relationship("Reservation", back_populates="customer")
|
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
|
@staticmethod
|
||||||
def _normalize_and_hash(value):
|
def _normalize_and_hash(value):
|
||||||
"""Normalize and hash a value according to Meta Conversion API requirements."""
|
"""Normalize and hash a value according to Meta Conversion API requirements."""
|
||||||
|
|||||||
Reference in New Issue
Block a user