resa-padel/resa_padel/booking.py

92 lines
3.3 KiB
Python

import asyncio
import logging
import config
from gestion_sports_services import GestionSportsServices
from models import Action, BookingFilter, Club, Court, Tournament, User
LOGGER = logging.getLogger(__name__)
async def book_court(
club: Club, users: list[User], booking_filter: BookingFilter
) -> tuple[Court, User]:
"""
Book any court that meets the condition from the filter. IThe user that will make
the booking is chosen among a list of users and should not have any ongoing bookings
:param club: the club in which the booking will be made
:param users: the list of users who have an account in the club
:param booking_filter: the conditions the court to book should meet
:return: a tuple containing the court that was booked and the user who made the
booking
"""
service = GestionSportsServices()
for user in users:
if not await service.has_user_available_slots(user, club):
return await service.book(club, user, booking_filter), user
async def cancel_booking(club: Club, user: User, booking_filter: BookingFilter) -> None:
"""
Cancel the booking that matches the specified filter
:param club: the club in which the booking was made
:param user: the user who made the booking
:param booking_filter: the conditions to meet to cancel the booking
"""
service = GestionSportsServices()
await service.cancel_booking(user, club, booking_filter)
async def get_tournaments(club: Club, user: User) -> list[Tournament]:
"""
Get the list of all current tournaments, their price, date and availability
:param club: the club in which the tournaments are
:param user: a user of the club in order to retrieve the information
"""
service = GestionSportsServices()
return await service.get_all_tournaments(user, club)
def main() -> tuple[Court, User] | list[Tournament] | None:
"""
Main function used to book a court
:return: the id of the booked court, or None if no court was booked
"""
action = config.get_action()
if action == Action.BOOK:
club = config.get_club()
users = config.get_users(club.id)
booking_filter = config.get_booking_filter()
LOGGER.info(
f"Booking a court of {booking_filter.sport_name} at {booking_filter.date} "
f"at club {club.name}"
)
court_booked, user = asyncio.run(book_court(club, users, booking_filter))
if court_booked:
LOGGER.info(
f"Court of {booking_filter.sport_name} {court_booked} was booked "
f"successfully at {booking_filter.date} at club {club.name} "
f"for user {user}"
)
return court_booked, user
else:
LOGGER.info(
f"No court of {booking_filter.sport_name} at {booking_filter.date} "
f"at club {club.name} was booked"
)
elif action == Action.CANCEL:
user = config.get_user()
club = config.get_club()
booking_filter = config.get_booking_filter()
asyncio.run(cancel_booking(club, user, booking_filter))
elif action == Action.TOURNAMENTS:
user = config.get_user()
club = config.get_club()
return asyncio.run(get_tournaments(club, user))