Search in sources :

Example 11 with Order

use of com.ncedu.fooddelivery.api.v1.entities.order.Order 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 12 with Order

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

the class DeliverySessionServiceImpl method finishSession.

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

Example 13 with Order

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

the class OrderServiceImpl1 method changeOrderStatus.

@Override
public void changeOrderStatus(Order order, User user, ChangeOrderStatusDTO dto) {
    if (user.getRole() == Role.MODERATOR) {
        if (!order.getWarehouse().getId().equals(user.getModerator().getWarehouseId()))
            throw new CustomAccessDeniedException();
    } else if (user.getRole() == Role.COURIER) {
        if (!order.getCourier().getId().equals(user.getCourier().getId()))
            throw new CustomAccessDeniedException();
    } else if (user.getRole() == Role.CLIENT) {
        if (!order.getClient().getId().equals(user.getClient().getId()))
            throw new CustomAccessDeniedException();
    }
    OrderStatus oldStatus = order.getStatus();
    OrderStatus newStatus = dto.getNewStatus();
    if (oldStatus == OrderStatus.CANCELLED || oldStatus == OrderStatus.DELIVERED || (oldStatus.ordinal() > newStatus.ordinal()))
        throw new OrderStatusChangeException(order.getId());
    order.setStatus(newStatus);
    orderRepo.save(order);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 14 with Order

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

the class OrderServiceImpl1 method changeDeliveryRating.

@Override
public void changeDeliveryRating(Order order, ChangeRatingDTO dto, User user) {
    if (order.getCourier() == null)
        throw new CourierNotSetException();
    if (!user.getId().equals(order.getClient().getId()))
        throw new CustomAccessDeniedException();
    order.setDeliveryRating(dto.getRating());
    orderRepo.save(order);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 15 with Order

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

the class OrderServiceImpl1 method buildOrder.

private Order buildOrder(User user, Geometry coords, CreateOrderDTO dto, List<ProductPosition> productPositions) {
    Order order = new Order();
    final int weightLimit = 15000;
    int currentWeight = 0;
    Map<Long, Integer> currOrderPositions = new HashMap<>();
    // adding records in DB table 'orders_product_positions'
    for (int j = 0; j < productPositions.size() && weightLimit != currentWeight; j++) {
        ProductPosition currPos = productPositions.get(j);
        int currPosWeight = currPos.getProduct().getWeight();
        if ((weightLimit - currentWeight) >= currPosWeight) {
            currentWeight += currPosWeight;
            currOrderPositions.merge(currPos.getId(), 1, Integer::sum);
            productPositions.remove(j);
            j--;
        }
    }
    Double orderCost = 0d;
    Double orderDiscount = 0d;
    for (Long psId : currOrderPositions.keySet()) {
        int amount = currOrderPositions.get(psId);
        ProductPosition ps = productPositionRepo.findById(psId).get();
        Double price = (ps.getProduct().getPrice() - ps.getProduct().getDiscount()) * amount;
        Double discount = amount * ps.getProduct().getDiscount();
        orderCost += price;
        orderDiscount += discount;
    }
    order.setDiscount(orderDiscount);
    order.setHighDemandCoeff(dto.getHighDemandCoeff());
    order.setOverallCost(orderCost);
    order.setClient(user.getClient());
    order.setWarehouse(warehouseService.findById(dto.getWarehouseId()));
    order.setAddress(dto.getAddress());
    order.setCoordinates(coords);
    order.setStatus(OrderStatus.CREATED);
    order.setDateStart(LocalDateTime.now());
    order = orderRepo.save(order);
    for (Long psId : currOrderPositions.keySet()) {
        int amount = currOrderPositions.get(psId);
        ProductPosition ps = productPositionRepo.findById(psId).get();
        orderProductPositionRepo.save(new OrderProductPosition(order, ps, amount, (ps.getProduct().getPrice() - ps.getProduct().getDiscount()) * amount));
    }
    return order;
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)

Aggregations

Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)19 Test (org.junit.jupiter.api.Test)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)7 Executable (org.junit.jupiter.api.function.Executable)7 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)6 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)4 ProductPositionsShipmentDTO (com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO)2 com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO)2 ChangeOrderStatusDTO (com.ncedu.fooddelivery.api.v1.dto.order.ChangeOrderStatusDTO)2 ChangeRatingDTO (com.ncedu.fooddelivery.api.v1.dto.order.ChangeRatingDTO)2 ProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)2 Warehouse (com.ncedu.fooddelivery.api.v1.entities.Warehouse)2 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)2 CourierAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.CourierAvailabilityEx)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 CoordsDTO (com.ncedu.fooddelivery.api.v1.dto.CoordsDTO)1 ProductPositionsFromOrderDTO (com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsFromOrderDTO)1 com.ncedu.fooddelivery.api.v1.dto.order (com.ncedu.fooddelivery.api.v1.dto.order)1