From db467f3aa3c00ad8bff2a3f319d5bda473d63ca4 Mon Sep 17 00:00:00 2001 From: manuraf Date: Tue, 1 Oct 2024 09:21:32 +0200 Subject: [PATCH] feat: added validRolesByProductRole on ProductUtiles --- .../selfcare/product/utils/ProductUtils.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java index 899a4f39d..511cf5255 100644 --- a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java +++ b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java @@ -3,6 +3,7 @@ import it.pagopa.selfcare.onboarding.common.PartyRole; import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED; import it.pagopa.selfcare.product.entity.Product; +import it.pagopa.selfcare.product.entity.ProductRoleInfo; import java.util.List; import java.util.Map; @@ -15,10 +16,35 @@ public class ProductUtils { /** * Returns list of product's PartyRole associates with that PHASE_ADDITION_ALLOWED * @param product Product - * @param phase phase + * @param phase PHASE_ADDITION_ALLOWED * @return List */ public static List validRoles(Product product, PHASE_ADDITION_ALLOWED phase) { + return validEntryRoles(product, phase) + .stream() + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + /** + * Returns list of product's PartyRole associates with that PHASE_ADDITION_ALLOWED and filtering for productRole + * @param product Product + * @param phase PHASE_ADDITION_ALLOWED + * @param productRole String + * @return List + */ + public static List validRolesByProductRole(Product product, PHASE_ADDITION_ALLOWED phase, String productRole) { + if(Objects.isNull(product)) return List.of(); + return validEntryRoles(product, phase) + .stream() + .filter(entry -> Objects.nonNull(entry.getValue().getRoles()) && + entry.getValue().getRoles().stream() + .anyMatch(item -> productRole.equals(item.getCode()))) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + private static List> validEntryRoles(Product product, PHASE_ADDITION_ALLOWED phase) { if (Objects.isNull(product)) { throw new IllegalArgumentException("Product must not be null!"); } @@ -27,7 +53,6 @@ public static List validRoles(Product product, PHASE_ADDITION_ALLOWED .entrySet().stream() .filter(entry -> Objects.nonNull(entry.getValue().getPhasesAdditionAllowed()) && entry.getValue().getPhasesAdditionAllowed().contains(phase.value)) - .map(Map.Entry::getKey) .collect(Collectors.toList()); }