From 98da11b940c194f5e4bd423cb57d252df6185715 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Wed, 7 Oct 2020 08:45:32 -0500 Subject: Add budget goals --- src/index.html | 35 ++++++++++++++++++++++++++++++++++- src/index.js | 18 ++++++++++++++++++ src/login.html | 15 ++++++++++++--- src/main.js | 22 ++++++++++++++++++++++ src/server.js | 41 ++++++++++++++++++++++++++++++++++++++--- 5 files changed, 124 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/index.html b/src/index.html index ef86835..08c5007 100644 --- a/src/index.html +++ b/src/index.html @@ -18,6 +18,7 @@
+
@@ -64,7 +65,6 @@ Amount Category Subcategory - @@ -148,6 +148,39 @@ + + +
+
+
+ New Goal + + + +
+
+ Allocate funds + + out of {{total_to_allocate}} + + +
+ + + + + + + + + +
NameAmountTotalRemaining
{{i+1}}{{goal.name}}{{goal.amount}}{{goal.total}}{{goal.total - goal.amount}}
+
+
diff --git a/src/index.js b/src/index.js index f783076..bf64586 100644 --- a/src/index.js +++ b/src/index.js @@ -72,6 +72,24 @@ function setUpModels() { allowNull: false, }, }), + "goals": database.define('goal', { + username: { + type: Sequelize.STRING, + allowNull: false, + }, + name: { + type: Sequelize.STRING, + allowNull: false, + }, + total: { + type: Sequelize.DECIMAL, + allowNull: false, + }, + amount: { + type: Sequelize.DECIMAL, + allowNull: false, + } + }), "users": database.define('user', { username: { type: Sequelize.STRING, diff --git a/src/login.html b/src/login.html index d1dbe1d..ec41762 100644 --- a/src/login.html +++ b/src/login.html @@ -7,9 +7,11 @@ @@ -30,7 +39,7 @@

Login

- +
diff --git a/src/main.js b/src/main.js index b5e6b86..78b918e 100644 --- a/src/main.js +++ b/src/main.js @@ -6,6 +6,8 @@ window.onload = function () { transactions: [], summary: {username : ""}, selTodoType: "all", + total_to_allocate: 0, + goals: [] }, methods: { setTab: function (value) { @@ -26,6 +28,15 @@ window.onload = function () { category: "", subcategory: "", } + this.ng = { + name: "", + total: "", + amount: 0 + } + this.na = { + selected: "", + amount: "" + } }, requestThenUpdate: function (request) { fetch(request) @@ -81,6 +92,8 @@ window.onload = function () { }, created() { this.clearData(); + fetch(new Request(`/goals`)).then(response => response.json()) + .then(response => this.goals = response); fetch(new Request(`/transaction`)).then(response => response.json()) .then(response => this.transactions = response); fetch(new Request(`/summary`)).then(response => response.json()) @@ -160,6 +173,12 @@ window.onload = function () { // Note we flip these since income is negative item.negative = el.s > 0 item.positive = el.s < 0 + + // -= since its flipped + this.total_to_allocate -= item.net + }) + this.goals.forEach(el => { + this.total_to_allocate -= el.amount }) this.summary.week.sort(function(a, b){ @@ -173,6 +192,9 @@ window.onload = function () { this.summary.year.sort(function(a, b){ return a.y-b.y; }) + + seriesX = this.summary.month.map(el => el.y) + this.summary.username = response.username }); diff --git a/src/server.js b/src/server.js index 53db084..69d3968 100644 --- a/src/server.js +++ b/src/server.js @@ -14,7 +14,7 @@ server.use(bodyParser.json()); //server.use(bodyParser.urlencoded({ extended: true })); function listen(port) { - server.listen(port, () => console.info(`Listening on port ${port}!`)); + server.listen(port, () => console.info(`Listening: http://localhost:${port} `)); } function hashWithSalt(password, salt){ @@ -36,7 +36,6 @@ function setUpRoutes(models, jwtFunctions, database) { try { const decryptedUserId = jwtFunctions.verify(cookie); var user = await models.users.findOne({ where: { username: decryptedUserId } }); - // .then((user, error) => { if (user) { res.locals.user = user.get({ plain: true }); } else { @@ -44,7 +43,6 @@ function setUpRoutes(models, jwtFunctions, database) { res.redirect('/login'); return; } - // }); } catch (e) { res.status(400).send(e.message); } @@ -128,6 +126,43 @@ function setUpRoutes(models, jwtFunctions, database) { res.status(400).send(e.message); } }) + server.get(`/goals`, async (req, res, next) => { + try { + var result = await database.query("SELECT * FROM goals WHERE username = '" + res.locals.user.username + "' ORDER BY `name` DESC", { type: database.QueryTypes.SELECT }) + res.status(200).send(result); + next(); + } catch (e) { + console.log(e) + res.status(400).send(e.message); + } + }) + server.post(`/goals`, async (req, res, next) => { + try { + let item = req.body; + console.log(item); + item.username = res.locals.user.username + await models.goals.create(item); + var result = await database.query("SELECT * FROM goals WHERE username = '" + res.locals.user.username + "' ORDER BY `name` DESC", { type: database.QueryTypes.SELECT }) + res.status(200).send(result); + } catch (e) { + console.log(e); + res.status(400).send(e.message); + } + }) + server.post(`/allocate`, async (req, res, next) => { + try { + let name = req.body.name; + let amount = req.body.amount; + var toUpdate = await models.goals.findOne({ where: { name: name, username:res.locals.user.username } }); + var update = {amount: toUpdate.amount + amount} + await toUpdate.update(update); + var result = await await database.query("SELECT * FROM goals WHERE username = '" + res.locals.user.username + "' ORDER BY `name` DESC", { type: database.QueryTypes.SELECT }) + res.status(200).send(result); + } catch (e) { + console.log(e); + res.status(400).send(e.message); + } + }) server.get(`/summary`, async (req, res, next) => { try { res.status(200).send({ -- cgit v1.2.3