Skip to content

Commit

Permalink
[VAS-1034] feat: Add check on create public bundle request (#105)
Browse files Browse the repository at this point in the history
* [VAS-1034] added check for generic attribute on public bundle request creation

* [VAS-1034] updated unit tests

---------

Co-authored-by: giomella <[email protected]>
  • Loading branch information
gioelemella and giomella authored May 14, 2024
1 parent 5e03c28 commit 1fe0057
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum AppError {
CI_BUNDLE_ID_NOT_FOUND(HttpStatus.NOT_FOUND, "No CI-BUNDLE relationship found", "CI-Bundle %s not found."),

BUNDLE_REQUEST_BAD_REQUEST(HttpStatus.BAD_REQUEST, "Bundle request bad request", "Bundle request with id %s not configured with %s."),
BUNDLE_REQUEST_BAD_ATTRIBUTE(HttpStatus.BAD_REQUEST, "Bundle request bad attribute", "Bundle request with id %s has invalid attributes: %s."),
BUNDLE_REQUEST_BAD_DATE(HttpStatus.BAD_REQUEST, "Bundle request bad request", "Date %s is not valid."),
BUNDLE_REQUEST_NOT_FOUND(HttpStatus.NOT_FOUND, "Bundle request not found", "Bundle request with id %s not found."),
BUNDLE_REQUEST_CONFLICT(HttpStatus.CONFLICT, "Bundle request conflict", "Bundle request with id %s. %s"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import it.pagopa.afm.marketplacebe.exception.AppException;
import it.pagopa.afm.marketplacebe.model.PageInfo;
import it.pagopa.afm.marketplacebe.model.request.BundleRequestId;
import it.pagopa.afm.marketplacebe.model.request.CiBundleAttributeModel;
import it.pagopa.afm.marketplacebe.model.request.PublicBundleRequest;
import it.pagopa.afm.marketplacebe.model.request.CiBundleSubscriptionRequest;
import it.pagopa.afm.marketplacebe.model.request.PublicBundleRequests;
Expand Down Expand Up @@ -62,11 +63,16 @@ public BundleRequestService(
this.modelMapper = modelMapper;
}

/**
* Create a public bundle's request with the provided info
*
* @param ciFiscalCode creditor institution's tax code
* @param ciBundleSubscriptionRequest object that contain the request info
* @return bundle request id
*/
public BundleRequestId createBundleRequest(String ciFiscalCode, CiBundleSubscriptionRequest ciBundleSubscriptionRequest) {
// retrieve bundle by idBundle and check if it is public

String idBundle = ciBundleSubscriptionRequest.getIdBundle();

Bundle bundle = getBundle(idBundle);

// a bundle request is acceptable if validityDateTo is after now
Expand All @@ -78,38 +84,21 @@ public BundleRequestId createBundleRequest(String ciFiscalCode, CiBundleSubscrip
throw new AppException(AppError.BUNDLE_OFFER_CONFLICT, idBundle, "Type not public");
}

// rule R15: attribute payment amount should be lower than bundle one
if (ciBundleSubscriptionRequest.getCiBundleAttributeModelList() != null) {
ciBundleSubscriptionRequest.getCiBundleAttributeModelList().parallelStream().forEach(attribute -> {
if (attribute.getMaxPaymentAmount().compareTo(bundle.getPaymentAmount()) > 0) {
throw new AppException(AppError.BUNDLE_REQUEST_BAD_REQUEST, idBundle, "Payment amount should be lower than or equal to bundle payment amount.");
}
});
List<CiBundleAttributeModel> attributeModelList = ciBundleSubscriptionRequest.getCiBundleAttributeModelList();
List<CiBundleAttribute> attributes = new ArrayList<>();
if (attributeModelList != null && !attributeModelList.isEmpty()) {
validateCIBundleAttributes(attributeModelList, bundle, idBundle);
attributes = buildCiBundleAttributes(idBundle, attributeModelList);
}

List<CiBundleAttribute> attributes = (ciBundleSubscriptionRequest.getCiBundleAttributeModelList() != null
&& !ciBundleSubscriptionRequest.getCiBundleAttributeModelList().isEmpty()) ?
ciBundleSubscriptionRequest.getCiBundleAttributeModelList()
.stream()
.map(attribute ->
CiBundleAttribute.builder()
.id(idBundle + "-" + UUID.randomUUID())
.insertedDate(LocalDateTime.now())
.maxPaymentAmount(attribute.getMaxPaymentAmount())
.transferCategory(attribute.getTransferCategory())
.transferCategoryRelation(attribute.getTransferCategoryRelation())
.build()
).toList() : new ArrayList<>();

BundleRequestEntity request = BundleRequestEntity.builder()
.idBundle(bundle.getId())
.idPsp(bundle.getIdPsp())
.ciFiscalCode(ciFiscalCode)
.ciBundleAttributes(attributes)
.build();

bundleRequestRepository.save(request);

this.bundleRequestRepository.save(request);
return BundleRequestId.builder().idBundleRequest(request.getId()).build();
}

Expand Down Expand Up @@ -283,4 +272,37 @@ private Bundle getBundle(String idBundle) {
private int calculateTotalPages(Integer limit, double totalItems) {
return (int) Math.ceil(totalItems / limit);
}

private List<CiBundleAttribute> buildCiBundleAttributes(String idBundle, List<CiBundleAttributeModel> ciBundleAttributeModelList) {
return ciBundleAttributeModelList.parallelStream()
.map(attribute -> CiBundleAttribute.builder()
.id(String.format("%s-%s", idBundle, UUID.randomUUID()))
.insertedDate(LocalDateTime.now())
.maxPaymentAmount(attribute.getMaxPaymentAmount())
.transferCategory(attribute.getTransferCategory())
.transferCategoryRelation(attribute.getTransferCategoryRelation())
.build()).toList();
}

private void validateCIBundleAttributes(List<CiBundleAttributeModel> attributeModelList, Bundle bundle, String idBundle) {
attributeModelList.parallelStream().forEach(
attribute -> {
// rule R15: attribute payment amount should be lower than bundle one
if (attribute.getMaxPaymentAmount().compareTo(bundle.getPaymentAmount()) > 0) {
throw new AppException(
AppError.BUNDLE_REQUEST_BAD_REQUEST,
idBundle,
"Payment amount should be lower than or equal to bundle payment amount."
);
}
if (attribute.getTransferCategory() == null && attributeModelList.size() > 1) {
throw new AppException(
AppError.BUNDLE_REQUEST_BAD_ATTRIBUTE,
idBundle,
"Only one attribute can be specified if the attribute has transfer category null"
);
}

});
}
}
Loading

0 comments on commit 1fe0057

Please sign in to comment.