Skip to content

Commit

Permalink
Handle possible errors in exchange rates (#1374)
Browse files Browse the repository at this point in the history
* ui:added an error message when there is no response from the 3rd party

* ui: checking array length instead of object

* ui:fixed a bug where currency was negated in if statement

* ui:added a check for dataSets inside response
  • Loading branch information
egdmrsy authored Jun 13, 2023
1 parent e1ab644 commit ea3ba9c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
47 changes: 26 additions & 21 deletions frontend/src/getExchangeRates.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,36 @@ export async function getExchangeRates(baseCurrency = "EUR") {
"https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D..EUR.SP00.A?lastNObservations=1",
{ headers: {} }
);
const series = response.data.dataSets[0].series;
const exchangeRates = {};
for (let index = 0; index < Object.keys(series).length; index++) {
// TODO: what happens if you the currency is not where we expect it to be?
const currency = response.data.structure.dimensions.series[1].values[index].id;
const exchangeRate = getRate(series["0:" + index + ":0:0:0"]);
exchangeRates[currency] = exchangeRate;
}
if (response.data && response.data.dataSets && response.data.dataSets.length) {
const series = response.data.dataSets[0].series;
for (let index = 0; index < Object.keys(series).length; index++) {
const currency = response.data.structure.dimensions.series[1].values[index].id;
if (currency) {
const exchangeRate = getRate(series["0:" + index + ":0:0:0"]);
exchangeRates[currency] = exchangeRate;
}
}

exchangeRates["EUR"] = 1;
// Hardcoded exchange rates
exchangeRates["XOF"] = 655.957;
exchangeRates["KES"] = 114.882;
exchangeRates["TND"] = 3.2924;
exchangeRates["ETB"] = 47.156;

if (baseCurrency !== "EUR") {
const baseRate = exchangeRates[baseCurrency];
for (const key in exchangeRates) {
exchangeRates[key] = exchangeRates[key] / baseRate;
exchangeRates["EUR"] = 1;
// Hardcoded exchange rates
exchangeRates["XOF"] = 655.957;
exchangeRates["KES"] = 114.882;
exchangeRates["TND"] = 3.2924;
exchangeRates["ETB"] = 47.156;

if (baseCurrency !== "EUR" && exchangeRates[baseCurrency]) {
const baseRate = exchangeRates[baseCurrency];
for (const key in exchangeRates) {
exchangeRates[key] = exchangeRates[key] / baseRate;
}
exchangeRates[baseCurrency] = 1;
}
exchangeRates[baseCurrency] = 1;
}

return exchangeRates;
return exchangeRates;
} else {
throw new Error();
}
}

export default getExchangeRates;
1 change: 1 addition & 0 deletions frontend/src/pages/Analytics/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const GET_SUBPROJECT_KPIS_SUCCESS = "GET_SUBPROJECT_KPIS_SUCCESS";
export const GET_SUBPROJECT_KPIS_FAIL = "GET_SUBPROJECT_KPIS_FAIL";
export const GET_EXCHANGE_RATES = "GET_EXCHANGE_RATES";
export const GET_EXCHANGE_RATES_SUCCESS = "GET_EXCHANGE_RATES_SUCCESS";
export const GET_EXCHANGE_RATES_FAILED = "GET_EXCHANGE_RATES_FAILED";

export const STORE_EXCHANGE_RATE = "STORE_EXCHANGE_RATE";
export const STORE_DISPLAY_CURRENCY = "STORE_DISPLAY_CURRENCY";
Expand Down
34 changes: 27 additions & 7 deletions frontend/src/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { all, call, cancel, delay, put, select, takeEvery, takeLatest, takeLeadi

import {
GET_EXCHANGE_RATES,
GET_EXCHANGE_RATES_FAILED,
GET_EXCHANGE_RATES_SUCCESS,
GET_PROJECT_KPIS,
GET_PROJECT_KPIS_FAIL,
Expand Down Expand Up @@ -2978,13 +2979,32 @@ export function* getSubProjectKPIs({ projectId, subProjectId, showLoading = true
}

export function* getExchangeRatesSaga({ baseCurrency, showLoading = true }) {
yield execute(function* () {
const exchangeRates = yield getExchangeRates(baseCurrency);
yield put({
type: GET_EXCHANGE_RATES_SUCCESS,
exchangeRates
});
}, showLoading);
yield execute(
function* () {
const exchangeRates = yield getExchangeRates(baseCurrency);
yield put({
type: GET_EXCHANGE_RATES_SUCCESS,
exchangeRates
});
},
showLoading,
function* (error) {
yield put({
type: GET_EXCHANGE_RATES_FAILED,
error
});
yield put({
type: SNACKBAR_MESSAGE,
message: "Error while retrieving exchange rates!"
});
yield put({
type: SHOW_SNACKBAR,
show: true,
isError: true,
isWarning: false
});
}
);
}

function* exportDataSaga({ devModeEnvironment }) {
Expand Down

0 comments on commit ea3ba9c

Please sign in to comment.