From 8e651558e9b95868e0886814c26be8dd5261f634 Mon Sep 17 00:00:00 2001
From: anasElhaddad <anas.elhaddad@marketingconfort.com>
Date: Thu, 27 Feb 2025 11:45:35 +0000
Subject: [PATCH] add pagination, sort and search for deleted sale session

---
 CHANGELOG.md                                  | 21 +++++++++++++-
 .../mydressin/constants/Paths.java            |  2 ++
 .../controllers/SaleSessionController.java    | 28 +++++++++++++++++++
 .../repositories/SaleSessionRepository.java   | 24 ++++++++++++++++
 .../services/SaleSessionService.java          |  4 +++
 .../services/impl/SaleSessionServiceImp.java  | 17 +++++++++++
 6 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 168c4de..97721a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,25 @@
+## [1.0.72-RELEASE]
+### Added
+- MYD-628/add pagination, sort and search for deleted sale session
+
+### Changed
+- Changes in existing functionality.
+
+### Deprecated
+- Soon-to-be removed features.
+
+### Removed
+- Features that have been removed.
+
+### Fixed
+- fix item count
+
+### Security
+- Any security improvements.
+
 ## [1.0.71-RELEASE]
 ### Added
-- MYD-628/add pagination, sort and search for sale ssesion
+- MYD-628/add pagination, sort and search for sale session
 
 ### Changed
 - Changes in existing functionality.
diff --git a/src/main/java/com/marketingconfort/mydressin/constants/Paths.java b/src/main/java/com/marketingconfort/mydressin/constants/Paths.java
index 503aacd..e156a0a 100644
--- a/src/main/java/com/marketingconfort/mydressin/constants/Paths.java
+++ b/src/main/java/com/marketingconfort/mydressin/constants/Paths.java
@@ -29,4 +29,6 @@ public class Paths {
     public static final String THEORETICAL_AMOUNT = "/theoretical-amount";
     public static final String SALE_SESSION_PAGE = "/page";
     public static final String COUNT_TYPE_SALE_SESSION = "/count-type";
+    public static final String DELETED_SALE_SESSION_PAGE = "/deleted/page";
+    public static final String DELETED_COUNT_TYPE_SALE_SESSION = "/deleted/count-type";
 }
diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java
index 6d60f5b..45b124d 100644
--- a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java
+++ b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java
@@ -169,5 +169,33 @@ public class SaleSessionController {
         return saleSessionService.getTypeCount(name, type, startDate, endDate);
     }
 
+
+    @GetMapping(Paths.DELETED_SALE_SESSION_PAGE)
+    @PreAuthorize("@securityCustomExpressions.isClientTrusted(#requestAuthorization)")
+    public Page<SaleSessionDTO> getDeletedSaleSessionsPage(
+            @RequestParam(required = false) String name,
+            @RequestParam(required = false) String type,
+            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
+            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate,
+            @RequestParam(defaultValue = "0") int page,
+            @RequestParam(defaultValue = "10") int size,
+            @RequestParam(defaultValue = "id") String sortBy,
+            @RequestParam(defaultValue = "ASC") String sortDir,
+            @RequestHeader String requestAuthorization) {
+
+        return saleSessionService.getDeletedSaleSessionsPage(name, type, startDate, endDate, page, size, sortBy, sortDir);
+    }
+
+    @GetMapping(Paths.DELETED_COUNT_TYPE_SALE_SESSION)
+    @PreAuthorize("@securityCustomExpressions.isClientTrusted(#requestAuthorization)")
+    public List<String[]> getDeletedStatusCount(
+            @RequestParam(required = false) String name,
+            @RequestParam(required = false) String type,
+            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
+            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate,
+            @RequestHeader String requestAuthorization) {
+        return saleSessionService.getDeletedTypeCount(name, type, startDate, endDate);
+    }
+
 }
 
diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java
index 84317f3..1b10344 100644
--- a/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java
+++ b/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java
@@ -93,4 +93,28 @@ public interface SaleSessionRepository extends JpaRepository<SaleSession, Long>
                                   @Param("name") String name ,
                                   @Param("startDate") Date startDate ,
                                   @Param("endDate") Date endDate);
+
+
+
+    @Query("SELECT sl FROM SaleSession sl WHERE " +
+            "(:name IS NULL OR :name = '' OR LOWER(sl.name) LIKE LOWER(CONCAT('%', :name, '%'))) " +
+            "AND (:type IS NULL OR :type = '' OR LOWER(sl.type)  like LOWER(CONCAT('%', :type,'%')))" +
+            "AND ( CAST(:startDate as string) IS NULL OR CAST(sl.startedDate as date) BETWEEN :startDate AND :endDate)" +
+            "AND sl.status = 'DELETED' ")
+    Page<SaleSession> getSaleSessionsDeletedPage(@Param("type") String type,
+                                          @Param("name") String name ,
+                                          @Param("startDate") Date startDate ,
+                                          @Param("endDate") Date endDate ,
+                                          Pageable pageable);
+
+    @Query("SELECT sl.type, COUNT(sl) FROM SaleSession sl WHERE " +
+            "(:name IS NULL OR :name = '' OR LOWER(sl.name) LIKE LOWER(CONCAT('%', :name, '%'))) " +
+            "AND (:type IS NULL OR :type = '' OR LOWER(sl.type)  like LOWER(CONCAT('%', :type,'%')))" +
+            "AND ( CAST(:startDate as string) IS NULL OR CAST(sl.startedDate as date) BETWEEN :startDate AND :endDate)" +
+            "AND sl.status = 'DELETED' " +
+            "GROUP BY sl.type")
+    List<Object[]> getDeletedTypeCount(@Param("type") String type,
+                                @Param("name") String name ,
+                                @Param("startDate") Date startDate ,
+                                @Param("endDate") Date endDate);
 }
diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java
index 3f86b89..7d93f42 100644
--- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java
+++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java
@@ -34,4 +34,8 @@ public interface SaleSessionService {
     Page<SaleSessionDTO> getSaleSessionsPage(String name , String type, Date startDate, Date endDate,
                                              int page, int size, String sortBy, String order);
     List<String[]> getTypeCount(String name , String type, Date startDate, Date endDate);
+
+    Page<SaleSessionDTO> getDeletedSaleSessionsPage(String name , String type, Date startDate, Date endDate,
+                                             int page, int size, String sortBy, String order);
+    List<String[]> getDeletedTypeCount(String name , String type, Date startDate, Date endDate);
 }
diff --git a/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java
index 86e8f08..abb4cab 100644
--- a/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java
+++ b/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java
@@ -262,6 +262,23 @@ public class SaleSessionServiceImp implements SaleSessionService {
         return convertObjectListToStringList(saleSessionRepository.getTypeCount(type, name, startDate, endDate));
     }
 
+    @Override
+    public Page<SaleSessionDTO> getDeletedSaleSessionsPage(String name, String type, Date startDate, Date endDate, int page, int size, String sortBy, String sortDir) {
+        if("createdAt".equals(sortBy))
+            sortBy = "id";
+        Sort.Direction direction = Sort.Direction.fromString(sortDir);
+        Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sortBy));
+
+        return saleSessionRepository.getSaleSessionsDeletedPage(type,name,startDate,endDate,pageable)
+                .map(saleSessionMapper::toDTO);
+    }
+
+    @Override
+    public List<String[]> getDeletedTypeCount(String name, String type, Date startDate, Date endDate) {
+        return convertObjectListToStringList(saleSessionRepository.getDeletedTypeCount(type, name, startDate, endDate));
+
+    }
+
     private List<String[]> convertObjectListToStringList(List<Object[]> objectList) {
         if (objectList == null) {
             return new ArrayList<>();
-- 
GitLab