-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
P4PU-146 added business logic to retrieve transactions list and added…
… mapper and Utilities class
- Loading branch information
1 parent
aeb2cba
commit c5c8a6b
Showing
9 changed files
with
140 additions
and
49 deletions.
There are no files selected for viewing
14 changes: 8 additions & 6 deletions
14
src/main/java/it/gov/pagopa/arc/controller/TransactionsControllerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
package it.gov.pagopa.arc.controller; | ||
|
||
|
||
import it.gov.pagopa.arc.dto.TransactionDTO; | ||
import it.gov.pagopa.arc.controller.generated.ArcTransactionsApi; | ||
import it.gov.pagopa.arc.model.generated.TransactionsListDTO; | ||
import it.gov.pagopa.arc.service.TransactionsService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class TransactionsControllerImpl implements TransactionsController { | ||
public class TransactionsControllerImpl implements ArcTransactionsApi { | ||
private final TransactionsService transactionsService; | ||
|
||
public TransactionsControllerImpl(TransactionsService transactionsService) { | ||
this.transactionsService = transactionsService; | ||
} | ||
|
||
@Override | ||
public List<TransactionDTO> getTransactionsList(String fiscalCode, String continuationToken, int size) { | ||
return transactionsService.retrieveTransactionsList(fiscalCode); | ||
public ResponseEntity<TransactionsListDTO> getTransactionsList(Integer page, Integer size, String filter) { | ||
TransactionsListDTO transactionsListDTO = transactionsService.retrieveTransactionsList(page,size,filter); | ||
return new ResponseEntity<>(transactionsListDTO, HttpStatus.OK); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package it.gov.pagopa.arc.dto.mapper; | ||
|
||
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDTO; | ||
import it.gov.pagopa.arc.model.generated.TransactionDTO; | ||
import it.gov.pagopa.arc.utils.Utilities; | ||
import org.springframework.stereotype.Service; | ||
@Service | ||
public class BizEventsTransactionDTO2TransactionDTO { | ||
public TransactionDTO apply(BizEventsTransactionDTO transaction){ | ||
return TransactionDTO.builder() | ||
.transactionId(transaction.getTransactionId()) | ||
.payeeName(transaction.getPayeeName()) | ||
.payeeTaxCode(transaction.getPayeeTaxCode()) | ||
.amount(Utilities.euroToCents(transaction.getAmount())) | ||
.transactionDate(Utilities.dateStringToZonedDateTime(transaction.getTransactionDate())) | ||
.isCart(transaction.getIsCart()) | ||
.payedByMe(transaction.getIsPayer()) | ||
.registeredToMe(transaction.getIsDebtor()) | ||
.build(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...n/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionsListDTO2TransactionsListDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package it.gov.pagopa.arc.dto.mapper; | ||
|
||
import it.gov.pagopa.arc.model.generated.TransactionDTO; | ||
import it.gov.pagopa.arc.model.generated.TransactionsListDTO; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class BizEventsTransactionsListDTO2TransactionsListDTO { | ||
public TransactionsListDTO apply(List<TransactionDTO> transactionsList, Integer size){ | ||
return TransactionsListDTO.builder() | ||
.transactions(transactionsList) | ||
.currentPage(1) | ||
.itemsForPage(size) | ||
.totalItems(10) | ||
.totalPages(1) | ||
.build(); | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
src/main/java/it/gov/pagopa/arc/service/TransactionsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
package it.gov.pagopa.arc.service; | ||
|
||
|
||
import it.gov.pagopa.arc.dto.TransactionDTO; | ||
|
||
import java.util.List; | ||
import it.gov.pagopa.arc.model.generated.TransactionsListDTO; | ||
|
||
public interface TransactionsService { | ||
List<TransactionDTO> retrieveTransactionsList(String fiscalCode); | ||
TransactionsListDTO retrieveTransactionsList(Integer page, Integer size, String filter); | ||
} |
65 changes: 35 additions & 30 deletions
65
src/main/java/it/gov/pagopa/arc/service/TransactionsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,52 @@ | ||
package it.gov.pagopa.arc.service; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import it.gov.pagopa.arc.dto.TransactionDTO; | ||
import it.gov.pagopa.arc.dto.UserDTO; | ||
import it.gov.pagopa.arc.connector.bizevents.BizEventsConnector; | ||
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionsListDTO; | ||
import it.gov.pagopa.arc.dto.mapper.BizEventsTransactionDTO2TransactionDTO; | ||
import it.gov.pagopa.arc.dto.mapper.BizEventsTransactionsListDTO2TransactionsListDTO; | ||
import it.gov.pagopa.arc.model.generated.TransactionDTO; | ||
import it.gov.pagopa.arc.model.generated.TransactionsListDTO; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@Slf4j | ||
public class TransactionsServiceImpl implements TransactionsService { | ||
private final ObjectMapper objectMapper; | ||
|
||
public TransactionsServiceImpl(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
private final String fakeFiscalCode; | ||
private final BizEventsConnector bizEventsConnector; | ||
private final BizEventsTransactionDTO2TransactionDTO transactionDTOMapper; | ||
private final BizEventsTransactionsListDTO2TransactionsListDTO transactionsListDTOMapper; | ||
|
||
|
||
public TransactionsServiceImpl( | ||
@Value("${rest-client.biz-events.fake-fiscal-code}") String fakeFiscalCode, | ||
BizEventsConnector bizEventsConnector, | ||
BizEventsTransactionDTO2TransactionDTO transactionDTOMapper, | ||
BizEventsTransactionsListDTO2TransactionsListDTO transactionsListDTOMapper) { | ||
this.fakeFiscalCode = fakeFiscalCode; | ||
this.bizEventsConnector = bizEventsConnector; | ||
this.transactionDTOMapper = transactionDTOMapper; | ||
this.transactionsListDTOMapper = transactionsListDTOMapper; | ||
} | ||
|
||
@Override | ||
public List<TransactionDTO> retrieveTransactionsList(String fiscalCode) { | ||
List<TransactionDTO> transactionDTOList = new ArrayList<>(); | ||
|
||
if (StringUtils.isNotBlank(fiscalCode)) { | ||
try { | ||
InputStream mockedTransactionsListJson = TransactionDTO.class.getResourceAsStream("/mock/transactions_response.json"); | ||
List<UserDTO> userDTOList = objectMapper.readValue(mockedTransactionsListJson, new TypeReference<>() { | ||
}); | ||
transactionDTOList = userDTOList.stream() | ||
.filter(u -> u.getTaxCode().equals(fiscalCode)) | ||
.map(UserDTO::getTransactions) | ||
.flatMap(List::stream) | ||
.toList(); | ||
|
||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
} | ||
|
||
public TransactionsListDTO retrieveTransactionsList(Integer page, Integer size, String filter) { | ||
List<TransactionDTO> transactions; | ||
BizEventsTransactionsListDTO bizEventsTransactionsList = bizEventsConnector.getTransactionsList(fakeFiscalCode, size); | ||
if(!bizEventsTransactionsList.getTransactions().isEmpty()) { | ||
transactions = bizEventsTransactionsList | ||
.getTransactions() | ||
.stream() | ||
.map(transactionDTOMapper::apply) | ||
.toList(); | ||
} else { | ||
transactions = new ArrayList<>(); | ||
} | ||
return transactionDTOList; | ||
return transactionsListDTOMapper.apply(transactions, size); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package it.gov.pagopa.arc.utils; | ||
|
||
|
||
import io.micrometer.common.util.StringUtils; | ||
|
||
import java.text.NumberFormat; | ||
import java.text.ParseException; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Locale; | ||
|
||
public class Utilities { | ||
private Utilities(){} | ||
|
||
/** To convert euro into cents */ | ||
public static Long euroToCents(String euroString){ | ||
if(StringUtils.isNotBlank(euroString)){ | ||
Double euroDouble = euroStringToLong(euroString); | ||
if( euroDouble != null){ | ||
return (long) (euroDouble * 100); | ||
}else { | ||
return null; | ||
} | ||
} else{ | ||
return null; | ||
} | ||
} | ||
|
||
/** To convert euro from String to double */ | ||
private static Double euroStringToLong(String euroString){ | ||
try { | ||
NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN); | ||
Number parse = nf.parse(euroString); | ||
return parse.doubleValue(); | ||
}catch (ParseException e){ | ||
return null; | ||
} | ||
} | ||
|
||
/** To convert Date from String to ZonedDateTime*/ | ||
public static ZonedDateTime dateStringToZonedDateTime(String dateString){ | ||
if(StringUtils.isNotBlank(dateString)){ | ||
try { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"); | ||
return ZonedDateTime.parse(dateString, formatter); | ||
}catch (DateTimeParseException e){ | ||
return null; | ||
} | ||
}else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters