diff --git a/server/v1/controllers/createAccount.js b/server/v1/controllers/createAccount.js index 8c55a8b..179e238 100644 --- a/server/v1/controllers/createAccount.js +++ b/server/v1/controllers/createAccount.js @@ -11,6 +11,17 @@ const CreateAccountController = { }).status(201); }, + // all accounts + async allAccounts(req, res) { + const queryParams = req.query.status; + const allAccounts = await CreateAccountService + .allAccounts(queryParams); + return res.json({ + status: 'success', + data: allAccounts, + }).status(201); + }, + // get transaction history async allAccountTransaction(req, res) { const { accountNumber } = req.params; diff --git a/server/v1/routes/createAccount.js b/server/v1/routes/createAccount.js index bba3bd2..53b0d74 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('/', jwtMiddleware.verifyJwt, CreateAccountController.allAccounts); router.get('/:accountNumber/transactions', jwtMiddleware.verifyJwt, CreateAccountController.allAccountTransaction); router.post('/', jwtMiddleware.verifyJwt, CreateAccountController.createAccount); router.patch('/:accountNumber', jwtMiddleware.verifyJwt, CreateAccountController.patchAccount); diff --git a/server/v1/services/createAccount.js b/server/v1/services/createAccount.js index 15d532a..35503d9 100644 --- a/server/v1/services/createAccount.js +++ b/server/v1/services/createAccount.js @@ -41,6 +41,20 @@ const CreateAccountService = { return accountOutput; }, + async allAccounts(queryParams) { + if (typeof queryParams === 'undefined' || queryParams === null) { + const allAccounts = await dbConnection + .dbConnect('SELECT * from accounts'); + return allAccounts.rows; + } + const allAccounts = await dbConnection + .dbConnect('SELECT * from accounts WHERE status=$1', [queryParams]); + if (allAccounts.rows.length > 0) { + return allAccounts.rows; + } + return 'no account found'; + }, + async allAccountTransaction(accountNumber) { const userTransaction = await dbConnection .dbConnect('SELECT * from transactions WHERE accountnumber=$1', [accountNumber]); diff --git a/server/v1/test/accounts.js b/server/v1/test/accounts.js index 784f265..d141603 100644 --- a/server/v1/test/accounts.js +++ b/server/v1/test/accounts.js @@ -124,6 +124,36 @@ describe('Testing Accounts Controller', () => { }, ); + it( + 'get all accounts 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') + .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('email'); + expect(res.body.data[0]).to.have.property('firstname'); + expect(res.body.data[0]).to.have.property('lastname'); + expect(res.body.data[0]).to.have.property('accountnumber'); + expect(res.body.data[0]).to.have.property('createdon'); + expect(res.body.data[0]).to.have.property('owner'); + expect(res.body.data[0]).to.have.property('type'); + expect(res.body.data[0]).to.have.property('status'); + expect(res.body.data[0]).to.have.property('balance'); + }, + ); + it( 'should notify when account does not exist', async () => {