-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
49 lines (45 loc) · 1.52 KB
/
controller.js
File metadata and controls
49 lines (45 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const request = require("request");
exports.apiGetController = (req, res, next) => {
let currency = req.query.currency.toLowerCase();
// console.log(currency);
let today = new Date();
let priorDate = new Date(new Date().setDate(today.getDate() - 30));
let todayFormatted = new Date().toISOString().slice(0, 10);
let priorDateFormatted = priorDate.toISOString().slice(0, 10);
// console.log(priorDateFormatted);
// console.log(todayFormatted);
if (currency === "usd" || currency === "eur" || currency === "gbp") {
request.get(
{
url: `https://api.coindesk.com/v1/bpi/historical/close.json?start=${priorDateFormatted}&end=${todayFormatted}¤cy=${currency}`,
json: true,
},
(error, response) => {
if (error) {
return res.send({
msg: "Error occured fetching data from coindesk api",
});
}
let obj = response.body.bpi;
let rates = Object.values(obj);
// console.log(rates);
let maxRate = Math.max(...rates);
let minRate = Math.min(...rates);
let newObj = {
date: todayFormatted,
requestedCurrency: currency.toUpperCase(),
currentBitCoinRate: obj[Object.keys(obj)[29]],
lowestBitCoinRate: minRate,
highestBitCoinRate: maxRate,
interval: "30 Days",
};
// console.log(newObj);
return res.json(newObj);
}
);
} else {
res.json({
Message: "Provide a valid currency: USD/EUR/GBP",
});
}
};