feature/improved the getting of rates
This commit is contained in:
1 parent
93a5c45dd9
commit
17f11c85df
9 files changed
+44
-58
No files matched your search
@@ -1,6 +0,0 @@
|
||||
async function convert(from) {
|
||||
const res = await fetch(`https://api.frankfurter.dev/v1/latest?base=${from}`);
|
||||
const data = await res.json();
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -1,43 +1,20 @@
|
||||
async function diffCountries() {
|
||||
const res = await fetch("/static/json/countries.json");
|
||||
const data = await res.json();
|
||||
|
||||
return data.countries;
|
||||
}
|
||||
|
||||
function getLocation() {
|
||||
if (navigator.geolocation) {
|
||||
navigator.geolocation.getCurrentPosition(success, error);
|
||||
navigator.geolocation.getCurrentPosition(getLatestExchange, error);
|
||||
}
|
||||
}
|
||||
|
||||
async function success(position) {
|
||||
const { latitude, longitude } = position.coords;
|
||||
let countryCurrency;
|
||||
|
||||
const countries = await diffCountries();
|
||||
|
||||
const res = await fetch("api/location" , {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ latitude, longitude })
|
||||
});
|
||||
|
||||
async function getLatestExchange(position) {
|
||||
let lat = position.coords.latitude;
|
||||
let lon = position.coords.longitude;
|
||||
const res = await fetch(`/exRates/${lat}/${lon}`);
|
||||
const data = await res.json();
|
||||
|
||||
countries.forEach(country => {
|
||||
if (country.country === data.results[0].formatted_address) {
|
||||
countryCurrency = country.currency_abbreviation;
|
||||
|
||||
convert(countryCurrency);
|
||||
}
|
||||
});
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
function error(err) {
|
||||
console.error("Geolocation error: ", err);
|
||||
}
|
||||
|
||||
//getLocation();
|
||||
getLocation();
|
||||
@@ -1,4 +1,5 @@
|
||||
const status = require("../util/statuses");
|
||||
const getRates = require("../util/exchangeRate");
|
||||
const bcrypt = require('bcrypt');
|
||||
const joi = require("joi");
|
||||
const salt = 12;
|
||||
@@ -222,5 +223,17 @@ module.exports = (middleware, users, plans) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/exRates/:lat/:lon", async (req, res) => {
|
||||
if (!req.session.rates || Object.keys(req.session.rates).length == 0) {
|
||||
const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${req.params.lat},${req.params.lon}&result_type=country&key=${process.env.geolocation_api}`);
|
||||
const data = await response.json();
|
||||
|
||||
let country = data.results[0].formatted_address;
|
||||
req.session.rates = await getRates(country);
|
||||
}
|
||||
|
||||
return res.status(status.Ok).send({ rates: req.session.rates });
|
||||
})
|
||||
|
||||
return router;
|
||||
};
|
||||
File renamed without changes.
@@ -1,6 +0,0 @@
|
||||
const { getCollection } = require('./src/database/connection');
|
||||
|
||||
async function saveExchangeRates() {
|
||||
const { currencies } = getCollection();
|
||||
await currencies.insertMany(convert());
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
const countries = require("./countries.json").countries;
|
||||
|
||||
async function getRates(country) {
|
||||
let abbr;
|
||||
countries.forEach(c => {
|
||||
if (c.country === country) {
|
||||
abbr = c.currency_abbreviation;
|
||||
}
|
||||
});
|
||||
|
||||
const res = await fetch(`https://api.frankfurter.dev/v1/latest?base=${abbr}`);
|
||||
const data = await res.json();
|
||||
let rates = data.rates;
|
||||
|
||||
return rates;
|
||||
}
|
||||
|
||||
module.exports = getRates;
|
||||
@@ -11,5 +11,7 @@
|
||||
<% } %>
|
||||
</main>
|
||||
|
||||
<script src="/static/scripts/geolocation.js"></script>
|
||||
|
||||
<%- include("./partials/navBar") %>
|
||||
<%- include("./partials/footer") %>
|
||||
@@ -5,7 +5,5 @@
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
<script src="/static/scripts/exchangeRate.js"></script>
|
||||
<script src="/static/scripts/geolocation.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user