Skip to content

Commit

Permalink
[MODFIN-349] - Calculate exchange API simplified (#226)
Browse files Browse the repository at this point in the history
* [MODFIN-349] - Changed parameters

* [MODFIN-349] - Changed parameters

* [MODFIN-346] - Made simplified by joining two similar api

* [MODFIN-349] - Fixed tests

* [MODFIN-349] - Used Vertx.executingBlock()

* [MODFIN-349] - Used Vertx.executingBlock()

* [MODFIN-349] - Used Vertx.executingBlock()
  • Loading branch information
azizbekxm authored Jan 29, 2024
1 parent 25260f2 commit 98aa079
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 237 deletions.
66 changes: 0 additions & 66 deletions ramls/exchange-rate.raml

This file was deleted.

81 changes: 66 additions & 15 deletions ramls/calculate-exchange.raml → ramls/exchange.raml
Original file line number Diff line number Diff line change
@@ -1,59 +1,110 @@
#%RAML 1.0

title: Calculate exchange
title: Exchange
version: v1
protocols: [ HTTP, HTTPS ]
baseUri: https://github.com/folio-org/mod-finance

documentation:
- title: Calculate exchange API
content: This documents the API calls that can be made to exchange calculation
- title: Exchange API
content: This documents the API calls that can be made to get exchange operations

types:
exchangeRate: !include acq-models/mod-finance/schemas/exchange_rate.json
errors: !include raml-util/schemas/error.schema
amount:
type: number
description: Amount expressed as a number of major currency units that needs to be calculate
example: 99.95
currency_code:
description: currency_code expressed as a code of currency
type: string
example: USD
amount:
type: number
description: Amount expressed as a number of major currency units that needs to be calculate
example: 99.95
exchange_calculation:
type: number
description: currency_code expressed as a total of exchange calculation
description: Exchange calculation expressed as a total calculation of exchange
example: 99.95

/finance/calculate_exchange:
/finance/calculate-exchange:
displayName: Calculate exchange
description: Calculate exchange API
get:
description: "Get exchange calculation"
queryParameters:
source_currency:
from:
description: "Source currency code"
type: currency_code
required: true
example: USD
target_currency:
to:
description: "Target currency code"
type: currency_code
required: true
example: EUR
amount:
description: "The amount of money to calculate exchange"
type: amount
type: exchange_calculation
required: true
example: 100.0
responses:
200:
description: "Exchange calculation successfully retrieved"
body:
application/json:
type: amount
example:
strict: false
value: 200.0
400:
description: "Bad request, e.g. malformed request body or query parameter. Details of the error (e.g. name of the parameter or line/character number with malformed data) provided in the response."
body:
application/json:
type: errors
example:
strict: false
value: !include raml-util/examples/error.sample
404:
description: "Exchange rate is not available"
body:
application/json:
type: errors
example:
strict: false
value: !include raml-util/examples/error.sample
500:
description: "Internal server error, e.g. due to misconfiguration"
body:
application/json:
type: errors
example:
strict: false
value: !include raml-util/examples/error.sample

/finance/exchange-rate:
displayName: Exchange rate
description: Exchange rate API
get:
description: "Get exchange rate"
queryParameters:
from:
description: "From currency code"
type: currency_code
required: true
example: USD
to:
description: "To currency code"
type: currency_code
required: true
example: EUR
responses:
200:
description: "Exchange calculation successfully retrieved"
description: "Exchange rate successfully retrieved"
body:
application/json:
type: exchange_calculation
type: exchangeRate
example:
strict: false
value: 200.0
value: sample
400:
description: "Bad request, e.g. malformed request body or query parameter. Details of the error (e.g. name of the parameter or line/character number with malformed data) provided in the response."
body:
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/folio/rest/helper/ExchangeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import javax.money.convert.MonetaryConversions;

import io.vertx.core.Context;
import io.vertx.core.Future;
import org.folio.rest.exception.HttpException;
import org.folio.rest.jaxrs.model.ExchangeRate;
import org.javamoney.moneta.Money;
Expand Down Expand Up @@ -36,15 +35,12 @@ public ExchangeRate getExchangeRate(String from, String to) {
}
}

public Future<Double> calculateExchange(String sourceCurrency, String targetCurrency, Number amount) {
return Future.succeededFuture()
.map(v -> {
var initialAmount = Money.of(amount, sourceCurrency);
var rate = getExchangeRate(sourceCurrency, targetCurrency).getExchangeRate();
public Double calculateExchange(String sourceCurrency, String targetCurrency, Number amount) {
var initialAmount = Money.of(amount, sourceCurrency);
var rate = getExchangeRate(sourceCurrency, targetCurrency).getExchangeRate();

return initialAmount.multiply(rate)
.with(Monetary.getDefaultRounding())
.getNumber().doubleValue();
});
return initialAmount.multiply(rate)
.with(Monetary.getDefaultRounding())
.getNumber().doubleValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import org.folio.rest.annotations.Validate;
import org.folio.rest.helper.ExchangeHelper;
import org.folio.rest.jaxrs.resource.FinanceCalculateExchange;
import org.folio.rest.jaxrs.resource.FinanceExchangeRate;

public class CalculateExchangeApi implements FinanceCalculateExchange {
public class ExchangeApi implements FinanceExchangeRate, FinanceCalculateExchange {

@Override
public void getFinanceCalculateExchange(String sourceCurrency, String targetCurrency, Number amount,
@Validate
public void getFinanceExchangeRate(String from, String to, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
ExchangeHelper helper = new ExchangeHelper(vertxContext);
vertxContext.executeBlocking(() -> helper.getExchangeRate(from, to))
.onSuccess(body -> asyncResultHandler.handle(succeededFuture(Response.ok(body, APPLICATION_JSON).build())))
.onFailure(t -> handleErrorResponse(asyncResultHandler, helper, t));
}

@Override
public void getFinanceCalculateExchange(String from, String to, Number amount,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
ExchangeHelper helper = new ExchangeHelper(vertxContext);
helper.calculateExchange(sourceCurrency, targetCurrency, amount)
vertxContext.executeBlocking(() -> helper.calculateExchange(from, to, amount))
.onSuccess(body -> asyncResultHandler.handle(succeededFuture(Response.ok(body, APPLICATION_JSON).build())))
.onFailure(e -> handleErrorResponse(asyncResultHandler, helper, e));
}
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/org/folio/rest/impl/ExchangeRateApi.java

This file was deleted.

9 changes: 2 additions & 7 deletions src/test/java/org/folio/ApiTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import java.util.concurrent.TimeoutException;

import org.folio.rest.impl.BudgetsApiTest;
import org.folio.rest.impl.CalculateExchangeApiTest;
import org.folio.rest.impl.EncumbrancesTest;
import org.folio.rest.impl.EntitiesCrudBasicsTest;
import org.folio.rest.impl.ExchangeRateTest;
import org.folio.rest.impl.ExchangeTest;
import org.folio.rest.impl.FiscalYearTest;
import org.folio.rest.impl.FundCodeExpenseClassesApiTest;
import org.folio.rest.impl.FundsApiTest;
Expand Down Expand Up @@ -118,11 +117,7 @@ class LedgerRolloversProgressApiTestNested extends LedgerRolloverProgressApiTest
}

@Nested
class CalculateExchangeApiTestNested extends CalculateExchangeApiTest {
}

@Nested
class ExchangeRateTestNested extends ExchangeRateTest {
class ExchangeTestNested extends ExchangeTest {
}

@Nested
Expand Down
Loading

0 comments on commit 98aa079

Please sign in to comment.