Skip to content

Commit

Permalink
[SELC-5244] Feat: Refactor API /v2/institutions (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo authored Jul 19, 2024
1 parent eb7eee3 commit 83df2a5
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 129 deletions.
2 changes: 1 addition & 1 deletion connector-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.1.10</version>
<version>0.1.14</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface MsCoreConnector {

List<String> getInstitutionUserProductsV2(String institutionId, String id);

Institution getInstitution(String institutionId);

String createPgInstitution(String description, String taxId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package it.pagopa.selfcare.external_api.model.onboarding;

import it.pagopa.selfcare.commons.base.utils.InstitutionType;
import it.pagopa.selfcare.external_api.model.institutions.RootParent;
import lombok.Data;

import java.util.Collection;
import java.util.UUID;

@Data
public class OnboardedInstitutionResource {

private UUID id;
private String description;
private String externalId;
private String originId;
private InstitutionType institutionType;
private String digitalAddress;
private String status;
private String address;
private String zipCode;
private String taxCode;
private String taxCodeInvoicing;
private String origin;
private Collection<String> userProductRoles;
private String recipientCode;
private String rea;
private String shareCapital;
private String businessRegisterPlace;
private String supportEmail;
private String supportPhone;
private PaymentServiceProvider paymentServiceProvider;
private DataProtectionOfficer dataProtectionOfficer;
private String subunitCode;
private String subunitType;
private RootParent rootParent;
private String aooParentCode;
private String country;
private String county;
private String city;

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
package it.pagopa.selfcare.external_api.model.onboarding.mapper;

import it.pagopa.selfcare.external_api.model.institutions.Institution;
import it.pagopa.selfcare.external_api.model.institutions.Onboarding;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionInfo;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionResource;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionResponse;
import it.pagopa.selfcare.external_api.model.user.OnboardedProductResponse;
import it.pagopa.selfcare.external_api.model.user.RelationshipState;
import it.pagopa.selfcare.external_api.model.user.UserInstitution;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring")

public interface OnboardingInstitutionMapper {
OnboardedInstitutionResponse toOnboardedInstitutionResponse(OnboardedInstitutionInfo onboardedInstitutionInfo);

@Mapping(target = "userProductRoles", expression = "java(retrieveUserProductRole(userInstitution, productId))")
@Mapping(target = "status", expression = "java(retrieveStatus(institution, productId))")
@Mapping(target = "id", source = "institution.id")
OnboardedInstitutionResource toOnboardedInstitutionResource(Institution institution, UserInstitution userInstitution, String productId);


@Named("retrieveStatus")
default String retrieveStatus(Institution institution, String productId) {
List<RelationshipState> statusList = institution.getOnboarding().stream()
.filter(onboarding -> productId.equalsIgnoreCase(onboarding.getProductId()))
.map(Onboarding::getStatus)
.toList();

if(CollectionUtils.isEmpty(statusList))
return null;

return Collections.min(statusList).name();
}

@Named("retrieveUserProductRole")
default Collection<String> retrieveUserProductRole(UserInstitution userInstitution, String productId) {
return userInstitution.getProducts().stream()
.collect(Collectors.groupingBy(OnboardedProductResponse::getProductId, Collectors.mapping(OnboardedProductResponse::getProductRole, Collectors.toList())))
.get(productId);
}
}
6 changes: 6 additions & 0 deletions connector/rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>it.pagopa.selfcare</groupId>
<artifactId>onboarding-sdk-product</artifactId>
<version>0.1.8</version>
<scope>compile</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.pagopa.selfcare.external_api.connector.rest;

import it.pagopa.selfcare.commons.base.logging.LogUtils;
import it.pagopa.selfcare.core.generated.openapi.v1.dto.CreatePgInstitutionRequest;
import it.pagopa.selfcare.core.generated.openapi.v1.dto.InstitutionResponse;
import it.pagopa.selfcare.core.generated.openapi.v1.dto.OnboardingResponse;
Expand All @@ -9,15 +8,12 @@
import it.pagopa.selfcare.external_api.connector.rest.client.MsCoreRestClient;
import it.pagopa.selfcare.external_api.connector.rest.client.MsUserApiRestClient;
import it.pagopa.selfcare.external_api.connector.rest.mapper.InstitutionMapper;
import it.pagopa.selfcare.external_api.connector.rest.model.pnpg.CreatePnPgInstitutionRequest;
import it.pagopa.selfcare.external_api.connector.rest.model.pnpg.InstitutionPnPgResponse;
import it.pagopa.selfcare.external_api.exceptions.ResourceNotFoundException;
import it.pagopa.selfcare.external_api.model.institutions.GeographicTaxonomy;
import it.pagopa.selfcare.external_api.model.institutions.Institution;
import it.pagopa.selfcare.external_api.model.institutions.SearchMode;
import it.pagopa.selfcare.external_api.model.onboarding.InstitutionOnboarding;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionInfo;
import it.pagopa.selfcare.external_api.model.pnpg.CreatePnPgInstitution;
import it.pagopa.selfcare.user.generated.openapi.v1.dto.OnboardedProductResponse;
import it.pagopa.selfcare.user.generated.openapi.v1.dto.UserDataResponse;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -141,6 +137,13 @@ public List<OnboardedInstitutionInfo> getInstitutionDetails(String institutionId
return Collections.emptyList();
}

@Override
public Institution getInstitution(String institutionId) {
return institutionMapper.toInstitution(Objects.requireNonNull(institutionApiClient._retrieveInstitutionByIdUsingGET(institutionId))
.getBody());
}


@Override
public String createPgInstitution(String description, String taxId) {
log.trace("createPgInstitution start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.pagopa.selfcare.external_api.model.institutions.AssistanceContacts;
import it.pagopa.selfcare.external_api.model.institutions.CompanyInformations;
import it.pagopa.selfcare.external_api.model.institutions.Institution;
import it.pagopa.selfcare.external_api.model.institutions.Onboarding;
import it.pagopa.selfcare.external_api.model.onboarding.Billing;
import it.pagopa.selfcare.external_api.model.onboarding.InstitutionOnboarding;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionInfo;
Expand Down Expand Up @@ -89,4 +90,26 @@ default OffsetDateTime map(LocalDateTime localDateTime) {
.map(time -> time.atZone(ZoneId.systemDefault()).toOffsetDateTime())
.orElse(null);
}

@Mapping(target = "onboarding", source = "onboarding", qualifiedByName = "toOnboardingWithDate")
Institution toInstitution(it.pagopa.selfcare.core.generated.openapi.v1.dto.InstitutionResponse body);

@Named("toOnboardingWithDate")
default Onboarding toOnboardingWithDate(OnboardedProductResponse onboardedProductResponse) {
Onboarding onboarding = toOnboardingWithoutDate(onboardedProductResponse);
onboarding.setCreatedAt(convertDate(onboardedProductResponse.getCreatedAt()));
onboarding.setUpdatedAt(convertDate(onboardedProductResponse.getUpdatedAt()));
return onboarding;
}

@Mapping(target = "createdAt", ignore = true)
@Mapping(target = "updatedAt", ignore = true)
@Mapping(target = "closedAt", ignore = true)
Onboarding toOnboardingWithoutDate(OnboardedProductResponse onboardedProductResponse);

default OffsetDateTime convertDate(LocalDateTime localDateTime) {
return Optional.ofNullable(localDateTime)
.map(time -> time.atZone(ZoneId.systemDefault()).toOffsetDateTime())
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package it.pagopa.selfcare.external_api.connector.rest;

import it.pagopa.selfcare.core.generated.openapi.v1.dto.InstitutionResponse;
import it.pagopa.selfcare.core.generated.openapi.v1.dto.OnboardedProductResponse;
import it.pagopa.selfcare.external_api.connector.rest.mapper.InstitutionMapperImpl;
import it.pagopa.selfcare.external_api.model.institutions.Institution;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;
import java.util.List;

@ExtendWith(MockitoExtension.class)
public class InstitutionMapperTest {

@InjectMocks
InstitutionMapperImpl institutionMapperImpl;

@Test
void toOnboardingWithDate(){
InstitutionResponse institutionResponse = new InstitutionResponse();
OnboardedProductResponse onboardedProductResponse = new OnboardedProductResponse();
onboardedProductResponse.setProductId("productId");
onboardedProductResponse.setStatus(OnboardedProductResponse.StatusEnum.ACTIVE);
onboardedProductResponse.setCreatedAt(LocalDateTime.now());
institutionResponse.setOnboarding(List.of(onboardedProductResponse));
Institution institution = institutionMapperImpl.toInstitution(institutionResponse);
Assertions.assertNotNull(institution);
Assertions.assertNotNull(institution.getOnboarding());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void getGeographicTaxonomyList() throws IOException {
}

@Test
void getInstitutionsByGeoTaxonomiesWithInstitutionNull() {
void getInstitutionsByGeoTaxonomiesWithInstitutionNull() {

String geoTaxIds = "geoTaxIds";
SearchMode searchMode = SearchMode.any;
Expand Down Expand Up @@ -219,11 +219,11 @@ void getInstitutionUserProductsV2() throws IOException {

List<String> response = msCoreConnector.getInstitutionUserProductsV2(institutionId, userId);
Assertions.assertEquals(1, response.size());
Assertions.assertEquals("productId",String.join(",", response));
Assertions.assertEquals("productId", String.join(",", response));
}

@Test
void getInstitutionUserProductsV2EmptyList1() {
void getInstitutionUserProductsV2EmptyList1() {

String institutionId = "institutionId";
String userId = "userId";
Expand Down Expand Up @@ -308,7 +308,7 @@ void createPgInstitution() {
response.setId(institutionId);
when(institutionApiClient._createPgInstitutionUsingPOST(any())).thenReturn(ResponseEntity.of(Optional.of(response)));
//when
String institutionPnPgResponse = msCoreConnector.createPgInstitution("description", "taxId");
String institutionPnPgResponse = msCoreConnector.createPgInstitution("description", "taxId");
//then
assertEquals(institutionId, institutionPnPgResponse);

Expand Down Expand Up @@ -339,4 +339,19 @@ void getInstitutionsDetailsBillingNull() throws IOException {
verify(institutionApiClient, times(1))._retrieveInstitutionByIdUsingGET(institutionId);

}

@Test
void getInstitution_happyPath() {
String institutionId = "institutionId";
InstitutionResponse institutionResponse = new InstitutionResponse();
institutionResponse.setId(institutionId);
when(institutionApiClient._retrieveInstitutionByIdUsingGET(institutionId)).thenReturn(ResponseEntity.ok(institutionResponse));
when(institutionMapper.toInstitution(any(InstitutionResponse.class))).thenReturn(new Institution());

Institution result = msCoreConnector.getInstitution(institutionId);

assertNotNull(result);
verify(institutionApiClient, times(1))._retrieveInstitutionByIdUsingGET(institutionId);
verify(institutionMapper, times(1)).toInstitution(any(InstitutionResponse.class));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package it.pagopa.selfcare.external_api.connector.rest;

import it.pagopa.selfcare.external_api.connector.rest.mapper.UserMapperImpl;
import it.pagopa.selfcare.external_api.connector.rest.mapper.UserResourceMapperImpl;
import it.pagopa.selfcare.onboarding.common.PartyRole;
import com.fasterxml.jackson.core.type.TypeReference;
import it.pagopa.selfcare.external_api.connector.rest.client.MsUserApiRestClient;
import it.pagopa.selfcare.external_api.connector.rest.config.BaseConnectorTest;
import it.pagopa.selfcare.external_api.connector.rest.mapper.UserMapperImpl;
import it.pagopa.selfcare.external_api.connector.rest.mapper.UserResourceMapperImpl;
import it.pagopa.selfcare.external_api.model.institutions.Institution;
import it.pagopa.selfcare.external_api.model.user.UserInstitution;
import it.pagopa.selfcare.external_api.model.user.UserToOnboard;
Expand Down Expand Up @@ -85,7 +85,7 @@ void getUserInstitution() throws IOException {
}

@Test
public void searchUserByExternalId_returnsUser() {
void searchUserByExternalId_returnsUser() {

String fiscalCode = "fiscalCode";
SearchUserDto searchUserDto = new SearchUserDto(fiscalCode);
Expand All @@ -103,7 +103,7 @@ public void searchUserByExternalId_returnsUser() {
}

@Test
public void searchUserByExternalId_returnsNull_whenUserDoesNotExist() {
void searchUserByExternalId_returnsNull_whenUserDoesNotExist() {

String fiscalCode = "fiscalCode";
SearchUserDto searchUserDto = new SearchUserDto(fiscalCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.pagopa.selfcare.external_api.core;

import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionInfo;
import it.pagopa.selfcare.external_api.model.onboarding.OnboardedInstitutionResource;
import it.pagopa.selfcare.external_api.model.user.RelationshipState;
import it.pagopa.selfcare.external_api.model.user.UserDetailsWrapper;
import it.pagopa.selfcare.external_api.model.user.UserInfoWrapper;
Expand All @@ -18,7 +19,7 @@ public interface UserService {

UserDetailsWrapper getUserOnboardedProductsDetailsV2(String userId, String institutionId, String productId);

List<OnboardedInstitutionInfo> getOnboardedInstitutionsDetailsActive(String userId, String productId);
List<OnboardedInstitutionResource> getOnboardedInstitutionsDetailsActive(String userId, String productId);

List<UserInstitution> getUsersInstitutions(String userId, String institutionId, Integer page, Integer size, List<String> productRoles, List<String> products, List<PartyRole> roles, List<String> states);
}
Loading

0 comments on commit 83df2a5

Please sign in to comment.