From 2dd5f6e55017337b79eca135e1c89f11b85c774c Mon Sep 17 00:00:00 2001 From: cavdy Date: Thu, 18 Apr 2019 02:49:18 +0100 Subject: [PATCH] feature(connect transactions endpoint to database): connect transaction endpoint to database [Starts #165434274] --- q | 53 +++++++ server/v1/config/transaction.sql | 28 ++++ server/v1/controllers/createAccount.js | 11 ++ server/v1/controllers/transaction.js | 19 ++- server/v1/model/Transaction.js | 10 +- server/v1/routes/createAccount.js | 1 + server/v1/routes/transaction.js | 1 + server/v1/services/createAccount.js | 9 ++ server/v1/services/transaction.js | 148 +++++++++++------ server/v1/test/accounts.js | 32 +++- server/v1/test/transactions.js | 211 +++++++++++++------------ 11 files changed, 362 insertions(+), 161 deletions(-) create mode 100644 q create mode 100644 server/v1/config/transaction.sql diff --git a/q b/q new file mode 100644 index 0000000..a0a0175 --- /dev/null +++ b/q @@ -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) + diff --git a/server/v1/config/transaction.sql b/server/v1/config/transaction.sql new file mode 100644 index 0000000..fbabb50 --- /dev/null +++ b/server/v1/config/transaction.sql @@ -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; \ No newline at end of file diff --git a/server/v1/controllers/createAccount.js b/server/v1/controllers/createAccount.js index 94c7b49..8c55a8b 100644 --- a/server/v1/controllers/createAccount.js +++ b/server/v1/controllers/createAccount.js @@ -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; diff --git a/server/v1/controllers/transaction.js b/server/v1/controllers/transaction.js index 4fc9061..693c0f7 100644 --- a/server/v1/controllers/transaction.js +++ b/server/v1/controllers/transaction.js @@ -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', diff --git a/server/v1/model/Transaction.js b/server/v1/model/Transaction.js index cb3706f..6538568 100644 --- a/server/v1/model/Transaction.js +++ b/server/v1/model/Transaction.js @@ -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; } } diff --git a/server/v1/routes/createAccount.js b/server/v1/routes/createAccount.js index 85ad9f7..bba3bd2 100644 --- a/server/v1/routes/createAccount.js +++ b/server/v1/routes/createAccount.js @@ -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); diff --git a/server/v1/routes/transaction.js b/server/v1/routes/transaction.js index 039a714..3986d5a 100644 --- a/server/v1/routes/transaction.js +++ b/server/v1/routes/transaction.js @@ -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); diff --git a/server/v1/services/createAccount.js b/server/v1/services/createAccount.js index 9853bcb..15d532a 100644 --- a/server/v1/services/createAccount.js +++ b/server/v1/services/createAccount.js @@ -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; diff --git a/server/v1/services/transaction.js b/server/v1/services/transaction.js index fa8f412..10bad74 100644 --- a/server/v1/services/transaction.js +++ b/server/v1/services/transaction.js @@ -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; }, }; diff --git a/server/v1/test/accounts.js b/server/v1/test/accounts.js index e402757..784f265 100644 --- a/server/v1/test/accounts.js +++ b/server/v1/test/accounts.js @@ -8,10 +8,6 @@ import app from '../app'; chai.use(chaiHttp); describe('Testing Accounts Controller', () => { - before(async () => { - await dbConnection - .dbConnect('INSERT into users(email, firstName, lastName, password, type, isAdmin) values($1, $2, $3, $4, $5, $6)', ['admin@banka.com', 'cavdy', 'ikenna', '$2a$10$CmmIst1.D3QjaWuafKbBaOuAFu0r9o7xxQY.0SMKiAN.h9z52a2y2', 'staff', true]); - }); describe('Testing accounts controller', () => { it( 'accounts should have all required details', @@ -100,6 +96,34 @@ describe('Testing Accounts Controller', () => { }, ); + it( + 'transaction should have these propertise', + async () => { + const signinUrl = '/api/auth/signin'; + const response = await chai.request(app) + .post(signinUrl) + .send({ + email: 'banka872@banka4.com', + password: 'passworD4@', + }); + const { token } = response.body.data[0]; + const res = await chai.request(app) + .get('/api/v1/accounts/3003801983/transactions') + .set('Authorization', `Bearer ${token}`) + .send(); + expect(res.body).to.be.an('object'); + expect(res.body.status).to.equal('success'); + expect(res.body.data[0]).to.have.property('id'); + expect(res.body.data[0]).to.have.property('createdon'); + expect(res.body.data[0]).to.have.property('type'); + expect(res.body.data[0]).to.have.property('accountnumber'); + expect(res.body.data[0]).to.have.property('cashier'); + expect(res.body.data[0]).to.have.property('amount'); + expect(res.body.data[0]).to.have.property('oldbalance'); + expect(res.body.data[0]).to.have.property('newbalance'); + }, + ); + it( 'should notify when account does not exist', async () => { diff --git a/server/v1/test/transactions.js b/server/v1/test/transactions.js index e799905..552e880 100644 --- a/server/v1/test/transactions.js +++ b/server/v1/test/transactions.js @@ -1,103 +1,118 @@ -// /* eslint-disable no-undef */ -// import chaiHttp from 'chai-http'; -// import chai, { expect } from 'chai'; +/* eslint-disable no-undef */ +import chaiHttp from 'chai-http'; +import chai, { expect } from 'chai'; +import dbConnection from '../config/database'; -// import app from '../app'; +import app from '../app'; -// chai.use(chaiHttp); +chai.use(chaiHttp); -// describe('Testing Transactions Controller', () => { -// describe('Testing transactions controller', () => { -// it( -// 'transactions should have all required propertise', -// (done) => { -// const signinUrl = '/api/auth/signin'; -// chai.request(app) -// .post(signinUrl) -// .send({ -// email: 'banka3@banka.com', -// password: 'passworD3@', -// }) -// .end((error, response) => { -// const { token } = response.body.data; -// chai.request(app) -// .post('/api/v1/transactions/306363789207/debit') -// .set('Authorization', `Bearer ${token}`) -// .send({ -// amount: 200, -// }) -// .end((err, res) => { -// expect(res.body).to.be.an('object'); -// expect(res.body.status).to.equal('success'); -// expect(res.body.status).to.equal('success'); -// expect(res.body.data).to.have.property('id'); -// expect(res.body.data).to.have.property('createdOn'); -// expect(res.body.data).to.have.property('type'); -// expect(res.body.data).to.have.property('accountNumber'); -// expect(res.body.data).to.have.property('cashier'); -// expect(res.body.data).to.have.property('amount'); -// expect(res.body.data).to.have.property('oldBalance'); -// expect(res.body.data).to.have.property('newBalance'); -// }); -// done(); -// }); -// }, -// ); +describe('Testing Transactions Controller', () => { + before(async () => { + await dbConnection + .dbConnect('INSERT into users(email, firstName, lastName, password, type, isAdmin) values($1, $2, $3, $4, $5, $6)', ['admin@banka.com', 'cavdy', 'ikenna', '$2a$10$CmmIst1.D3QjaWuafKbBaOuAFu0r9o7xxQY.0SMKiAN.h9z52a2y2', 'staff', true]); + }); + describe('Testing transactions controller', () => { + it( + 'transactions should have all required propertise', + async () => { + const signinUrl = '/api/auth/signin'; + const response = await chai.request(app) + .post(signinUrl) + .send({ + email: 'admin@banka.com', + password: 'passworD4@', + }); + const { token } = response.body.data[0]; + const res = await chai.request(app) + .post('/api/v1/transactions/3003801983/debit') + .set('Authorization', `Bearer ${token}`) + .send({ + amount: 500, + }); + expect(res.body).to.be.an('object'); + expect(res.body.status).to.equal('success'); + expect(res.body.data).to.have.property('transactionId'); + expect(res.body.data).to.have.property('accountNumber'); + expect(res.body.data).to.have.property('cashier'); + expect(res.body.data).to.have.property('amount'); + expect(res.body.data).to.have.property('transactionType'); + expect(res.body.data).to.have.property('accountBalance'); + }, + ); -// it( -// 'only admin and staffs should perform debit transaction', -// (done) => { -// const signinUrl = '/api/auth/signin'; -// chai.request(app) -// .post(signinUrl) -// .send({ -// email: 'banka@banka.com', -// password: 'passworD1@', -// }) -// .end((error, response) => { -// const { token } = response.body.data; -// chai.request(app) -// .post('/api/v1/transactions/306363789207/debit') -// .set('Authorization', `Bearer ${token}`) -// .send({ -// amount: 200, -// }) -// .end((err, res) => { -// expect(res.body).to.be.an('object'); -// expect(res.body.status).to.equal('success'); -// expect(res.body.data).to.equal('you must be a staff to perform this transaction'); -// }); -// done(); -// }); -// }, -// ); + it( + 'only admin and staffs should perform debit transaction', + async () => { + const signinUrl = '/api/auth/signin'; + const response = await chai.request(app) + .post(signinUrl) + .send({ + email: 'banka872@banka4.com', + password: 'passworD4@', + }); + const { token } = response.body.data[0]; + const res = await chai.request(app) + .post('/api/v1/transactions/3003801983/debit') + .set('Authorization', `Bearer ${token}`) + .send({ + amount: 500, + }); + expect(res.body).to.be.an('object'); + expect(res.body.status).to.equal('success'); + expect(res.body.data).to.equal('You must be a staff or admin to perform this transaction'); + }, + ); -// it( -// 'only admin and staffs should perform credit transaction', -// (done) => { -// const signinUrl = '/api/auth/signin'; -// chai.request(app) -// .post(signinUrl) -// .send({ -// email: 'banka@banka.com', -// password: 'passworD1@', -// }) -// .end((error, response) => { -// const { token } = response.body.data; -// chai.request(app) -// .post('/api/v1/transactions/306363789207/credit') -// .set('Authorization', `Bearer ${token}`) -// .send({ -// amount: 200, -// }) -// .end((err, res) => { -// expect(res.body).to.be.an('object'); -// expect(res.body.status).to.equal('success'); -// expect(res.body.data).to.equal('you must be a staff to perform this transaction'); -// }); -// done(); -// }); -// }, -// ); -// }); -// }); + it( + 'transaction should have these propertise', + async () => { + const signinUrl = '/api/auth/signin'; + const response = await chai.request(app) + .post(signinUrl) + .send({ + email: 'banka872@banka4.com', + password: 'passworD4@', + }); + const { token } = response.body.data[0]; + const res = await chai.request(app) + .get('/api/v1/transactions/4') + .set('Authorization', `Bearer ${token}`) + .send(); + expect(res.body).to.be.an('object'); + expect(res.body.status).to.equal('success'); + expect(res.body.data[0]).to.have.property('id'); + expect(res.body.data[0]).to.have.property('createdon'); + expect(res.body.data[0]).to.have.property('type'); + expect(res.body.data[0]).to.have.property('accountnumber'); + expect(res.body.data[0]).to.have.property('cashier'); + expect(res.body.data[0]).to.have.property('amount'); + expect(res.body.data[0]).to.have.property('oldbalance'); + expect(res.body.data[0]).to.have.property('newbalance'); + }, + ); + + it( + 'only admin and staffs should perform credit transaction', + async () => { + const signinUrl = '/api/auth/signin'; + const response = await chai.request(app) + .post(signinUrl) + .send({ + email: 'banka872@banka4.com', + password: 'passworD4@', + }); + const { token } = response.body.data[0]; + const res = await chai.request(app) + .post('/api/v1/transactions/3003801983/credit') + .set('Authorization', `Bearer ${token}`) + .send({ + amount: 500, + }); + expect(res.body).to.be.an('object'); + expect(res.body.status).to.equal('success'); + expect(res.body.data).to.equal('You must be a staff or admin to perform this transaction'); + }, + ); + }); +});