Skip to content

Commit

Permalink
feature(connect transactions endpoint to database):
Browse files Browse the repository at this point in the history
connect transaction endpoint to database
[Starts #165434274]
  • Loading branch information
Cavdy committed Apr 18, 2019
1 parent 52a0a88 commit 2dd5f6e
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 161 deletions.
53 changes: 53 additions & 0 deletions q
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
id | email | firstname | lastname | accountnumber | createdon | owner | type | status | balance
----+---------------------+-----------+----------+---------------+-----------+-------+---------+---------+---------
1 | banka872@banka4.com | | | 3702581476 | 17/4/2019 | 50 | savings | active | 0
2 | banka872@banka4.com | | | 3872146107 | 17/4/2019 | 50 | savings | active | 0
5 | banka872@banka4.com | cavdy | isaiah | 3548652217 | 17/4/2019 | 50 | savings | active | 0
6 | banka872@banka4.com | cavdy | isaiah | 3491564558 | 17/4/2019 | 50 | savings | active | 0
7 | banka872@banka4.com | cavdy | isaiah | 3146859791 | 17/4/2019 | 54 | savings | active | 0
8 | banka872@banka4.com | cavdy | isaiah | 3779533562 | 17/4/2019 | 55 | savings | active | 0
3 | banka872@banka4.com | cavdy | isaiah | 3699742813 | 17/4/2019 | 50 | savings | dormant | 0
10 | banka872@banka4.com | cavdy | isaiah | 3484311663 | 17/4/2019 | 57 | savings | active | 0
11 | banka872@banka4.com | cavdy | isaiah | 3204762909 | 17/4/2019 | 57 | savings | active | 0
12 | banka872@banka4.com | cavdy | isaiah | 3877851924 | 17/4/2019 | 59 | savings | active | 0
13 | banka872@banka4.com | cavdy | isaiah | 3072181085 | 17/4/2019 | 60 | savings | active | 0
14 | banka872@banka4.com | cavdy | isaiah | 3432650514 | 17/4/2019 | 60 | savings | active | 0
15 | banka872@banka4.com | cavdy | isaiah | 3983443368 | 17/4/2019 | 61 | savings | active | 0
16 | banka872@banka4.com | cavdy | isaiah | 3438930243 | 17/4/2019 | 61 | savings | active | 0
17 | banka872@banka4.com | cavdy | isaiah | 3655609161 | 17/4/2019 | 62 | savings | active | 0
18 | banka872@banka4.com | cavdy | isaiah | 3297739483 | 17/4/2019 | 62 | savings | active | 0
19 | banka872@banka4.com | cavdy | isaiah | 3633960829 | 17/4/2019 | 62 | savings | active | 0
20 | banka872@banka4.com | cavdy | isaiah | 3003801983 | 17/4/2019 | 63 | savings | active | 0
21 | banka872@banka4.com | cavdy | isaiah | 3453420061 | 17/4/2019 | 63 | savings | active | 0
22 | banka872@banka4.com | cavdy | isaiah | 3797532048 | 17/4/2019 | 63 | savings | active | 0
23 | admin@banka.com | cavdy | ikenna | 3515833161 | 17/4/2019 | 64 | savings | active | 0
24 | banka872@banka4.com | cavdy | isaiah | 3931715014 | 17/4/2019 | 65 | savings | active | 0
25 | banka872@banka4.com | cavdy | isaiah | 3502561578 | 17/4/2019 | 65 | savings | active | 0
26 | banka872@banka4.com | cavdy | isaiah | 3261422912 | 17/4/2019 | 65 | savings | active | 0
27 | banka872@banka4.com | cavdy | isaiah | 3298245957 | 17/4/2019 | 67 | savings | active | 0
28 | banka872@banka4.com | cavdy | isaiah | 3461967601 | 17/4/2019 | 67 | savings | active | 0
29 | banka872@banka4.com | cavdy | isaiah | 3243384247 | 17/4/2019 | 67 | savings | active | 0
30 | admin@banka.com | cavdy | ikenna | 3563254210 | 17/4/2019 | 68 | savings | active | 0
31 | banka872@banka4.com | cavdy | isaiah | 3534066798 | 17/4/2019 | 69 | savings | active | 0
32 | banka872@banka4.com | cavdy | isaiah | 3482397810 | 17/4/2019 | 69 | savings | active | 0
33 | banka872@banka4.com | cavdy | isaiah | 3110547517 | 17/4/2019 | 69 | savings | active | 0
34 | banka872@banka4.com | cavdy | isaiah | 3437719577 | 17/4/2019 | 69 | savings | active | 0
35 | banka872@banka4.com | cavdy | isaiah | 3548538073 | 17/4/2019 | 71 | savings | active | 0
36 | banka872@banka4.com | cavdy | isaiah | 3703724774 | 17/4/2019 | 73 | savings | active | 0
37 | banka872@banka4.com | cavdy | isaiah | 3124616562 | 17/4/2019 | 75 | savings | active | 0
38 | banka872@banka4.com | cavdy | isaiah | 3057087124 | 17/4/2019 | 77 | savings | active | 0
39 | banka872@banka4.com | cavdy | isaiah | 3753591929 | 17/4/2019 | 79 | savings | active | 0
40 | banka872@banka4.com | cavdy | isaiah | 3008302349 | 17/4/2019 | 81 | savings | active | 0
41 | banka872@banka4.com | cavdy | isaiah | 3164295216 | 17/4/2019 | 83 | savings | active | 0
42 | banka872@banka4.com | cavdy | isaiah | 3355341006 | 17/4/2019 | 85 | savings | active | 0
43 | banka872@banka4.com | cavdy | isaiah | 3043952815 | 17/4/2019 | 87 | savings | active | 0
44 | banka872@banka4.com | cavdy | isaiah | 3331809305 | 17/4/2019 | 89 | savings | active | 0
45 | banka872@banka4.com | cavdy | isaiah | 3983804072 | 17/4/2019 | 91 | savings | active | 0
46 | banka872@banka4.com | cavdy | isaiah | 3446930353 | 17/4/2019 | 93 | savings | active | 0
47 | banka872@banka4.com | cavdy | isaiah | 3571949659 | 17/4/2019 | 95 | savings | active | 0
48 | banka872@banka4.com | cavdy | isaiah | 3242133029 | 17/4/2019 | 97 | savings | active | 0
49 | banka872@banka4.com | cavdy | isaiah | 3384304769 | 17/4/2019 | 107 | savings | active | 0
50 | banka872@banka4.com | cavdy | isaiah | 3429391534 | 17/4/2019 | 107 | savings | active | 0
51 | banka872@banka4.com | cavdy | isaiah | 3344395362 | 17/4/2019 | 107 | savings | active | 0
(49 rows)

28 changes: 28 additions & 0 deletions server/v1/config/transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Transactions SQL query

-- create transactions table
CREATE TABLE transactions (
id SERIAL PRIMARY KEY UNIQUE,
createdOn VARCHAR(40),
type VARCHAR(10),
accountNumber BIGINT,
cashier INTEGER,
amount FLOAT,
oldBalance FLOAT,
newBalance FLOAT
);

-- select all from transactions table
SELECT * FROM transactions LIMIT 10

-- select account number fro transactions
SELECT accountNumber FROM transactions WHERE accountNumber=$1, ['accountNumber']

-- insert into transactions table
INSERT into transactions values($1), ['value']

-- update
UPDATE transactions SET balance=$1 WHERE accountnumber=$2, [newBalance, accountNumber]

-- delete from transactions table
DELETE FROM transactions;
11 changes: 11 additions & 0 deletions server/v1/controllers/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ const CreateAccountController = {
}).status(201);
},

