Skip to content

Commit

Permalink
Merge branch '153-savings' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
nohorjo committed Jan 23, 2019
2 parents ba6112b + 3d2bf56 commit 8761acc
Show file tree
Hide file tree
Showing 25 changed files with 375 additions and 140 deletions.
54 changes: 32 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "centsa",
"version": "2.15.3",
"version": "2.16.0",
"description": "Centsa money managing solution",
"main": "src/index.js",
"directories": {
Expand All @@ -16,7 +16,8 @@
"google-auth-library": "1.5.0",
"mysql": "2.15.0",
"otplib": "10.0.1",
"supports-color": "5.4.0"
"supports-color": "5.4.0",
"underscore": "^1.9.1"
},
"devDependencies": {
"chai": "4.1.2",
Expand All @@ -29,7 +30,7 @@
},
"scripts": {
"start": "node src/index.js",
"dev": "node-dev src/index.js",
"dev": "NODE_ENV=debug node-dev --inspect src/index.js",
"debug": "NODE_ENV=debug node --inspect-brk=9009 src/index.js",
"test": "DEBUG=centsa:* DB_IP=someip DB_PORT=1234 DB_USER=username DB_PASSWORD=password DB_NAME=testdb nyc mocha test/**/*Test.js --reporter mochawesome",
"xtest": "DB_IP=someip DB_PORT=1234 DB_USER=username DB_PASSWORD=password DB_NAME=testdb nyc mocha --reporter mochawesome",
Expand Down
4 changes: 2 additions & 2 deletions sql/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ CREATE TABLE notifications
id BIGINT PRIMARY KEY AUTO_INCREMENT,
message VARCHAR(500) NOT NULL,
user_id BIGINT NOT NULL,
is_read BOOL NOT NULL DEFAULT FALSE,
UNIQUE(message,user_id)
is_read BOOL NOT NULL DEFAULT FALSE
);
CREATE TABLE usercontrol
(
Expand Down Expand Up @@ -56,6 +55,7 @@ CREATE TABLE accounts
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT NOT NULL,
name VARCHAR(50) NOT NULL,
savings BOOL NOT NULL DEFAULT FALSE,
UNIQUE(user_id,name),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
Expand Down
2 changes: 2 additions & 0 deletions sql/update_190118.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* #153 savings column */
ALTER TABLE accounts ADD COLUMN savings BOOL NOT NULL DEFAULT FALSE;
26 changes: 23 additions & 3 deletions src/Accounts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { Router } = require('express');
const _ = require('underscore');

const dao = require('./dao/Accounts');
const log = require('./log');

Expand All @@ -18,7 +20,6 @@ _route.getAll = (req, resp) => {
log.error(err);
resp.status(500).send(err);
} else {
result.forEach(a => a.balance = a.balance || 0);
req.session.userData.accounts = result;
log('returning accounts');
resp.send(result);
Expand All @@ -29,15 +30,15 @@ _route.getAll = (req, resp) => {

_route.insert = (req, resp) => {
log('insert account');
const account = (({ name, id }) => ({ name, id }))(req.body || {});
const account = _.pick(req.body, 'name', 'id', 'savings');
account['user_id'] = req.session.userData.user_id;
if (!account.id) {
delete account.id;
}
dao.insert(account, (err, id) => {
if (err) {
log.error(err);
resp.status(500).send(err);
resp.status(500).send(err.errno == 1062 ? 'An account with this name already exists' : err);
} else {
const { userData } = req.session;
userData.accounts = [
Expand All @@ -50,9 +51,28 @@ _route.insert = (req, resp) => {
});
};

_route.delete = (req, resp) => {
log('deleting account');
const {
params: { id },
query: { transfer },
session: { userData },
} = req;
dao.deleteAccount(userData.user_id, id, transfer, err => {
if (err) {
log.error(err);
resp.status(500).send(transfer == -1 && err.errno == 1451 ? "This account is part of a 'savings goal'. Please delete that first from the 'expenses' page" : err);
} else {
log('deleted account');
userData.accounts = userData.accounts.filter(a => a.id != id);
resp.sendStatus(201);
}
});
};

route.get('/', _route.getAll);
route.post('/', _route.insert);
route.delete('/:id', _route.delete);

_route.use('/accounts', route);

Expand Down
4 changes: 3 additions & 1 deletion src/Authentication.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const axios = require('axios');
const _ = require('underscore');

const Users = require('./Users');
const UsersDao = require('./dao/Users');
const log = require('./log');
Expand Down Expand Up @@ -192,7 +194,7 @@ const getDetailsFromGoogle = async token => {
idToken: token,
audience: CLIENT_ID
});
return (({name,email})=>({name,email}))(ticket.getPayload() || {});
return _.pick(ticket.getPayload(), 'name', 'email');
};

const getDetailsFromFB = async ({userID, accessToken} = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/Expenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ _route.insert = (req, resp) => {
dao.insert(expense, (err, id) => {
if (err) {
log.error(err);
resp.status(500).send(err);
resp.status(500).send(err.errno == 1062 ? 'An expense with this name already exists' : err);
} else {
if (expense.automatic) {
_route.applyAutoTransactions(true, id, new Date(req.get('x-date')));
Expand Down
Loading

0 comments on commit 8761acc

Please sign in to comment.