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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations