diff --git a/helpers/getCompanyNameAndLogo.js b/helpers/getCompanyNameAndLogo.js new file mode 100644 index 0000000..cd01c71 --- /dev/null +++ b/helpers/getCompanyNameAndLogo.js @@ -0,0 +1,21 @@ +// Get Stock Company symbol Function +const axios = require("axios"); + +module.exports = async function getCompanyNameAndLogo(symbol) { + + let data = await axios + .get( + `https://www.alphavantage.co/query?function=OVERVIEW&symbol=${symbol}&apikey=${process.env.ALPHA_VANTAGE_KEY}` + ) + .then(async (resp) => { + let companyName = resp.data.Name; + console.log(companyName); + let logoSrc = await axios.get(`https://autocomplete.clearbit.com/v1/companies/suggest?query=:${companyName.split(' ')[0]}`).then((resp) => resp.data[0].logo).catch(err => console.log(err)); + return { companyName, logoSrc }; + }) + .catch((err) => { + console.log(err); + }); + + return data; +}; diff --git a/routes/api/cart.js b/routes/api/cart.js index 5f5f3ee..3324d45 100644 --- a/routes/api/cart.js +++ b/routes/api/cart.js @@ -5,6 +5,7 @@ const { ensureAuth } = require("../../middleware/auth"); const User = require("../../models/User"); const getPrice = require("../../helpers/getPrice"); +const getCompanyNameAndLogo = require("../../helpers/getCompanyNameAndLogo"); // TODO // Stocks Cart Buy/Sell @@ -16,9 +17,9 @@ const getPrice = require("../../helpers/getPrice"); // @route GET /cart/:symbol router.get("/:symbol", ensureAuth, async (req, res) => { const symbol = req.params.symbol; - const price = await getPrice(symbol); - res.status(200).render("cart", { layout: "layouts/app", symbol, price, href: '/market', avatar: req.user.image }); - + const { latestPrice } = await getPrice(symbol); + const { companyName, logoSrc } = await getCompanyNameAndLogo(symbol); + res.status(200).render("cart", { layout: "layouts/app", symbol, latestPrice, logoSrc, companyName, href: '/market', avatar: req.user.image }); }); // @desc To buy @@ -26,14 +27,11 @@ router.get("/:symbol", ensureAuth, async (req, res) => { router.post("/buy", ensureAuth, async (req, res) => { let data = req.body; let user = req.user; - let stockPrice = req.body.stockPrice; - let noOfStock = req.body.noOfStock; - let totalAmount = stockPrice * noOfStock; + let stockPrice = data.stockPrice; + let noOfStock = data.noOfStock; + let totalAmount = parseFloat(stockPrice * noOfStock).toFixed(4); try { - console.log(req.user); - console.log(req.body); - // req.body.user = req.user.id if (totalAmount > req.user.balance) { let ExtraBalance = totalAmount - req.user.balance; res.render("transaction", { diff --git a/views/cart.ejs b/views/cart.ejs index 3be7e79..87780ae 100644 --- a/views/cart.ejs +++ b/views/cart.ejs @@ -1,106 +1,73 @@ -
Product | -- Qtd - Quantity - | -Unit price | -Total price | -
---|---|---|---|
-
- Earphone - - - |
-
-
-
-
-
+
+
+ Buy Stock+ |
- - - 10.00€ - - | -- - 20.00€ - - | -
- Tesla Model 3 - - |
-
-
-
-
-
-
- |
- - - 49,600.01€ - - | -- - 148,800.03€ - - | -
- Bic 4 colour pen - - |
-
-
-
-
-
-
-
- |
- - - 1.50€ - - | -- - 7.50€ - - | -
+ + <%= latestPrice %> + $ +
+<%- totalData[i]["Company Name"] %>
-