// get transaction history
async allAccountTransaction(req, res) {
const { accountNumber } = req.params;
const transactionHistory = await CreateAccountService
.allAccountTransaction(accountNumber);
return res.json({
status: 'success',
data: transactionHistory,
}).status(201);
},

// patchAccount
async patchAccount(req, res) {
const { accountNumber } = req.params;
Expand Down
19 changes: 15 additions & 4 deletions server/v1/controllers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@
import TransactionService from '../services/transaction';

const TransactionController = {
debitTransaction(req, res) {
async debitTransaction(req, res) {
const { accountNumber } = req.params;
const transactionData = req.body;
const debitedData = TransactionService
const debitedData = await TransactionService
.debitTransaction(accountNumber, req.authorizedData, transactionData);
return res.json({
status: 'success',
data: debitedData,
}).status(201);
},
creditTransaction(req, res) {

async getSpecificTransaction(req, res) {
const { transactionid } = req.params;
const getTransaction = await TransactionService
.getSpecificTransaction(transactionid);
return res.json({
status: 'success',
data: getTransaction,
}).status(201);
},

async creditTransaction(req, res) {
const { accountNumber } = req.params;
const transactionData = req.body;
const creditedData = TransactionService
const creditedData = await TransactionService
.creditTransaction(accountNumber, req.authorizedData, transactionData);
return res.json({
status: 'success',
Expand Down
10 changes: 4 additions & 6 deletions server/v1/model/Transaction.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
export default class Transaction {
constructor() {
this.id = null;
this.createdOn = null;
this.type = null; // credit or debit
this.transactionId = null;
this.accountNumber = null;
this.cashier = null; // cashier id
this.amount = null;
this.oldBalance = null;
this.newBalance = null;
this.cashier = null; // cashier id
this.transactionType = null; // credit or debit
this.accountBalance = null;
}
}
1 change: 1 addition & 0 deletions server/v1/routes/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CreateAccountController from '../controllers/createAccount';
const router = express.Router();

// creating our routes
router.get('/:accountNumber/transactions', jwtMiddleware.verifyJwt, CreateAccountController.allAccountTransaction);
router.post('/', jwtMiddleware.verifyJwt, CreateAccountController.createAccount);
router.patch('/:accountNumber', jwtMiddleware.verifyJwt, CreateAccountController.patchAccount);
router.delete('/:accountNumber', jwtMiddleware.verifyJwt, CreateAccountController.deleteAccount);
Expand Down
1 change: 1 addition & 0 deletions server/v1/routes/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TransactionController from '../controllers/transaction';
const router = express.Router();

// creating our routes
router.get('/:transactionid', jwtMiddleware.verifyJwt, TransactionController.getSpecificTransaction);
router.post('/:accountNumber/debit', jwtMiddleware.verifyJwt, TransactionController.debitTransaction);
router.post('/:accountNumber/credit', jwtMiddleware.verifyJwt, TransactionController.creditTransaction);

Expand Down
9 changes: 9 additions & 0 deletions server/v1/services/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ const CreateAccountService = {
return accountOutput;
},

async allAccountTransaction(accountNumber) {
const userTransaction = await dbConnection
.dbConnect('SELECT * from transactions WHERE accountnumber=$1', [accountNumber]);
if (userTransaction.rows.length > 0) {
return userTransaction.rows;
}
return 'no transaction found';
},

async patchAccount(accountNumber, accountUpdate, staff) {
let account;

Expand Down
148 changes: 99 additions & 49 deletions server/v1/services/transaction.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,113 @@
/* eslint-disable no-param-reassign */
import transactionsData from '../../dummyJson/transactions';
import accountsData from '../../dummyJson/accounts';
import dbConnection from '../config/database';
import TransactionModel from '../model/Transaction';

const { accounts } = accountsData;
const TransactionService = {
async debitTransaction(accountNumber, loggedInUser, transactionData) {
let returnData;

const { transactions } = transactionsData;
// check the users table
const userDetails = await dbConnection
.dbConnect('SELECT id, type, isadmin FROM users WHERE email=$1', [loggedInUser.email]);
const { id, type, isadmin } = userDetails.rows[0];

const TransactionService = {
debitTransaction(accountNumber, loggedInUser, transactionData) {
if (loggedInUser.loggedUser.type === 'staff' || loggedInUser.loggedUser.isAdmin === true) {
// eslint-disable-next-line no-plusplus
for (let i = 0; i <= accounts.length - 1; i++) {
// eslint-disable-next-line eqeqeq
if (accounts[i].accountNumber == accountNumber) {
const date = new Date();
const createdOn = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
const transactionLength = transactions.length;
const newId = transactionLength + 1;
transactionData.id = newId;
transactionData.createdOn = createdOn;
transactionData.type = 'debit';
transactionData.accountNumber = accountNumber;
transactionData.cashier = loggedInUser.loggedUser.id;
transactionData.amount = transactionData.amount;
transactionData.oldBalance = accounts[i].balance;
const newBalance = accounts[i].balance - transactionData.amount;
transactionData.newBalance = newBalance;
transactions.push(transactionData);
return transactionData;
// checks if logged in user is an admin or staff
if (type === 'staff' || isadmin === true) {
const date = new Date();
const createdOn = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;

// pull accountnumber details from database
const accountDbData = await dbConnection
.dbConnect('SELECT accountnumber, balance FROM accounts WHERE accountnumber=$1', [accountNumber]);
const { accountnumber, balance } = accountDbData.rows[0];

// check if a string
const checkForDigit = /^-?\d+\.?\d*$/;
if (checkForDigit.test(transactionData.amount)) {
// substract the passed in amount from the current balance
const newBalance = balance - transactionData.amount;
const transactionDbData = await dbConnection
.dbConnect('INSERT into transactions(createdon, type, accountNumber, cashier, amount, oldbalance, newbalance) values($1, $2, $3, $4, $5,$6, $7)',
[createdOn, 'debit', accountnumber, id, transactionData.amount, balance, newBalance]);
if (transactionDbData.command === 'INSERT') {
// get the data from transaction
const accountData = await dbConnection.dbConnect('SELECT * FROM transactions WHERE accountnumber=$1', [accountNumber]);
// update the account table
await dbConnection.dbConnect('UPDATE accounts SET balance=$1 WHERE accountnumber=$2', [newBalance, accountNumber]);
const transaction = new TransactionModel();
transaction.transactionId = accountData.rows[0].id;
transaction.accountNumber = accountData.rows[0].accountnumber;
transaction.amount = accountData.rows[0].amount;
transaction.cashier = accountData.rows[0].cashier;
transaction.transactionType = accountData.rows[0].type;
transaction.accountBalance = accountData.rows[0].newbalance;
returnData = transaction;
} else {
returnData = 'Something wrong happened';
}
}
} else {
returnData = 'You must be a staff or admin to perform this transaction';
}
return returnData;
},

async getSpecificTransaction(transactionId) {
const userTransaction = await dbConnection
.dbConnect('SELECT * from transactions WHERE id=$1', [transactionId]);
if (userTransaction.rows.length > 0) {
return userTransaction.rows;
}
return 'you must be a staff to perform this transaction';
return 'no transaction found';
},
creditTransaction(accountNumber, loggedInUser, transactionData) {
if (loggedInUser.loggedUser.type === 'staff' || loggedInUser.loggedUser.isAdmin === true) {
// eslint-disable-next-line no-plusplus
for (let i = 0; i <= accounts.length - 1; i++) {
// eslint-disable-next-line eqeqeq
if (accounts[i].accountNumber == accountNumber) {
const date = new Date();
const createdOn = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
const transactionLength = transactions.length;
const newId = transactionLength + 1;
transactionData.id = newId;
transactionData.createdOn = createdOn;
transactionData.type = 'credit';
transactionData.accountNumber = accountNumber;
transactionData.cashier = loggedInUser.loggedUser.id;
transactionData.amount = transactionData.amount;
transactionData.oldBalance = accounts[i].balance;
const newBalance = accounts[i].balance + transactionData.amount;
transactionData.newBalance = newBalance;
transactions.push(transactionData);
return transactionData;

async creditTransaction(accountNumber, loggedInUser, transactionData) {
let returnData;

// check the users table
const userDetails = await dbConnection
.dbConnect('SELECT id, type, isadmin FROM users WHERE email=$1', [loggedInUser.email]);
const { id, type, isadmin } = userDetails.rows[0];

// checks if logged in user is an admin or staff
if (type === 'staff' || isadmin === true) {
const date = new Date();
const createdOn = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;

// pull accountnumber details from database
const accountDbData = await dbConnection
.dbConnect('SELECT accountnumber, balance FROM accounts WHERE accountnumber=$1', [accountNumber]);
const { accountnumber, balance } = accountDbData.rows[0];

// check if a string
const checkForDigit = /^-?\d+\.?\d*$/;
if (checkForDigit.test(transactionData.amount)) {
// adding the passed in amount from the current balance
const newBalance = balance + transactionData.amount;
const transactionDbData = await dbConnection
.dbConnect('INSERT into transactions(createdon, type, accountNumber, cashier, amount, oldbalance, newbalance) values($1, $2, $3, $4, $5,$6, $7)',
[createdOn, 'credit', accountnumber, id, transactionData.amount, balance, newBalance]);
if (transactionDbData.command === 'INSERT') {
// get the data from transaction
const accountData = await dbConnection.dbConnect('SELECT * FROM transactions WHERE accountnumber=$1', [accountNumber]);
// update the account table
await dbConnection.dbConnect('UPDATE accounts SET balance=$1 WHERE accountnumber=$2', [newBalance, accountNumber]);
const transaction = new TransactionModel();
transaction.transactionId = accountData.rows[0].id;
transaction.accountNumber = accountData.rows[0].accountnumber;
transaction.amount = accountData.rows[0].amount;
transaction.cashier = accountData.rows[0].cashier;
transaction.transactionType = accountData.rows[0].type;
transaction.accountBalance = accountData.rows[0].newbalance;
returnData = transaction;
} else {
returnData = 'Something wrong happened';
}
}
} else {
returnData = 'You must be a staff or admin to perform this transaction';
}
return 'you must be a staff to perform this transaction';
return returnData;
},
};

Expand Down
Loading

0 comments on commit 2dd5f6e

Please sign in to comment.