More cleanup
This commit is contained in:
@@ -1042,87 +1042,6 @@ class ConversionService:
|
||||
)
|
||||
return matches[0]
|
||||
|
||||
async def _match_by_guest_details(
|
||||
self,
|
||||
hotel_id: str | None,
|
||||
guest_first_name: str | None,
|
||||
guest_last_name: str | None,
|
||||
guest_email: str | None,
|
||||
session: AsyncSession | None = None,
|
||||
) -> Reservation | None:
|
||||
"""Match reservation by guest name and email using cached data.
|
||||
|
||||
This method uses the reservation cache populated at the start of XML processing.
|
||||
If cache is not available, falls back to database queries.
|
||||
|
||||
Args:
|
||||
hotel_id: Hotel ID for filtering
|
||||
guest_first_name: Guest first name
|
||||
guest_last_name: Guest last name
|
||||
guest_email: Guest email
|
||||
session: AsyncSession to use. If None, uses self.session.
|
||||
|
||||
Returns:
|
||||
Matched Reservation or None
|
||||
|
||||
"""
|
||||
if session is None:
|
||||
session = self.session
|
||||
|
||||
# Try to use cache first
|
||||
if self._cache_initialized and self._reservation_cache:
|
||||
all_reservations = []
|
||||
|
||||
# Get reservations from cache for this hotel
|
||||
if hotel_id and hotel_id in self._reservation_cache:
|
||||
# Extract reservations AND reattach their cached hashed_customer relationships
|
||||
for reservation, hashed_customer in self._reservation_cache[hotel_id]:
|
||||
if reservation.customer:
|
||||
# Manually set the hashed_version from cache to ensure it's available
|
||||
reservation.customer.hashed_version = hashed_customer
|
||||
all_reservations.append(reservation)
|
||||
elif not hotel_id:
|
||||
# If no hotel_id specified, use all cached reservations
|
||||
for reservations_list in self._reservation_cache.values():
|
||||
for reservation, hashed_customer in reservations_list:
|
||||
if reservation.customer:
|
||||
# Manually set the hashed_version from cache to ensure it's available
|
||||
reservation.customer.hashed_version = hashed_customer
|
||||
all_reservations.append(reservation)
|
||||
|
||||
if all_reservations:
|
||||
_LOGGER.debug(
|
||||
"Using cached reservations for matching (hotel=%s, count=%d)",
|
||||
hotel_id,
|
||||
len(all_reservations),
|
||||
)
|
||||
return self._match_reservations_by_guest_details(
|
||||
all_reservations,
|
||||
guest_first_name,
|
||||
guest_last_name,
|
||||
guest_email,
|
||||
)
|
||||
|
||||
# Fallback: Query database if cache is not available or empty
|
||||
_LOGGER.debug(
|
||||
"Cache unavailable or empty, falling back to database query (hotel=%s)",
|
||||
hotel_id,
|
||||
)
|
||||
from sqlalchemy.orm import selectinload
|
||||
query = select(Reservation).options(
|
||||
selectinload(Reservation.customer).selectinload(Customer.hashed_version)
|
||||
)
|
||||
|
||||
if hotel_id:
|
||||
query = query.where(Reservation.hotel_code == hotel_id)
|
||||
|
||||
db_result = await session.execute(query)
|
||||
all_reservations = db_result.scalars().all()
|
||||
|
||||
return self._match_reservations_by_guest_details(
|
||||
all_reservations, guest_first_name, guest_last_name, guest_email
|
||||
)
|
||||
|
||||
def _filter_reservations_by_guest_details(
|
||||
self,
|
||||
reservations: list[Reservation],
|
||||
|
||||
Reference in New Issue
Block a user