diff --git a/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/UserInstitutionCdcService.java b/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/UserInstitutionCdcService.java index 2fafb4dd..61218ff6 100644 --- a/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/UserInstitutionCdcService.java +++ b/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/UserInstitutionCdcService.java @@ -169,40 +169,21 @@ public void propagateDocumentToConsumers(ChangeStreamDocument d assert document.getDocumentKey() != null; UserInstitution userInstitutionChanged = document.getFullDocument(); - boolean hasActiveFdProduct = userInstitutionChanged.getProducts().stream() - .anyMatch(product -> (PROD_FD.getValue().equals(product.getProductId()) || PROD_FD_GARANTITO.getValue().equals(product.getProductId())) - && OnboardedProductState.ACTIVE.equals(product.getStatus())); + boolean hasFdProduct = userInstitutionChanged.getProducts().stream() + .anyMatch(product -> (PROD_FD.getValue().equals(product.getProductId()) || PROD_FD_GARANTITO.getValue().equals(product.getProductId()))); boolean userMailIsChanged = isUserMailChanged(userInstitutionChanged); if (Boolean.FALSE.equals(userMailIsChanged)) { - publisher.subscribe().with( - this::consumerUserInstitutionRepositoryEvent, - failure -> { - log.error(ERROR_DURING_SUBSCRIBE_COLLECTION_EXCEPTION_MESSAGE, failure.toString(), failure.getMessage()); - telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(TrackEventInput.builder().exception(failure.getClass().toString()).build()), Map.of(USER_INFO_UPDATE_FAILURE, 1D)); - Quarkus.asyncExit(); - }); + consumerUserInstitutionRepositoryEvent(document); } if (Boolean.TRUE.equals(sendEventsEnabled)) { - publisher.subscribe().with( - this::consumerToSendScUserEvent, - failure -> { - log.error(ERROR_DURING_SUBSCRIBE_COLLECTION_EXCEPTION_MESSAGE, failure.toString(), failure.getMessage()); - telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(TrackEventInput.builder().exception(failure.getClass().toString()).build()), Map.of(EVENTS_USER_INSTITUTION_FAILURE, 1D)); - Quarkus.asyncExit(); - }); + consumerToSendScUserEvent(document); } - if (Boolean.TRUE.equals(sendFdEventsEnabled) && hasActiveFdProduct) { - publisher.subscribe().with( - subscription -> consumerToSendUserEventForFD(document, userMailIsChanged), - failure -> { - log.error(ERROR_DURING_SUBSCRIBE_COLLECTION_EXCEPTION_MESSAGE, failure.toString(), failure.getMessage()); - telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(TrackEventInput.builder().exception(failure.getClass().toString()).build()), Map.of(EVENTS_USER_INSTITUTION_FAILURE, 1D)); - Quarkus.asyncExit(); - }); + if (Boolean.TRUE.equals(sendFdEventsEnabled) && hasFdProduct) { + consumerToSendUserEventForFD(document, userMailIsChanged); } } diff --git a/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java b/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java index 88632682..2226f680 100644 --- a/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java +++ b/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java @@ -30,7 +30,7 @@ default String toUniqueIdNotification(UserInstitution userInstitution, Onboarded return UserUtils.uniqueIdNotification(userInstitution.getId().toHexString(), product.getProductId(), product.getProductRole()); } - @Mapping(target = "id", expression = "java(toUniqueIdNotification(userInstitutionChanged, product))") + @Mapping(target = "id", expression = "java(java.util.UUID.randomUUID().toString())") @Mapping(target = "onboardingTokenId", source = "product.tokenId") @Mapping(target = "product", source = "product.productId") @Mapping(target = "createdAt", source = "product.createdAt") diff --git a/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/UserUtilsTest.java b/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/UserUtilsTest.java index e411a0e6..3840e309 100644 --- a/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/UserUtilsTest.java +++ b/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/UserUtilsTest.java @@ -140,10 +140,10 @@ void retrieveFdProduct_withValidProducts_shouldReturnMostRecentlyUpdatedProduct( List productIdToCheck = List.of("1", "2"); - List result = UserUtils.retrieveFdProduct(products, productIdToCheck); + List result = UserUtils.retrieveFdProduct(products, productIdToCheck, false); assertNotNull(result); - assertEquals("1", result.get(0).getProductId()); + assertEquals("2", result.get(0).getProductId()); } @Test @@ -151,39 +151,30 @@ void retrieveFdProduct_withEmptyProductList() { List products = Collections.emptyList(); List productIdToCheck = List.of("1", "2"); - List result = UserUtils.retrieveFdProduct(products, productIdToCheck); + List result = UserUtils.retrieveFdProduct(products, productIdToCheck, false); - assertNull(result); + assertEquals(0, result.size()); } @Test - void retrieveFdProduct_withNoFdProductIds() { + void retrieveFdProduct_mailChanges() { List products = new ArrayList<>(); - OnboardedProduct product1 = new OnboardedProduct(); - product1.setProductId("1"); - product1.setUpdatedAt(OffsetDateTime.now().minusDays(1)); - product1.setCreatedAt(OffsetDateTime.now().minusDays(2)); - products.add(product1); - - OnboardedProduct product2 = new OnboardedProduct(); - product2.setProductId("2"); - product2.setUpdatedAt(OffsetDateTime.now()); - product2.setCreatedAt(OffsetDateTime.now().minusDays(1)); - products.add(product2); - - List productIdToCheck = List.of("3", "4"); + OnboardedProduct product = dummyOnboardedProduct("example", OnboardedProductState.ACTIVE, 1); + product.setProductId("prod-fd"); + products.add(product); + List productIdToCheck = List.of("prod-fd", "2");; - List result = UserUtils.retrieveFdProduct(products, productIdToCheck); + List result = UserUtils.retrieveFdProduct(products, productIdToCheck, true); - assertEquals(0, result.size()); + assertEquals(1, result.size()); } @Test - void retrieveFdProduct_withProductsWithSameUpdatedAt() { + void retrieveFdProduct_withNoFdProductIds() { List products = new ArrayList<>(); OnboardedProduct product1 = new OnboardedProduct(); product1.setProductId("1"); - product1.setUpdatedAt(OffsetDateTime.now()); + product1.setUpdatedAt(OffsetDateTime.now().minusDays(1)); product1.setCreatedAt(OffsetDateTime.now().minusDays(2)); products.add(product1); @@ -193,11 +184,10 @@ void retrieveFdProduct_withProductsWithSameUpdatedAt() { product2.setCreatedAt(OffsetDateTime.now().minusDays(1)); products.add(product2); - List productIdToCheck = List.of("1", "2"); + List productIdToCheck = List.of("3", "4"); - List result = UserUtils.retrieveFdProduct(products, productIdToCheck); + List result = UserUtils.retrieveFdProduct(products, productIdToCheck, false); - assertNotNull(result); - assertEquals("1", result.get(0).getProductId()); + assertEquals(0, result.size()); } } diff --git a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/UserUtils.java b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/UserUtils.java index 43d1ec6e..c19fef4c 100644 --- a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/UserUtils.java +++ b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/UserUtils.java @@ -84,16 +84,20 @@ public static List retrieveFdProduct(List pr if (Objects.nonNull(products) && !products.isEmpty()) { if(isUserMailChanged){ return products.stream() - .filter(onboardedProduct -> productIdToCheck.contains(onboardedProduct.getProductId())) + .filter(onboardedProduct -> productIdToCheck.contains(onboardedProduct.getProductId()) && OnboardedProductState.ACTIVE.equals(onboardedProduct.getStatus())) .toList(); } - return List.of(Objects.requireNonNull(products.stream() + OnboardedProduct onboardedProduct = products.stream() .max(Comparator.comparing(OnboardedProduct::getUpdatedAt, nullsLast(naturalOrder())) .thenComparing(OnboardedProduct::getCreatedAt, nullsLast(naturalOrder()))) - .filter(onboardedProduct -> productIdToCheck.contains(onboardedProduct.getProductId())) - .orElse(null))); + .filter(prod -> productIdToCheck.contains(prod.getProductId())) + .orElse(null); + + if(Objects.nonNull(onboardedProduct)) { + return List.of(onboardedProduct); + } } - return null; + return Collections.emptyList(); } public static String getSASToken(String resourceUri, String keyName, String key) { @@ -127,13 +131,4 @@ public static String getHMAC256(String key, String input) { return hash; } - - public static List retrieveFdProduct(List products, List productIdToCheck) { - if (Objects.nonNull(products) && !products.isEmpty()) { - return products.stream() - .filter(onboardedProduct -> productIdToCheck.contains(onboardedProduct.getProductId())) - .toList(); - } - return null; - } }