29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
from asgiref.sync import sync_to_async
|
||
from hotels.models import UserHotel, Hotel, Reservation
|
||
from users.models import User
|
||
|
||
|
||
async def get_user_from_chat_id(chat_id):
|
||
"""Получение пользователя из базы по chat_id."""
|
||
return await sync_to_async(User.objects.filter(chat_id=chat_id).first)()
|
||
|
||
|
||
async def get_hotels_for_user(user_id):
|
||
"""Получение отелей, связанных с пользователем."""
|
||
user = await sync_to_async(User.objects.filter(chat_id=user_id).first)()
|
||
if not user:
|
||
return []
|
||
return await sync_to_async(list)(
|
||
Hotel.objects.filter(userhotel__user=user).distinct()
|
||
)
|
||
|
||
|
||
async def get_reservations(hotel_id, start_date=None, end_date=None):
|
||
"""Получение статистики бронирований по отелю с гостями."""
|
||
query = Reservation.objects.filter(hotel_id=hotel_id)
|
||
if start_date:
|
||
query = query.filter(check_in__gte=start_date)
|
||
if end_date:
|
||
query = query.filter(check_out__lte=end_date)
|
||
return await sync_to_async(list)(query.prefetch_related('guests'))
|