Skip to content

Commit

Permalink
[SELC-5971] refactor code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo committed Nov 12, 2024
1 parent ee686cc commit d9d5638
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,32 @@ public Uni<String> 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<String> evaluateRoleAndCreateOrUpdateUserByUserId(AddUserRoleDto userDto, String userId, LoggedUser loggedUser, PartyRole roleOnProduct) {
if (Objects.isNull(roleOnProduct)) {
return createOrUpdateUserByUserId(userDto, userId, loggedUser);
}else if(PartyRole.valueOf(userDto.getProduct().getRole()).compareTo(roleOnProduct) < 0){
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d9d5638

Please sign in to comment.