Skip to content

Commit

Permalink
[SELC-5633] feat: retrieve product Role label from allRoleMappings (#195
Browse files Browse the repository at this point in the history
)
  • Loading branch information
manuraf authored Oct 4, 2024
1 parent 1fe438e commit e956237
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 35 deletions.
4 changes: 2 additions & 2 deletions apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-common</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
Expand Down
2 changes: 1 addition & 1 deletion apps/user-ms/src/main/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
"tags" : [ "Institution" ],
"summary" : "Retrieve users with optional filters",
"description" : "Fetches a list of users associated with a specific institution, applying optional filters such as userId, roles, states, products, and productRoles. This allows for flexible querying based on various user attributes and statuses.",
"operationId" : "retrieveUsers",
"operationId" : "retrieveUserInstitutions",
"parameters" : [ {
"name" : "institutionId",
"in" : "path",
Expand Down
2 changes: 1 addition & 1 deletion apps/user-ms/src/main/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ paths:
description: "Fetches a list of users associated with a specific institution,\
\ applying optional filters such as userId, roles, states, products, and productRoles.\
\ This allows for flexible querying based on various user attributes and statuses."
operationId: retrieveUsers
operationId: retrieveUserInstitutions
parameters:
- name: institutionId
in: path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Multi<UserProductResponse> getInstitutionUsers(@PathParam(value = "instit
@GET
@Path(value = "/{institutionId}/user-institutions")
@Produces(MediaType.APPLICATION_JSON)
public Multi<UserInstitutionResponse> retrieveUsers(@PathParam(value = "institutionId") String institutionId,
public Multi<UserInstitutionResponse> retrieveUserInstitutions(@PathParam(value = "institutionId") String institutionId,
@QueryParam(value = "userId") String userId,
@QueryParam(value = "roles") List<String> roles,
@QueryParam(value = "states") List<String> states,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import org.openapi.quarkus.user_registry_json.model.*;
import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring;
import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto;
import org.openapi.quarkus.user_registry_json.model.UserResource;
import org.openapi.quarkus.user_registry_json.model.WorkContactResource;
import org.openapi.quarkus.user_registry_json.model.SaveUserDto;
import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfLocalDate;

import java.time.LocalDate;
import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.smallrye.mutiny.infrastructure.Infrastructure;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRole;
import it.pagopa.selfcare.product.utils.ProductUtils;
import it.pagopa.selfcare.user.client.EventHubRestClient;
import it.pagopa.selfcare.user.conf.CloudTemplateLoader;
import it.pagopa.selfcare.user.entity.UserInstitution;
Expand Down Expand Up @@ -100,41 +101,44 @@ public Uni<Void> sendCreateUserNotification(String institutionDescription, List<
.onItem().invoke(() -> log.debug("sendCreateNotification end"));
}

private Map<String, String> buildCreateEmailDataModel(LoggedUser loggedUser, Product product, String institutionDescription, List<String> roleLabels) {
private Map<String, String> buildCreateEmailDataModel(LoggedUser loggedUser, Product product, String institutionDescription, List<String> productRoleCodes) {
Map<String, String> dataModel = new HashMap<>();
dataModel.put("requesterName", Optional.ofNullable(loggedUser.getName()).orElse(""));
dataModel.put("requesterSurname", Optional.ofNullable(loggedUser.getFamilyName()).orElse(""));

dataModel.put("productName", Optional.ofNullable(product.getTitle()).orElse(""));
dataModel.put("institutionName", Optional.ofNullable(institutionDescription).orElse(""));
if (roleLabels.size() > 1) {
if (productRoleCodes.size() > 1) {
List<String> roleLabel = new ArrayList<>();

if (CollectionUtils.isNotEmpty(product.getRoleMappings())) {
roleLabel = product.getRoleMappings().values().stream()
if (CollectionUtils.isNotEmpty(product.getAllRoleMappings())) {
roleLabel = product.getAllRoleMappings().values().stream()
.flatMap(List::stream)
.filter(productRoleInfo -> !CollectionUtils.isNullOrEmpty(productRoleInfo.getRoles()))
.flatMap(productRoleInfo -> productRoleInfo.getRoles().stream()
.filter(productRole -> roleLabels.contains(productRole.getCode())))
.filter(productRole -> productRoleCodes.contains(productRole.getCode())))
.map(ProductRole::getLabel)
.distinct()
.toList();
}

if(CollectionUtils.isNullOrEmpty(roleLabel)) {
roleLabel = List.of("no_role_found");
}

dataModel.put("productRoles", roleLabel.stream().limit(roleLabels.size() - 1L)
dataModel.put("productRoles", roleLabel.stream().limit(productRoleCodes.size() - 1L)
.collect(Collectors.joining(", ")));
if(roleLabel.size() > 1) {
dataModel.put("lastProductRole", roleLabel.get(roleLabel.size() - 1));
}

} else {
String roleLabel = "no_role_found";
if (CollectionUtils.isNotEmpty(product.getRoleMappings())) {
roleLabel = product.getRoleMappings().values().stream()
if (CollectionUtils.isNotEmpty(product.getAllRoleMappings())) {
roleLabel = product.getAllRoleMappings().values().stream()
.flatMap(List::stream)
.flatMap(productRoleInfo -> productRoleInfo.getRoles().stream())
.filter(productRole -> productRole.getCode().equals(roleLabels.get(0)))
.filter(productRole -> productRole.getCode().equals(productRoleCodes.get(0)))
.map(ProductRole::getLabel)
.findAny()
.orElse("no_role_found");
Expand All @@ -150,12 +154,13 @@ private Map<String, String> buildEmailDataModel(UserInstitution institution, Pro
&& (StringUtils.isBlank(givenProductRole) || StringUtils.equals(p.getProductRole(), givenProductRole))).findFirst();

Optional<String> roleLabel = Optional.empty();
if (productDb.isPresent() && CollectionUtils.isNotEmpty(product.getRoleMappings())) {
roleLabel = product.getRoleMappings().values().stream()
.filter(productRoleInfo -> !CollectionUtils.isNullOrEmpty(productRoleInfo.getRoles()))
.flatMap(productRoleInfo -> productRoleInfo.getRoles().stream())
.filter(productRole -> productRole.getCode().equals(productDb.get().getProductRole()))
.map(ProductRole::getLabel).findAny();
if (productDb.isPresent() ) {
try {
ProductRole productRole = ProductUtils.getProductRole(productDb.get().getProductRole(),
productDb.get().getRole(),
product);
roleLabel = Optional.ofNullable(productRole.getLabel());
} catch (IllegalArgumentException ignored) { }
}

Map<String, String> dataModel = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.user.entity.UserInstitution;
import it.pagopa.selfcare.user.model.UpdateUserRequest;
import org.openapi.quarkus.user_registry_json.model.*;
import org.openapi.quarkus.user_registry_json.model.SaveUserDto;
import org.openapi.quarkus.user_registry_json.model.UserId;
import org.openapi.quarkus.user_registry_json.model.UserResource;
import org.openapi.quarkus.user_registry_json.model.UserSearchDto;
import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto;

import java.util.List;

public interface UserRegistryService {
Uni<List<UserInstitution>> updateUserRegistry(UpdateUserRequest updateUserRequest, String userId, String institutionId);
Uni<UserResource> findByIdUsingGET( String fl, String id);
Uni<UserResource> findByIdUsingGET(String fl, String id);
Uni<UserId> saveUsingPATCH(SaveUserDto saveUserDto);
Uni<UserResource> searchUsingPOST(String fl, UserSearchDto userSearchDto);
Uni<jakarta.ws.rs.core.Response> updateUsingPATCH(String id, MutableUserFieldsDto mutableUserFieldsDto );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import it.pagopa.selfcare.user.entity.filter.UserInstitutionFilter;
import it.pagopa.selfcare.user.mapper.UserMapper;
import it.pagopa.selfcare.user.model.UpdateUserRequest;
import it.pagopa.selfcare.user.model.UserNotificationToSend;
import it.pagopa.selfcare.user.model.constants.QueueEvent;
import it.pagopa.selfcare.user.util.UserUtils;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
Expand All @@ -19,7 +16,11 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.gradle.internal.impldep.org.apache.commons.lang.StringUtils;
import org.openapi.quarkus.user_registry_json.api.UserApi;
import org.openapi.quarkus.user_registry_json.model.*;
import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto;
import org.openapi.quarkus.user_registry_json.model.SaveUserDto;
import org.openapi.quarkus.user_registry_json.model.UserId;
import org.openapi.quarkus.user_registry_json.model.UserResource;
import org.openapi.quarkus.user_registry_json.model.UserSearchDto;

import java.time.Duration;
import java.util.*;
Expand All @@ -35,23 +36,21 @@ public class UserRegistryServiceImpl implements UserRegistryService {
private static final String USERS_FIELD_LIST_WITHOUT_FISCAL_CODE = "name,familyName,email,workContacts";

private final UserInstitutionService userInstitutionService;
private final UserUtils userUtils;
private final UserNotificationService userNotificationService;
private final UserMapper userMapper;

@ConfigProperty(name = "user-ms.retry.min-backoff")
private Integer retryMinBackOff;
Integer retryMinBackOff;

@ConfigProperty(name = "user-ms.retry.max-backoff")
private Integer retryMaxBackOff;
Integer retryMaxBackOff;

@ConfigProperty(name = "user-ms.retry")
private Integer maxRetry;
Integer maxRetry;


@RestClient
@Inject
private UserApi userRegistryApi;
UserApi userRegistryApi;

@Override
public Uni<UserResource> findByIdUsingGET(String fl, String id) {
Expand Down Expand Up @@ -107,12 +106,12 @@ public Uni<List<UserInstitution>> updateUserRegistry(UpdateUserRequest updateUse
.onItem().ifNotNull().invoke(() -> log.debug("UserInstitution founded for userId: {} and institutionId: {}", userId, institutionId)))
.asTuple()
.onItem().transformToMulti(tuple -> findMailUuidAndUpdateUserRegistry(tuple.getItem1(), updateUserRequest)
.onItem().transformToMulti(uuidMail -> updateUserInstitution(tuple.getItem1(), tuple.getItem2(), uuidMail)))
.onItem().transformToMulti(uuidMail -> updateUserInstitution(tuple.getItem2(), uuidMail)))
.collect().asList()
.onItem().invoke(items -> log.trace("update {} users on userRegistry", items.size()));
}

private Multi<UserInstitution> updateUserInstitution(UserResource userResource, List<UserInstitution> userInstitutions, String mailUuid) {
private Multi<UserInstitution> updateUserInstitution(List<UserInstitution> userInstitutions, String mailUuid) {
return Multi.createFrom().iterable(userInstitutions.stream()
.peek(userInstitution -> userInstitution.setUserMailUuid(mailUuid))
.toList())
Expand Down

0 comments on commit e956237

Please sign in to comment.