def test_deliver_order_updates_status_and_delivery(self, an_order,
                                                       a_delivery_user):
        order_service.deliver(an_order.id)

        order = order_repository.get_order(an_order.id)
        delivery = user_repository.get_user(a_delivery_user.id)

        assert order.status == Order.DELIVERED_STATUS
        assert order.delivery.id == a_delivery_user.id
        assert delivery.available
    def test_take_order_updates_status_and_delivery(self, an_order,
                                                    a_delivery_user):
        order_service.take(an_order.id, a_delivery_user.id)

        order = order_repository.get_order(an_order.id)
        delivery = user_repository.get_user(a_delivery_user.id)

        assert order.status == Order.TAKEN_STATUS
        assert order.delivery.id == a_delivery_user.id
        assert not delivery.available
Exemplo n.º 3
0
def unassign(order_id, status=Order.WAITING_STATUS):
    order = order_repository.get_order(order_id)
    is_favour = order.type == Order.FAVOR_TYPE

    if order.delivery:
        delivery_service.handle_status_change(order.delivery.id, status,
                                              is_favour)
    return order_repository.update(order_id, {
        'status': status,
        'delivery': None
    })
Exemplo n.º 4
0
    def test_update_should_change_order_status(self, a_client, an_order,
                                               a_delivery_user, a_client_user):
        response = self.patch_order(a_client, an_order, a_client_user, {
            'status': Order.TAKEN_STATUS,
            'delivery': str(a_delivery_user.id)
        })

        assert_200(response)

        assert order_repository.get_order(
            an_order.id).status == Order.TAKEN_STATUS
Exemplo n.º 5
0
    def test_update_should_return_updated_order(self, a_client, an_order,
                                                a_delivery_user,
                                                a_client_user):
        response = self.patch_order(a_client, an_order, a_client_user, {
            'status': Order.TAKEN_STATUS,
            'delivery': str(a_delivery_user.id)
        })

        updated_order = json.loads(response.data)

        assert updated_order == self.get_order_json(
            order_repository.get_order(an_order.id))
    def test_unassign_order_updates_status_and_delivery(
            self, an_order, a_delivery_user):
        order_service.take(an_order.id, a_delivery_user.id)

        delivery = user_repository.get_user(a_delivery_user.id)
        assert not delivery.available

        order_service.update(an_order.id, {'status': Order.WAITING_STATUS})
        order = order_repository.get_order(an_order.id)
        delivery = user_repository.get_user(a_delivery_user.id)

        assert order.status == Order.WAITING_STATUS
        assert order.delivery is None
        assert order.quotation == 0
        assert delivery.available
Exemplo n.º 7
0
    def quote_price(self, order_id):
        order = order_repository.get_order(order_id)
        total = 0
        rules_to_apply = self.rule_repository.active_sorted_by_value()\
            .filter(
                (Q(benefit=user_service.is_premium(order.owner)) & Q(redeemable=False)) |
                (Q(redeemable=True) & Q(redeemed_by__in=[order.owner]))
            )

        for rule in rules_to_apply:
            result = self.condition_service.apply(order, *rule.conditions)
            if result:
                total = self.consequence_service.apply(rule.consequence, total,
                                                       order)
        return total if total > 0 else 0
Exemplo n.º 8
0
def deliver(order_id):
    order = order_repository.get_order(order_id)
    is_favour = order.type == Order.FAVOR_TYPE

    user_repository.increment_deliveries_completed(str(order.owner.id))
    user_repository.increment_deliveries_completed(str(order.delivery.id))

    delivery_service.handle_status_change(order.delivery.id,
                                          Order.DELIVERED_STATUS, is_favour)
    delivery_service.complete_order(order)
    user_service.confirm_favor_order(order)
    user_repository.update(order.delivery.id,
                           {'balance': order.quotation * DELIVERY_PERCENTAGE})

    return order_repository.update(order_id, {
        'status': Order.DELIVERED_STATUS,
        'completed_date': datetime.now()
    })
Exemplo n.º 9
0
def take(order_id, delivery):
    order = order_repository.get_order(order_id)
    is_favour = order.type == Order.FAVOR_TYPE

    if not user_repository.delivery_exists(delivery, is_favour):
        raise NonExistingDeliveryException()

    delivery_service.handle_status_change(delivery, Order.TAKEN_STATUS,
                                          is_favour)

    data_to_update = {
        'delivery': delivery,
        'status': Order.TAKEN_STATUS,
    }

    if order.type != order.FAVOR_TYPE:
        data_to_update['quotation'] = rule_service.quote_price(order_id)

    return order_repository.update(order_id, data_to_update)
Exemplo n.º 10
0
def directions(order_id):
    order = order_repository.get_order(order_id)

    if not order.delivery:
        return []

    owner_latitude = order.owner.location.latitude
    owner_longitude = order.owner.location.longitude
    delivery_latitude = order.delivery.location.latitude
    delivery_longitude = order.delivery.location.longitude

    key = Config.MAP_QUEST_API_KEY
    url = 'http://www.mapquestapi.com/directions/v2/route?key={}&from={},{}&to={},{}'.format(
        key, delivery_latitude, delivery_longitude, owner_latitude,
        owner_longitude)

    response = requests.get(url)

    return json.loads(response.content)
Exemplo n.º 11
0
def order_details(order_id):
    data = DetailsOrderSchema().dump(order_repository.get_order(order_id))
    return jsonify(data)