Skip to content

Commit a218f09

Browse files
committed
exchange default case of fromCurrency === toCurrency
1 parent 681eded commit a218f09

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

routes/currency-exchange-routes.js

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ router.post('/', (req, res) => {
2626
promiseData = getExchangeRateData(Fc, Tc, amnt);
2727

2828
promiseData.then ( (data) => {
29-
// console.log(data);
29+
console.log(data);
3030
if (data != null && data.result === 'error') {
3131
console.log('Sending code 403: ' + data.result);
3232

@@ -45,35 +45,50 @@ function getExchangeRateData(fromCurrency, toCurrency, amount) {
4545
setTimeout(() => {
4646
let URLStr = EXCHANGE_RATE_URI + EXCHANGE_RATE_APIKEY + '/pair/' + fromCurrency + '/' + toCurrency + '/' + amount;
4747
console.log(URLStr);
48-
try {
49-
if (fromCurrency === undefined) {
50-
URLStr = EXCHANGE_RATE_URI + EXCHANGE_RATE_APIKEY + '/pair/EUR/GBP/1';
51-
}
52-
// console.log("Calling URL: " + URLStr);
53-
request(URLStr, async function (err, response, body) {
54-
console.log(response.statusCode);
55-
if (response.statusCode == 429) {
56-
console.log("WARNING: You have exceeded your API call limit of 1500 calls per month!");
57-
resolve(null);
58-
}
59-
if (response.statusCode == 403) {
60-
console.log("WARNING: Free plan limit is used up. Data will be available again on the 7th of next month.");
61-
let result = await JSON.parse(body);
62-
resolve(result);
63-
}
64-
if (response.statusCode == 200) {
65-
let result = await JSON.parse(body);
66-
resolve(result);
67-
} else {
68-
// Ignoring grainular status codes for now.
69-
resolve(null);
70-
}
71-
})
72-
} catch (err) {
73-
console.log(err);
48+
if (fromCurrency === toCurrency) {
49+
// no need to run request.
50+
let xdate = new Date();
51+
let retData = {
52+
result: 'success',
53+
time_last_update_utc: xdate,
54+
conversion_rate: amount,
55+
conversion_result: amount
56+
}
57+
58+
console.log("No need to run request.")
59+
resolve(retData);
60+
} else {
61+
try {
62+
if (fromCurrency === undefined) {
63+
URLStr = EXCHANGE_RATE_URI + EXCHANGE_RATE_APIKEY + '/pair/EUR/GBP/1';
7464
}
75-
76-
}, 200);
77-
})};
65+
66+
// console.log("Calling URL: " + URLStr);
67+
request(URLStr, async function (err, response, body) {
68+
console.log(response.statusCode);
69+
if (response.statusCode == 429) {
70+
console.log("WARNING: You have exceeded your API call limit of 1500 calls per month!");
71+
resolve(null);
72+
}
73+
if (response.statusCode == 403) {
74+
console.log("WARNING: Free plan limit is used up. Data will be available again on the 7th of next month.");
75+
let result = await JSON.parse(body);
76+
resolve(result);
77+
}
78+
if (response.statusCode == 200) {
79+
let result = await JSON.parse(body);
80+
console.log(result);
81+
resolve(result);
82+
} else {
83+
// Ignoring grainular status codes for now.
84+
resolve(null);
85+
}
86+
})
87+
} catch (err) {
88+
console.log(err);
89+
}
90+
}
91+
}, 200);
92+
})};
7893

7994
export { router };

views/pages/exchangeRate.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<% } else { %>
4646
4747
<p style="color:gray"><strong>Last update</strong>: <%= exchange.time_last_update_utc %></p>
48-
<p style="color:gray"><strong>Conversion rate </strong>: 1 <%= frC %> to <%= exchange.conversion_rate %> <%= toC %></p>
48+
<p style="color:gray"><strong>Conversion rate </strong>: <%= amount %> <%= frC %> to <%= exchange.conversion_rate %> <%= toC %></p>
4949
5050
<p style="color:blue"><strong> <%= amount %> <%= frC %> </strong> == <strong style="color: green"><%= exchange.conversion_result %> <%= toC %> </strong></p>
5151

0 commit comments

Comments
 (0)