Skip to content

Commit

Permalink
Merge f40055b into 83d6f96
Browse files Browse the repository at this point in the history
  • Loading branch information
Cavdy committed Apr 26, 2019
2 parents 83d6f96 + f40055b commit e6d40e3
Show file tree
Hide file tree
Showing 13 changed files with 665 additions and 684 deletions.
6 changes: 3 additions & 3 deletions server/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"v1/auth/signup": {
"post": {
"tags": [
"Signup"
"Auth"
],
"summary": "Users can signup",
"description": "this endpoint uses get request to create users",
Expand Down Expand Up @@ -65,7 +65,7 @@
"v1/auth/signup/addstaff": {
"post": {
"tags": [
"Signup"
"Auth"
],
"summary": "Admin can signup staffs",
"description": "this endpoint uses get request to create users",
Expand Down Expand Up @@ -135,7 +135,7 @@
"v1/auth/signin": {
"post": {
"tags": [
"Signin"
"Auth"
],
"summary": "Users can signup",
"description": "this endpoint uses get request to create users",
Expand Down
27 changes: 0 additions & 27 deletions server/v1/config/account.sql

This file was deleted.

20 changes: 0 additions & 20 deletions server/v1/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ const dbConnection = {
return debug('query')(e.stack);
}
},

/**
* Connect to database - For test
* @constructor
* @param {*} passedQuery - passed in SQL query.
*/
async dbTesting(passedQuery) {
try {
return (async () => {
const client = await pool.connect();
try {
return await client.query(passedQuery);
} finally {
client.release();
}
})();
} catch (e) {
return debug('query')(e.stack);
}
},
};

export default dbConnection;
28 changes: 0 additions & 28 deletions server/v1/config/transaction.sql

This file was deleted.

27 changes: 0 additions & 27 deletions server/v1/config/user.sql

This file was deleted.

6 changes: 6 additions & 0 deletions server/v1/helper/statusHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const statusHelper = {
status: 409,
data: error,
});
} else if (status === 403) { // conflict
res.status(403);
return res.json({
status: 403,
data: error,
});
} else if (status === 201) { // created
res.status(201);
return res.json({
Expand Down
140 changes: 78 additions & 62 deletions server/v1/middleware/jwt.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,78 @@
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';

dotenv.config();

const jwtMiddleware = {
/**
* Check Token
* @constructor
* @param {*} req - get request.
* @param {*} res -get response
* @param {*} next - run next
*/
checkToken(req, res, next) {
const header = req.headers.authorization;
if (typeof header !== 'undefined') {
const bearer = header.split(' ');
const token = bearer[1];
req.token = token;
next();
} else {
// If header is undefined return Forbidden (403)
res.sendStatus(403);
}
},

/**
* Signin Jwt
* @constructor
* @param {*} req - get request.
* @param {*} res -get response
* @param {*} next - run next
*/
signinJwt(req, res, next) {
jwt.sign(req.body, process.env.JWTSECRETKEY, async (err, token) => {
if (err) {
return res.sendStatus(403);
}
req.signintoken = token;
return next();
});
},

/**
* Verify Jwt
* @constructor
* @param {*} req - get request.
* @param {*} res -get response
* @param {*} next - run next
*/
verifyJwt(req, res, next) {
jwt.verify(req.token, process.env.JWTSECRETKEY, (err, authorizedData) => {
if (err) {
return res.sendStatus(403);
}
req.authorizedData = authorizedData;
return next();
});
},
};

export default jwtMiddleware;
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import statusHelper from '../helper/statusHelper';

dotenv.config();

const jwtMiddleware = {
/**
* Check Token
* @constructor
* @param {*} req - get request.
* @param {*} res -get response
* @param {*} next - run next
*/
checkToken(req, res, next) {
const header = req.headers.authorization;
if (typeof header !== 'undefined') {
const bearer = header.split(' ');
const token = bearer[1];
req.token = token;
next();
} else {
// If header is undefined return Forbidden (403)
return statusHelper
.statusHelper(req,
res,
403,
'you are not logged in',
'');
}
},

/**
* Signin Jwt
* @constructor
* @param {*} req - get request.
* @param {*} res -get response
* @param {*} next - run next
*/
signinJwt(req, res, next) {
jwt.sign(req.body, process.env.JWTSECRETKEY, async (err, token) => {
if (err) {
return statusHelper
.statusHelper(req,
res,
403,
'you are not logged in',
'');
}
req.signintoken = token;
return next();
});
},

/**
* Verify Jwt
* @constructor
* @param {*} req - get request.
* @param {*} res -get response
* @param {*} next - run next
*/
verifyJwt(req, res, next) {
jwt.verify(req.token, process.env.JWTSECRETKEY, (err, authorizedData) => {
if (err) {
return statusHelper
.statusHelper(req,
res,
403,
'invalid token',
'');
}
req.authorizedData = authorizedData;
return next();
});
},
};

export default jwtMiddleware;
File renamed without changes.
File renamed without changes.
37 changes: 21 additions & 16 deletions server/v1/services/accounts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbConnection from '../config/database';
import AccountModel from '../model/CreateAccount';
import AccountModel from '../model/accounts';

const CreateAccountService = {
/**
Expand All @@ -18,7 +18,7 @@ const CreateAccountService = {

// pulling users data from database
const userDetails = await dbConnection
.dbConnect('SELECT id,firstname,lastname FROM users WHERE email=$1',
.dbConnect('SELECT * FROM users WHERE email=$1',
[userData.email]);
const { firstname, lastname, id } = userDetails.rows[0];

Expand Down Expand Up @@ -175,21 +175,26 @@ const CreateAccountService = {
const { type, isadmin } = userDetails.rows[0];

if (type === 'staff' || isadmin === true) {
const accountDbData = await dbConnection
.dbConnect('SELECT accountnumber FROM accounts WHERE accountnumber=$1',
[accountNumber]);
if (accountDbData.rows.length > 0) {
const updateAccount = await dbConnection
.dbConnect('UPDATE accounts SET status=$1 WHERE accountnumber=$2',
[accountUpdate.status, accountNumber]);
if (updateAccount.command === 'UPDATE') {
const userDbData = await dbConnection
.dbConnect('SELECT accountnumber, status FROM accounts WHERE accountnumber=$1',
[accountNumber]);
const { accountnumber, status } = userDbData.rows[0];
returnStatus = 200;
returnSuccess = { accountnumber, status };
if (accountUpdate.status === 'active' || accountUpdate.status === 'dormant') {
const accountDbData = await dbConnection
.dbConnect('SELECT accountnumber FROM accounts WHERE accountnumber=$1',
[accountNumber]);
if (accountDbData.rows.length > 0) {
const updateAccount = await dbConnection
.dbConnect('UPDATE accounts SET status=$1 WHERE accountnumber=$2 RETURNING accountnumber, status',
[accountUpdate.status, accountNumber]);
if (updateAccount.command === 'UPDATE') {
const { accountnumber, status } = updateAccount.rows[0];
returnStatus = 200;
returnSuccess = { accountnumber, status };
}
} else {
returnStatus = 404;
returnError = 'account not found';
}
} else {
returnStatus = 422;
returnError = 'account status can only be active or dormant';
}
} else {
returnStatus = 401;
Expand Down
10 changes: 3 additions & 7 deletions server/v1/services/transaction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-param-reassign */
import dbConnection from '../config/database';
import TransactionModel from '../model/Transaction';
import TransactionModel from '../model/transaction';

const TransactionService = {

Expand Down Expand Up @@ -33,9 +33,7 @@ const TransactionService = {
);
const { accountnumber, balance } = accountDbData.rows[0];

// check if a string
const checkForDigit = /^-?\d+\.?\d*$/;
if (checkForDigit.test(transactionData.amount)) {
if (typeof transactionData.amount === 'number') {
// substract the passed in amount from the current balance
const newBalance = balance - transactionData.amount;

Expand Down Expand Up @@ -132,9 +130,7 @@ const TransactionService = {
);
const { accountnumber, balance } = accountDbData.rows[0];

// check if a string
const checkForDigit = /^-?\d+\.?\d*$/;
if (checkForDigit.test(transactionData.amount)) {
if (typeof transactionData.amount === 'number') {
if (transactionData.amount <= 0) {
returnStatus = 422;
returnError = 'please credit an account with positive value';
Expand Down
Loading

0 comments on commit e6d40e3

Please sign in to comment.