Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#61 #70

Merged
merged 4 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified public/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion public/css/style.css

This file was deleted.

35 changes: 34 additions & 1 deletion public/css/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ body {
height: 100%;
overflow-x: hidden;
box-sizing: border-box;
}
background: linear-gradient(122.51deg, #e6ecfc 30%, #7fb4f1 100%) !important;
}

@tailwind components;

Expand Down Expand Up @@ -85,6 +86,38 @@ body {
font-size: 1.1rem;
}

.transaction-page {
margin-left: 280px;
margin-right: 280px;
margin-top: 90px;
margin-bottom: 100px;
background-color: white;
padding-left: 100px;
padding-right: 100px;
padding-top: 30px;
padding-bottom: 40px;
}

.transaction-input:focus {
outline: none;
}

.transaction-cancel {
background-color: rgb(246, 76, 76);
}

.transaction-cancel:hover {
background-color: rgb(208, 7, 7);
}

.transaction-buy {
background-color: #52af93;
}

.transaction-buy:hover {
background-color: #089e7e;
}

.symbolicon {
font-size: 72px;
background: -webkit-linear-gradient(#eee, #333);
Expand Down
Binary file modified public/images/.DS_Store
Binary file not shown.
Binary file added public/images/graph1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/graph2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/graph3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion routes/api/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ router.post("/buy", ensureAuth, async (req, res) => {
user,
totalAmount,
stockPrice,
message: "Order Review",
message: "Transaction Review",
layout: "layouts/app",
href: "/buy",
});
Expand Down
38 changes: 36 additions & 2 deletions routes/api/portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Transaction = require("../../models/Transaction");
// @access Private
router.get("/", ensureAuth, async (req, res) => {
let avatar = req.user.image;
const user = req.user;
// See if prevUser cookie exists and the new user have balance of 10000 (amount for the newly created account)
if (req.cookies.prevUser !== "" && req.user.balance === 10000) {
try {
Expand Down Expand Up @@ -83,12 +84,45 @@ router.get("/", ensureAuth, async (req, res) => {
res.cookie("prevUser", "");
}

res.status(200).render("portfolio", {
layout: "layouts/app",
console.log(user)

const transactions = await Transaction.find({
user: req.user.id
})
.populate("user")
.sort({
createdAt: -1
})
.lean();

var TransactionMessage = ''

if(Object.keys(transactions).length == 0) {
TransactionMessage = 'No Transaction'
} else {
TransactionMessage = ''
}

var StockMessage = ''
stocks = user.stock

if(Object.keys(stocks).length == 0) {
StockMessage = 'No Stock'
} else {
StockMessage = ''
}

res.render("portfolio", {
StockMessage,
TransactionMessage,
transactions,
user,
avatar,
totalData,
layout: "layouts/app",
href: "/portfolio",
});

});

module.exports = router;
101 changes: 73 additions & 28 deletions routes/api/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const express = require("express");
const router = express.Router();
const { ensureAuth } = require("../../middleware/auth");
const {
ensureAuth
} = require("../../middleware/auth");
const User = require("../../models/User");
const Transaction = require("../../models/Transaction");
const getPrice = require("../../helpers/getPrice");
Expand All @@ -15,20 +17,40 @@ const emailHelper = require("../../helpers/emailHelper");
// @access Private
router.put("/confirm", ensureAuth, async (req, res) => {
try {
const totalPrice = req.body.totalAmount;
let balance = req.user.balance - totalPrice;
console.log(req.user.displayName + "this is the name");
const stockCount = Number(req.body.noOfStock)
const totalPrice = Number(req.body.totalAmount);
let balance = Number(req.user.balance) - totalPrice;
console.log(req.user.displayName + " this is the name");
req.body.user = req.user.id;
const { email, displayName } = req.user;

const updatedUser = await User.findOneAndUpdate(
{ _id: req.user.id },
{ balance: balance, $push: { stock: req.body } },
{
new: true, // it will create a new one, if it doesn't exist
runValidators: true, // it check weather the fields are valid or not
const {
email,
displayName
} = req.user;

// console.log(stockCount)

// const updatedUser = await User.updateOne(
// { _id: req.user.id, stock: { $elemMatch: {companySymbol: req.body.companySymbol } } },
// { balance: balance,
// $inc: {"stock.$.noOfStock": stockCount},
// $inc: {"stock.$.totalAmount": totalPrice},
// }
// );
// console.log(`${updatedUser} : U P D A T E D U S E R`);

const addedNewUser = await User.findOneAndUpdate(
{ _id: req.user.id },
{ balance: balance,
$push: {
stock: req.body
}
);
},
{
new: true, // it will create a new one, if it doesn't exist
runValidators: true, // it check weather the fields are valid or not
});
console.log(addedNewUser);


const options = {
to: email, // list of receivers
Expand All @@ -40,8 +62,6 @@ router.put("/confirm", ensureAuth, async (req, res) => {
<p>Have a great Day!</p>
`, // html body
};
console.log(updatedUser);

emailHelper.sendEmail(options);

// Add data transaction
Expand All @@ -59,6 +79,7 @@ router.put("/confirm", ensureAuth, async (req, res) => {
});

console.log(updateTransactoin);

res.redirect("/done");
} catch (err) {
console.error(err);
Expand All @@ -72,17 +93,35 @@ router.put("/confirm", ensureAuth, async (req, res) => {
router.get("/", ensureAuth, async (req, res) => {
const user = req.user;
try {
const transactions = await Transaction.find({ user: req.user.id })
const transactions = await Transaction.find({
user: req.user.id
})
.populate("user")
.sort({ created: "desc" })
.sort({
createdAt: -1
})
.lean();

if(Object.keys(transactions).length == 0) {
var message = 'No Transaction'
res.render("transaction/history", {
message,
transactions,
user,
layout: "layouts/app",
href: "/transaction",
});
} else {
var message = ''
res.render("transaction/history", {
message,
transactions,
user,
layout: "layouts/app",
href: "/transaction",
});
}

res.render("transaction/history", {
transactions,
user,
layout: "layouts/app",
href: "/transaction",
});
} catch (err) {
console.error(err);
res.render("error/500");
Expand Down Expand Up @@ -118,10 +157,16 @@ router.post("/sell/:id", ensureAuth, async (req, res) => {
});

// Update the User Balance and Deleted the Sold Stock
const updatedBalance = await User.findOneAndUpdate(
{ _id: req.user.id },
{ balance: newBalance, $pull: { stock: { _id: req.params.id } } }
);
const updatedBalance = await User.findOneAndUpdate({
_id: req.user.id
}, {
balance: newBalance,
$pull: {
stock: {
_id: req.params.id
}
}
});

console.log(updatedBalance);
console.log(updateTransaction);
Expand All @@ -136,4 +181,4 @@ router.post("/sell/:id", ensureAuth, async (req, res) => {
}
});

module.exports = router;
module.exports = router;
2 changes: 1 addition & 1 deletion views/cart.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class="mx-auto max-w-6xl flex flex-col w-full px-4 md:py-12 md:px-8 text-gray-800"
>
<form action="/cart/buy" method="POST">
<h1 class="text-2xl lg:text-4xl font-bold mt-8 mb-4 flex">
<h1 class="text-xl lg:text-3xl font-bold mt-8 mb-4 flex">
Buy Stock :<input
class="bg-gray-300 max-w-sm w-24 ml-2"
type="text"
Expand Down
45 changes: 20 additions & 25 deletions views/layouts/app.ejs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/css/all.min.css"
integrity="sha256-46r060N2LrChLLb5zowXQ72/iKKNiw/lAmygmHExk/o="
crossorigin="anonymous"
/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>

<link type="text/css" rel="stylesheet" href="./css/style.css" />
<link type="text/css" rel="stylesheet" href="../css/style.css" />
<title>TradeByte</title>
</head>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/css/all.min.css"
integrity="sha256-46r060N2LrChLLb5zowXQ72/iKKNiw/lAmygmHExk/o=" crossorigin="anonymous" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>

<body>
<div class="md:flex flex-col bg-gray-300 md:flex-row min-h-screen w-full">
<%- include('../partials/_nav.ejs') %> <%- body %>
</div>
<script type="application/javascript" src="./javascript/script.js"></script>
<script
type="application/javascript"
src="../javascript/script.js"
></script>
</body>
</html>
<link type="text/css" rel="stylesheet" href="../css/style.css" />
<link rel="stylesheet" href="../css/tailwind.css">
<link rel="shortcut icon" href="../images/TradeByte-Favicon.png" type="image/x-icon">
<title>TradeByte</title>
</head>

<body>
<div class="md:flex flex-col md:flex-row min-h-screen w-full">
<%- include('../partials/_nav.ejs') %> <%- body %>
</div>
<script type="application/javascript" src="../javascript/script.js"></script>
</body>

</html>
4 changes: 1 addition & 3 deletions views/partials/_footer.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<div
class="flex flex-col justify-center align-center text-center text-gray-600 mt-4"
>
<div class="my-5 flex flex-col justify-center align-center text-center text-gray-800 mt-4">
<p>Made with ❤ by TradeByte Team</p>
<p>&copy; Copyright 2020</p>
</div>
Loading