Skip to content

Commit

Permalink
fix update User product Status add checkTo existing product when crea…
Browse files Browse the repository at this point in the history
…te new User (#101)
  • Loading branch information
flaminiaScarciofolo authored Apr 15, 2024
1 parent 84caf96 commit a74796a
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Map;

import static it.pagopa.selfcare.user.constant.CollectionUtil.*;
import static it.pagopa.selfcare.user.constant.OnboardedProductState.ACTIVE;
import static it.pagopa.selfcare.user.constant.OnboardedProductState.SUSPENDED;
import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductEnum.*;
import static it.pagopa.selfcare.user.util.GeneralUtils.formatQueryParameterList;

Expand Down Expand Up @@ -69,15 +71,22 @@ public Uni<UserInstitution> retrieveFirstFilteredUserInstitution(Map<String, Obj

@Override
public Uni<Long> deleteUserInstitutionProduct(String userId, String institutionId, String productId) {
OnboardedProductFilter onboardedProductFilter = OnboardedProductFilter.builder().productId(productId).build();
OnboardedProductFilter onboardedProductFilter = OnboardedProductFilter.builder().productId(productId).status(ACTIVE.name()).build();
UserInstitutionFilter userInstitutionFilter = UserInstitutionFilter.builder().userId(userId).institutionId(institutionId).build();
Map<String, Object> filterMap = userUtils.retrieveMapForFilter(onboardedProductFilter.constructMap(), userInstitutionFilter.constructMap());
return updateUserStatusDao(filterMap, OnboardedProductState.DELETED);
}

@Override
public Uni<Long> updateUserStatusWithOptionalFilterByInstitutionAndProduct(String userId, String institutionId, String productId, PartyRole role, String productRole, OnboardedProductState status) {
Map<String, Object> onboardedProductFilterMap = OnboardedProductFilter.builder().productId(productId).role(role).productRole(productRole).build().constructMap();
Map<String, Object> onboardedProductFilterMap;
if(status.equals(OnboardedProductState.ACTIVE)) {
onboardedProductFilterMap = OnboardedProductFilter.builder().productId(productId).role(role).productRole(productRole).status(SUSPENDED.name()).build().constructMap();
}else if(status.equals(SUSPENDED)) {
onboardedProductFilterMap = OnboardedProductFilter.builder().productId(productId).role(role).productRole(productRole).status(ACTIVE.name()).build().constructMap();
}else {
onboardedProductFilterMap = OnboardedProductFilter.builder().productId(productId).role(role).productRole(productRole).build().constructMap();
}
Map<String, Object> userInstitutionFilterMap = UserInstitutionFilter.builder().userId(userId).institutionId(institutionId).build().constructMap();
Map<String, Object> filterMap = userUtils.retrieveMapForFilter(onboardedProductFilterMap, userInstitutionFilterMap);
return updateUserStatusDao(filterMap, status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import it.pagopa.selfcare.user.controller.response.UserDetailResponse;
import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse;
import it.pagopa.selfcare.user.controller.response.UserProductResponse;
import it.pagopa.selfcare.user.entity.OnboardedProduct;
import it.pagopa.selfcare.user.entity.UserInfo;
import it.pagopa.selfcare.user.entity.UserInstitution;
import it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter;
Expand Down Expand Up @@ -77,6 +78,9 @@ public class UserServiceImpl implements UserService {

private static final String USERS_FIELD_LIST_WITHOUT_FISCAL_CODE = "name,familyName,email,workContacts";

private static final String USER_INSTITUTION_FOUNDED = "UserInstitution with userId: {} and institutionId: {} founded";
private static final String USER_INSTITUTION_NOT_FOUND = "UserInstitution with userId: {} and institutionId: {} not found";

/**
* The updateUserStatus function updates the status of a user's onboarded product.
*/
Expand Down Expand Up @@ -390,19 +394,6 @@ private Uni<PrepareNotificationData> createUserOnUserRegistryAndUserInstitution(
.onFailure().invoke(exception -> log.error("Error during persist user on UserInstitution: {} ", exception.getMessage(), exception));
}

private UserInstitution updateOrCreateUserInstitution(CreateUserDto userDto, String mailUuid, UserInstitution userInstitution, String userId) {
if (userInstitution == null) {
log.info("UserInstitution with userId: {} and institutionId: {} not found", userId, userDto.getInstitutionId());
return userInstitutionMapper.toNewEntity(userDto, userId, mailUuid);
}

log.info("UserInstitution with userId: {} and institutionId: {} found", userId, userDto.getInstitutionId());
userInstitution.setUserMailUuid(mailUuid);
userInstitution.getProducts().add(onboardedProductMapper.toNewOnboardedProduct(userDto.getProduct()));

return userInstitution;
}

/**
* The createOrUpdateUserByUserId method is a method that either add to existingUser a new user Role.
* The method starts by calling the findByIdUsingGET method on the userRegistryApi object,
Expand Down Expand Up @@ -437,14 +428,57 @@ private Uni<Void> updateUserInstitutionByUserId(UserResource userResource, AddUs

private UserInstitution updateOrCreateUserInstitution(AddUserRoleDto userDto, UserInstitution userInstitution, String userId) {
if (userInstitution == null) {
log.info("UserInstitution with userId: {} and institutionId: {} not found", userId, userDto.getInstitutionId());
log.info(USER_INSTITUTION_NOT_FOUND, userId, userDto.getInstitutionId());
return userInstitutionMapper.toNewEntity(userDto, userId);
}
log.info("UserInstitution with userId: {} and institutionId: {} found", userId, userDto.getInstitutionId());

log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId());

List<String> productRoleToAdd = checkAlreadyOnboardedProdcutRole(userDto.getProduct().getProductId(), userDto.getProduct().getProductRoles(), userInstitution);
userDto.getProduct().setProductRoles(productRoleToAdd);

userInstitution.getProducts().add(onboardedProductMapper.toNewOnboardedProduct(userDto.getProduct()));
return userInstitution;
}

private UserInstitution updateOrCreateUserInstitution(CreateUserDto userDto, String mailUuid, UserInstitution userInstitution, String userId) {
if (userInstitution == null) {
log.info(USER_INSTITUTION_NOT_FOUND, userId, userDto.getInstitutionId());
return userInstitutionMapper.toNewEntity(userDto, userId, mailUuid);
}

log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId());

List<String> productRoleToAdd = checkAlreadyOnboardedProdcutRole(userDto.getProduct().getProductId(), userDto.getProduct().getProductRoles(), userInstitution);
userDto.getProduct().setProductRoles(productRoleToAdd);

userInstitution.setUserMailUuid(mailUuid);
userInstitution.getProducts().add(onboardedProductMapper.toNewOnboardedProduct(userDto.getProduct()));

return userInstitution;
}

private List<String> checkAlreadyOnboardedProdcutRole(String productId, List<String> productRole, UserInstitution userInstitution) {
List<String> 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<String> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,22 @@ void retrieveFilteredUserInstitution() {
}

@Test
void updateUserStatusWithInstitutionAndOnboardedFilter() {
void updateUserStatusToSuspendWithInstitutionAndOnboardedFilter() {
final String userId = "userId";
String institutionId = "institutionId";
String productId = "productId";
PanacheMock.mock(UserInstitution.class);
ReactivePanacheUpdate update = Mockito.mock(ReactivePanacheUpdate.class);
when(UserInstitution.update(any(Document.class)))
.thenReturn(update);
when(update.where(any())).thenReturn(Uni.createFrom().item(1L));
UniAssertSubscriber<Long> subscriber = userInstitutionService.updateUserStatusWithOptionalFilterByInstitutionAndProduct(userId, institutionId, productId, null, null, OnboardedProductState.SUSPENDED)
.subscribe().withSubscriber(UniAssertSubscriber.create());
subscriber.assertCompleted().assertItem(1L);
}

@Test
void updateUserStatusToActiveWithInstitutionAndOnboardedFilter() {
final String userId = "userId";
String institutionId = "institutionId";
String productId = "productId";
Expand All @@ -166,6 +181,21 @@ void updateUserStatusWithInstitutionAndOnboardedFilter() {
subscriber.assertCompleted().assertItem(1L);
}

@Test
void updateUserStatusWithInstitutionAndOnboardedFilter() {
final String userId = "userId";
String institutionId = "institutionId";
String productId = "productId";
PanacheMock.mock(UserInstitution.class);
ReactivePanacheUpdate update = Mockito.mock(ReactivePanacheUpdate.class);
when(UserInstitution.update(any(Document.class)))
.thenReturn(update);
when(update.where(any())).thenReturn(Uni.createFrom().item(1L));
UniAssertSubscriber<Long> subscriber = userInstitutionService.updateUserStatusWithOptionalFilterByInstitutionAndProduct(userId, institutionId, productId, null, null, OnboardedProductState.REJECTED)
.subscribe().withSubscriber(UniAssertSubscriber.create());
subscriber.assertCompleted().assertItem(1L);
}

@Test
void updateUserStatusWithInstitutionFilter() {
final String userId = "userId";
Expand Down
Loading

0 comments on commit a74796a

Please sign in to comment.