Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-5665] feat: enable addition Roles using phasesAdditionAllowed #510

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package it.pagopa.selfcare.onboarding.event;

import io.quarkus.test.InjectMock;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.quarkus.test.mongodb.MongoTestResource;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
Expand All @@ -25,6 +27,7 @@
import static org.mockito.Mockito.*;

@QuarkusTest
@QuarkusTestResource(MongoTestResource.class)
public class NotificationServiceTest {
@Mock
private OnboardingMapper onboardingMapper;
Expand Down
6 changes: 3 additions & 3 deletions apps/onboarding-functions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-crypto</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-azure-storage</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
</dependencies>
<build>
Expand Down
4 changes: 2 additions & 2 deletions apps/onboarding-ms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-azure-storage</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import it.pagopa.selfcare.onboarding.service.util.OnboardingUtils;
import it.pagopa.selfcare.onboarding.util.QueryUtils;
import it.pagopa.selfcare.onboarding.util.SortEnum;
import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
import it.pagopa.selfcare.product.service.ProductService;
Expand Down Expand Up @@ -72,6 +73,7 @@
import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_PAGOPA;
import static it.pagopa.selfcare.onboarding.constants.CustomError.*;
import static it.pagopa.selfcare.onboarding.util.ErrorMessage.*;
import static it.pagopa.selfcare.product.utils.ProductUtils.validRoles;

@ApplicationScoped
public class OnboardingServiceDefault implements OnboardingService {
Expand Down Expand Up @@ -303,8 +305,8 @@ private Uni<OnboardingUtils.ProxyResource> getRegistryResource(Onboarding onboar
private Uni<Onboarding> persistOnboarding(Onboarding onboarding, List<UserRequest> userRequests, Product product, List<AggregateInstitutionRequest>aggregates) {
/* I have to retrieve onboarding id for saving reference to pdv */
return Panache.withTransaction(() -> Onboarding.persist(onboarding).replaceWith(onboarding)
.onItem().transformToUni(onboardingPersisted -> validationRole(userRequests)
.onItem().transformToUni(ignore -> validateUserAggregatesRoles(aggregates))
.onItem().transformToUni(onboardingPersisted -> validationRole(userRequests, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING))
.onItem().transformToUni(ignore -> validateUserAggregatesRoles(aggregates, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING)))
.onItem().transformToUni(ignore -> retrieveAndSetUserAggregatesResources(onboardingPersisted, product, aggregates))
.onItem().transformToUni(ignore -> retrieveUserResources(userRequests, product))
.onItem().invoke(onboardingPersisted::setUsers).replaceWith(onboardingPersisted)));
Expand Down Expand Up @@ -548,9 +550,7 @@ private String retrieveProductRole(UserRequest userInfo, Map<PartyRole, ProductR
}
}

private Uni<List<UserRequest>> validationRole(List<UserRequest> users) {

List<PartyRole> validRoles = List.of(PartyRole.MANAGER, PartyRole.DELEGATE);
private Uni<List<UserRequest>> validationRole(List<UserRequest> users, List<PartyRole> validRoles) {

List<UserRequest> usersNotValidRole = users.stream()
.filter(user -> !validRoles.contains(user.getRole()))
Expand All @@ -566,13 +566,13 @@ private Uni<List<UserRequest>> validationRole(List<UserRequest> users) {
return Uni.createFrom().item(users);
}

private Uni<Void> validateUserAggregatesRoles(List<AggregateInstitutionRequest> aggregates) {
private Uni<Void> validateUserAggregatesRoles(List<AggregateInstitutionRequest> aggregates, List<PartyRole> validRoles) {
LOG.debug("starting validateUserAggregatesRoles");
if (!CollectionUtils.isEmpty(aggregates)) {
return Multi.createFrom().iterable(aggregates)
.filter(aggregate -> !CollectionUtils.isEmpty(aggregate.getUsers()))
.onItem().invoke(aggregate -> LOG.debugf("Validating role for users of aggregate: %s", aggregate.getTaxCode()))
.onItem().transformToUniAndMerge(aggregate -> validationRole(aggregate.getUsers())
.onItem().transformToUniAndMerge(aggregate -> validationRole(aggregate.getUsers(), validRoles)
.onFailure().invoke(throwable -> LOG.error("Error during validation role for aggregate: %s", aggregate.getTaxCode(), throwable)))
.collect().asList().replaceWithVoid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import it.pagopa.selfcare.onboarding.service.profile.OnboardingTestProfile;
import it.pagopa.selfcare.onboarding.service.strategy.OnboardingValidationStrategy;
import it.pagopa.selfcare.onboarding.service.util.OnboardingUtils;
import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRole;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
Expand Down Expand Up @@ -969,6 +970,7 @@ Product createDummyProduct(String productId, boolean hasParent) {
productRole.setCode("admin");
ProductRoleInfo productRoleInfo = new ProductRoleInfo();
productRoleInfo.setRoles(List.of(productRole));
productRoleInfo.setPhasesAdditionAllowed(List.of(PHASE_ADDITION_ALLOWED.ONBOARDING.value));
roleMapping.put(manager.getRole(), productRoleInfo);
productResource.setRoleMappings(roleMapping);

Expand Down
2 changes: 1 addition & 1 deletion apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-common</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion libs/onboarding-sdk-azure-storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-pom</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
<relativePath>../onboarding-sdk-pom</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion libs/onboarding-sdk-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-pom</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
<relativePath>../onboarding-sdk-pom</relativePath>
</parent>
<artifactId>onboarding-sdk-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion libs/onboarding-sdk-crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-pom</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
<relativePath>../onboarding-sdk-pom</relativePath>
</parent>
<artifactId>onboarding-sdk-crypto</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion libs/onboarding-sdk-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>onboarding-sdk-pom</artifactId>
<packaging>pom</packaging>
<name>onboarding-sdk-pom</name>
<version>0.2.2</version>
<version>0.2.3</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
4 changes: 2 additions & 2 deletions libs/onboarding-sdk-product/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-pom</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
<relativePath>../onboarding-sdk-pom</relativePath>
</parent>
<artifactId>onboarding-sdk-product</artifactId>
<name>onboarding-sdk-product</name>
<version>0.2.2</version>
<version>0.2.3</version>

<properties>
<jackson.version>2.15.2</jackson.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.pagopa.selfcare.product.entity;

public enum PHASE_ADDITION_ALLOWED {

//Phase on onboarding process
ONBOARDING("onboarding"),

//Phase on dashboard "Aggiunta Utenti" with any constraints
DASHBOARD("dashboard"),

//Phase on dashboard "Aggiunta Utenti" when a sign contract is needed
DASHBOARD_ASYNC("dashboard-async");

public final String value;

PHASE_ADDITION_ALLOWED(String value){
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
public class ProductRoleInfo {

private boolean multiroleAllowed;

/**
* List of phases where addition of the role is allowed
*/
private List<String> phasesAdditionAllowed;
private List<ProductRole> roles;

public boolean isMultiroleAllowed() {
Expand All @@ -15,6 +20,14 @@ public void setMultiroleAllowed(boolean multiroleAllowed) {
this.multiroleAllowed = multiroleAllowed;
}

public List<String> getPhasesAdditionAllowed() {
return phasesAdditionAllowed;
}

public void setPhasesAdditionAllowed(List<String> phasesAdditionAllowed) {
this.phasesAdditionAllowed = phasesAdditionAllowed;
}

public List<ProductRole> getRoles() {
return roles;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package it.pagopa.selfcare.product.utils;

import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public class ProductUtils {

/**
* Returns list of product's PartyRole associates with that PHASE_ADDITION_ALLOWED
* @param product Product
* @param phase PHASE_ADDITION_ALLOWED
* @return List<PartyRole>
*/
public static List<PartyRole> validRoles(Product product, PHASE_ADDITION_ALLOWED phase) {
return validEntryRoles(product, phase)
.stream()
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}

/**
* Returns list of product's PartyRole associates with that PHASE_ADDITION_ALLOWED and filtering for productRole
* @param product Product
* @param phase PHASE_ADDITION_ALLOWED
* @param productRole String
* @return List<PartyRole>
*/
public static List<PartyRole> validRolesByProductRole(Product product, PHASE_ADDITION_ALLOWED phase, String productRole) {
if(Objects.isNull(product)) return List.of();
return validEntryRoles(product, phase)
.stream()
.filter(entry -> Objects.nonNull(entry.getValue().getRoles()) &&
entry.getValue().getRoles().stream()
.anyMatch(item -> productRole.equals(item.getCode())))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}

private static List<Map.Entry<PartyRole, ProductRoleInfo>> validEntryRoles(Product product, PHASE_ADDITION_ALLOWED phase) {
if (Objects.isNull(product)) {
throw new IllegalArgumentException("Product must not be null!");
}
return Optional.ofNullable(product.getRoleMappings())
.orElse(Map.of())
.entrySet().stream()
.filter(entry -> Objects.nonNull(entry.getValue().getPhasesAdditionAllowed()) &&
entry.getValue().getPhasesAdditionAllowed().contains(phase.value))
.collect(Collectors.toList());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
import it.pagopa.selfcare.product.exception.InvalidRoleMappingException;
import it.pagopa.selfcare.product.exception.ProductNotFoundException;
import it.pagopa.selfcare.product.service.ProductServiceDefault;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
Expand All @@ -24,7 +23,7 @@ public class ProductServiceDefaultTest {
"{\"id\":\"prod-inactive\",\"status\":\"INACTIVE\"}]";

final private String PRODUCT_JSON_STRING_WITH_ROLEMAPPING = "[{\"id\":\"prod-test-parent\",\"status\":\"ACTIVE\"}," +
"{\"id\":\"prod-test\", \"parentId\":\"prod-test-parent\",\"status\":\"ACTIVE\", \"roleMappings\" : {\"MANAGER\":{\"roles\":[{\"code\":\"operatore\"}]}}}," +
"{\"id\":\"prod-test\", \"parentId\":\"prod-test-parent\",\"status\":\"ACTIVE\", \"roleMappings\" : {\"MANAGER\":{\"roles\":[{\"code\":\"operatore\"}], \"phasesAdditionAllowed\":[\"onboarding\"]}}}," +
"{\"id\":\"prod-inactive\",\"status\":\"INACTIVE\"}]";

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package it.pagopa.selfcare.product.utils;

import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRole;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ProductUtilsTest {

private final static String productRoleManager = "admin";

@Test
void validRoles() {
Product product = dummyProduct();
List<PartyRole> partyRoles = ProductUtils.validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING);
assertEquals(1, partyRoles.size());
assertEquals(PartyRole.MANAGER, partyRoles.get(0));
}

@Test
void validRolesByProductRole() {
Product product = dummyProduct();
List<PartyRole> partyRoles = ProductUtils.validRolesByProductRole(product, PHASE_ADDITION_ALLOWED.ONBOARDING, productRoleManager);
assertEquals(1, partyRoles.size());
assertEquals(PartyRole.MANAGER, partyRoles.get(0));
}

Product dummyProduct() {

ProductRole productRole = new ProductRole();
productRole.setCode(productRoleManager);
ProductRoleInfo productRoleInfo = new ProductRoleInfo();
productRoleInfo.setRoles(List.of(productRole));
productRoleInfo.setPhasesAdditionAllowed(List.of(PHASE_ADDITION_ALLOWED.ONBOARDING.value));

ProductRole productRoleOperator = new ProductRole();
productRoleOperator.setCode("operator");
ProductRoleInfo productRoleInfoOperator = new ProductRoleInfo();
productRoleInfoOperator.setRoles(List.of(productRoleOperator));
productRoleInfoOperator.setPhasesAdditionAllowed(List.of(PHASE_ADDITION_ALLOWED.DASHBOARD.value));

Map<PartyRole, ProductRoleInfo> roleMapping = new HashMap<>();
roleMapping.put(PartyRole.MANAGER, productRoleInfo);
roleMapping.put(PartyRole.OPERATOR, productRoleInfoOperator);

Product productResource = new Product();
productResource.setId("productId");
productResource.setRoleMappings(roleMapping);
return productResource;
}
}
8 changes: 4 additions & 4 deletions test-coverage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-common</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-azure-storage</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-crypto</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
</dependencies>

Expand Down
Loading