Skip to content

Commit

Permalink
feat: using roleMappings by institutiontype
Browse files Browse the repository at this point in the history
  • Loading branch information
manuraf committed Oct 3, 2024
1 parent 5e37b4a commit d4590ca
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 43 deletions.
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.3</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-azure-storage</artifactId>
<version>0.2.3</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.2.3</version>
<version>0.3.0</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.3</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.2.3</version>
<version>0.3.0</version>
</dependency>

</dependencies>
Expand Down
3 changes: 3 additions & 0 deletions apps/onboarding-ms/src/main/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,9 @@
"subunitType" : {
"type" : "string"
},
"vatNumber" : {
"type" : "string"
},
"geographicTaxonomies" : {
"type" : "array",
"items" : {
Expand Down
2 changes: 2 additions & 0 deletions apps/onboarding-ms/src/main/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ components:
type: string
subunitType:
type: string
vatNumber:
type: string
geographicTaxonomies:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public Uni<OnboardingResponse> onboardingUsers(OnboardingUserRequest request, St
.onItem().transform(response -> institutionMapper.toEntity(response))
.onItem().transform(institution -> {
Onboarding onboarding = onboardingMapper.toEntity(request, userId);
institution.setInstitutionType(request.getInstitutionType());
onboarding.setInstitution(institution);
onboarding.setExpiringDate(OffsetDateTime.now().plusDays(onboardingExpireDate).toLocalDateTime());
return onboarding;
Expand Down Expand Up @@ -303,12 +304,19 @@ private Uni<OnboardingUtils.ProxyResource> getRegistryResource(Onboarding onboar
}

private Uni<Onboarding> persistOnboarding(Onboarding onboarding, List<UserRequest> userRequests, Product product, List<AggregateInstitutionRequest>aggregates) {

Log.infof("persist onboarding for: product %s, product parent %s", product.getId(), product.getParentId());

Map<PartyRole, ProductRoleInfo> roleMappings = Objects.nonNull(product.getParent())
? product.getParent().getRoleMappings(onboarding.getInstitution().getInstitutionType().name())
: product.getRoleMappings(onboarding.getInstitution().getInstitutionType().name());

/* I have to retrieve onboarding id for saving reference to pdv */
return Panache.withTransaction(() -> Onboarding.persist(onboarding).replaceWith(onboarding)
.onItem().transformToUni(onboardingPersisted -> validationRole(userRequests, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING))
.onItem().transformToUni(ignore -> validateUserAggregatesRoles(aggregates, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING)))
.onItem().transformToUni(onboardingPersisted -> validationRole(userRequests, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING, onboarding.getInstitution().getInstitutionType()))
.onItem().transformToUni(ignore -> validateUserAggregatesRoles(aggregates, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING, onboarding.getInstitution().getInstitutionType())))
.onItem().transformToUni(ignore -> retrieveAndSetUserAggregatesResources(onboardingPersisted, product, aggregates))
.onItem().transformToUni(ignore -> retrieveUserResources(userRequests, product))
.onItem().transformToUni(ignore -> retrieveUserResources(userRequests, roleMappings))
.onItem().invoke(onboardingPersisted::setUsers).replaceWith(onboardingPersisted)));
}

Expand Down Expand Up @@ -580,12 +588,18 @@ private Uni<Void> validateUserAggregatesRoles(List<AggregateInstitutionRequest>
}

private Uni<Void> retrieveAndSetUserAggregatesResources(Onboarding onboarding, Product product, List<AggregateInstitutionRequest> aggregates) {
LOG.debug("Retrieving user resources for aggregates");

Log.infof("Retrieving user resources for aggregates: product %s, product parent %s", product.getId(), product.getParentId());

Map<PartyRole, ProductRoleInfo> roleMappings = Objects.nonNull(product.getParent())
? product.getParent().getRoleMappings(onboarding.getInstitution().getInstitutionType().name())
: product.getRoleMappings(onboarding.getInstitution().getInstitutionType().name());

if (!CollectionUtils.isEmpty(aggregates)) {
return Multi.createFrom().iterable(aggregates)
.filter(aggregate -> !CollectionUtils.isEmpty(aggregate.getUsers()))
.onItem().invoke(aggregate -> LOG.debugf("Retrieving user resources for aggregate: %s", aggregate.getTaxCode()))
.onItem().transformToUni(aggregate -> retrieveUserResources(aggregate.getUsers(), product)
.onItem().transformToUni(aggregate -> retrieveUserResources(aggregate.getUsers(), roleMappings)
.onFailure().invoke(throwable -> LOG.errorf("Error during retrieving user resources for aggregate: %s", aggregate.getTaxCode(), throwable))
.onItem().invoke(users -> setUsersInAggregateToPersist(onboarding, aggregate, users)))
.concatenate().onItem().ignoreAsUni();
Expand All @@ -601,13 +615,7 @@ private static void setUsersInAggregateToPersist(Onboarding onboarding, Aggregat
.ifPresent(aggregateInstitutionRequest -> aggregateInstitutionRequest.setUsers(users));
}

private Uni<List<User>> retrieveUserResources(List<UserRequest> users, Product product) {

Log.infof("Retrieving user resources for: product %s, product parent %s", product.getId(), product.getParentId());

Map<PartyRole, ProductRoleInfo> roleMappings = Objects.nonNull(product.getParent())
? product.getParent().getRoleMappings()
: product.getRoleMappings();
private Uni<List<User>> retrieveUserResources(List<UserRequest> users, Map<PartyRole, ProductRoleInfo> roleMappings) {

return Multi.createFrom().iterable(users)
.onItem().transformToUni(user -> userRegistryApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class OnboardingServiceDefaultTest {
static final UserResource managerResourceWk;
static final UserResource managerResourceWkSpid;

static final String productRoleAdminCode = "admin";
static final String productRoleAdminPspCode = "admin-psp";

static final File testFile = new File("src/test/resources/application.properties");

static final FormItem TEST_FORM_ITEM = FormItem.builder()
Expand Down Expand Up @@ -411,7 +414,7 @@ void onboardingPa_throwExceptionIfProductParentRoleIsNotValid(UniAsserter assert
@RunOnVertxContext
void onboarding_shouldThrowExceptionIfRoleNotValid(UniAsserter asserter) {
Onboarding onboardingDefaultRequest = new Onboarding();
onboardingDefaultRequest.setInstitution(new Institution());
onboardingDefaultRequest.setInstitution(dummyInstitution());
onboardingDefaultRequest.setProductId(PROD_INTEROP.getValue());

List<UserRequest> users = List.of(UserRequest.builder()
Expand Down Expand Up @@ -962,25 +965,29 @@ Product mockSimpleProductValidAssert(String productId, boolean hasParent, UniAss
return productResource;
}

Product createDummyProduct(String productId, boolean hasParent) {
Product productResource = new Product();
productResource.setId(productId);
Map<PartyRole, ProductRoleInfo> roleMapping = new HashMap<>();
ProductRoleInfo dummyProductRoleInfo(String productRolCode) {
ProductRole productRole = new ProductRole();
productRole.setCode("admin");
productRole.setCode(productRolCode);
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);
return productRoleInfo;
}

Product createDummyProduct(String productId, boolean hasParent) {

Map<PartyRole, ProductRoleInfo> roleMappingByInstitutionType = new HashMap<>();
roleMappingByInstitutionType.put(manager.getRole(), dummyProductRoleInfo(productRoleAdminPspCode));

Product productResource = new Product();
productResource.setId(productId);
productResource.setRoleMappings(Map.of(manager.getRole(), dummyProductRoleInfo(productRoleAdminCode)));
productResource.setRoleMappingsByInstitutionType(Map.of(InstitutionType.PSP.name(), roleMappingByInstitutionType));

if (hasParent) {
Product parent = new Product();
parent.setId("productParentId");
Map<PartyRole, ProductRoleInfo> roleParentMapping = new HashMap<>();
roleParentMapping.put(manager.getRole(), productRoleInfo);
parent.setRoleMappings(roleParentMapping);

parent.setRoleMappings(Map.of(manager.getRole(), dummyProductRoleInfo(productRoleAdminCode)));
productResource.setParentId(parent.getId());
productResource.setParent(parent);
}
Expand Down Expand Up @@ -1045,14 +1052,19 @@ void onboardingPsp_whenUserFoundedAndWillNotUpdateAndProductHasParent(UniAsserte
});
}

Institution dummyInstitution() {
Institution institution = new Institution();
institution.setInstitutionType(InstitutionType.SA);
return institution;
}

@Test
@RunOnVertxContext
void onboarding_whenUserFoundedAndWillNotUpdate(UniAsserter asserter) {
Onboarding onboardingDefaultRequest = new Onboarding();
List<UserRequest> users = List.of(manager);
onboardingDefaultRequest.setProductId("productId");
onboardingDefaultRequest.setInstitution(new Institution());
onboardingDefaultRequest.setInstitution(dummyInstitution());

mockPersistOnboarding(asserter);
mockSimpleSearchPOSTAndPersist(asserter);
Expand Down Expand Up @@ -1203,7 +1215,7 @@ void onboarding_whenUserNotFoundedAndWillSave(UniAsserter asserter) {
Onboarding request = new Onboarding();
List<UserRequest> users = List.of(manager);
request.setProductId("productId");
request.setInstitution(new Institution());
request.setInstitution(dummyInstitution());
final UUID createUserId = UUID.randomUUID();

mockPersistOnboarding(asserter);
Expand Down Expand Up @@ -1242,7 +1254,7 @@ void onboarding_whenUserNotFoundedAndWillSave(UniAsserter asserter) {
@RunOnVertxContext
void onboarding_shouldThrowExceptionIfUserRegistryFails(UniAsserter asserter) {
Onboarding onboardingDefaultRequest = new Onboarding();
onboardingDefaultRequest.setInstitution(new Institution());
onboardingDefaultRequest.setInstitution(dummyInstitution());
onboardingDefaultRequest.setProductId(PROD_INTEROP.getValue());
List<UserRequest> users = List.of(manager);

Expand Down Expand Up @@ -1820,6 +1832,7 @@ void onboarding_aggregationCompletion(UniAsserter asserter) {
request.setProductId(PROD_IO.getValue());
Institution institutionBaseRequest = new Institution();
institutionBaseRequest.setTaxCode("taxCode");
institutionBaseRequest.setInstitutionType(InstitutionType.SA);
request.setInstitution(institutionBaseRequest);

mockPersistOnboarding(asserter);
Expand Down Expand Up @@ -1855,6 +1868,7 @@ void onboardingUsers(UniAsserter asserter) {
List<UserRequest> users = List.of(manager);
request.setProductId(PROD_INTEROP.getValue());
request.setUsers(users);
request.setInstitutionType(InstitutionType.PA);
mockPersistOnboarding(asserter);
mockPersistToken(asserter);

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.3</version>
<version>0.3.0</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.3</version>
<version>0.3.0</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.3</version>
<version>0.3.0</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.3</version>
<version>0.3.0</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.3</version>
<version>0.3.0</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.3</version>
<version>0.3.0</version>
<relativePath>../onboarding-sdk-pom</relativePath>
</parent>
<artifactId>onboarding-sdk-product</artifactId>
<name>onboarding-sdk-product</name>
<version>0.2.3</version>
<version>0.3.0</version>

<properties>
<jackson.version>2.15.2</jackson.version>
Expand Down
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.3</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-common</artifactId>
<version>0.2.3</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-azure-storage</artifactId>
<version>0.2.3</version>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-crypto</artifactId>
<version>0.2.3</version>
<version>0.3.0</version>
</dependency>
</dependencies>

Expand Down

0 comments on commit d4590ca

Please sign in to comment.