Skip to content

Commit

Permalink
[SELC-5981] refactor setId in FdUserNotificationToSend and fix retrie…
Browse files Browse the repository at this point in the history
…veFdProduct in UserUtils (#227)
  • Loading branch information
flaminiaScarciofolo authored Nov 14, 2024
1 parent 8ecd327 commit 3559106
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,40 +169,21 @@ public void propagateDocumentToConsumers(ChangeStreamDocument<UserInstitution> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,50 +140,41 @@ void retrieveFdProduct_withValidProducts_shouldReturnMostRecentlyUpdatedProduct(

List<String> productIdToCheck = List.of("1", "2");

List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck);
List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck, false);

assertNotNull(result);
assertEquals("1", result.get(0).getProductId());
assertEquals("2", result.get(0).getProductId());
}

@Test
void retrieveFdProduct_withEmptyProductList() {
List<OnboardedProduct> products = Collections.emptyList();
List<String> productIdToCheck = List.of("1", "2");

List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck);
List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck, false);

assertNull(result);
assertEquals(0, result.size());
}

@Test
void retrieveFdProduct_withNoFdProductIds() {
void retrieveFdProduct_mailChanges() {
List<OnboardedProduct> 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<String> productIdToCheck = List.of("3", "4");
OnboardedProduct product = dummyOnboardedProduct("example", OnboardedProductState.ACTIVE, 1);
product.setProductId("prod-fd");
products.add(product);
List<String> productIdToCheck = List.of("prod-fd", "2");;

List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck);
List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck, true);

assertEquals(0, result.size());
assertEquals(1, result.size());
}

@Test
void retrieveFdProduct_withProductsWithSameUpdatedAt() {
void retrieveFdProduct_withNoFdProductIds() {
List<OnboardedProduct> 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);

Expand All @@ -193,11 +184,10 @@ void retrieveFdProduct_withProductsWithSameUpdatedAt() {
product2.setCreatedAt(OffsetDateTime.now().minusDays(1));
products.add(product2);

List<String> productIdToCheck = List.of("1", "2");
List<String> productIdToCheck = List.of("3", "4");

List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck);
List<OnboardedProduct> result = UserUtils.retrieveFdProduct(products, productIdToCheck, false);

assertNotNull(result);
assertEquals("1", result.get(0).getProductId());
assertEquals(0, result.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,20 @@ public static List<OnboardedProduct> retrieveFdProduct(List<OnboardedProduct> 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) {
Expand Down Expand Up @@ -127,13 +131,4 @@ public static String getHMAC256(String key, String input) {

return hash;
}

public static List<OnboardedProduct> retrieveFdProduct(List<OnboardedProduct> products, List<String> productIdToCheck) {
if (Objects.nonNull(products) && !products.isEmpty()) {
return products.stream()
.filter(onboardedProduct -> productIdToCheck.contains(onboardedProduct.getProductId()))
.toList();
}
return null;
}
}

0 comments on commit 3559106

Please sign in to comment.