Esempio n. 1
0
    def make_order(self, requester, room, order_item):
        # type: (str, str, str) -> OrderItem
        """Adds an item to the existing order.

        Args:
            requester: User ID of buyer
            room: Chat room ID
            order_item: Name of item being ordered

        Returns:
            Instance of OrderItem

        Raises:
            InvalidAction: if there are no open offers
            InvalidAction: if item being ordered is not valid
        """
        active_offer = self.deps.db_get_active_order(room=room)
        if not active_offer:
            raise InvalidAction('There are no open offers in this room')

        if not self.deps.is_valid_menu_item(item_name=order_item):
            raise InvalidAction(order_item + ' is not a valid menu item')

        order_item = self.deps.db_add_order_item(room=room,
                                                 item=order_item,
                                                 recipient=requester)
        return order_item
Esempio n. 2
0
    def get_order_details(self, room):
        try:
            order_details = self._orders[room]
        except KeyError:
            raise InvalidAction('No open orders for room ' + room)

        return deepcopy(order_details)
Esempio n. 3
0
 def archive_order(self, order_details):
     # type: (OrderDetails) -> OrderDetails
     """Archives a closed order."""
     if order_details.close_ts is None:
         raise InvalidAction('Cannot archive open orders')
     stored = self.deps.db_store_closed_order(order_details=order_details)
     return stored
Esempio n. 4
0
    def close_order(self, room):
        # type: (str) -> OrderDetails
        try:
            order = self._orders.pop(room)
        except KeyError:
            raise InvalidAction('No open orders for room ' + room)

        closed_order = order._replace(close_ts=self.deps.get_current_ts(),
                                      orders=tuple(order.orders))
        return closed_order
Esempio n. 5
0
    def add_order_item(self, room, item, recipient):
        try:
            order_details = self._orders[room]
        except KeyError:
            raise InvalidAction('No open orders for room ' + room)

        order_item = OrderItem(item=item,
                               recipient=recipient,
                               order_ts=self.deps.get_current_ts())
        order_details.orders.append(order_item)
        return order_item
Esempio n. 6
0
    def open_for_orders(self, requester, room):
        # type: (str, str) -> OrderSummary
        """Creates an offer to buy coffer by requester in given room.

        Args:
            requester: User ID of buyer
            room: Chat room ID

        Returns:
            Order summary or new order

        Raises:
            InvalidAction: if there is already an open offer in the given room
        """
        active_offer = self.deps.db_get_active_order(room=room)
        if active_offer and active_offer.buyer == requester:
            raise InvalidAction('You already have an open offer to buy coffee')
        elif active_offer:
            raise InvalidAction('There is already an offer to by coffee by ' + active_offer.buyer)

        return self.deps.db_create_order(room=room, buyer=requester)
Esempio n. 7
0
    def close_orders(self, requester, room):
        # type: (str, str) -> OrderDetails
        """Closes an open order.

        Args:
            requester: User ID of buyer
            room: Chat room ID

        Returns:
            Details of the closed order

        Raises:
            InvalidAction: if there are no open offers in the room
            InvalidAction: if requester is not the buyer who opened the offer
        """
        active_offer = self.deps.db_get_active_order(room=room)
        if not active_offer:
            raise InvalidAction('There are no open offers in this room')
        elif active_offer.buyer != requester:
            raise InvalidAction("You cannot close someone else's order")

        order_details = self.deps.db_close_order(room=room)
        self.deps.archive_order(order_details=order_details)
        return order_details
Esempio n. 8
0
    def create_order(self, room, buyer):
        # type: (str, str) -> OrderSummary
        if room in self._orders:
            raise InvalidAction('Order already open for room ' + room)

        order_details = OrderDetails(
            order_id=self.deps.get_next_unique_id(),
            buyer=buyer,
            room=room,
            orders=[],
            open_ts=self.deps.get_current_ts(),
            close_ts=None,
        )

        self._orders[room] = order_details
        order_summary = self._extract_summary(order_details=order_details)
        return order_summary
Esempio n. 9
0
    def show_orders(self, room):
        # type: (str) -> OrderDetails
        """Returns details of existing open offer.

        Args:
            room: Chat room ID

        Returns:
            Instance of OrderDetails

        Raises:
            InvalidAction: if there are no open offers in the room
        """
        order_details = self.deps.db_get_order_details(room=room)
        if not order_details:
            raise InvalidAction('There are no open offers in this room')

        return order_details