From a1baf96009692aff2ca123b3071d34fdcba0de63 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sun, 18 Oct 2020 16:09:40 -0500 Subject: Add monthly averages, fix login tempalte --- README.md | 1 + src/server.js | 69 ++++++++++++++++++++++++++-------------------- src/templates/login.html | 8 +++--- src/templates/summary.html | 22 +++++++++++++++ 4 files changed, 66 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index a51fe9b..5e5b47a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ fill out a `config.json` file as specified below, and then `npm run run`. ``` ## TODO +- Add expected summary (maybe in /summary? compare to averages?) - Add graphs - Add initial balance somewhere - Add assets/liabilities? diff --git a/src/server.js b/src/server.js index b7b5ce4..b8ccef4 100644 --- a/src/server.js +++ b/src/server.js @@ -58,7 +58,10 @@ function setUpRoutes(models, jwtFunctions, database, templates) { server.use('/static', express.static(path.join(__dirname, '/static'))) server.get('/', (req, res) => res.redirect("/ledger")) - server.get('/login', (req, res) => res.sendFile(path.join(__dirname, "/login.html"))) + server.get('/login', (req, res) => { + let body = templates["login"]({}); + res.status(200).send(body) + }) server.get('/ledger', async (req, res) => { var ledger = await database.query(`SELECT * FROM transactions WHERE username = '${res.locals.user.username}' ORDER BY \`when\` DESC`, { type: database.QueryTypes.SELECT }) ledger.forEach((element, i) => { @@ -120,7 +123,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { const token = jwtFunctions.sign(user.username); res.cookie('authorization', token, { expires: new Date(Date.now() + (1000 * 60 * 60 * 24 * 30)) }); console.debug("Redirecting to page - logged in") - res.redirect('/'); + res.redirect('/ledger'); } else { console.debug("Redirecting to login - invalid login") res.redirect('/login'); @@ -261,15 +264,8 @@ async function formatSummary(database, username) { }) response.week.net.forEach(el => { var item = findOrCreateWeek(summary, el); - item.net = el.s - // Note we flip these since income is negative - item.classes = "" - if(el.s > 0){ - item.classes += "net-negative" - } - if(el.s < 0){ - item.classes += "net-positive" - } + item.net = Number(el.s) + item.classes = getClass(el.s) }) response.month.in.forEach(el => { @@ -280,15 +276,8 @@ async function formatSummary(database, username) { }) response.month.net.forEach(el => { var item = findOrCreateMonth(summary, el); - item.net = el.s - // Note we flip these since income is negative - item.classes = "" - if(el.s > 0){ - item.classes += "net-negative" - } - if(el.s < 0){ - item.classes += "net-positive" - } + item.net = Number(el.s) + item.classes = getClass(el.s) }) response.year.in.forEach(el => { @@ -299,15 +288,8 @@ async function formatSummary(database, username) { }) response.year.net.forEach(el => { var item = findOrCreateYear(summary, el); - item.net = el.s - // Note we flip these since income is negative - item.classes = "" - if(el.s > 0){ - item.classes += "net-negative" - } - if(el.s < 0){ - item.classes += "net-positive" - } + item.net = Number(el.s) + item.classes = getClass(el.s) }) summary.week.sort(function (a, b) { @@ -321,11 +303,38 @@ async function formatSummary(database, username) { summary.year.sort(function (a, b) { return a.y - b.y; }) - + summary.year_avg = getBudgetAverage(summary.year) + summary.month_avg = getBudgetAverage(summary.month) + summary.week_avg = getBudgetAverage(summary.week) summary.name = username return summary } +function getBudgetAverage(list){ + let avg = { out: 0, in: 0, net: 0} + list.forEach(item => { + avg.out += item.out + avg.in += item.in + avg.net += item.net + }) + avg.out = Math.round(avg.out / list.length) + avg.in = Math.round(avg.in / list.length) + avg.net = Math.round(avg.net / list.length) + avg.classes = getClass(avg.net) + return avg; +} + +function getClass(value){ + if(value > 0){ + return "net-negative" + } + if(value < 0){ + return "net-positive" + } + return "" +} + + module.exports = { listen, setUpRoutes diff --git a/src/templates/login.html b/src/templates/login.html index ec41762..f090108 100644 --- a/src/templates/login.html +++ b/src/templates/login.html @@ -38,10 +38,10 @@

Login

-
- - - +
+ + +
diff --git a/src/templates/summary.html b/src/templates/summary.html index bffd46b..d78182a 100644 --- a/src/templates/summary.html +++ b/src/templates/summary.html @@ -35,6 +35,14 @@ {{this.net}} {{/each}} + Avg: + + Avg: + + {{week_avg.in}} + {{week_avg.out}} + {{week_avg.net}} +
@@ -56,6 +64,13 @@ {{this.net}} {{/each}} + + Avg: + + {{month_avg.in}} + {{month_avg.out}} + {{month_avg.net}} +
@@ -75,6 +90,13 @@ {{this.net}} {{/each}} + Avg: + + Avg: + {{year_avg.in}} + {{year_avg.out}} + {{year_avg.net}} +
-- cgit v1.2.3