From 17f11c85df33858db943abeecd4c4166d01a37e0 Mon Sep 17 00:00:00 2001 From: Joaquin Date: Tue, 6 May 2025 21:26:45 -0700 Subject: [PATCH] feature/improved the getting of rates --- app.js | 18 +++--------- src/public/scripts/exchangeRate.js | 6 ---- src/public/scripts/geolocation.js | 37 +++++------------------- src/router/user.js | 13 +++++++++ src/{public/json => util}/countries.json | 0 src/util/currencies.js | 6 ---- src/util/exchangeRate.js | 18 ++++++++++++ src/views/home.ejs | 2 ++ src/views/partials/footer.ejs | 2 -- 9 files changed, 44 insertions(+), 58 deletions(-) delete mode 100644 src/public/scripts/exchangeRate.js rename src/{public/json => util}/countries.json (100%) delete mode 100644 src/util/currencies.js create mode 100644 src/util/exchangeRate.js diff --git a/app.js b/app.js index ce48655..f8504dd 100644 --- a/app.js +++ b/app.js @@ -6,12 +6,11 @@ const path = require('path'); const joi = require('joi'); require('dotenv').config(); - const app = express(); const port = process.env.PORT || 3000; -const mongoURI = process.env.mongoURI || "mongodb://localhost:27017/"; -const database = process.env.database || "knoldus"; // Database name +const mongoURI = process.env.mongoURI; +const database = process.env.database; // Database name const secret = process.env.secret || "123-secret-xyz"; /*** Sessions ***/ @@ -34,13 +33,14 @@ app.use(express.json()); const { connectMongo, getCollection } = require("./src/database/connection"); let users; +let rates; async function initDatabase() { const db = await connectMongo(mongoURI, database); // For any collection, init here users = await getCollection(db, "users"); plans = await getCollection(db, "plans"); - currencies = await getCollection(db, "currencies"); + rates = await getCollection(db, "rates"); } /*** ROUTINGS ***/ @@ -72,16 +72,6 @@ app.get('/aboutUs', (req, res) => { return res.status(status.Ok); }); -app.post('/api/location', async (req,res) => { - const { latitude, longitude } = req.body; - - const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&result_type=country&key=${process.env.geolocation_api}`); - - const data = await response.json(); - - res.json(data); -}); - // Initialize database and start app initDatabase().then(() => { console.log("Successfully connected to MongoDB"); diff --git a/src/public/scripts/exchangeRate.js b/src/public/scripts/exchangeRate.js deleted file mode 100644 index 8d6e08f..0000000 --- a/src/public/scripts/exchangeRate.js +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/src/public/scripts/geolocation.js b/src/public/scripts/geolocation.js index e5c55b8..e4a48e2 100644 --- a/src/public/scripts/geolocation.js +++ b/src/public/scripts/geolocation.js @@ -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(); \ No newline at end of file +getLocation(); \ No newline at end of file diff --git a/src/router/user.js b/src/router/user.js index 9daa7cb..60bf114 100644 --- a/src/router/user.js +++ b/src/router/user.js @@ -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; }; diff --git a/src/public/json/countries.json b/src/util/countries.json similarity index 100% rename from src/public/json/countries.json rename to src/util/countries.json diff --git a/src/util/currencies.js b/src/util/currencies.js deleted file mode 100644 index 712720d..0000000 --- a/src/util/currencies.js +++ /dev/null @@ -1,6 +0,0 @@ -const { getCollection } = require('./src/database/connection'); - -async function saveExchangeRates() { - const { currencies } = getCollection(); - await currencies.insertMany(convert()); -} diff --git a/src/util/exchangeRate.js b/src/util/exchangeRate.js new file mode 100644 index 0000000..6313988 --- /dev/null +++ b/src/util/exchangeRate.js @@ -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; \ No newline at end of file diff --git a/src/views/home.ejs b/src/views/home.ejs index 3637163..b3b69e7 100644 --- a/src/views/home.ejs +++ b/src/views/home.ejs @@ -11,5 +11,7 @@ <% } %> + + <%- include("./partials/navBar") %> <%- include("./partials/footer") %> diff --git a/src/views/partials/footer.ejs b/src/views/partials/footer.ejs index ed0882c..ef5e262 100644 --- a/src/views/partials/footer.ejs +++ b/src/views/partials/footer.ejs @@ -5,7 +5,5 @@ - - \ No newline at end of file