Search in sources :

Example 6 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 findFilteredAmount.

@Override
public OrdersAmountDTO findFilteredAmount(User user, OrderFilterDTO dto) {
    Specification<Order> spec;
    if (user.getRole() == Role.MODERATOR) {
        Long moderatorWarehouseId = user.getModerator().getWarehouseId();
        if (dto.getWarehouseId() != null) {
            if (!dto.getWarehouseId().equals(moderatorWarehouseId))
                throw new CustomAccessDeniedException();
        }
    }
    spec = OrderSpecifications.getFilterSpecification(dto);
    return new OrdersAmountDTO(orderRepo.count(spec));
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 7 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 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 8 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 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 9 with Order

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

the class ProductPositionServiceImpl1 method shipProductPositions.

@Override
public void shipProductPositions(Long orderId, ProductPositionsShipmentDTO productPositionsShipmentDTO) {
    Optional<Order> optionalOrder = orderRepo.findById(orderId);
    if (optionalOrder.isEmpty())
        throw new NotFoundEx(String.valueOf(orderId));
    Order order = optionalOrder.get();
    Long orderWarehouseId = order.getWarehouse().getId();
    List<ProductPositionsShipmentDTO.ProductPositionAmountPair> positionAmountPairs = productPositionsShipmentDTO.getPositionAmountPairs();
    checkIdsUnique(positionAmountPairs);
    checkPositionsExist(positionAmountPairs, orderWarehouseId);
    checkPositionsCurrentAmount(positionAmountPairs);
    // if everything is ok, we can finally manipulate with database
    // updating order status
    order.setStatus(OrderStatus.DELIVERING);
    orderRepo.save(order);
    shipPositionsFromWarehouse(positionAmountPairs);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order)

Example 10 with Order

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

the class ProductPositionController method shipProductPositionsFromOrder.

@PatchMapping("/api/v1/order/{id}/productPositions/currentAmount")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> shipProductPositionsFromOrder(@Min(value = 1) @Max(value = Long.MAX_VALUE) @PathVariable(name = "id") Long id, @Valid @RequestBody ProductPositionsShipmentDTO productPositionsShipmentDTO, @AuthenticationPrincipal User user) {
    Order order = orderService.getOrder(id);
    if (order == null)
        throw new NotFoundEx(String.valueOf(id));
    if (user.getRole() == Role.MODERATOR) {
        if (!user.getModerator().getWarehouseId().equals(order.getWarehouse().getId()))
            throw new CustomAccessDeniedException();
    }
    productPositionService.shipProductPositions(id, productPositionsShipmentDTO);
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

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