Skip to content

Commit

Permalink
Merge pull request #67 from Cavdy/ft-accounts-endpoints-to-database-1…
Browse files Browse the repository at this point in the history
…65436536

#165436536 accounts endpoints to database
  • Loading branch information
Cavdy committed Apr 18, 2019
2 parents 57e571e + 76a66ed commit a75962a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
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);
},

// 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;
Expand Down
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('/', 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);
Expand Down
14 changes: 14 additions & 0 deletions server/v1/services/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
30 changes: 30 additions & 0 deletions server/v1/test/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit a75962a

Please sign in to comment.