Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[SELC-4717] Add order param to getDelegation (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-putzu authored Apr 17, 2024
1 parent f943976 commit 9f8092d
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 39 deletions.
10 changes: 10 additions & 0 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@
"type" : "string",
"enum" : [ "FULL", "NORMAL" ]
}
}, {
"name" : "order",
"in" : "query",
"description" : "Order to show response NONE, ASC, DESC",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string",
"enum" : [ "ASC", "DESC", "NONE" ]
}
}, {
"name" : "page",
"in" : "query",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.model.delegation.Delegation;

import java.util.List;
Expand All @@ -10,7 +11,7 @@ public interface DelegationConnector {

Delegation save(Delegation delegation);
boolean checkIfExists(Delegation delegation);
List<Delegation> find(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode, Integer page, Integer size);
List<Delegation> find(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode, Order order, Integer page, Integer size);
Delegation findByIdAndModifyStatus(String delegationId, DelegationState status);
boolean checkIfDelegationsAreActive(String institutionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.pagopa.selfcare.mscore.constant;

public enum Order {

NONE,
ASC,
DESC;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import it.pagopa.selfcare.mscore.connector.dao.model.mapper.DelegationInstitutionMapper;
import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.model.delegation.Delegation;
import it.pagopa.selfcare.mscore.model.delegation.DelegationInstitution;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
Expand All @@ -29,6 +31,8 @@
@Component
public class DelegationConnectorImpl implements DelegationConnector {

public static final String INSTITUTIONS = "institutions";
public static final String DELEGATIONS = "Delegations";
private final DelegationRepository repository;
private final DelegationEntityMapper delegationMapper;
private final DelegationInstitutionMapper delegationInstitutionMapper;
Expand Down Expand Up @@ -63,11 +67,13 @@ public boolean checkIfExists(Delegation delegation) {
}

@Override
public List<Delegation> find(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode, Integer page, Integer size) {
public List<Delegation> find(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode, Order order, Integer page, Integer size) {
List<Criteria> criterias = new ArrayList<>();
Criteria criteria = new Criteria();
Pageable pageable = PageRequest.of(page, size);

criterias.add(Criteria.where(DelegationEntity.Fields.status.name()).is(DelegationState.ACTIVE.name()));

if (Objects.nonNull(from)) {
criterias.add(Criteria.where(DelegationEntity.Fields.from.name()).is(from));
}
Expand All @@ -80,40 +86,65 @@ public List<Delegation> find(String from, String to, String productId, String se
if (Objects.nonNull(search)) {
criterias.add(Criteria.where(DelegationEntity.Fields.institutionFromName.name()).regex("(?i)" + Pattern.quote(search)));
}

Sort.Direction sortDirection = order.equals(Order.ASC) ? Sort.Direction.ASC : Sort.Direction.DESC;

if (GetDelegationsMode.FULL.equals(mode)) {

GraphLookupOperation.GraphLookupOperationBuilder lookup = Aggregation.graphLookup("Institution")
.startWith(Objects.nonNull(from) ? "to" : "from")
.connectFrom(Objects.nonNull(from) ? "to" : "from")
.connectTo("_id");
GraphLookupOperation.GraphLookupOperationBuilder lookup = createInstitutionGraphLookupOperationBuilder(from);

MatchOperation matchOperation = new MatchOperation(new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()])));

long skipLimit = (long) page * size;
SkipOperation skip = Aggregation.skip(skipLimit);
LimitOperation limit = Aggregation.limit(skipLimit + size);
LimitOperation limit = Aggregation.limit(size);
Aggregation aggregation;

if (Objects.nonNull(taxCode)) {
Criteria taxCodeCriteria = Criteria.where("institutions." + InstitutionEntity.Fields.taxCode.name()).is(taxCode);
MatchOperation matchTaxCodeOperation = new MatchOperation(new Criteria().andOperator(taxCodeCriteria));
aggregation = Aggregation.newAggregation(matchOperation, lookup.as("institutions"), matchTaxCodeOperation, skip, limit);
} else {
aggregation = Aggregation.newAggregation(matchOperation, lookup.as("institutions"), skip, limit);
MatchOperation matchTaxCodeOperation = getMatchTaxCodeOperation(taxCode);
aggregation = Aggregation.newAggregation(matchOperation, lookup.as(INSTITUTIONS), matchTaxCodeOperation, skip, limit);
}
else {
if (!order.equals(Order.NONE)) {
SortOperation sortOperation = new SortOperation(Sort.by(sortDirection, DelegationEntity.Fields.institutionFromName.name()));
aggregation = Aggregation.newAggregation(matchOperation, sortOperation, lookup.as(INSTITUTIONS), skip, limit);
}
else {
aggregation = Aggregation.newAggregation(matchOperation, lookup.as(INSTITUTIONS), skip, limit);
}
}

List<DelegationInstitution> result = mongoTemplate.aggregate(aggregation, "Delegations", DelegationInstitution.class).getMappedResults();
List<DelegationInstitution> result = mongoTemplate.aggregate(aggregation, DELEGATIONS, DelegationInstitution.class).getMappedResults();
return result.stream()
.map(Objects.nonNull(from) ?
delegationInstitutionMapper::convertToDelegationBroker :
delegationInstitutionMapper::convertToDelegationInstitution)
.collect(Collectors.toList());
}

return repository.find(Query.query(criteria.andOperator(criterias)), pageable, DelegationEntity.class)
Query query = Query.query(criteria.andOperator(criterias));
if (!order.equals(Order.NONE)) {
query = query.with(Sort.by(sortDirection, DelegationEntity.Fields.institutionFromName.name()));
}

return repository.find(query, pageable, DelegationEntity.class)
.stream()
.map(delegationMapper::convertToDelegation)
.collect(Collectors.toList());
}

private static GraphLookupOperation.GraphLookupOperationBuilder createInstitutionGraphLookupOperationBuilder(String from) {
return Aggregation.graphLookup(INSTITUTIONS)
.startWith(Objects.nonNull(from) ? "to" : "from")
.connectFrom(Objects.nonNull(from) ? "to" : "from")
.connectTo("_id");
}

private static MatchOperation getMatchTaxCodeOperation(String taxCode) {
Criteria taxCodeCriteria = Criteria.where("institutions." + InstitutionEntity.Fields.taxCode.name()).is(taxCode);
return new MatchOperation(new Criteria().andOperator(taxCodeCriteria));
}

@Override
public Delegation findByIdAndModifyStatus(String delegationId, DelegationState status) {
Query query = Query.query(Criteria.where(DelegationEntity.Fields.id.name()).is(delegationId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.DelegationType;
import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.model.delegation.Delegation;
import it.pagopa.selfcare.mscore.model.delegation.DelegationInstitution;
Expand Down Expand Up @@ -130,7 +131,7 @@ void find_shouldGetData() {
.find(any(), any(), any());

List<Delegation> response = delegationConnectorImpl.find(delegationEntity.getFrom(),
delegationEntity.getTo(), delegationEntity.getProductId(), null, null, GetDelegationsMode.NORMAL, PAGE_SIZE, MAX_PAGE_SIZE);
delegationEntity.getTo(), delegationEntity.getProductId(), null, null, GetDelegationsMode.NORMAL, Order.NONE, PAGE_SIZE, MAX_PAGE_SIZE);

//Then
assertNotNull(response);
Expand Down Expand Up @@ -158,7 +159,7 @@ void getBrokersFullMode() {
thenReturn(results);

List<Delegation> response = delegationConnectorImpl.find(dummyDelegationEntity.getFrom(), null,
dummyDelegationEntity.getProductId(), null, null, GetDelegationsMode.FULL, PAGE_SIZE, MAX_PAGE_SIZE);
dummyDelegationEntity.getProductId(), null, null, GetDelegationsMode.FULL, Order.NONE, PAGE_SIZE, MAX_PAGE_SIZE);

//Then
assertNotNull(response);
Expand Down Expand Up @@ -188,7 +189,7 @@ void getInstitutionsFullMode() {
thenReturn(results);

List<Delegation> response = delegationConnectorImpl.find(null, dummyDelegationEntity.getTo(),
dummyDelegationEntity.getProductId(), null, null, GetDelegationsMode.FULL, PAGE_SIZE, MAX_PAGE_SIZE);
dummyDelegationEntity.getProductId(), null, null, GetDelegationsMode.FULL, Order.ASC, PAGE_SIZE, MAX_PAGE_SIZE);

//Then
assertNotNull(response);
Expand Down Expand Up @@ -247,7 +248,7 @@ void find_shouldGetDataPaginated() {
thenReturn(results);

List<Delegation> response = delegationConnectorImpl.find(null,
TO1, "productId", null, null, GetDelegationsMode.FULL, 0, 1);
TO1, "productId", null, null, GetDelegationsMode.FULL, Order.NONE, 0, 1);

//Then
assertNotNull(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.pagopa.selfcare.mscore.core;

import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.model.delegation.Delegation;

import java.util.List;
Expand All @@ -10,7 +11,7 @@ public interface DelegationService {

Delegation createDelegation(Delegation delegation);
boolean checkIfExists(Delegation delegation);
List<Delegation> getDelegations(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode, Optional<Integer> page, Optional<Integer> size);
List<Delegation> getDelegations(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode, Optional<Order> order, Optional<Integer> page, Optional<Integer> size);
Delegation createDelegationFromInstitutionsTaxCode(Delegation delegation);
void deleteDelegationByDelegationId(String delegationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.pagopa.selfcare.mscore.constant.CustomError;
import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.exception.ResourceConflictException;
import it.pagopa.selfcare.mscore.exception.ResourceNotFoundException;
Expand Down Expand Up @@ -163,8 +164,8 @@ public boolean checkIfExists(Delegation delegation) {

@Override
public List<Delegation> getDelegations(String from, String to, String productId, String search, String taxCode, GetDelegationsMode mode,
Optional<Integer> page, Optional<Integer> size) {
Optional<Order> order, Optional<Integer> page, Optional<Integer> size) {
int pageSize = size.filter(s -> s > 0).filter(s -> s <= DEFAULT_DELEGATIONS_PAGE_SIZE).orElse(DEFAULT_DELEGATIONS_PAGE_SIZE);
return delegationConnector.find(from, to, productId, search, taxCode, mode, page.orElse(0), pageSize);
return delegationConnector.find(from, to, productId, search, taxCode, mode, order.orElse(Order.NONE), page.orElse(0), pageSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.pagopa.selfcare.mscore.api.DelegationConnector;
import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.exception.ResourceConflictException;
import it.pagopa.selfcare.mscore.exception.ResourceNotFoundException;
Expand Down Expand Up @@ -176,12 +177,12 @@ void find_shouldGetData() {
//Given
Delegation delegation = new Delegation();
delegation.setId("id");
when(delegationConnector.find(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(List.of(delegation));
when(delegationConnector.find(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(List.of(delegation));
//When
List<Delegation> response = delegationServiceImpl.getDelegations("from", "to", "productId", null, null,
GetDelegationsMode.NORMAL, Optional.of(0), Optional.of(100));
GetDelegationsMode.NORMAL, Optional.empty(), Optional.of(0), Optional.of(100));
//Then
verify(delegationConnector).find(any(), any(), any(), any(), any(), any(), any(), any());
verify(delegationConnector).find(any(), any(), any(), any(), any(), any(), any(), any(), any());

assertNotNull(response);
assertFalse(response.isEmpty());
Expand All @@ -196,12 +197,12 @@ void find_shouldGetData_fullMode() {
//Given
Delegation delegation = new Delegation();
delegation.setId("id");
when(delegationConnector.find(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(List.of(delegation));
when(delegationConnector.find(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(List.of(delegation));
//When
List<Delegation> response = delegationServiceImpl.getDelegations("from", null, "productId", null, null,
GetDelegationsMode.FULL, Optional.of(0), Optional.of(0));
GetDelegationsMode.FULL, Optional.of(Order.DESC), Optional.of(0), Optional.of(0));
//Then
verify(delegationConnector).find(any(), any(), any(), any(), any(), any(), any(), any());
verify(delegationConnector).find(any(), any(), any(), any(), any(), any(), any(), any(), any());

assertNotNull(response);
assertFalse(response.isEmpty());
Expand All @@ -216,12 +217,12 @@ void find_shouldGetData_fullMode_defaultPage() {
//Given
Delegation delegation = new Delegation();
delegation.setId("id");
when(delegationConnector.find(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(List.of(delegation));
when(delegationConnector.find(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(List.of(delegation));
//When
List<Delegation> response = delegationServiceImpl.getDelegations("from", null, "productId", null, null,
GetDelegationsMode.FULL, Optional.empty(), Optional.empty());
GetDelegationsMode.FULL, Optional.of(Order.DESC), Optional.empty(), Optional.empty());
//Then
verify(delegationConnector).find(any(), any(), any(), any(), any(), any(), any(), any());
verify(delegationConnector).find(any(), any(), any(), any(), any(), any(), any(), any(), any());

assertNotNull(response);
assertFalse(response.isEmpty());
Expand Down
2 changes: 2 additions & 0 deletions infra/container_apps/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.tags.Tags;
import it.pagopa.selfcare.mscore.constant.GenericError;
import it.pagopa.selfcare.mscore.constant.GetDelegationsMode;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.core.DelegationService;
import it.pagopa.selfcare.mscore.exception.InvalidRequestException;
import it.pagopa.selfcare.mscore.model.delegation.Delegation;
Expand Down Expand Up @@ -105,13 +106,15 @@ public ResponseEntity<List<DelegationResponse>> getDelegations(@ApiParam("${swag
@RequestParam(name = "taxCode", required = false) String taxCode,
@ApiParam("${swagger.mscore.institutions.delegations.mode}")
@RequestParam(name = "mode", required = false) GetDelegationsMode mode,
@ApiParam("${swagger.mscore.institutions.delegations.order}")
@RequestParam(name = "order", required = false) Optional<Order> order,
@RequestParam(name = "page", required = false) Optional<Integer> page,
@RequestParam(name = "size", required = false) Optional<Integer> size) {

if(Objects.isNull(institutionId) && Objects.isNull(brokerId))
throw new InvalidRequestException("institutionId or brokerId must not be null!!", GenericError.GENERIC_ERROR.getCode());

return ResponseEntity.status(HttpStatus.OK).body(delegationService.getDelegations(institutionId, brokerId, productId, search, taxCode, mode, page, size).stream()
return ResponseEntity.status(HttpStatus.OK).body(delegationService.getDelegations(institutionId, brokerId, productId, search, taxCode, mode, order, page, size).stream()
.map(delegationMapper::toDelegationResponse)
.collect(Collectors.toList()));
}
Expand Down
1 change: 1 addition & 0 deletions web/src/main/resources/swagger/swagger_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ swagger.mscore.api.users.updateUserStatus=Update user status with optional filte
swagger.mscore.api.users.getOnboardedUsers=Retrieve onboarded users according to identifiers in input
swagger.mscore.institutions.delegations=Retrieve institution's delegations
swagger.mscore.institutions.delegations.mode=Mode (full or normal) to retreieve institution's delegations
swagger.mscore.institutions.delegations.order=Order to show response NONE, ASC, DESC
swagger.mscore.institutions.brokers=Retrieve institution brokers
swagger.mscore.institutions.getInstitutionBrokers=Retrieve institution brokers
swagger.mscore.users.userId=User's unique identifier
Expand Down
Loading

0 comments on commit 9f8092d

Please sign in to comment.