Skip to content

Commit

Permalink
Merge branch 'main' into feature/SELC-3904
Browse files Browse the repository at this point in the history
  • Loading branch information
empassaro committed Jan 26, 2024
2 parents 8bf7760 + e59873b commit 469ddf0
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 192 deletions.
Original file line number Diff line number Diff line change
@@ -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 = ".$[].";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Multi;
import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.user.constant.OnboardedProductState;
import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse;
import it.pagopa.selfcare.user.controller.response.UserProductResponse;
import it.pagopa.selfcare.user.service.UserService;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import lombok.AllArgsConstructor;
import org.eclipse.microprofile.openapi.annotations.Operation;

import java.util.List;

@Authenticated
@Path("/institutions")
@AllArgsConstructor
Expand All @@ -26,4 +29,17 @@ public class InstitutionController {
public Multi<UserProductResponse> getInstitutionUsers(@PathParam(value = "institutionId") String institutionId) {
return userService.getUserProductsByInstitution(institutionId);
}

@Operation(summary = "The API retrieves users with optional filters in input as query params")
@GET
@Path(value = "/{institutionId}/user-institutions")
@Produces(MediaType.APPLICATION_JSON)
public Multi<UserInstitutionResponse> retrieveUsers(@PathParam(value = "institutionId") String institutionId,
@QueryParam(value = "userId") String userId,
@QueryParam(value = "roles") List<String> roles,
@QueryParam(value = "states") List<String> states,
@QueryParam(value = "products") List<String> products,
@QueryParam(value = "productRoles") List<String> productRoles) {
return userService.findAllUserInstitutions(institutionId, userId, roles, states, products, productRoles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,34 +26,43 @@ 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<String> retrieveParent(String child){
return Arrays.stream(values())
.filter(onboardedProductEnum -> onboardedProductEnum.getChild().equalsIgnoreCase(child))
.findFirst()
.map(OnboardedProductEnum::getParent);
}
}

public Map<String, Object> constructMap() {
Map<String, Object> 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.values().removeIf(Objects::isNull);
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.entrySet().removeIf(e -> Objects.isNull(e.getValue()) ||
(e.getValue() instanceof Collection && ((Collection) e.getValue()).isEmpty()));

return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public Map<String, Object> constructMap() {
map.put(INSTITUTION_ID.getDescription(), institutionId);
map.put(INSTITUTION_DESCRIPTION.getDescription(), institutionDescription);

map.values().removeIf(Objects::isNull);
map.entrySet().removeIf(e -> Objects.isNull(e.getValue()) ||
(e.getValue() instanceof Collection && ((Collection) e.getValue()).isEmpty()));

return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> retrieveParent(String child){
return Arrays.stream(values())
.filter(userInstitutionRoleEnum -> userInstitutionRoleEnum.getChild().equalsIgnoreCase(child))
.findFirst()
.map(UserInstitutionRoleEnum::getParent);
}
}

public Map<String, Object> constructMap() {
Map<String, Object> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public interface UserInstitutionService {

Multi<UserInstitutionResponse> findByUserId(String userId);

Uni<Long> updateUserStatusDao(Map<String, Object> filterMap, OnboardedProductState status);

Uni<Long> updateUserStatusDaoByRelationshipId(String relationshipId, OnboardedProductState status);

Uni<List<UserInstitution>> paginatedFindAllWithFilter(Map<String, Object> queryParameter, Integer page, Integer size);

Multi<UserInstitution> findAllWithFilter(Map<String, Object> queryParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -31,7 +31,6 @@ public class UserInstitutionServiceDefault implements UserInstitutionService {
private static final String CURRENT = ".$.";
private static final String CURRENT_ANY = ".$[].";


private final UserInstitutionMapper userInstitutionMapper;
private final QueryUtils queryUtils;

Expand All @@ -53,59 +52,43 @@ public Multi<UserInstitutionResponse> findByUserId(String userId) {
return userInstitutions.onItem().transform(userInstitutionMapper::toResponse);
}

@Override
public Uni<Long> updateUserStatusDao(Map<String, Object> filterMap, OnboardedProductState status) {

Map<String, Object> 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<Long> updateUserStatusDaoByRelationshipId(String relationshipId, OnboardedProductState status) {

Map<String, Object> 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<List<UserInstitution>> paginatedFindAllWithFilter(Map<String, Object> 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<UserInstitution> retrieveFirstFilteredUserInstitution(Map<String, Object> queryParameter) {
Document query = queryUtils.buildQueryDocument(queryParameter);
Document query = queryUtils.buildQueryDocument(queryParameter, USER_INSTITUTION_COLLECTION);
return runUserInstitutionFindQuery(query, null).firstResult();
}

@Override
public Multi<UserInstitution> findAllWithFilter(Map<String, Object> queryParameter) {
Document query = queryUtils.buildQueryDocument(queryParameter);
Document query = queryUtils.buildQueryDocument(queryParameter, USER_INSTITUTION_COLLECTION);
log.debug("Query: {}", query);
return runUserInstitutionFindQuery(query, null).stream();
}

private Uni<Long> updateUserStatusDao(Map<String, Object> filterMap, OnboardedProductState status) {

Map<String, Object> 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<String, Object> 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<UserInstitution> runUserInstitutionFindQuery(Document query, Document sort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.user.constant.OnboardedProductState;
import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse;
import it.pagopa.selfcare.user.controller.response.UserProductResponse;
import org.openapi.quarkus.user_registry_json.model.UserResource;

Expand All @@ -13,5 +16,10 @@ public interface UserService {
Uni<List<String>> getUsersEmails(String institutionId, String productId);
Multi<UserProductResponse> getUserProductsByInstitution(String institutionId);
Uni<UserResource> retrievePerson(String userId, String productId, String institutionId);

Multi<UserInstitutionResponse> findAllUserInstitutions(String institutionId,
String userId,
List<String> roles,
List<String> states,
List<String> products,
List<String> productRoles);
}
Loading

0 comments on commit 469ddf0

Please sign in to comment.