-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(bus): add create bus feature
- add endpoint to create bus - write test to check for edge cases [Finishes #167195764]
- Loading branch information
Showing
8 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import moment from 'moment'; | ||
import db from './db'; | ||
import { | ||
handleServerError, | ||
handleServerResponse, | ||
handleServerResponseError, | ||
} from '../helpers/utils'; | ||
|
||
export default { | ||
async create(req, res) { | ||
const { | ||
// eslint-disable-next-line camelcase | ||
model, numberPlate, manufacturer, year, capacity | ||
} = req.body; | ||
try { | ||
const createQuery = `INSERT INTO | ||
Buses(model, number_plate, manufacturer, year, capacity, created_date, modified_date) | ||
VALUES($1, $2, $3, $4, $5, $6, $7) | ||
returning *`; | ||
const values = [ | ||
model.trim().toLowerCase(), | ||
numberPlate.trim().toLowerCase(), | ||
manufacturer.trim().toLowerCase(), | ||
year, | ||
capacity, | ||
moment(new Date()), | ||
moment(new Date()) | ||
]; | ||
const { rows } = await db.query(createQuery, values); | ||
return handleServerResponse(res, 201, { | ||
bus: rows[0] | ||
}); | ||
} catch (error) { | ||
if (error.routine === '_bt_check_unique') { | ||
return handleServerResponseError(res, 409, `Bus with number plate:- ${numberPlate.trim().toLowerCase()} already exists`); | ||
} | ||
handleServerError(res, error); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import express from 'express'; | ||
import Bus from '../controllers/Bus'; | ||
import ValidateInput from '../helpers/validateInput'; | ||
import { isAdmin, hasToken } from '../helpers/utils'; | ||
|
||
const { create } = Bus; | ||
const { validateCreateBus } = ValidateInput; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/', hasToken, isAdmin, validateCreateBus, create); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const bus = { | ||
model: 'Hiace', | ||
manufacturer: 'Toyota', | ||
year: '2012', | ||
numberPlate: 'fh3du5', | ||
capacity: 16 | ||
}; | ||
|
||
export const incompleteBus = { | ||
model: 'Hiace', | ||
manufacturer: 'Toyota', | ||
year: '2012', | ||
capacity: 16 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import chai from 'chai'; | ||
import api from '../test.config'; | ||
import { | ||
normalUser, adminUser | ||
} from '../__mocks__/auth.mocks'; | ||
import { bus } from '../__mocks__/bus.mocks'; | ||
|
||
const { expect } = chai; | ||
let adminToken, | ||
userToken; | ||
|
||
describe('Bus controller', () => { | ||
it('should login an admin', async () => { | ||
const server = await api.post('/api/v1/auth/signin') | ||
.type('form') | ||
.set('Content-Type', 'application/json') | ||
.send(adminUser); | ||
adminToken = server.body.data.token; | ||
expect(server.statusCode).to.equal(200); | ||
}); | ||
|
||
it('should create a new bus', async () => { | ||
const server = await api.post('/api/v1/bus') | ||
.type('form') | ||
.set('Content-Type', 'application/json') | ||
.set('x-access-token', adminToken) | ||
.send(bus); | ||
expect(server.statusCode).to.equal(201); | ||
}); | ||
|
||
it('should not create a new bus if token is not present', async () => { | ||
const server = await api.post('/api/v1/bus') | ||
.type('form') | ||
.set('Content-Type', 'application/json') | ||
.send(bus); | ||
expect(server.statusCode).to.equal(403); | ||
}); | ||
|
||
it('should signin a user', async () => { | ||
const server = await api.post('/api/v1/auth/signin') | ||
.type('form') | ||
.set('Content-Type', 'application/json') | ||
.send(normalUser); | ||
userToken = server.body.data.token; | ||
expect(server.statusCode).to.equal(200); | ||
}); | ||
|
||
it('should not create a new bus if user is not an admin', async () => { | ||
const server = await api.post('/api/v1/bus') | ||
.type('form') | ||
.set('Content-Type', 'application/json') | ||
.set('x-access-token', userToken) | ||
.send(bus); | ||
expect(server.statusCode).to.equal(403); | ||
}); | ||
|
||
it('should not create a new bus if plate number exists', async () => { | ||
const server = await api.post('/api/v1/bus') | ||
.type('form') | ||
.set('Content-Type', 'application/json') | ||
.set('x-access-token', adminToken) | ||
.send(bus); | ||
expect(server.statusCode).to.equal(409); | ||
}); | ||
}); |