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 b0ba92be..39a0d6be 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 @@ -471,6 +471,15 @@ public Uni createUserByUserId(AddUserRoleDto userDto, String userId, Log .onFailure().invoke(exception -> log.error("Error during createOrUpdateManagerByUserId for userId: {}, institutionId: {}: {}", userId, userDto.getInstitutionId(), exception.getMessage(), exception)); } + /** + * The evaluateRoleAndCreateOrUpdateUserByUserId method is designed to evaluate the role of a user for a specific product + * and either create or update the user's role based on certain conditions. + * First, the method checks if the roleOnProduct is null. If it is, we need to create new role for the user on selected product. + * Next, the method compares the role specified in userDto with the existing roleOnProduct. + * If the new role is lower than the existing role (es. new role DELEGATE, old role MANAGER), it then calls + * we have to delete the old role, and subsequently we create new role for the user on selected product. + * If the new role is equals or higher than the existing role, throw a UserRoleAlreadyPresentException to indicate that we need to keep the old role. + */ private Uni evaluateRoleAndCreateOrUpdateUserByUserId(AddUserRoleDto userDto, String userId, LoggedUser loggedUser, PartyRole roleOnProduct) { if (Objects.isNull(roleOnProduct)) { return createOrUpdateUserByUserId(userDto, userId, loggedUser); @@ -478,16 +487,16 @@ private Uni evaluateRoleAndCreateOrUpdateUserByUserId(AddUserRoleDto use log.info("User {}, for product {}, has role {}, which is lower than {}. The old role will be deleted, and the new role will be created.", userId, userDto.getProduct().getProductId(), roleOnProduct, userDto.getProduct().getRole()); return userInstitutionService.updateUserStatusWithOptionalFilterByInstitutionAndProduct(userId, userDto.getInstitutionId(), userDto.getProduct().getProductId(), null, null, DELETED) .onItem().transformToUni(longValue -> createOrUpdateUserByUserId(userDto, userId, loggedUser)); - }else if(PartyRole.valueOf(userDto.getProduct().getRole()).compareTo(roleOnProduct) > 0) { - log.info("User {}, for product {}, has role {}, which is biggest than {}. The old role is kept.", userId, userDto.getProduct().getProductId(), roleOnProduct, userDto.getProduct().getRole()); - return Uni.createFrom().failure(new UserRoleAlreadyPresentException(String.format("User already has a role bigger than %s for the product [%s] we cannot create %s role", + } else { + log.info("User {}, for product {}, has role {}, which is equals or biggest than {}. The old role is kept.", userId, userDto.getProduct().getProductId(), roleOnProduct, userDto.getProduct().getRole()); + return Uni.createFrom().failure(new UserRoleAlreadyPresentException(String.format("User already has a role equals or bigger than %s for the product [%s] we cannot create %s role", roleOnProduct, userDto.getProduct().getProductId(), userDto.getProduct().getRole()))); - }else{ - return Uni.createFrom().failure(new UserRoleAlreadyPresentException(String.format("User already has the requested role %s on product [%s]", - userDto.getProduct().getRole(), userDto.getProduct().getProductId()))); } } + /** + * This method retrieve the user role on a specific product if exists (status = ACTIVE). + */ private PartyRole retrieveUserRoleOnProduct(UserInstitution userInstitution, String productId) { if (Objects.nonNull(userInstitution.getProducts()) && !userInstitution.getProducts().isEmpty()) { return userInstitution.getProducts().stream() diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java index 578183b6..564bfc13 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java @@ -1266,7 +1266,7 @@ void testCreateOrUpdateUser_UpdateUser_UserInstitutionUpdateFailedByUserId_userI } @Test - void createManagerByUserIdUserIsAlreadyManager() { + void createUserFromOnboardingByUserIdUserIsAlreadyManager() { AddUserRoleDto addUserRoleDto = new AddUserRoleDto(); addUserRoleDto.setInstitutionId("institutionId"); AddUserRoleDto.Product addUserRoleProduct = new AddUserRoleDto.Product(); @@ -1294,11 +1294,11 @@ void createManagerByUserIdUserIsAlreadyManager() { userService.createUserByUserId(addUserRoleDto, "userId", loggedUser) .subscribe().withSubscriber(UniAssertSubscriber.create()) - .assertFailedWith(UserRoleAlreadyPresentException.class, "User already has the requested role MANAGER on product [test]"); + .assertFailedWith(UserRoleAlreadyPresentException.class, "User already has a role equals or bigger than MANAGER for the product [test] we cannot create MANAGER role"); } @Test - void createManagerByUserIdUserWithBiggestActiveRoleOnProduct() { + void createUserFromOnboardingByUserIdUserWithBiggestActiveRoleOnProduct() { AddUserRoleDto addUserRoleDto = new AddUserRoleDto(); addUserRoleDto.setInstitutionId("institutionId"); AddUserRoleDto.Product addUserRoleProduct = new AddUserRoleDto.Product(); @@ -1326,7 +1326,7 @@ void createManagerByUserIdUserWithBiggestActiveRoleOnProduct() { userService.createUserByUserId(addUserRoleDto, "userId", loggedUser) .subscribe().withSubscriber(UniAssertSubscriber.create()) - .assertFailedWith(UserRoleAlreadyPresentException.class, "User already has a role bigger than MANAGER for the product [test] we cannot create SUB_DELEGATE role"); + .assertFailedWith(UserRoleAlreadyPresentException.class, "User already has a role equals or bigger than MANAGER for the product [test] we cannot create SUB_DELEGATE role"); } @Test