Search in sources :

Example 1 with Courier

use of com.ncedu.fooddelivery.api.v1.entities.Courier in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceTest method replaceCourierReplaceExTest.

@Test
public void replaceCourierReplaceExTest() {
    User fakeUser = getFakeUserModerator();
    Courier mockCourier = Mockito.mock(Courier.class);
    Order fakeOrder = getFakeOrder();
    fakeOrder.setStatus(OrderStatus.DELIVERED);
    Assertions.assertThrows(CourierReplaceException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            orderService.replaceCourier(fakeOrder, fakeUser);
        }
    });
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Courier

use of com.ncedu.fooddelivery.api.v1.entities.Courier in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method replaceCourier.

@Override
public void replaceCourier(Order order, User user) {
    if (user.getRole() == Role.MODERATOR) {
        if (!order.getWarehouse().getId().equals(user.getModerator().getWarehouseId()))
            throw new CustomAccessDeniedException();
    }
    if (order.getStatus() == OrderStatus.CANCELLED || order.getStatus() == OrderStatus.DELIVERED)
        throw new CourierReplaceException();
    Courier currentCourier = order.getCourier();
    if (currentCourier == null)
        throw new CourierNotSetException();
    Courier newCourier = courierRepo.getWaitingCourierByWarehouse(order.getWarehouse().getId());
    order.setCourier(newCourier);
    orderRepo.save(order);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 3 with Courier

use of com.ncedu.fooddelivery.api.v1.entities.Courier in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method createOrder.

@Override
@Transactional
public areCreatedDTO createOrder(CreateOrderDTO dto, User user) {
    checkOrderDataActuality(dto);
    Long warehouseId = dto.getWarehouseId();
    Geometry coords = geometryFactory.createPoint(new Coordinate(dto.getGeo().getLon().doubleValue(), dto.getGeo().getLat().doubleValue()));
    HashMap<Long, Integer> pairs = dto.getProductAmountPairs();
    Object[] productPosMaps = getProductPositionsData(pairs, warehouseId);
    // we will use later Maps below to reserve product positions from warehouse and put them in order(s)
    HashMap<Long, Double> productPosPriceMap = (HashMap<Long, Double>) productPosMaps[0];
    HashMap<Long, Integer> productPosAmountMap = (HashMap<Long, Integer>) productPosMaps[1];
    Double orderWeight = countOrderWeight(pairs);
    if (orderWeight > 15000d) {
        int neededCouriersAmount = (int) Math.ceil(orderWeight / 15000d);
        List<ProductPosition> productPositions = getSortedProductPositions(productPosAmountMap);
        // we've got sorted by weight sequence of product positions, so we can put them into different orders
        List<Order> orders = new ArrayList<>();
        for (int i = 0; i < neededCouriersAmount; i++) {
            Order order = buildOrder(user, coords, dto, productPositions);
            // attaching courier and then order status = 'courier_appointed'
            Courier courier;
            try {
                courier = courierService.findFreeCourier(warehouseId);
            } catch (CourierAvailabilityEx ex) {
                for (Order o : orders) {
                    // DB trigger will return all reserved product
                    o.setStatus(OrderStatus.CANCELLED);
                    // positions back to warehouse
                    orderRepo.save(o);
                }
                throw ex;
            }
            order.setCourier(courier);
            order.setStatus(OrderStatus.COURIER_APPOINTED);
            orders.add(orderRepo.save(order));
        }
        return new areCreatedDTO(orders.stream().map(order -> order.getId()).collect(Collectors.toList()));
    } else {
        Order order = buildOrder(user, coords, dto, productPosPriceMap, productPosAmountMap);
        // attaching courier and then order status = 'courier_appointed'
        Courier courier;
        try {
            courier = courierService.findFreeCourier(warehouseId);
        } catch (CourierAvailabilityEx ex) {
            // DB trigger will return all reserved product positions back
            order.setStatus(OrderStatus.CANCELLED);
            // to warehouse
            orderRepo.save(order);
            throw ex;
        }
        order.setCourier(courier);
        order.setStatus(OrderStatus.COURIER_APPOINTED);
        orderRepo.save(order);
        return new areCreatedDTO(List.of(order.getId()));
    }
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) CourierAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.CourierAvailabilityEx) Geometry(com.vividsolutions.jts.geom.Geometry) Coordinate(com.vividsolutions.jts.geom.Coordinate) com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO(com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Courier

use of com.ncedu.fooddelivery.api.v1.entities.Courier in project 2021-msk-food-delivery by netcracker-edu.

the class DeliverySessionServiceImpl method finishSession.

@Override
public void finishSession(User user, User targetUser) {
    if (targetUser.getRole() != Role.COURIER)
        throw new IncorrectUserRoleRequestException();
    Courier courier = targetUser.getCourier();
    if (user.getRole() == Role.MODERATOR)
        checkModeratorAccess(user.getModerator(), courier);
    DeliverySession currentSession = deliverySessionRepo.getActiveSession(courier.getId());
    if (currentSession == null)
        throw new NoActiveDeliverySessionException();
    Order activeOrder = orderService.findCouriersActiveOrder(courier);
    if (activeOrder != null)
        throw new DeliverySessionFinishException(activeOrder.getId());
    currentSession.setEndTime(LocalDateTime.now());
    // db trigger does other work
    deliverySessionRepo.save(currentSession);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order)

Example 5 with Courier

use of com.ncedu.fooddelivery.api.v1.entities.Courier in project 2021-msk-food-delivery by netcracker-edu.

the class DeliverySessionServiceImpl method convertToInfoDto.

public DeliverySessionInfoDTO convertToInfoDto(DeliverySession deliverySession) {
    Duration d = deliverySession.getAverageTimePerOrder();
    Courier c = deliverySession.getCourier();
    String formattedDuration = null;
    if (d != null)
        formattedDuration = DurationFormatUtils.formatDuration(deliverySession.getAverageTimePerOrder().toMillis(), "HH:mm:ss", true);
    return new DeliverySessionInfoDTO(deliverySession.getId(), new CourierInfoDTO(c.getId(), Role.COURIER.name(), c.getUser().getFullName(), c.getUser().getEmail(), c.getUser().getLastSigninDate(), c.getUser().getAvatarId(), c.getPhoneNumber(), c.getRating(), c.getWarehouse().getId(), c.getAddress(), c.getCurrentBalance()), deliverySession.getStartTime(), deliverySession.getEndTime(), deliverySession.getOrdersCompleted(), formattedDuration, deliverySession.getMoneyEarned());
}
Also used : CourierInfoDTO(com.ncedu.fooddelivery.api.v1.dto.user.CourierInfoDTO) Duration(java.time.Duration) DeliverySessionInfoDTO(com.ncedu.fooddelivery.api.v1.dto.deliverySession.DeliverySessionInfoDTO)

Aggregations

Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)5 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 DeliverySessionInfoDTO (com.ncedu.fooddelivery.api.v1.dto.deliverySession.DeliverySessionInfoDTO)2 CourierInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.CourierInfoDTO)2 CourierAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.CourierAvailabilityEx)2 Executable (org.junit.jupiter.api.function.Executable)2 com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO)1 Client (com.ncedu.fooddelivery.api.v1.entities.Client)1 Courier (com.ncedu.fooddelivery.api.v1.entities.Courier)1 User (com.ncedu.fooddelivery.api.v1.entities.User)1 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)1 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)1 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)1 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)1 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Transactional (org.springframework.transaction.annotation.Transactional)1