-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#13 - Added endpoint to query one ride
- Loading branch information
1 parent
df4ab4e
commit 7156fb4
Showing
21 changed files
with
275 additions
and
86 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 was deleted.
Oops, something went wrong.
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,37 @@ | ||
'use strict'; | ||
|
||
const RideRepository = require('./RideRepository'); | ||
|
||
class FindOneRideService { | ||
|
||
constructor(databaseManager) { | ||
this._databaseManager = databaseManager; | ||
this._rideRepository = new RideRepository(databaseManager); | ||
} | ||
|
||
findOne(id, loginData) { | ||
if(loginData.role === 'driver'){ | ||
return Promise.resolve(null); | ||
} | ||
|
||
const connection = this._databaseManager.createConnection(); | ||
return this._findOne(id, loginData, connection) | ||
.finally(() => this._databaseManager.closeConnection(connection)); | ||
} | ||
|
||
_findOne(id, loginData, connection) { | ||
let jsonQuery = this._createQuery(id, loginData); | ||
return this._rideRepository.list(jsonQuery, connection) | ||
.then(items => items[0] || null); | ||
} | ||
|
||
_createQuery(id, loginData) { | ||
return { | ||
id: id, | ||
facilitatorEmail: loginData.role === 'facilitator' ? loginData.email : undefined, | ||
includePickupTimeInPast: true | ||
}; | ||
} | ||
} | ||
|
||
module.exports = FindOneRideService; |
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 |
---|---|---|
|
@@ -86,30 +86,17 @@ class ExpressAuthApis { | |
} | ||
|
||
_getDriver(uri){ | ||
let data = { | ||
"email": "[email protected]", | ||
"gender": "male", | ||
"car": "suv", | ||
"role": "driver" | ||
}; | ||
let data = require('../users/driver.json'); | ||
return uri ? this._uriBased(data, uri) : data; | ||
} | ||
|
||
_getAdmin(uri){ | ||
let data = { | ||
"email": "[email protected]", | ||
"gender": "male", | ||
"role": "admin" | ||
}; | ||
let data = require('../users/admin.json'); | ||
return uri ? this._uriBased(data, uri) : data; | ||
} | ||
|
||
_getFacilitator(uri){ | ||
let data = { | ||
"email": "[email protected]", | ||
"gender": "female", | ||
"role": "facilitator" | ||
}; | ||
let data = require('../users/facilitator.json'); | ||
return uri ? this._uriBased(data, uri) : data; | ||
} | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,50 @@ | ||
const express = require('express'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const CreateRideService = require('../main/rides/CreateRideService'); | ||
const ListRidesService = require('../main/rides/ListRidesService'); | ||
const FindOneRideService = require('../main/rides/FindOneRideService'); | ||
const DatabaseManager = require('../main/database/DatabaseManager'); | ||
const ExpressRideApis = require('./rides/express/ExpressRidesApis'); | ||
const ExpressAuthApis = require('./auth/ExpressAuthApis'); | ||
const AwsLambdaRideApis = require('../main/rides/aws/AwsLambdaRideApis'); | ||
const bodyParser = require('body-parser'); | ||
const databaseManager = new DatabaseManager(); | ||
|
||
const https = require('https'); | ||
const http = require('http'); | ||
|
||
process.on('uncaughtException', function (err) { | ||
console.log(err); | ||
}); | ||
|
||
process.env.DOMAIN = 'localhost:8081'; | ||
|
||
const createRideService = new CreateRideService(databaseManager); | ||
const listRidesService = new ListRidesService(databaseManager); | ||
const findOneRideService = new FindOneRideService(databaseManager); | ||
const app = express(); | ||
app.use(bodyParser.json()); // for parsing application/json | ||
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded | ||
|
||
app.use(function (req, res, next) { | ||
res.header("Access-Control-Allow-Origin", "*"); | ||
res.header("Access-Control-Allow-Headers", "*"); | ||
next(); | ||
}); | ||
|
||
new ExpressAuthApis(app); | ||
let awsLambdaRideApis = new AwsLambdaRideApis(createRideService, listRidesService, findOneRideService); | ||
new ExpressRideApis(app, awsLambdaRideApis); | ||
|
||
const options = { | ||
key: fs.readFileSync(path.resolve(__dirname, './config/express/certs/key.pem')), | ||
cert: fs.readFileSync(path.resolve(__dirname, './config/express/certs/certificate.pem')) | ||
}; | ||
|
||
http.createServer(app).listen(8080, () => { | ||
console.log("HTTP Server started and listening on port 8080") | ||
}); | ||
https.createServer(options, app).listen(8081, () => { | ||
console.log("HTTPS Server started and listening on port 8081") | ||
}); |
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,126 @@ | ||
const DatabaseManager = require('../../main/database/DatabaseManager'); | ||
const FindOneRideService = require('../../main/rides/FindOneRideService'); | ||
const RideTestRepository = require('./RideTestRepository'); | ||
const databaseTestConfig = require('../database/databaseTestConfig'); | ||
const RideEntityBuilder = require('./RideEntityBuilder'); | ||
const RideRepository = require('../../main/rides/RideRepository'); | ||
const RandomUtils = require('../RandomUtils'); | ||
|
||
let findOneRideService; | ||
let databaseManager; | ||
let mockDatabaseManager; | ||
let rideRepository; | ||
let connection; | ||
let rideTestRepository; | ||
|
||
const chai = require('chai'); | ||
const chaiExclude = require('chai-exclude'); | ||
const assert = chai.assert; | ||
chai.use(chaiExclude); | ||
|
||
before(async () => { | ||
databaseManager = new DatabaseManager(databaseTestConfig); | ||
mockDatabaseManager = new DatabaseManager(databaseTestConfig); | ||
connection = databaseManager.createConnection() ; | ||
mockDatabaseManager.createConnection = () => connection; | ||
mockDatabaseManager.closeConnection = () => Promise.resolve(null); | ||
}); | ||
|
||
after(async () => { | ||
await databaseManager.closeConnection(connection); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await databaseManager.beginTransaction(connection); | ||
|
||
findOneRideService = new FindOneRideService(mockDatabaseManager); | ||
rideTestRepository = new RideTestRepository(mockDatabaseManager); | ||
rideRepository = new RideRepository(databaseManager); | ||
}); | ||
|
||
afterEach(async () => { | ||
await databaseManager.rollback(connection); | ||
}); | ||
|
||
describe('When find one ride', async () => { | ||
it('should show single ride that was created by facilitator', async function () { | ||
// given | ||
const loginData = {email: RandomUtils.randomEmail(), role: 'facilitator'}; | ||
const email = loginData.email; | ||
const ride = randomRideWithFacilitator(email); | ||
const rideEntity = await databaseContainsRide(ride); | ||
|
||
// when | ||
const storedRide = await findOneRideService.findOne(rideEntity.id, loginData); | ||
|
||
// then | ||
assert.deepEqualExcluding(transformRideToDbFormat(ride), storedRide, 'id'); | ||
}); | ||
|
||
it('should NOT show ride that was created by other facilitator', async function () { | ||
// given | ||
const loginData = {email: RandomUtils.randomEmail(), role: 'facilitator'}; | ||
const ride = randomRideWithFacilitator(RandomUtils.randomEmail()); | ||
const rideEntity = await databaseContainsRide(ride); | ||
|
||
// when | ||
const storedRide = await findOneRideService.findOne(rideEntity.id, loginData); | ||
|
||
// then | ||
assert.isNull(storedRide); | ||
}); | ||
|
||
it('should show single ride when user is admin', async function () { | ||
// given | ||
const loginData = {email: RandomUtils.randomEmail(), role: 'admin'}; | ||
const ride = randomRideWithFacilitator(RandomUtils.randomEmail()); | ||
const rideEntity = await databaseContainsRide(ride); | ||
|
||
// when | ||
const storedRide = await findOneRideService.findOne(rideEntity.id, loginData); | ||
|
||
// then | ||
assert.deepEqualExcluding(transformRideToDbFormat(ride), storedRide, 'id'); | ||
}); | ||
|
||
it('should not show single ride when user is driver', async function () { | ||
// given | ||
const loginData = {email: RandomUtils.randomEmail(), role: 'driver'}; | ||
const ride = randomRideWithFacilitator(RandomUtils.randomEmail()); | ||
const rideEntity = await databaseContainsRide(ride); | ||
|
||
// when | ||
const storedRide = await findOneRideService.findOne(rideEntity.id, loginData); | ||
|
||
// then | ||
assert.isNull(storedRide); | ||
}); | ||
|
||
function transformRideToDbFormat(ride){ | ||
ride.locationFrom.x = ride.locationFrom.latitude; | ||
ride.locationFrom.y = ride.locationFrom.longitude; | ||
ride.locationTo.x = ride.locationTo.latitude; | ||
ride.locationTo.y = ride.locationTo.longitude; | ||
ride.status = ride.status.statusName; | ||
delete ride.locationFrom.latitude; | ||
delete ride.locationFrom.longitude; | ||
delete ride.locationTo.latitude; | ||
delete ride.locationTo.longitude; | ||
return ride; | ||
} | ||
|
||
async function databaseContainsRide(ride) { | ||
await rideRepository.create(ride, connection); | ||
let rideEntity = rideTestRepository.findOneByClientEmail(ride.client); | ||
return rideEntity; | ||
} | ||
|
||
function randomRideWithFacilitator(facilitatorEmail) { | ||
const ride = RideEntityBuilder.randomRide(); | ||
ride.facilitatorEmail = facilitatorEmail; | ||
return ride; | ||
} | ||
}); | ||
|
||
|
||
|
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
Oops, something went wrong.