async def get_current_user_bookings(uuid: int = Depends(get_uuid_from_xtoken)): path = f"/bookings?roomOwnerId={uuid}" bookings_received, _ = Requester.payment_fetch( method="GET", path=path, expected_statuses={HTTP_200_OK}) path = f"/bookings?bookerId={uuid}" bookings_made, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) # TODO: Change BookingDB model to match camelcase in payment server for i in range(len(bookings_made)): bookings_made[i] = payment_camel_to_snake(bookings_made[i]) for i in range(len(bookings_received)): bookings_received[i] = payment_camel_to_snake(bookings_received[i]) bookings = { "made": { "amount": len(bookings_made), "bookings": bookings_made, }, "received": { "amount": len(bookings_received), "bookings": bookings_received, }, } return bookings
async def delete_booking(booking_id: int, uuid: int = Depends(get_uuid_from_xtoken)): path = f"/bookings/{booking_id}" booking, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) room_id = booking["roomId"] room_owner_id = booking["roomOwnerId"] if not AuthSender.has_permission_to_modify(uuid, room_owner_id): raise UnauthorizedRequestError("Can't reject other users bookings") path = f"/bookings/{booking_id}" book_deleted, _ = Requester.payment_fetch("DELETE", path, {HTTP_200_OK}) # Delete the rejected booking in post server, # if it is not found it is also OK! booking_path = f"/rooms/{room_id}/bookings/{booking_id}" booking, _ = Requester.room_srv_fetch("DELETE", booking_path, {HTTP_200_OK, HTTP_404_NOT_FOUND}) # TODO: Change BookingDB model to match camelcase in payment server booking_camel = payment_camel_to_snake(book_deleted) return booking_camel
async def accept_booking(booking_id: int, uuid: int = Depends(get_uuid_from_xtoken)): path = f"/bookings/{booking_id}" booking, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) room_owner_id = booking["roomOwnerId"] if not AuthSender.has_permission_to_modify(uuid, room_owner_id): raise UnauthorizedRequestError("Can't accept other users bookings") path = f"/bookings/{booking_id}/accept" payload_accept = {"roomOwnerId": room_owner_id} book_accepted, _ = Requester.payment_fetch("POST", path, {HTTP_200_OK}, payload=payload_accept) # TODO: Change BookingDB model to match camelcase in payment server booking_camel = payment_camel_to_snake(book_accepted) # Send notification sender, _ = Requester.user_srv_fetch( "GET", f"/users/{booking_camel['room_owner_id']}", {HTTP_200_OK}) sender_name = f"{sender['firstname']} {sender['lastname']}" room, _ = Requester.room_srv_fetch("GET", f"/rooms/{booking_camel['room_id']}", {HTTP_200_OK}) room_title = room["title"] notifier.send_booking_accepted_notification(sender_name, room_title, booking_camel["booker_id"]) return booking_camel
async def create_room(payload: RoomSchema, uuid: int = Depends(get_uuid_from_xtoken)): path = f"/users/{uuid}" user, _ = Requester.user_srv_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) owner = f"{user['firstname']} {user['lastname']}" req_payload = payload.dict() req_payload.update({"owner_uuid": uuid, "owner": owner}) payment_payload = { "ownerId": req_payload["owner_uuid"], "price": req_payload["price_per_day"], } # Create room in payment server and generate an ID room_pay_srv, _ = Requester.payment_fetch( method="POST", path="/rooms", expected_statuses={HTTP_201_CREATED}, payload=payment_payload, ) # Add id to the room created in room server req_payload["id"] = room_pay_srv["id"] room, _ = Requester.room_srv_fetch( method="POST", path="/rooms", expected_statuses={HTTP_201_CREATED}, payload=req_payload, ) return room
async def create_user( payload: UserSchema, x_access_token: Optional[str] = Header(None), ): auth_payload = {"email": payload.dict()["email"]} auth_header = {"x-access-token": x_access_token} registered_user, _ = Requester.auth_srv_fetch( method="POST", path="/user/registered", expected_statuses={HTTP_201_CREATED}, payload=auth_payload, extra_headers=auth_header, ) path = "/users" payload_user = payload.dict() payload_user.update({"id": registered_user["uuid"]}) user, _ = Requester.user_srv_fetch( method="POST", path=path, expected_statuses={HTTP_201_CREATED}, payload=payload_user, ) # create wallet path = "/wallets" payload_wallet = {"uuid": registered_user["uuid"]} wallet, _ = Requester.payment_fetch( method="POST", path=path, expected_statuses={HTTP_201_CREATED}, payload=payload_wallet, ) return user
async def create_new_booking( payload: BookingSchema, uuid: int = Depends(get_uuid_from_xtoken), ): room_id = payload.dict()["room_id"] room_path = f"/rooms/{room_id}" room, _ = Requester.room_srv_fetch("GET", room_path, {HTTP_200_OK}) if not AuthSender.can_book_room(room["owner_uuid"], uuid): raise NotAllowedRequestError("Can't create booking of your own room") if room["blocked"]: raise NotAllowedRequestError( "Can't create booking because the room is blocked") # Create intent book in payment server booking_path = "/bookings" payload_booking = { "bookerId": uuid, "roomId": room_id, "dateFrom": payload.dict()["date_from"].strftime("%d-%m-%Y"), "dateTo": payload.dict()["date_to"].strftime("%d-%m-%Y"), } booking, _ = Requester.payment_fetch( method="POST", path=booking_path, expected_statuses={HTTP_201_CREATED}, payload=payload_booking, ) # Create booking in room server booking_path = f"/rooms/{room_id}/bookings" # Add the booking id received from the payment server payload_booking = { "id": booking["id"], "date_from": payload.dict()["date_from"].strftime("%Y-%m-%d"), "date_to": payload.dict()["date_to"].strftime("%Y-%m-%d"), } booking_room, _ = Requester.room_srv_fetch("POST", booking_path, {HTTP_201_CREATED}, payload=payload_booking) # TODO: Change BookingDB model to match camelcase in payment server booking_camel = payment_camel_to_snake(booking) # Send notification sender, _ = Requester.user_srv_fetch("GET", f"/users/{uuid}", {HTTP_200_OK}) sender_name = f"{sender['firstname']} {sender['lastname']}" notifier.send_new_booking_received_notification(sender_name, room["title"], room["owner_uuid"]) return booking_camel
async def get_booking(booking_id: int): path = f"/bookings/{booking_id}" booking, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) # TODO: Change BookingDB model to match camelcase in payment server booking_camel = payment_camel_to_snake(booking) return booking_camel
async def delete_room(room_id: int, viewer_uuid: int = Depends(get_uuid_from_xtoken)): path = "/rooms" + f"/{room_id}" room, _ = Requester.room_srv_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) if not AuthSender.has_permission_to_modify(viewer_uuid, room["owner_uuid"]): raise BadRequestError("You can't delete other users rooms!") path = "/rooms" + f"/{room_id}" room, _ = Requester.room_srv_fetch(method="DELETE", path=path, expected_statuses={HTTP_200_OK}) room_pay, _ = Requester.payment_fetch(method="DELETE", path=path, expected_statuses={HTTP_200_OK}) return room
async def get_all_bookings( bookerId: Optional[int] = None, roomOwnerId: Optional[int] = None, roomId: Optional[int] = None, bookingStatus: Optional[int] = None, ): query = "?" path = "/bookings" if bookerId is not None: query = query + f"bookerId={bookerId}&" if roomOwnerId is not None: query = query + f"roomOwnerId={roomOwnerId}&" if roomId is not None: query = query + f"roomId={roomId}&" if bookingStatus is not None: query = query + f"bookingStatus={bookingStatus}&" if len(query) > 1: # strip last & in the query query = query[:(len(query) - 1)] path = path + query bookings, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) # TODO: Change BookingDB model to match camelcase in payment server for i in range(len(bookings)): bookings[i] = payment_camel_to_snake(bookings[i]) booking_list = {"amount": len(bookings), "bookings": bookings} return booking_list
async def get_current_user_wallet(uuid: int = Depends(get_uuid_from_xtoken)): path = f"/wallets/{uuid}" wallet, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) return wallet