From e18ce766255b8494b86dcad07c7b36a3d490263b Mon Sep 17 00:00:00 2001 From: toki-ai Date: Fri, 1 Nov 2024 04:17:43 +0700 Subject: [PATCH] [PBS-84][Anh Dao] fix: get order by staff id --- .../repository/OrderRepository.java | 16 ++++++++++++++++ .../PodBookingSystem/service/OrderService.java | 9 +++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java index 8da3c831..21d47e96 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java @@ -63,6 +63,22 @@ Page 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 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); diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java index d7801350..09ea98ad 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java @@ -85,11 +85,16 @@ public PaginationResponse> 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);