Skip to content

Commit

Permalink
[PRDP-349] feat: Rework TransactionList API (#38)
Browse files Browse the repository at this point in the history
* [PRDP-358] Added entities' models and moved classes

* [PRDP-358] Added collections repo interfaces and container name envs

* [PRDP-356] updated openapi definition as defined in documentation

* [PRDP-358] Added get biz-events-view-cart filtered by fiscal code

* [PRDP-358] Reworked transaction service getTransactionDetails and added errors

* [PRDP-358] Updated partially tests

* [PRDP-358] Completed unit test for transaction details

* [PRDP-358] Changed entity annotation

* [PRDP-358] Added id autogeneration to view entities

* [PRDP-349] Changed view user query to sliced

* [PRDP-358] Changed fiscalCode to taxCode

* [PRDP-349] Reworked Transactionlist API

* [PRDP-349] Updated unit test

* [PRDP-349] Cleaned unused import

* [PRDP-349] Handled continuation token in response

* [PRDP-349] Updated unit tests

* [PRDP-349] Changed query method for BizEventsViewGeneral and entity

* [PRDP-349] Fixed AppError in case no view user

* [PRDP-349] cleaned

* [PRDP-349] Merge fix

---------

Co-authored-by: giomella <gioele.mella@emeal.nttdata.com>
  • Loading branch information
svariant and giomella authored Feb 1, 2024
1 parent 3d4ede6 commit a330f05
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.constraints.NotBlank;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Tag(name = "IO Transactions REST APIs")
@RequestMapping("/transactions")
@Validated
public interface ITransactionController {

String X_CONTINUATION_TOKEN = "x-continuation-token";
String X_FISCAL_CODE = "x-fiscal-code";
String PAGE_SIZE = "size";

/**
* recovers biz-event data for the transaction list
*
Expand All @@ -39,7 +45,7 @@ public interface ITransactionController {
@GetMapping
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Obtained transaction list.",
headers = @Header(name = "x-continuation-token", description = "continuation token for paginated query", schema = @Schema(implementation = String.class)),
headers = @Header(name = X_CONTINUATION_TOKEN, description = "continuation token for paginated query", schema = @Schema(implementation = String.class)),
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(name = "TransactionListItem", implementation = List.class))),
@ApiResponse(responseCode = "401", description = "Wrong or missing function key.", content = @Content(schema = @Schema())),
@ApiResponse(responseCode = "404", description = "Not found the transaction.", content = @Content(schema = @Schema(implementation = ProblemJson.class))),
Expand All @@ -49,9 +55,10 @@ public interface ITransactionController {
@Operation(summary = "Retrieve the paged transaction list from biz events.", security = {
@SecurityRequirement(name = "ApiKey")}, operationId = "getTransactionList")
ResponseEntity<List<TransactionListItem>> getTransactionList(
@RequestHeader(name = "x-fiscal-code") String fiscalCode,
@RequestHeader(name = "x-continuation-token", required = false) String continuationToken,
@RequestHeader(name = "x-page-size", required = false, defaultValue = "5") Integer size
@RequestHeader(name = X_FISCAL_CODE) String fiscalCode,
@RequestHeader(name = X_CONTINUATION_TOKEN, required = false) String continuationToken,
@RequestParam(name = PAGE_SIZE, required = false, defaultValue = "5") Integer size

);

@Operation(summary = "Retrieve the transaction details given its id.", security = {
Expand All @@ -66,6 +73,6 @@ ResponseEntity<List<TransactionListItem>> getTransactionList(
@ApiResponse(responseCode = "500", description = "Service unavailable.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class)))})
@GetMapping(value = "/{transaction-id}", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<TransactionDetailResponse> getTransactionDetails(
@RequestHeader("x-fiscal-code") @NotBlank String fiscalCode,
@RequestHeader(X_FISCAL_CODE) @NotBlank String fiscalCode,
@Parameter(description = "The id of the transaction.", required = true) @NotBlank @PathVariable("transaction-id") String transactionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.gov.pagopa.bizeventsservice.controller.ITransactionController;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListItem;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionDetailResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListResponse;
import it.gov.pagopa.bizeventsservice.service.ITransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -28,9 +29,11 @@ public TransactionController(ITransactionService transactionService) {
@Override
public ResponseEntity<List<TransactionListItem>> getTransactionList(
String fiscalCode, String continuationToken, Integer size) {
return new ResponseEntity<>(
transactionService.getTransactionList(fiscalCode, continuationToken, size),
HttpStatus.OK);
TransactionListResponse transactionListResponse = transactionService.getTransactionList(fiscalCode, continuationToken, size);

return ResponseEntity.ok()
.header(X_CONTINUATION_TOKEN, transactionListResponse.getContinuationToken())
.body(transactionListResponse.getTransactionList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package it.gov.pagopa.bizeventsservice.entity.view;

import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.GeneratedValue;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
import it.gov.pagopa.bizeventsservice.entity.view.enumeration.OriginType;
import it.gov.pagopa.bizeventsservice.entity.view.enumeration.PaymentMethodType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;

/**
* Entity model for biz-events-view-general
Expand All @@ -19,8 +19,7 @@
@NoArgsConstructor
@Getter
public class BizEventsViewGeneral {
@GeneratedValue
private String id;
@Id
@PartitionKey
private String transactionId;
private String authCode;
Expand All @@ -33,5 +32,6 @@ public class BizEventsViewGeneral {
private boolean isCart;
private String fee;
private OriginType origin;
private int totalNotice;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class BizEventsViewUser {
@GeneratedValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public enum AppError {

INVALID_FISCAL_CODE(HttpStatus.BAD_REQUEST, INVALID_DATA, "Provided Fiscal Code %s is invalid"),

VIEW_USER_NOT_FOUND_WITH_TAX_CODE(HttpStatus.NOT_FOUND, VIEW_USER_NOT_FOUND, "Not found a biz-events-view-user with the given tax code"),
VIEW_GENERAL_NOT_FOUND_WITH_TRANSACTION_ID(HttpStatus.NOT_FOUND, VIEW_GENERAL_NOT_FOUND, "Not found a biz-events-view-general with id %s"),
VIEW_CART_NOT_FOUND_WITH_TRANSACTION_ID_FOR_USER(HttpStatus.NOT_FOUND, VIEW_CART_NOT_FOUND, "Not found a biz-events-view-cart with id %s for the given fiscal code"),
VIEW_CART_NOT_FOUND_WITH_TRANSACTION_ID_AND_TAX_CODE(HttpStatus.NOT_FOUND, VIEW_CART_NOT_FOUND, "Not found a biz-events-view-cart with id %s for the given tax code"),

ERROR_MAPPING_BIZ_EVENT_TO_TRANSACTION_DETAIL(HttpStatus.INTERNAL_SERVER_ERROR, INVALID_DATA, "Error mapping bizEvent data to transaction details, missing property %s for bizEvent with id %s"),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", "Something was wrong");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewCart;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewGeneral;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewUser;
import it.gov.pagopa.bizeventsservice.model.response.transaction.CartItem;
import it.gov.pagopa.bizeventsservice.model.response.transaction.InfoTransaction;

import it.gov.pagopa.bizeventsservice.model.response.transaction.*;
import org.springframework.beans.factory.annotation.Value;

import java.math.BigDecimal;
import java.text.NumberFormat;
Expand All @@ -15,6 +17,9 @@
public class ConvertViewsToTransactionDetailResponse {
private ConvertViewsToTransactionDetailResponse(){}

@Value("transaction.payee.cartName")
private static String payeeCartName;

public static TransactionDetailResponse convertTransactionDetails(BizEventsViewGeneral bizEventsViewGeneral, List<BizEventsViewCart> listOfCartViews) {
List<CartItem> listOfCartItems = new ArrayList<>();
AtomicReference<BigDecimal> totalAmount = new AtomicReference<>(BigDecimal.ZERO);
Expand Down Expand Up @@ -55,6 +60,22 @@ public static TransactionDetailResponse convertTransactionDetails(BizEventsViewG
.build();
}

public static TransactionListItem convertTransactionListItem(BizEventsViewUser viewUser, List<BizEventsViewCart> listOfCartViews){
AtomicReference<BigDecimal> totalAmount = new AtomicReference<>(BigDecimal.ZERO);
for (BizEventsViewCart bizEventsViewCart : listOfCartViews) {
BigDecimal amountExtracted = new BigDecimal(bizEventsViewCart.getAmount());
totalAmount.updateAndGet(v -> v.add(amountExtracted));
}
return TransactionListItem.builder()
.transactionId(viewUser.getTransactionId())
.payeeName(listOfCartViews.size() > 1 ? payeeCartName : listOfCartViews.get(0).getPayee().getName())
.payeeTaxCode(listOfCartViews.size() > 1 ? "" : listOfCartViews.get(0).getPayee().getTaxCode())
.amount(currencyFormat(totalAmount.get().toString()))
.transactionDate(viewUser.getTransactionDate())
.isCart(listOfCartViews.size() > 1)
.build();
}

private static String currencyFormat(String value) {
BigDecimal valueToFormat = new BigDecimal(value);
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ITALY);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.gov.pagopa.bizeventsservice.model.response.transaction;

import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Builder
@Getter
public class TransactionListResponse {
private List<TransactionListItem> transactionList;
private String continuationToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface BizEventsViewCartRepository extends CosmosRepository<BizEventsV
@Query("select * from c where " +
"c.transactionId = @transactionId and " +
"(c.payer.taxCode = @taxCode or c.debtor.taxCode = @taxCode)")
List<BizEventsViewCart> getBizEventsViewCartByTransactionIdAndFilteredByFiscalCode(
List<BizEventsViewCart> getBizEventsViewCartByTransactionIdAndFilteredByTaxCode(
@Param("transactionId") String transactionId,
@Param("taxCode") String taxCode);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package it.gov.pagopa.bizeventsservice.repository;

import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewGeneral;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Repository interface for biz-events-view-general collection
*/
@Repository
public interface BizEventsViewGeneralRepository extends CosmosRepository<BizEventsViewGeneral, String> {
@Query("select * from c where c.transactionId = @transactionId")
List<BizEventsViewGeneral> getBizEventsViewGeneralByTransactionId(@Param("transactionId") String transactionId);
}
public interface BizEventsViewGeneralRepository extends CosmosRepository<BizEventsViewGeneral, String> { }
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Repository interface for biz-events-view-user collection
*/
@Repository
public interface BizEventsViewUserRepository extends CosmosRepository<BizEventsViewUser, String> {
@Query("select * from c where c.taxCode = @taxCode")
List<BizEventsViewUser> getBizEventsViewUserByFiscalCode(@Param("taxCode") String taxCode);
Page<BizEventsViewUser> getBizEventsViewUserByTaxCode(@Param("taxCode") String taxCode, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.gov.pagopa.bizeventsservice.service;

import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListItem;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionDetailResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListResponse;


import java.util.List;
Expand All @@ -15,7 +15,7 @@ public interface ITransactionService {
* @param size offset size
* @return transaction list
*/
List<TransactionListItem> getTransactionList(String fiscalCode, String continuationToken, Integer size);
TransactionListResponse getTransactionList(String fiscalCode, String continuationToken, Integer size);
TransactionDetailResponse getTransactionDetails(String fiscalCode, String transactionId);

}
Loading

0 comments on commit a330f05

Please sign in to comment.