diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java new file mode 100644 index 00000000..7e176105 --- /dev/null +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java @@ -0,0 +1,9 @@ +package it.pagopa.selfcare.user.constant; + +public class CollectionUtil { + + public static final String USER_INSTITUTION_COLLECTION = "userInstitutions"; + public static final String USER_INFO_COLLECTION = "userInfo"; + public static final String CURRENT = ".$."; + public static final String CURRENT_ANY = ".$[]."; +} diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CustomError.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CustomError.java index 5b470e56..1b68aa29 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CustomError.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CustomError.java @@ -5,7 +5,9 @@ public enum CustomError { ROLE_IS_NULL("0000", "ROLE_IS_NULL - Role is required if productRole is present"), USER_NOT_FOUND_ERROR("0031", "User having userId %s not found"), - PRODUCT_ROLE_NOT_FOUND("0000", "PRODUCT_ROLE_NOT_FOUND"); + PRODUCT_ROLE_NOT_FOUND("0000", "PRODUCT_ROLE_NOT_FOUND"), + STATUS_IS_MANDATORY("0000", "STATUS IS MANDATORY"), + USER_TO_UPDATE_NOT_FOUND("0000", "USER TO UPDATE NOT FOUND"); private final String code; private final String detail; diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/OnboardedProductFilter.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/OnboardedProductFilter.java index 1fdfcbc3..432fae6f 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/OnboardedProductFilter.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/OnboardedProductFilter.java @@ -8,7 +8,7 @@ import java.util.*; -import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductFilterField.*; +import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductEnum.*; @Builder public class OnboardedProductFilter { @@ -26,32 +26,40 @@ public class OnboardedProductFilter { @Getter @AllArgsConstructor - public enum OnboardedProductFilterField{ - PRODUCT_ID(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.productId.name()), - RELATIONSHIP_ID(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.relationshipId.name()), - TOKEN_ID(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.tokenId.name()), - STATUS(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.status.name()), - PRODUCT_ROLE(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.productRole.name()), - ROLE(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.role.name()), - ENV(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.env.name()), - CREATED_AT(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.createdAt.name()), - UPDATED_AT(UserInstitution.Fields.products.name() + "." + OnboardedProduct.Fields.updatedAt.name()); - - private final String description; + public enum OnboardedProductEnum { + PRODUCT_ID(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.productId.name()), + RELATIONSHIP_ID(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.relationshipId.name()), + TOKEN_ID(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.tokenId.name()), + STATUS(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.status.name()), + PRODUCT_ROLE(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.productRole.name()), + ROLE(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.role.name()), + ENV(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.env.name()), + CREATED_AT(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.createdAt.name()), + UPDATED_AT(UserInstitution.Fields.products.name(), OnboardedProduct.Fields.updatedAt.name()); + + private final String parent; + private final String child; + + public static Optional retrieveParent(String child){ + return Arrays.stream(values()) + .filter(onboardedProductEnum -> onboardedProductEnum.getChild().equalsIgnoreCase(child)) + .findFirst() + .map(OnboardedProductEnum::getParent); + } } public Map constructMap() { Map map = new HashMap<>(); - map.put(PRODUCT_ID.getDescription(), productId); - map.put(RELATIONSHIP_ID.getDescription(), relationshipId); - map.put(TOKEN_ID.getDescription(), tokenId); - map.put(STATUS.getDescription(), status); - map.put(PRODUCT_ROLE.getDescription(), productRole); - map.put(ROLE.getDescription(), role); - map.put(ENV.getDescription(), env); - map.put(CREATED_AT.getDescription(), createdAt); - map.put(UPDATED_AT.getDescription(), updatedAt); + map.put(PRODUCT_ID.getChild(), productId); + map.put(RELATIONSHIP_ID.getChild(), relationshipId); + map.put(TOKEN_ID.getChild(), tokenId); + map.put(STATUS.getChild(), status); + map.put(PRODUCT_ROLE.getChild(), productRole); + map.put(ROLE.getChild(), role); + map.put(ENV.getChild(), env); + map.put(CREATED_AT.getChild(), createdAt); + map.put(UPDATED_AT.getChild(), updatedAt); map.values().removeIf(Objects::isNull); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/UserInstitutionRoleFilter.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/UserInstitutionRoleFilter.java index cff839d5..1677fa5f 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/UserInstitutionRoleFilter.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/entity/filter/UserInstitutionRoleFilter.java @@ -6,13 +6,9 @@ import lombok.Builder; import lombok.Getter; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.util.*; -import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductFilterField.*; -import static it.pagopa.selfcare.user.entity.filter.UserInstitutionRoleFilter.UserInstitutionRoleEnum.INSTITUTION_ID; -import static it.pagopa.selfcare.user.entity.filter.UserInstitutionRoleFilter.UserInstitutionRoleEnum.INSTITUTION_NAME; +import static it.pagopa.selfcare.user.entity.filter.UserInstitutionRoleFilter.UserInstitutionRoleEnum.*; @Builder public class UserInstitutionRoleFilter { @@ -25,20 +21,29 @@ public class UserInstitutionRoleFilter { @Getter @AllArgsConstructor public enum UserInstitutionRoleEnum{ - INSTITUTION_NAME(UserInfo.Fields.institutions.name() + "." + UserInstitutionRole.Fields.institutionName.name()), - INSTITUTION_ID(UserInfo.Fields.institutions.name() + "." + UserInstitutionRole.Fields.institutionId.name()), - STATUS(UserInfo.Fields.institutions.name() + "." + UserInstitutionRole.Fields.status.name()), - ROLE(UserInfo.Fields.institutions.name() + "." + UserInstitutionRole.Fields.role.name()); - private final String description; + INSTITUTION_NAME(UserInfo.Fields.institutions.name(), UserInstitutionRole.Fields.institutionName.name()), + INSTITUTION_ID(UserInfo.Fields.institutions.name(), UserInstitutionRole.Fields.institutionId.name()), + STATUS(UserInfo.Fields.institutions.name(), UserInstitutionRole.Fields.status.name()), + ROLE(UserInfo.Fields.institutions.name(), UserInstitutionRole.Fields.role.name()); + + private final String parent; + private final String child; + + public static Optional retrieveParent(String child){ + return Arrays.stream(values()) + .filter(userInstitutionRoleEnum -> userInstitutionRoleEnum.getChild().equalsIgnoreCase(child)) + .findFirst() + .map(UserInstitutionRoleEnum::getParent); + } } public Map constructMap() { Map map = new HashMap<>(); - map.put(INSTITUTION_ID.getDescription(), institutionId); - map.put(INSTITUTION_NAME.getDescription(), institutionName); - map.put(STATUS.getDescription(), status); - map.put(ROLE.getDescription(), role); + map.put(INSTITUTION_ID.getChild(), institutionId); + map.put(INSTITUTION_NAME.getChild(), institutionName); + map.put(STATUS.getChild(), status); + map.put(ROLE.getChild(), role); map.values().removeIf(Objects::isNull); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java index 75395d4b..fc3a1f83 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java @@ -18,10 +18,6 @@ public interface UserInstitutionService { Multi findByUserId(String userId); - Uni updateUserStatusDao(Map filterMap, OnboardedProductState status); - - Uni updateUserStatusDaoByRelationshipId(String relationshipId, OnboardedProductState status); - Uni> paginatedFindAllWithFilter(Map queryParameter, Integer page, Integer size); Multi findAllWithFilter(Map queryParameter); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java index b390dbc2..0021ad83 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java @@ -7,7 +7,6 @@ import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse; import it.pagopa.selfcare.user.entity.OnboardedProduct; import it.pagopa.selfcare.user.entity.UserInstitution; -import it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter; import it.pagopa.selfcare.user.mapper.UserInstitutionMapper; import it.pagopa.selfcare.user.util.QueryUtils; import jakarta.enterprise.context.ApplicationScoped; @@ -21,7 +20,8 @@ import java.util.List; import java.util.Map; -import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductFilterField.*; +import static it.pagopa.selfcare.user.constant.CollectionUtil.USER_INSTITUTION_COLLECTION; +import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductEnum.*; @Slf4j @ApplicationScoped @@ -53,59 +53,42 @@ public Multi findByUserId(String userId) { return userInstitutions.onItem().transform(userInstitutionMapper::toResponse); } - @Override - public Uni updateUserStatusDao(Map filterMap, OnboardedProductState status) { - - Map fieldToUpdateMap = new HashMap<>(); - if(productFilterIsEmpty(filterMap)) { - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.status.name(), status); - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); - }else{ - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.status.name(), status); - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); - } - return UserInstitution.update(queryUtils.buildUpdateDocument(fieldToUpdateMap)) - .where(queryUtils.buildQueryDocument(filterMap)); - } - - @Override - public Uni updateUserStatusDaoByRelationshipId(String relationshipId, OnboardedProductState status) { - - Map fieldToUpdateMap = new HashMap<>(); - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.status.name(), status); - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); - - OnboardedProductFilter onboardedProductFilter = OnboardedProductFilter - .builder() - .relationshipId(relationshipId) - .build(); - - return UserInstitution.update(queryUtils.buildUpdateDocument(fieldToUpdateMap)) - .where(queryUtils.buildQueryDocument(onboardedProductFilter.constructMap())); - } - @Override public Uni> paginatedFindAllWithFilter(Map queryParameter, Integer page, Integer size) { - Document query = queryUtils.buildQueryDocument(queryParameter); + Document query = queryUtils.buildQueryDocument(queryParameter, USER_INSTITUTION_COLLECTION); return runUserInstitutionFindQuery(query, null).page(page, size).list(); } @Override public Uni retrieveFirstFilteredUserInstitution(Map queryParameter) { - Document query = queryUtils.buildQueryDocument(queryParameter); + Document query = queryUtils.buildQueryDocument(queryParameter, USER_INSTITUTION_COLLECTION); return runUserInstitutionFindQuery(query, null).firstResult(); } @Override public Multi findAllWithFilter(Map queryParameter) { - Document query = queryUtils.buildQueryDocument(queryParameter); + Document query = queryUtils.buildQueryDocument(queryParameter, USER_INSTITUTION_COLLECTION); return runUserInstitutionFindQuery(query, null).stream(); } + private Uni updateUserStatusDao(Map filterMap, OnboardedProductState status) { + + Map fieldToUpdateMap = new HashMap<>(); + if(productFilterIsEmpty(filterMap)) { + fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.status.name(), status); + fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); + }else{ + fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.status.name(), status); + fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); + } + return UserInstitution.update(queryUtils.buildUpdateDocument(fieldToUpdateMap)) + .where(queryUtils.buildQueryDocument(filterMap, USER_INSTITUTION_COLLECTION)); + } + private boolean productFilterIsEmpty(Map filterMap) { - return !filterMap.containsKey(PRODUCT_ID.getDescription()) - && !filterMap.containsKey(PRODUCT_ROLE.getDescription()) - && !filterMap.containsKey(ROLE.getDescription()); + return !filterMap.containsKey(PRODUCT_ID.getChild()) + && !filterMap.containsKey(PRODUCT_ROLE.getChild()) + && !filterMap.containsKey(ROLE.getChild()); } public ReactivePanacheQuery runUserInstitutionFindQuery(Document query, Document sort) { diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java index e5ca9b60..66456792 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java @@ -5,6 +5,10 @@ import com.mongodb.client.model.Sorts; import com.mongodb.client.model.Updates; import it.pagopa.selfcare.user.constant.SortEnum; +import it.pagopa.selfcare.user.entity.OnboardedProduct; +import it.pagopa.selfcare.user.entity.UserInstitutionRole; +import it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter; +import it.pagopa.selfcare.user.entity.filter.UserInstitutionRoleFilter; import jakarta.enterprise.context.ApplicationScoped; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; @@ -18,6 +22,9 @@ import java.util.*; import java.util.stream.Collectors; +import static it.pagopa.selfcare.user.constant.CollectionUtil.USER_INSTITUTION_COLLECTION; +import static java.util.stream.Collectors.groupingBy; + @RequiredArgsConstructor(access = AccessLevel.NONE) @ApplicationScoped public class QueryUtils { @@ -43,9 +50,9 @@ public Document buildUpdateDocument(Map parameters) { * The function iterates through the key-value pairs in the map, and for each pair it adds an entry to * the query document. If there are multiple entries in the map, then they are combined using logical ANDs. */ - public Document buildQueryDocument(Map parameters) { + public Document buildQueryDocument(Map parameters, String collection) { if (!parameters.isEmpty()) { - return bsonToDocument(Filters.and(constructBsonFilter(parameters))); + return bsonToDocument(Filters.and(constructBsonFilter(parameters, collection))); } else { return new Document(); } @@ -63,37 +70,14 @@ public Document buildSortDocument(String field, SortEnum order) { } } - /** - * The constructBsonFilter function takes a Map of parameters and returns a List of Bson objects. - * The function iterates over the entries in the parameter map, and for each entry it creates a Bson object. - * If the value is an ArrayList, then it creates an "in" filter with that key and value. - * Otherwise, if there are any keys containing dots (.), then it calls constructElementMatch to create an element match filter with those keys/values; - * otherwise, it creates an "eq" filter with that key/value pair. - */ - private List constructBsonFilter(Map parameters) { - return parameters.entrySet().stream() - .map(entry -> { - if (entry.getValue() instanceof ArrayList) { - return Filters.in(entry.getKey(), (Iterable) entry.getValue()); - }else if(isPresentArrayFilter(parameters)){ - Map finalParameters = parameters.entrySet().stream().filter(stringObjectEntry -> stringObjectEntry.getKey().contains("\\.")) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> x)); - return constructElementMatch(finalParameters); - } - return Filters.eq(entry.getKey(), entry.getValue()); - }).toList(); - } - - /** - * The constructElementMatch function takes a map of parameters and returns a Bson object. - * The function is used to construct an element match filter for MongoDB queries. + * The bsonToDocument function converts a Bson object to a Document. */ - public Bson constructElementMatch(Map parameters) { - String arrayToMatch = Arrays.toString(parameters.keySet().stream().map(s -> s.split("\\.")) - .toList() - .get(0)); - return Filters.elemMatch(arrayToMatch, Filters.and(constructBsonFilter(parameters))); + private Document bsonToDocument(Bson bson) { + BsonDocument bsonDocument = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry()); + DocumentCodec codec = new DocumentCodec(); + DecoderContext decoderContext = DecoderContext.builder().build(); + return codec.decode(new BsonDocumentReader(bsonDocument), decoderContext); } /** @@ -107,18 +91,98 @@ private List constructBsonUpdate(Map parameters) { .toList(); } + + /** - * The bsonToDocument function converts a Bson object to a Document. + * The constructBsonFilter function takes in a map of parameters and the name of the collection + * that we are querying. + * The function first checks if there is an array field present in the parameter map, and if so it creates + * a new Map object containing only those fields that need to be filtered by elemMatch (i.e., arrays). + * This new Map object is then passed into addEleMatchOperatorAndCleanParameterMap(), + * which adds an $elemMatch operator to each key-value pair in this new Map object and removes these + * Finally iterates on cleaned parameters map to add in or eq query operator and + * returns a list of Bson objects, which will be used to filter our query. */ - private Document bsonToDocument(Bson bson) { - BsonDocument bsonDocument = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry()); - DocumentCodec codec = new DocumentCodec(); - DecoderContext decoderContext = DecoderContext.builder().build(); - return codec.decode(new BsonDocumentReader(bsonDocument), decoderContext); + private List constructBsonFilter(Map parameters, String collection) { + List bsonList = new ArrayList<>(); + + Map mapForElemMatch = retrieveArrayFilterIfPresent(parameters, collection); + + if (!mapForElemMatch.isEmpty()) { + addElemMatchOperator(mapForElemMatch, collection, bsonList); + parameters = parameters.entrySet().stream().filter(stringObjectEntry -> !mapForElemMatch.containsKey(stringObjectEntry.getKey())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + bsonList.addAll(addEqAndInFilters(parameters)); + + return bsonList; } + /** + * The addElemMatchOperator function is used to add the $elemMatch operator to the query. + * The function takes in a mapForElemMatch map, parameters map, collection name and bsonList as arguments. + * It then iterates through all of the keys in mapForElemMatch and retrieves their parent key using retrieveParent() from enum + * chosen based on collection argument. + * Then it groups all of these parent keys together into a Map<String, List<String>> object called groupedParentsMap. + * Finally it iterates through each entry in groupedParentsMap and adds an elemMatch filter for each one with its value being + * an AND filter containing all of the eq and in filters from addAnd + */ + private void addElemMatchOperator(Map mapForElemMatch, String collection, List bsonList) { + mapForElemMatch.keySet() + .stream().map(key -> retrieveParent(key, collection)) + .collect(groupingBy(o -> o)) + .forEach((s, strings) -> bsonList.add(Filters.elemMatch(s, Filters.and(addEqAndInFilters(mapForElemMatch))))); + } - private boolean isPresentArrayFilter(Map parameters) { - return parameters.entrySet().stream().anyMatch(stringObjectEntry -> stringObjectEntry.getKey().contains("\\.")); + /** + * The addEqAndInFilters function takes a Map of parameters and returns a List of Bson objects. + * The function iterates over the entries in the parameter map, and for each entry it creates + * either an eq or an in filter depending on whether the value is an ArrayList or not. It then adds + * that filter to a list which it returns at the end of its execution. This function is used by both + * findDocumentsWithParameters and updateDocumentsWithParameters to create filters based on user input. + */ + private List addEqAndInFilters(Map parameters) { + return parameters.entrySet().stream() + .map(entry -> { + if (entry.getValue() instanceof ArrayList) { + return Filters.in(entry.getKey(), (Iterable) entry.getValue()); + } + return Filters.eq(entry.getKey(), entry.getValue()); + }).toList(); + } + + + /** + * The retrieveParent function is used to retrieve the parent of a given key. + * The parent is retrieved from enum related to given collection name; + */ + private String retrieveParent(String key, String collection) { + if (USER_INSTITUTION_COLLECTION.equalsIgnoreCase(collection)) { + return OnboardedProductFilter.OnboardedProductEnum.retrieveParent(key) + .orElse(null); + } else { + return UserInstitutionRoleFilter.UserInstitutionRoleEnum.retrieveParent(key) + .orElse(null); + } + } + + + /** + * The retrieveArrayFilterIfPresent function is used to filter out the parameters that are not part of the + * OnboardedProduct.Fields or UserInstitutionRole.Fields enumerations, depending on which collection is being queried. + * This function retrieve a new parameterMap ,containing only those filters necessary to build an element match filter + * The second argument represents which collection we are filtering for (either USER_INSTITUTION_COLLECTION or USER_INFO_COLLECTION) + */ + private Map retrieveArrayFilterIfPresent(Map parameters, String collection) { + if (USER_INSTITUTION_COLLECTION.equalsIgnoreCase(collection)) { + return parameters.entrySet().stream() + .filter(mapEntry -> Arrays.stream(OnboardedProduct.Fields.values()).anyMatch(field -> field.name().equalsIgnoreCase(mapEntry.getKey()))) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> x)); + } else { + return parameters.entrySet().stream() + .filter(mapEntry -> Arrays.stream(UserInstitutionRole.Fields.values()).anyMatch(field -> field.name().equalsIgnoreCase(mapEntry.getKey()))) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> x)); + } } } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java index 5332470e..a7c35519 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java @@ -2,11 +2,12 @@ import jakarta.enterprise.context.ApplicationScoped; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @ApplicationScoped @RequiredArgsConstructor +@Slf4j public class UserUtils { - -} +} \ No newline at end of file diff --git a/apps/user-ms/src/test/java/it.pagopa.selfcare.user/service/UserInstitutionServiceTest.java b/apps/user-ms/src/test/java/it.pagopa.selfcare.user/service/UserInstitutionServiceTest.java index 609752a2..3c628b0f 100644 --- a/apps/user-ms/src/test/java/it.pagopa.selfcare.user/service/UserInstitutionServiceTest.java +++ b/apps/user-ms/src/test/java/it.pagopa.selfcare.user/service/UserInstitutionServiceTest.java @@ -26,8 +26,8 @@ import java.util.Map; import static io.smallrye.common.constraint.Assert.assertNotNull; +import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductEnum.PRODUCT_ID; import static org.junit.jupiter.api.Assertions.assertEquals; -import static it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter.OnboardedProductFilterField.PRODUCT_ID; import static it.pagopa.selfcare.user.entity.filter.UserInstitutionFilter.UserInstitutionFilterEnum.INSTITUTION_ID; import static it.pagopa.selfcare.user.entity.filter.UserInstitutionFilter.UserInstitutionFilterEnum.USER_ID; import static org.mockito.ArgumentMatchers.any; @@ -78,37 +78,6 @@ void findByUserId() { Assertions.assertNotNull(response); } - @Test - void updateUserStatusDao() { - final String userId = "userId"; - String institutionId = "institutionId"; - String productId = "productId"; - PanacheMock.mock(UserInstitution.class); - ReactivePanacheUpdate update = Mockito.mock(ReactivePanacheUpdate.class); - when(UserInstitution.update((Document) any())) - .thenReturn(update); - when(update.where(any())).thenReturn(Uni.createFrom().item(1L)); - Map map = new HashMap<>(); - map.put(USER_ID.getDescription(), userId); - map.put(INSTITUTION_ID.getDescription(), institutionId); - map.put(PRODUCT_ID.getDescription(), productId); - UniAssertSubscriber subscriber = userInstitutionService.updateUserStatusDao(map, OnboardedProductState.ACTIVE).subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.assertCompleted().assertItem(1L); - } - - @Test - void updateUserStatusDaoByRelationshipId() { - String relationshipId = "institutionId"; - PanacheMock.mock(UserInstitution.class); - ReactivePanacheUpdate update = Mockito.mock(ReactivePanacheUpdate.class); - when(UserInstitution.update((Document) any())) - .thenReturn(update); - when(update.where(any())).thenReturn(Uni.createFrom().item(1L)); - UniAssertSubscriber subscriber = userInstitutionService.updateUserStatusDaoByRelationshipId(relationshipId, OnboardedProductState.ACTIVE) - .subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.assertCompleted().assertItem(1L); - } - @Test void paginatedFindAllWithFilter() { Map parameterMap = new HashMap<>();