From ca0589d084e01e90d3e9a67dd119dfc9d62a0e52 Mon Sep 17 00:00:00 2001
From: Michael Hoennig <michael@hoennig.de>
Date: Wed, 19 Oct 2022 18:15:21 +0200
Subject: [PATCH] hs-office-coopshares: use only membership uuid in filter

---
 src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionController.java                |    3 -
 src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java  |   19 +++++++--
 src/main/resources/api-definition/hs-office/hs-office-coopshares.yaml                                                    |    6 ---
 src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java |   40 +++++++++++++++++++
 src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepository.java                |    4 +-
 5 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionController.java
index b37846a..bf6500d 100644
--- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionController.java
+++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionController.java
@@ -40,14 +40,13 @@
             final String currentUser,
             final String assumedRoles,
             final UUID membershipUuid,
-            Integer memberNumber, // TODO: remove
             final @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromValueDate,
             final @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toValueDate) {
         context.define(currentUser, assumedRoles);
 
 
         final var entities = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
-                memberNumber,
+                membershipUuid,
                 fromValueDate,
                 toValueDate);
 
diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepository.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepository.java
index 7736059..2e52b82 100644
--- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepository.java
+++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepository.java
@@ -14,13 +14,13 @@
 
     @Query("""
             SELECT st FROM HsOfficeCoopSharesTransactionEntity st
-                WHERE (:memberNumber IS NULL OR st.membership.memberNumber = :memberNumber)
+                WHERE ( CAST(:membershipUuid AS org.hibernate.type.UUIDCharType) IS NULL OR st.membership.uuid = :membershipUuid)
                     AND ( CAST(:fromValueDate AS java.time.LocalDate) IS NULL OR (st.valueDate >= :fromValueDate))
                     AND ( CAST(:toValueDate AS java.time.LocalDate)IS NULL OR (st.valueDate <= :toValueDate))
                 ORDER BY st.membership.memberNumber, st.valueDate
                """)
     List<HsOfficeCoopSharesTransactionEntity> findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
-            Integer memberNumber, LocalDate fromValueDate, LocalDate toValueDate);
+            UUID membershipUuid, LocalDate fromValueDate, LocalDate toValueDate);
 
     HsOfficeCoopSharesTransactionEntity save(final HsOfficeCoopSharesTransactionEntity entity);
 
diff --git a/src/main/resources/api-definition/hs-office/hs-office-coopshares.yaml b/src/main/resources/api-definition/hs-office/hs-office-coopshares.yaml
index d4ad825..f24853d 100644
--- a/src/main/resources/api-definition/hs-office/hs-office-coopshares.yaml
+++ b/src/main/resources/api-definition/hs-office/hs-office-coopshares.yaml
@@ -14,12 +14,6 @@
         type: string
         format: uuid
       description: Optional UUID of the related membership.
-    - name: memberNumber
-      in: query
-      required: false
-      schema:
-        type: integer
-      description: Optional member number of the related membership.
     - name: fromValueDate
       in: query
       required: false
diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java
index 76a5457..38a8d2e 100644
--- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java
+++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java
@@ -75,12 +75,16 @@
         @Test
         void globalAdmin_canFindCoopSharesTransactionsByMemberNumber() {
 
+            context.define("superuser-alex@hostsharing.net");
+            final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002)
+                    .get(0);
+
             RestAssured // @formatter:off
                     .given()
                     .header("current-user", "superuser-alex@hostsharing.net")
                     .port(port)
                 .when()
-                    .get("http://localhost/api/hs/office/coopsharestransactions?memberNumber=10002")
+                    .get("http://localhost/api/hs/office/coopsharestransactions?membershipUuid="+givenMembership.getUuid())
                 .then().log().all().assertThat()
                     .statusCode(200)
                     .contentType("application/json")
@@ -114,13 +118,18 @@
         @Test
         void globalAdmin_canFindCoopSharesTransactionsByMemberNumberAndDateRange() {
 
+            context.define("superuser-alex@hostsharing.net");
+            final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002)
+                    .get(0);
+
             RestAssured // @formatter:off
-                    .given()
+                .given()
                     .header("current-user", "superuser-alex@hostsharing.net")
                     .port(port)
-                    .when()
-                    .get("http://localhost/api/hs/office/coopsharestransactions?memberNumber=10002&fromValueDate=2020-01-01&toValueDate=2021-12-31")
-                    .then().log().all().assertThat()
+                .when()
+                    .get("http://localhost/api/hs/office/coopsharestransactions?membershipUuid="
+                            + givenMembership.getUuid() + "&fromValueDate=2020-01-01&toValueDate=2021-12-31")
+                .then().log().all().assertThat()
                     .statusCode(200)
                     .contentType("application/json")
                     .body("", lenientlyEquals("""
diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java
index d5fcac2..3dea549 100644
--- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java
+++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java
@@ -134,7 +134,7 @@
     class FindAllCoopSharesTransactions {
 
         @Test
-        public void globalAdmin_withoutAssumedRole_canViewAllCoopSharesTransactions() {
+        public void globalAdmin_anViewAllCoopSharesTransactions() {
             // given
             context("superuser-alex@hostsharing.net");
 
@@ -161,6 +161,44 @@
         }
 
         @Test
+        public void globalAdmin_canViewCoopSharesTransactions_filteredByMembershipUuid() {
+            // given
+            context("superuser-alex@hostsharing.net");
+            final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002).get(0);
+
+            // when
+            final var result = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
+                    givenMembership.getUuid(),
+                    null,
+                    null);
+
+            // then
+            allTheseCoopSharesTransactionsAreReturned(
+                    result,
+                    "CoopShareTransaction(10002, 2010-03-15, SUBSCRIPTION, 2, ref 10002-1)",
+                    "CoopShareTransaction(10002, 2021-09-01, SUBSCRIPTION, 24, ref 10002-2)",
+                    "CoopShareTransaction(10002, 2022-10-20, CANCELLATION, 12, ref 10002-3)");
+        }
+
+        @Test
+        public void globalAdmin_canViewCoopSharesTransactions_filteredByMembershipUuidAndValueDateRange() {
+            // given
+            context("superuser-alex@hostsharing.net");
+            final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002).get(0);
+
+            // when
+            final var result = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
+                    givenMembership.getUuid(),
+                    LocalDate.parse("2021-09-01"),
+                    LocalDate.parse("2021-09-01"));
+
+            // then
+            allTheseCoopSharesTransactionsAreReturned(
+                    result,
+                    "CoopShareTransaction(10002, 2021-09-01, SUBSCRIPTION, 24, ref 10002-2)");
+        }
+
+        @Test
         public void normalUser_canViewOnlyRelatedCoopSharesTransactions() {
             // given:
             context("superuser-alex@hostsharing.net", "hs_office_partner#FirstGmbH-firstcontact.admin");

--
Gitblit v1.9.3