Skip to content

Commit

Permalink
Merge pull request #131 from Hoang-Nguyen-Huy/PBS-84-Manage-Order-Ame…
Browse files Browse the repository at this point in the history
…nities

Pbs 84 manage order amenities
  • Loading branch information
nguyenhcp2004 authored Oct 31, 2024
2 parents 1925898 + e18ce76 commit ffe6134
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ Page<Order> findOrdersByBuildingNumberAndTimeRange(
@Param("status") OrderStatus status,
Pageable pageable);

@Query("""
SELECT DISTINCT o FROM Order o
JOIN OrderDetail od ON o.id = od.order.id
WHERE od.orderHandler.id = :staffId
AND o.createdAt >= :startTime
AND o.createdAt <= :endTime
AND (:status IS NULL OR od.status = :status)
ORDER BY o.createdAt DESC
""")
Page<Order> findOrdersByStaffIdAndTimeRange(
@Param("staffId") String staffId,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime,
@Param("status") OrderStatus status,
Pageable pageable);

@Modifying
@Transactional
void deleteById(String id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public void createOrderDetailAmenity(OrderDetailAmenityRequest request) {
}

public void updateAmenityQuantityAfterCreateODA(OrderDetailAmenity orderDetailAmenity) {
Amenity amenity = orderDetailAmenity.getAmenity();
Optional<Amenity> amenityOptional = amenityRepository.findById(orderDetailAmenity.getAmenity().getId());
if(amenityOptional.isEmpty()) {
throw new RuntimeException("Amenity not found");
}
Amenity amenity = amenityOptional.get();
if(amenity.getQuantity() < orderDetailAmenity.getQuantity()) {
throw new RuntimeException("Not enough quantity");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ private void createOrderDetailAmenities(OrderDetail orderDetail, List<Amenity> a
for (Amenity amenity : amenities) {
OrderDetailAmenity orderDetailAmenity = new OrderDetailAmenity();
orderDetailAmenity.setId(UUID.randomUUID().toString());
orderDetailAmenity.setStatus(OrderDetailAmenityStatus.Paid);
orderDetailAmenity.setCreatedAt(LocalDateTime.now());
orderDetailAmenity.setUpdatedAt(LocalDateTime.now());
orderDetailAmenity.setQuantity(amenity.getQuantity());
orderDetailAmenity.setPrice(amenity.getPrice());
orderDetailAmenity.setOrderDetail(orderDetail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ public PaginationResponse<List<OrderManagementResponse>> getOrdersByRole(
if (user.getRole().equals(AccountRole.Admin)) {
ordersPage = orderRepository.findAllWithTimeRange(
startDate, endDate, status, PageRequest.of(page, size));
} else if (user.getRole().equals(AccountRole.Staff) || user.getRole().equals(AccountRole.Manager)) {
} else if (user.getRole().equals(AccountRole.Manager)) {
int buildingNumber = user.getBuildingNumber();
ordersPage = orderRepository.findOrdersByBuildingNumberAndTimeRange(
buildingNumber, startDate, endDate, status, PageRequest.of(page, size));
} else {
}else if (user.getRole().equals(AccountRole.Staff)) {
String staffId = user.getId();
ordersPage = orderRepository.findOrdersByStaffIdAndTimeRange(
staffId, startDate, endDate, status, PageRequest.of(page, size));
}
else {
throw new IllegalArgumentException("User role not authorized to access orders.");
}
return convertToPaginationResponse(ordersPage);
Expand Down

0 comments on commit ffe6134

Please sign in to comment.