Search in sources :

Example 6 with Warehouse

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

the class OrderServiceImpl1 method countOrderCost.

@Override
public Double[] countOrderCost(CoordsDTO geo, HashMap<Long, Integer> pairs, Long clientWarehouseId) {
    WarehouseInfoDTO warehouse = warehouseService.getNearestWarehouse(geo.getLat(), geo.getLon());
    if (warehouse == null)
        throw new NotFoundEx(String.format("Lat: %s; lon: %s", geo.getLat().toString(), geo.getLon().toString()));
    Long warehouseId = warehouse.getId();
    if (!warehouseId.equals(clientWarehouseId))
        throw new WarehouseCoordsBindingEx();
    Map<Long, Integer> productsAvailableAmounts = new HashMap<>();
    Double overallOrderCost = 0.0;
    Double overallOrderDiscount = 0.0;
    Double orderHighDemandCoeff;
    boolean enoughProductPositions = true;
    for (Map.Entry<Long, Integer> pair : pairs.entrySet()) {
        Long productId = pair.getKey();
        Integer requestedAmount = pair.getValue();
        // 'findByProductIdAndWarehouseIdWithLock' sets lock on every product position in result set. So no one
        // other thread
        // will be able to interact with these positions before whole transaction will complete.
        List<ProductPosition> productPositions = productPositionRepo.findByProductIdAndWarehouseIdWithLock(productId, warehouseId);
        // filtering expired product positions
        productPositions = productPositions.stream().filter(new Predicate<ProductPosition>() {

            @Override
            public boolean test(ProductPosition productPosition) {
                Short expirationDays = productPosition.getProduct().getExpirationDays();
                Date manufactureDate = productPosition.getManufactureDate();
                Calendar c = Calendar.getInstance();
                c.setTime(manufactureDate);
                c.add(Calendar.DATE, expirationDays);
                if (c.before(Calendar.getInstance()))
                    return false;
                return true;
            }
        }).collect(Collectors.toList());
        Integer overallAmount = 0;
        for (ProductPosition pos : productPositions) {
            overallAmount += pos.getCurrentAmount();
        }
        if (overallAmount < requestedAmount) {
            productsAvailableAmounts.put(productId, overallAmount);
            enoughProductPositions = false;
        }
        if (enoughProductPositions) {
            // product price count
            Product product = productPositions.get(0).getProduct();
            Double productDiscount = product.getDiscount();
            Double productPrice = product.getPrice();
            overallOrderCost += requestedAmount * (productPrice - productDiscount);
            overallOrderDiscount += productDiscount * requestedAmount;
        }
    }
    if (!enoughProductPositions) {
        throw new ProductAvailabilityEx(productsAvailableAmounts);
    }
    orderHighDemandCoeff = countHighDemandCoeff(warehouseId);
    overallOrderCost = Math.round(overallOrderCost * orderHighDemandCoeff * 100.0) / 100.0;
    return new Double[] { overallOrderCost, overallOrderDiscount, orderHighDemandCoeff };
}
Also used : ProductAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx) WarehouseCoordsBindingEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)

Example 7 with Warehouse

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

the class ProductServiceImpl method getWarehouseIdByCoordinates.

private Long getWarehouseIdByCoordinates(CoordsDTO coordinates) {
    Point geo = makeGeoCoordinates(coordinates);
    WarehouseInfoDTO nearestWarehouse = warehouseService.getNearestWarehouse(geo);
    log.debug("WAREHOUSE " + nearestWarehouse.getId() + " for coords: " + geo);
    return nearestWarehouse.getId();
}
Also used : Point(com.vividsolutions.jts.geom.Point) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)

Example 8 with Warehouse

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

the class ProductPositionServiceTest method shipProductPositionsNotUniqueTest.

@Test
public void shipProductPositionsNotUniqueTest() {
    ProductPositionsShipmentDTO mockDTO = Mockito.mock(ProductPositionsShipmentDTO.class);
    List<ProductPositionsShipmentDTO.ProductPositionAmountPair> mockPairs;
    ProductPositionsShipmentDTO.ProductPositionAmountPair mockPair1 = new ProductPositionsShipmentDTO.ProductPositionAmountPair(), mockPair2 = new ProductPositionsShipmentDTO.ProductPositionAmountPair();
    mockPair1.setId(12L);
    mockPair1.setAmount(10);
    mockPair2.setId(13L);
    mockPair2.setAmount(20);
    mockPairs = new ArrayList<>(Arrays.asList(mockPair1, mockPair2, mockPair1));
    Long mockOrderId = 110L;
    Order mockOrder = new Order();
    Warehouse mockWarehouse = Mockito.mock(Warehouse.class);
    mockOrder.setWarehouse(mockWarehouse);
    Mockito.when(mockWarehouse.getId()).thenReturn(1L);
    Mockito.when(mockDTO.getPositionAmountPairs()).thenReturn(mockPairs);
    Mockito.when(orderRepo.findById(Mockito.any(Long.class))).thenReturn(Optional.of(mockOrder));
    assertThrows(NotUniqueIdException.class, new Executable() {

        @Override
        public void execute() {
            productPositionService.shipProductPositions(mockOrderId, mockDTO);
        }
    });
    Mockito.verify(orderRepo, times(1)).findById(110L);
    Mockito.verify(mockDTO, times(1)).getPositionAmountPairs();
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) Warehouse(com.ncedu.fooddelivery.api.v1.entities.Warehouse) Executable(org.junit.jupiter.api.function.Executable) ProductPositionsShipmentDTO(com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with Warehouse

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

the class ProductPositionServiceTest method shipProductPositionsNotEnoughTest.

@Test
public void shipProductPositionsNotEnoughTest() {
    Long mockOrderId = 110L;
    Order mockOrder = Mockito.mock(Order.class);
    ProductPositionsShipmentDTO mockDTO = Mockito.mock(ProductPositionsShipmentDTO.class);
    ProductPositionsShipmentDTO.ProductPositionAmountPair mockPair = new ProductPositionsShipmentDTO.ProductPositionAmountPair();
    mockPair.setId(10L);
    mockPair.setAmount(1000);
    ProductPosition mockPosition = Mockito.mock(ProductPosition.class);
    mockPosition.setCurrentAmount(1);
    Warehouse mockWarehouse = Mockito.mock(Warehouse.class);
    Mockito.when(mockWarehouse.getId()).thenReturn(1L);
    Mockito.when(mockOrder.getWarehouse()).thenReturn(mockWarehouse);
    Mockito.when(mockDTO.getPositionAmountPairs()).thenReturn(new ArrayList<>(Arrays.asList(mockPair)));
    Mockito.when(orderRepo.findById(Mockito.any(Long.class))).thenReturn(Optional.of(mockOrder));
    Mockito.when(productPositionRepo.findById(Mockito.any(Long.class))).thenReturn(Optional.of(mockPosition));
    Mockito.when(mockPosition.getWarehouse()).thenReturn(mockWarehouse);
    assertThrows(ProductPositionNotEnoughException.class, new Executable() {

        @Override
        public void execute() {
            productPositionService.shipProductPositions(mockOrderId, mockDTO);
        }
    });
    verify(mockDTO, times(1)).getPositionAmountPairs();
    verify(orderRepo, times(1)).findById(110L);
    verify(productPositionRepo, times(2)).findById(10L);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) Warehouse(com.ncedu.fooddelivery.api.v1.entities.Warehouse) Executable(org.junit.jupiter.api.function.Executable) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) ProductPositionsShipmentDTO(com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 10 with Warehouse

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

the class WarehouseController method getNearestWarehouse.

@PreAuthorize("isAuthenticated()")
@GetMapping(path = "/api/v1/warehouse/nearest")
public ResponseEntity<WarehouseInfoDTO> getNearestWarehouse(@RequestParam(required = true) @DecimalMin(value = "-90.0", inclusive = true) @DecimalMax(value = "90.0", inclusive = true) BigDecimal lat, @RequestParam(required = true) @DecimalMin(value = "-180.0", inclusive = false) @DecimalMax(value = "180.0", inclusive = false) BigDecimal lon) {
    Point geo = geometryFactory.createPoint(new Coordinate(lon.doubleValue(), lat.doubleValue()));
    WarehouseInfoDTO nearestWarehouse = warehouseService.getNearestWarehouse(geo);
    return new ResponseEntity<>(nearestWarehouse, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)6 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)5 WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)4 Warehouse (com.ncedu.fooddelivery.api.v1.entities.Warehouse)4 Point (com.vividsolutions.jts.geom.Point)4 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)3 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ProductPositionsShipmentDTO (com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO)2 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)2 ProductAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx)2 WarehouseCoordsBindingEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 Executable (org.junit.jupiter.api.function.Executable)2 com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO)1 CourierAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.CourierAvailabilityEx)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 ResponseEntity (org.springframework.http.ResponseEntity)1