@@ -452,28 +452,29 @@ private Uni<CreateOrUpdateUserByFiscalCodeResponse> createUserOnUserRegistryAndU
452452 * Finally, if any operation fails, it logs an error message and returns a Uni that emits a failure.
453453 */
454454 @ Override
455- public Uni <String > createOrUpdateUserByUserId (AddUserRoleDto userDto , String userId , LoggedUser loggedUser ) {
455+ public Uni <String > createOrUpdateUserByUserId (AddUserRoleDto userDto , String userId , LoggedUser loggedUser , OnboardedProductState status ) {
456456 return userRegistryService .findByIdUsingGET (USERS_FIELD_LIST_WITHOUT_FISCAL_CODE , userId )
457457 .onFailure (UserUtils ::isUserNotFoundExceptionOnUserRegistry ).retry ().atMost (2 )
458458 .onFailure (UserUtils ::isUserNotFoundExceptionOnUserRegistry ).recoverWithUni (throwable -> Uni .createFrom ().failure (new ResourceNotFoundException (throwable .getMessage ())))
459- .onItem ().transformToUni (userResource -> updateUserInstitutionByUserId (userResource , userDto , loggedUser ))
459+ .onItem ().transformToUni (userResource -> updateUserInstitutionByUserId (userResource , userDto , loggedUser , status ))
460460 .onFailure ().invoke (exception -> log .error ("Error during retrieve user from userRegistry: {} " , exception .getMessage (), exception ));
461461 }
462462
463463 public Uni <String > createUserByUserId (AddUserRoleDto userDto , String userId , LoggedUser loggedUser ) {
464464 var userInstitutionFilters = UserInstitutionFilter .builder ().userId (userId ).institutionId (userDto .getInstitutionId ()).build ().constructMap ();
465- var productFilters = OnboardedProductFilter .builder ().productId (userDto .getProduct ().getProductId ()).status (List .of (ACTIVE )).build ().constructMap ();
465+ var productFilters = OnboardedProductFilter .builder ().productId (userDto .getProduct ().getProductId ()).status (List .of (ACTIVE , SUSPENDED )).build ().constructMap ();
466466 Map <String , Object > queryParameter = userUtils .retrieveMapForFilter (userInstitutionFilters , productFilters );
467467 return userInstitutionService .retrieveFirstFilteredUserInstitution (queryParameter )
468468 .onItem ().transformToUni (userInstitution -> {
469469 if (Optional .ofNullable (userInstitution ).isPresent ()) {
470470 log .info ("User with userId: {} has already onboarded for product {}. Proceeding with check role" , userId , userDto .getProduct ().getProductId ());
471471 PartyRole roleOnProduct = retrieveUserRoleOnProduct (userInstitution , userDto .getProduct ().getProductId ());
472+ OnboardedProductState currentStatus = retrieveUserStatusOnProduct (userInstitution , userDto .getProduct ().getProductId ());
472473 return checkAndUpdateUserMail (userInstitution , userDto .getUserMailUuid ())
473- .onItem ().transformToUni (ignore -> evaluateRoleAndCreateOrUpdateUserByUserId (userDto , userId , loggedUser , roleOnProduct ));
474+ .onItem ().transformToUni (ignore -> evaluateRoleAndCreateOrUpdateUserByUserId (userDto , userId , loggedUser , roleOnProduct , currentStatus ));
474475 } else {
475476 log .info ("User with userId: {} has not onboarded for product {}. Proceeding with create" , userId , userDto .getProduct ().getProductId ());
476- return createOrUpdateUserByUserId (userDto , userId , loggedUser );
477+ return createOrUpdateUserByUserId (userDto , userId , loggedUser , ACTIVE );
477478 }
478479 })
479480 .onFailure ().invoke (exception -> log .error ("Error during createOrUpdateManagerByUserId for userId: {}, institutionId: {}: {}" , userId , userDto .getInstitutionId (), exception .getMessage (), exception ));
@@ -502,23 +503,33 @@ private Uni<UserInstitution> checkAndUpdateUserMail(UserInstitution userInstitut
502503 * we have to delete the old role, and subsequently we create new role for the user on selected product.
503504 * 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.
504505 */
505- private Uni <String > evaluateRoleAndCreateOrUpdateUserByUserId (AddUserRoleDto userDto , String userId , LoggedUser loggedUser , PartyRole roleOnProduct ) {
506+ private Uni <String > evaluateRoleAndCreateOrUpdateUserByUserId (AddUserRoleDto userDto , String userId , LoggedUser loggedUser , PartyRole roleOnProduct , OnboardedProductState currentStatus ) {
506507 PartyRole newRole ;
507508 try {
508509 newRole = PartyRole .valueOf (userDto .getProduct ().getRole ());
509510 } catch (IllegalArgumentException e ) {
510511 throw new InvalidRequestException ("Invalid role: " + userDto .getProduct ().getRole () + ". Allowed value are: " + Arrays .toString (PartyRole .values ()));
511512 }
512513 if (Objects .isNull (roleOnProduct )) {
513- return createOrUpdateUserByUserId (userDto , userId , loggedUser );
514- } else if (newRole .compareTo (roleOnProduct ) < 0 ) {
515- 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 ());
516- return userInstitutionService .updateUserStatusWithOptionalFilterByInstitutionAndProduct (userId , userDto .getInstitutionId (), userDto .getProduct ().getProductId (), null , null , DELETED )
517- .onItem ().transformToUni (longValue -> createOrUpdateUserByUserId (userDto , userId , loggedUser ));
514+ return createOrUpdateUserByUserId (userDto , userId , loggedUser , ACTIVE );
515+ }
516+
517+ if (newRole == PartyRole .MANAGER ) {
518+ if (PartyRole .MANAGER .equals (roleOnProduct )) {
519+ log .info ("User {} already has MANAGER role with status {}. No changes needed." , userId , currentStatus );
520+ return Uni .createFrom ().failure (new UserRoleAlreadyPresentException (
521+ String .format ("User already has MANAGER role with status %s for product [%s]." , currentStatus , userDto .getProduct ().getProductId ())));
522+ } else {
523+ log .info ("User {} has role {} with status {}. Replacing with MANAGER role keeping same status." , userId , roleOnProduct , currentStatus );
524+ return userInstitutionService .updateUserStatusWithOptionalFilterByInstitutionAndProduct (
525+ userId , userDto .getInstitutionId (), userDto .getProduct ().getProductId (), null , null , DELETED )
526+ .onItem ().transformToUni (ignore -> createOrUpdateUserByUserId (userDto , userId , loggedUser , currentStatus ));
527+ }
518528 } else {
519- 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 ());
520- 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" ,
521- roleOnProduct , userDto .getProduct ().getProductId (), userDto .getProduct ().getRole ())));
529+ log .info ("User {} already has status {} for product {}. Cannot assign {} role." , userId , currentStatus , userDto .getProduct ().getProductId (), newRole );
530+ return Uni .createFrom ().failure (new UserRoleAlreadyPresentException (
531+ String .format ("User already has status %s for product [%s]. Cannot assign %s role." ,
532+ currentStatus , userDto .getProduct ().getProductId (), newRole )));
522533 }
523534 }
524535
@@ -529,14 +540,27 @@ private PartyRole retrieveUserRoleOnProduct(UserInstitution userInstitution, Str
529540 if (Objects .nonNull (userInstitution .getProducts ()) && !userInstitution .getProducts ().isEmpty ()) {
530541 return userInstitution .getProducts ().stream ()
531542 .filter (onboardedProduct -> productId .equalsIgnoreCase (onboardedProduct .getProductId ()))
532- .filter (onboardedProduct -> ACTIVE . equals (onboardedProduct .getStatus ()))
543+ .filter (onboardedProduct -> List . of ( ACTIVE , SUSPENDED ). contains (onboardedProduct .getStatus ()))
533544 .findFirst ()
534545 .map (OnboardedProduct ::getRole )
535546 .orElse (null );
536547 }
537548 return null ;
538549 }
539550
551+ private OnboardedProductState retrieveUserStatusOnProduct (UserInstitution userInstitution , String productId ) {
552+ if (Objects .nonNull (userInstitution .getProducts ()) && !userInstitution .getProducts ().isEmpty ()) {
553+ return userInstitution .getProducts ().stream ()
554+ .filter (onboardedProduct -> productId .equalsIgnoreCase (onboardedProduct .getProductId ()))
555+ .filter (onboardedProduct -> List .of (ACTIVE , SUSPENDED ).contains (onboardedProduct .getStatus ()))
556+ .findFirst ()
557+ .map (OnboardedProduct ::getStatus )
558+ .orElse (null );
559+ }
560+ return null ;
561+ }
562+
563+
540564 /**
541565 * Updates or creates a UserInstitution by userId and institutionId, persists the changes,
542566 * and sends notifications if needed. If the productRole already exists return nullItem.
@@ -546,9 +570,9 @@ private PartyRole retrieveUserRoleOnProduct(UserInstitution userInstitution, Str
546570 * @param loggedUser The currently logged-in user.
547571 * @return A Uni containing the userId as a string.
548572 */
549- private Uni <String > updateUserInstitutionByUserId (UserResource userResource , AddUserRoleDto userDto , LoggedUser loggedUser ) {
573+ private Uni <String > updateUserInstitutionByUserId (UserResource userResource , AddUserRoleDto userDto , LoggedUser loggedUser , OnboardedProductState status ) {
550574 return userInstitutionService .findByUserIdAndInstitutionId (userResource .getId ().toString (), userDto .getInstitutionId ())
551- .onItem ().transformToUni (userInstitution -> updateOrCreateUserInstitution (userDto , userInstitution , userResource .getId ().toString ()))
575+ .onItem ().transformToUni (userInstitution -> updateOrCreateUserInstitution (userDto , userInstitution , userResource .getId ().toString (), status ))
552576 .onItem ().ifNotNull ().transformToUni (userInstitutionService ::persistOrUpdate )
553577 .onItem ().ifNotNull ().invoke (userInstitution -> log .info ("UserInstitution with userId: {}, institutionId: {} persisted" , userInstitution .getUserId (), userInstitution .getInstitutionId ()))
554578 .onFailure ().invoke (exception -> log .error ("Error during persist user on UserInstitution: {} " , exception .getMessage (), exception ))
@@ -557,7 +581,7 @@ private Uni<String> updateUserInstitutionByUserId(UserResource userResource, Add
557581
558582 }
559583
560- private Uni <UserInstitution > updateOrCreateUserInstitution (AddUserRoleDto userDto , UserInstitution userInstitution , String userId ) {
584+ private Uni <UserInstitution > updateOrCreateUserInstitution (AddUserRoleDto userDto , UserInstitution userInstitution , String userId , OnboardedProductState status ) {
561585 if (userInstitution == null ) {
562586 log .info (USER_INSTITUTION_NOT_FOUND , userId , userDto .getInstitutionId ());
563587 return Uni .createFrom ().item (userInstitutionMapper .toNewEntity (userDto , userId ));
@@ -572,7 +596,7 @@ private Uni<UserInstitution> updateOrCreateUserInstitution(AddUserRoleDto userDt
572596 List <String > productRoleToAdd = checkAlreadyOnboardedProductRole (userDto .getProduct ().getProductId (), userDto .getProduct ().getProductRoles (), userInstitution );
573597 userDto .getProduct ().setProductRoles (productRoleToAdd );
574598
575- productRoleToAdd .forEach (productRole -> userInstitution .getProducts ().add (onboardedProductMapper .toNewOnboardedProduct (userDto .getProduct (), productRole )));
599+ productRoleToAdd .forEach (productRole -> userInstitution .getProducts ().add (onboardedProductMapper .toNewOnboardedProduct (userDto .getProduct (), productRole , status )));
576600 return Uni .createFrom ().item (userInstitution );
577601 }
578602
0 commit comments