Skip to content

Commit

Permalink
P4PU-146 added business logic to retrieve transactions list and added…
Browse files Browse the repository at this point in the history
… mapper and Utilities class
  • Loading branch information
Giuseppe-LaManna committed Jun 5, 2024
1 parent aeb2cba commit c5c8a6b
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 49 deletions.
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);
}
}
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();
}
}
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();
}
}
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);
}
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);
}
}

54 changes: 54 additions & 0 deletions src/main/java/it/gov/pagopa/arc/utils/Utilities.java
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
},
"x-fiscal-code": {
"matches": "DUMMY_FISCAL_CODE_ERROR"
},
"x-continuation-token": {
"matches": "TOKEN"
}
},
"queryParameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
},
"x-fiscal-code": {
"matches": "DUMMY_FISCAL_CODE_404"
},
"x-continuation-token": {
"matches": "TOKEN"
}
},
"queryParameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
},
"x-fiscal-code": {
"matches": "DUMMY_FISCAL_CODE"
},
"x-continuation-token": {
"matches": "TOKEN"
}
},
"queryParameters": {
Expand Down

0 comments on commit c5c8a6b

Please sign in to comment.