Beispiel #1
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
Beispiel #2
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
Beispiel #3
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)
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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