Added reservation_service aswell
This commit is contained in:
@@ -15,7 +15,6 @@ from enum import Enum, IntEnum
|
||||
from typing import Any, Optional, override
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from sqlalchemy import select
|
||||
from xsdata.formats.dataclass.serializers.config import SerializerConfig
|
||||
from xsdata_pydantic.bindings import XmlParser, XmlSerializer
|
||||
|
||||
@@ -26,6 +25,7 @@ from alpine_bits_python.alpine_bits_helpers import (
|
||||
from alpine_bits_python.logging_config import get_logger
|
||||
|
||||
from .db import AckedRequest, Customer, Reservation
|
||||
from .reservation_service import ReservationService
|
||||
from .generated.alpinebits import (
|
||||
OtaNotifReportRq,
|
||||
OtaNotifReportRs,
|
||||
@@ -510,32 +510,29 @@ class ReadAction(AlpineBitsAction):
|
||||
hotel_read_request.selection_criteria.start
|
||||
)
|
||||
|
||||
# query all reservations for this hotel from the database, where start_date is greater than or equal to the given start_date
|
||||
# Use ReservationService to query reservations
|
||||
reservation_service = ReservationService(dbsession)
|
||||
|
||||
stmt = (
|
||||
select(Reservation, Customer)
|
||||
.join(Customer, Reservation.customer_id == Customer.id)
|
||||
.filter(Reservation.hotel_code == hotelid)
|
||||
)
|
||||
if start_date:
|
||||
_LOGGER.info("Filtering reservations from start date %s", start_date)
|
||||
stmt = stmt.filter(Reservation.created_at >= start_date)
|
||||
# remove reservations that have been acknowledged via client_id
|
||||
elif client_info.client_id:
|
||||
subquery = (
|
||||
select(Reservation.id)
|
||||
.join(
|
||||
AckedRequest,
|
||||
Reservation.md5_unique_id == AckedRequest.unique_id,
|
||||
reservation_customer_pairs = (
|
||||
await reservation_service.get_reservations_with_filters(
|
||||
start_date=start_date, hotel_code=hotelid
|
||||
)
|
||||
)
|
||||
elif client_info.client_id:
|
||||
# Remove reservations that have been acknowledged via client_id
|
||||
reservation_customer_pairs = (
|
||||
await reservation_service.get_unacknowledged_reservations(
|
||||
client_id=client_info.client_id, hotel_code=hotelid
|
||||
)
|
||||
)
|
||||
else:
|
||||
reservation_customer_pairs = (
|
||||
await reservation_service.get_reservations_with_filters(
|
||||
hotel_code=hotelid
|
||||
)
|
||||
.filter(AckedRequest.client_id == client_info.client_id)
|
||||
)
|
||||
stmt = stmt.filter(~Reservation.id.in_(subquery))
|
||||
|
||||
result = await dbsession.execute(stmt)
|
||||
reservation_customer_pairs: list[tuple[Reservation, Customer]] = (
|
||||
result.all()
|
||||
) # List of (Reservation, Customer) tuples
|
||||
|
||||
_LOGGER.info(
|
||||
"Querying reservations and customers for hotel %s from database",
|
||||
@@ -616,19 +613,16 @@ class NotifReportReadAction(AlpineBitsAction):
|
||||
"Error: Something went wrong", HttpStatusCode.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
timestamp = datetime.now(ZoneInfo("UTC"))
|
||||
# Use ReservationService to record acknowledgements
|
||||
reservation_service = ReservationService(dbsession)
|
||||
|
||||
for entry in (
|
||||
notif_report_details.hotel_notif_report.hotel_reservations.hotel_reservation
|
||||
): # type: ignore
|
||||
unique_id = entry.unique_id.id
|
||||
acked_request = AckedRequest(
|
||||
unique_id=unique_id,
|
||||
client_id=client_info.client_id,
|
||||
timestamp=timestamp,
|
||||
await reservation_service.record_acknowledgement(
|
||||
client_id=client_info.client_id, unique_id=unique_id
|
||||
)
|
||||
dbsession.add(acked_request)
|
||||
|
||||
await dbsession.commit()
|
||||
|
||||
return AlpineBitsResponse(response_xml, HttpStatusCode.OK)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user