Skip to content

Commit

Permalink
Merge pull request #41 from pagopa/VAS-688-add-filter-to-get-stations…
Browse files Browse the repository at this point in the history
…-api-by-ec-code

[VAS-688] feat: Add filter to get stations api by ec code
  • Loading branch information
jacopocarlini authored Mar 27, 2024
2 parents cfda9ff + ba273ad commit afd3369
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 113 deletions.
23 changes: 16 additions & 7 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
],
"paths": {
"/brokers/{brokerId}/creditor-institutions": {
"/brokers/{broker-tax-code}/creditor-institutions": {
"get": {
"tags": [
"Brokers"
Expand Down Expand Up @@ -66,9 +66,9 @@
}
},
{
"name": "brokerId",
"name": "broker-tax-code",
"in": "path",
"description": "Filter by broker code associated to creditor institutions",
"description": "Filter by broker tax code associated to creditor institutions",
"required": true,
"schema": {
"type": "string"
Expand Down Expand Up @@ -193,7 +193,7 @@
}
]
},
"/brokers/{brokerId}/stations": {
"/brokers/{broker-tax-code}/stations": {
"get": {
"tags": [
"Brokers"
Expand All @@ -202,9 +202,9 @@
"operationId": "getStationsDetailsFromBroker",
"parameters": [
{
"name": "brokerId",
"name": "broker-tax-code",
"in": "path",
"description": "The identifier of the broker.",
"description": "The broker's tax code.",
"required": true,
"schema": {
"type": "string"
Expand All @@ -219,6 +219,15 @@
"type": "string"
}
},
{
"name": "ciTaxCode",
"in": "query",
"description": "The creditor institution's tax code.",
"required": false,
"schema": {
"type": "string"
}
},
{
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -2287,4 +2296,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<properties>
<java.version>17</java.version>
<starter.version>1.19.0</starter.version>
<starter.version>1.19.1</starter.version>
<springdoc.version>1.6.14</springdoc.version>
<oracle.version>21.9.0.0</oracle.version>
<postgresql.version>42.5.4</postgresql.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import it.gov.pagopa.apiconfig.selfcareintegration.model.creditorinstitution.CreditorInstitutionDetails;
import it.gov.pagopa.apiconfig.selfcareintegration.model.station.StationDetailsList;
import it.gov.pagopa.apiconfig.selfcareintegration.service.BrokersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Max;
Expand All @@ -38,9 +41,9 @@ public BrokerController(BrokersService brokersService) {
}

/**
* GET /{brokerId}/stations : Get broker stations
* GET /{broker-tax-code}/stations : Get broker stations
*
* @param brokerId broker identifier. (required)
* @param brokerCode broker's tax code. (required)
* @return OK. (status code 200) or Not Found (status code 404) or Service unavailable (status
* code 500)
*/
Expand Down Expand Up @@ -87,14 +90,16 @@ public BrokerController(BrokersService brokersService) {
schema = @Schema(implementation = ProblemJson.class)))
})
@GetMapping(
value = "/{brokerId}/stations",
value = "/{broker-tax-code}/stations",
produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<StationDetailsList> getStationsDetailsFromBroker(
@Parameter(description = "The identifier of the broker.", required = true)
@PathVariable("brokerId")
String brokerId,
@Parameter(description = "The broker's tax code.", required = true)
@PathVariable("broker-tax-code")
String brokerCode,
@Parameter(description = "The identifier of the station.") @RequestParam(required = false)
String stationId,
@Parameter(description = "The creditor institution's tax code.") @RequestParam(required = false)
String ciTaxCode,
@Valid
@Parameter(
description = "The number of elements to be included in the page.",
Expand All @@ -107,10 +112,11 @@ public ResponseEntity<StationDetailsList> getStationsDetailsFromBroker(
@Parameter(description = "The index of the page, starting from 0.", required = true)
@Min(0)
@RequestParam(required = false, defaultValue = "0")
Integer page) {
Integer page
) {
return ResponseEntity.ok(
brokersService.getStationsDetailsFromBroker(
brokerId, stationId, PageRequest.of(page, limit)));
brokersService.getStationsDetailsFromBroker(brokerCode, stationId, ciTaxCode, PageRequest.of(page, limit))
);
}


Expand Down Expand Up @@ -154,7 +160,7 @@ public ResponseEntity<StationDetailsList> getStationsDetailsFromBroker(
schema = @Schema(implementation = ProblemJson.class)))
})
@GetMapping(
value = "/{brokerId}/creditor-institutions",
value = "/{broker-tax-code}/creditor-institutions",
produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<CreditorInstitutionDetails> getCreditorInstitutionsAssociatedToBroker(
@Parameter(description = "Number of elements on one page. Default = 50")
Expand All @@ -163,12 +169,12 @@ public ResponseEntity<CreditorInstitutionDetails> getCreditorInstitutionsAssocia
@Parameter(description = "Page number. Page value starts from 0", required = true)
@PositiveOrZero @RequestParam
Integer page,
@Parameter(description = "Filter by broker code associated to creditor institutions")
@PathVariable("brokerId")
String brokerId,
@Parameter(description = "Filter by broker tax code associated to creditor institutions")
@PathVariable("broker-tax-code")
String brokerCode,
@Parameter(description = "Filter by enabled station")
@RequestParam(required = false, name = "enabled")
Boolean enabled) {
return ResponseEntity.ok(brokersService.getCreditorInstitutionsAssociatedToBroker(brokerId, enabled, PageRequest.of(page, limit)));
return ResponseEntity.ok(brokersService.getCreditorInstitutionsAssociatedToBroker(brokerCode, enabled, PageRequest.of(page, limit)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.gov.pagopa.apiconfig.selfcareintegration.model.station.StationDetails;
import it.gov.pagopa.apiconfig.selfcareintegration.model.station.StationDetailsList;
import it.gov.pagopa.apiconfig.selfcareintegration.specification.PaStazionePaSpecifications;
import it.gov.pagopa.apiconfig.selfcareintegration.specification.StationSpecifications;
import it.gov.pagopa.apiconfig.selfcareintegration.util.Utility;
import it.gov.pagopa.apiconfig.starter.entity.IntermediariPa;
import it.gov.pagopa.apiconfig.starter.entity.PaStazionePa;
Expand All @@ -15,7 +16,6 @@
import it.gov.pagopa.apiconfig.starter.repository.PaStazionePaRepository;
import it.gov.pagopa.apiconfig.starter.repository.StazioniRepository;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -24,7 +24,6 @@
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
Expand All @@ -38,27 +37,40 @@ public class BrokersService {

private final ModelMapper modelMapper;

public BrokersService(StazioniRepository stazioniRepository, IntermediariPaRepository intermediariPaRepository, PaStazionePaRepository paStazionePaRepository, ModelMapper modelMapper) {
public BrokersService(
StazioniRepository stazioniRepository,
IntermediariPaRepository intermediariPaRepository,
PaStazionePaRepository paStazionePaRepository,
ModelMapper modelMapper) {
this.stazioniRepository = stazioniRepository;
this.intermediariPaRepository = intermediariPaRepository;
this.paStazionePaRepository = paStazionePaRepository;
this.modelMapper = modelMapper;
}

/**
* Retrieve the paginated list of station by broker tax code. the list can be filtered by station id and/or
* creditor institution's tax code
*
* @param brokerCode broker's tax code
* @param stationId station id
* @param ciTaxCode creditor institution's tax code
* @param pageable page request
* @return the paginated list of stations
*/
public StationDetailsList getStationsDetailsFromBroker(
@NotNull String brokerId, String stationId, Pageable pageable) throws AppException {
IntermediariPa broker = getBrokerIfExists(brokerId);
Page<Stazioni> queryResult;
if (stationId == null) {
queryResult = stazioniRepository.findAllByFiltersOrderById(broker.getObjId(), pageable);
} else {
queryResult =
stazioniRepository.findAllByFiltersOrderById(broker.getObjId(), stationId, pageable);
}
List<StationDetails> stations =
queryResult.stream()
.map(station -> modelMapper.map(station, StationDetails.class))
.toList();
@NotNull String brokerCode,
String stationId,
String ciTaxCode,
Pageable pageable
) {
IntermediariPa broker = getBrokerIfExists(brokerCode);
Page<Stazioni> queryResult = stazioniRepository
.findAll(StationSpecifications.filter(broker.getObjId(), stationId, ciTaxCode), pageable);

List<StationDetails> stations = queryResult.stream()
.map(station -> modelMapper.map(station, StationDetails.class))
.toList();
return StationDetailsList.builder()
.pageInfo(Utility.buildPageInfo(queryResult))
.stationsDetailsList(stations)
Expand All @@ -77,10 +89,10 @@ public CreditorInstitutionDetails getCreditorInstitutionsAssociatedToBroker(@Not
.build();
}

protected IntermediariPa getBrokerIfExists(String brokerId) throws AppException {
Optional<IntermediariPa> result = intermediariPaRepository.findByIdIntermediarioPa(brokerId);
protected IntermediariPa getBrokerIfExists(String brokerCode) throws AppException {
Optional<IntermediariPa> result = intermediariPaRepository.findByIdIntermediarioPa(brokerCode);
if (result.isEmpty()) {
throw new AppException(AppError.BROKER_NOT_FOUND, brokerId);
throw new AppException(AppError.BROKER_NOT_FOUND, brokerCode);
}
return result.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package it.gov.pagopa.apiconfig.selfcareintegration.specification;

import it.gov.pagopa.apiconfig.starter.entity.Pa;
import it.gov.pagopa.apiconfig.starter.entity.PaStazionePa;
import it.gov.pagopa.apiconfig.starter.entity.Stazioni;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
* Specification class that define queries on {@link Stazioni} table
*/
public class StationSpecifications {

public static Specification<Stazioni> filter(Long fkBroker, String stationId, String ciTaxCode) {
return (root, query, cb) -> {
List<Predicate> list = new ArrayList<>();

query.distinct(true);
query.orderBy(cb.asc(root.get("idStazione")));

if (ciTaxCode != null) {
Join<Stazioni, PaStazionePa> paStazionePaJoin = root.join("paStazionePaList", JoinType.LEFT);
Join<PaStazionePa, Pa> paJoin = paStazionePaJoin.join("pa", JoinType.LEFT);
list.add(cb.and(cb.equal(paJoin.get("idDominio"), ciTaxCode)));
}

if (stationId != null) {
Expression<String> pattern = cb.upper(cb.literal("%" + stationId + "%"));
list.add(cb.and(cb.like(cb.upper(root.get("idStazione")), pattern)));
}
list.add(cb.and(cb.equal(root.get("fkIntermediarioPa"), fkBroker)));

Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
};
}

private StationSpecifications() {}
}
2 changes: 1 addition & 1 deletion src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProfile name="local">
<springProfile name="local | h2">
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ class BrokersControllerTest {

@BeforeEach
void setup() throws IOException {
when(brokersService.getStationsDetailsFromBroker("1234", null, PageRequest.of(0, 50)))
when(brokersService.getStationsDetailsFromBroker("1234", null, null, PageRequest.of(0, 50)))
.thenReturn(getMockStationDetailsList());
when(brokersService.getStationsDetailsFromBroker(
"1234", "80007580279_01", PageRequest.of(0, 50)))
"1234", "80007580279_01", null, PageRequest.of(0, 50)))
.thenReturn(getMockStationDetailsList());
when(brokersService.getStationsDetailsFromBroker(
"1234", "80007580279_01", "80007580279", PageRequest.of(0, 50)))
.thenReturn(getMockStationDetailsList());
when(brokersService.getCreditorInstitutionsAssociatedToBroker("1234", null, PageRequest.of(0, 50)))
.thenReturn(getMockCreditorInstitutionDetails());
Expand All @@ -49,6 +52,7 @@ void setup() throws IOException {
@CsvSource({
"/brokers/1234/stations?limit=50&page=0",
"/brokers/1234/stations?stationId=80007580279_01&limit=50&page=0",
"/brokers/1234/stations?stationId=80007580279_01&ciTaxCode=80007580279&limit=50&page=0",
"/brokers/1234/creditor-institutions?limit=50&page=0",
"/brokers/1234/creditor-institutions?enabled=true&limit=50&page=0",
})
Expand Down
Loading

0 comments on commit afd3369

Please sign in to comment.