From cef9cd1112e42488dccd58c9028ff51a952b2ef2 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Mon, 15 Apr 2024 17:13:45 +0200 Subject: [PATCH] [SELC-4551] Refactor check already onboarded product role to remove code duplication --- .../user/service/UserServiceImpl.java | 57 ++++++++----------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java index 606b9acc..3fe1d6c6 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java @@ -431,24 +431,11 @@ private UserInstitution updateOrCreateUserInstitution(AddUserRoleDto userDto, Us log.info(USER_INSTITUTION_NOT_FOUND, userId, userDto.getInstitutionId()); return userInstitutionMapper.toNewEntity(userDto, userId); } - List productRoleAlreadyOnboarded = new ArrayList<>(); log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId()); - if (!CollectionUtils.isNullOrEmpty(userInstitution.getProducts())) { - for (OnboardedProduct onboardedProduct : userInstitution.getProducts()) { - if(onboardedProduct.getProductId().equals(userDto.getProduct().getProductId()) && - userDto.getProduct().getProductRoles().contains(onboardedProduct.getProductRole()) && onboardedProduct.getStatus().equals(ACTIVE)){ - productRoleAlreadyOnboarded.add(onboardedProduct.getProductRole()); - } - } - } - List productRoleToAdd = new ArrayList<>(userDto.getProduct().getProductRoles()); - productRoleToAdd.removeAll(productRoleAlreadyOnboarded); - userDto.getProduct().setProductRoles(productRoleToAdd); - if (userDto.getProduct().getProductRoles().isEmpty()) { - throw new InvalidRequestException(String.format("User already has this roles on Product %s",userDto.getProduct().getProductId())); - } + List productRoleToAdd = checkAlreadyOnboardedProdcutRole(userDto.getProduct().getProductId(), userDto.getProduct().getProductRoles(), userInstitution); + userDto.getProduct().setProductRoles(productRoleToAdd); userInstitution.getProducts().add(onboardedProductMapper.toNewOnboardedProduct(userDto.getProduct())); return userInstitution; @@ -460,32 +447,38 @@ private UserInstitution updateOrCreateUserInstitution(CreateUserDto userDto, Str return userInstitutionMapper.toNewEntity(userDto, userId, mailUuid); } - List productRoleAlreadyOnboarded = new ArrayList<>(); - log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId()); - if (!CollectionUtils.isNullOrEmpty(userInstitution.getProducts())) { - for (OnboardedProduct onboardedProduct : userInstitution.getProducts()) { - if(onboardedProduct.getProductId().equals(userDto.getProduct().getProductId()) && - userDto.getProduct().getProductRoles().contains(onboardedProduct.getProductRole()) && onboardedProduct.getStatus().equals(ACTIVE)){ - productRoleAlreadyOnboarded.add(onboardedProduct.getProductRole()); - } - } - } - List productRoleToAdd = new ArrayList<>(userDto.getProduct().getProductRoles()); - productRoleToAdd.removeAll(productRoleAlreadyOnboarded); - userDto.getProduct().setProductRoles(productRoleToAdd); - if (userDto.getProduct().getProductRoles().isEmpty()) { - throw new InvalidRequestException(String.format("User already has this roles on Product %s",userDto.getProduct().getProductId())); - } + List productRoleToAdd = checkAlreadyOnboardedProdcutRole(userDto.getProduct().getProductId(), userDto.getProduct().getProductRoles(), userInstitution); + userDto.getProduct().setProductRoles(productRoleToAdd); - log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId()); userInstitution.setUserMailUuid(mailUuid); userInstitution.getProducts().add(onboardedProductMapper.toNewOnboardedProduct(userDto.getProduct())); return userInstitution; } + private List checkAlreadyOnboardedProdcutRole(String productId, List productRole, UserInstitution userInstitution) { + List productRoleAlreadyOnboarded = Optional.ofNullable(userInstitution.getProducts()) + .orElse(Collections.emptyList()) + .stream() + .filter(onboardedProduct -> onboardedProduct.getProductId().equals(productId)) + .filter(onboardedProduct -> productRole.contains(onboardedProduct.getProductRole())) + .filter(onboardedProduct -> onboardedProduct.getStatus().equals(ACTIVE)) + .map(OnboardedProduct::getProductRole) + .toList(); + + + List productRoleToAdd = new ArrayList<>(productRole); + productRoleToAdd.removeAll(productRoleAlreadyOnboarded); + + if (productRoleToAdd.isEmpty()) { + throw new InvalidRequestException(String.format("User already has this roles on Product %s", productId)); + } + return productRoleToAdd; + } + + /** * The retrieveUsers function is used to retrieve a list of users from the database and userRegistry. * The function takes in an userId, institutionId, personId, roles, states, products and productRoles as parameters.