aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Powers <markppowers0@gmail.com>2020-10-18 16:09:40 -0500
committerMark Powers <markppowers0@gmail.com>2020-10-18 16:09:40 -0500
commita1baf96009692aff2ca123b3071d34fdcba0de63 (patch)
treef7d59d411b65581b3e008488a06183c7cf3a9d2c
parent292048edaf0e2d4c4d591cfec1d923cb6d845991 (diff)
Add monthly averages, fix login tempalte
-rw-r--r--README.md1
-rw-r--r--src/server.js69
-rw-r--r--src/templates/login.html8
-rw-r--r--src/templates/summary.html22
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 @@
<body>
<div>
<h1>Login</h1>
- <div class="form">
- <input on type="text" placeholder="Enter Username" name="username" id="username" required>
- <input type="password" placeholder="Enter Password" name="password" id="password" required>
- <button onclick="sendPost()">Log in</button>
+ <form method="post" action="/login">
+ <input name="username" type="text" placeholder="Enter Username" required>
+ <input type="password" placeholder="Enter Password" name="password" required>
+ <input type="submit" vallue="Log in">
</div>
</div>
</body>
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 @@
<td class="{{this.classes}}">{{this.net}}</td>
</tr>
{{/each}}
+ Avg:
+ <tr>
+ <td>Avg:</td>
+ <td></td>
+ <td>{{week_avg.in}}</td>
+ <td>{{week_avg.out}}</td>
+ <td class="{{week_avg.classes}}">{{week_avg.net}}</td>
+ </tr>
</table>
</div>
<div class="summary-panel">
@@ -56,6 +64,13 @@
<td class="{{this.classes}}">{{this.net}}</td>
</tr>
{{/each}}
+ <tr>
+ <td>Avg:</td>
+ <td></td>
+ <td>{{month_avg.in}}</td>
+ <td>{{month_avg.out}}</td>
+ <td class="{{month_avg.classes}}">{{month_avg.net}}</td>
+ </tr>
</table>
</div>
<div class="summary-panel">
@@ -75,6 +90,13 @@
<td class="{{this.classes}}">{{this.net}}</td>
</tr>
{{/each}}
+ Avg:
+ <tr>
+ <td>Avg:</td>
+ <td>{{year_avg.in}}</td>
+ <td>{{year_avg.out}}</td>
+ <td class="{{year_avg.classes}}">{{year_avg.net}}</td>
+ </tr>
</table>
</div>
</body>