Skip to content

Commit

Permalink
#13 - Added endpoint to query one ride
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascvas committed Aug 27, 2018
1 parent df4ab4e commit 7156fb4
Show file tree
Hide file tree
Showing 21 changed files with 275 additions and 86 deletions.
4 changes: 3 additions & 1 deletion backend/src/main/controller/awsLambdaApis.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const CreateRideService = require('../rides/CreateRideService');
const ListRidesService = require('../rides/ListRidesService');
const FindOneRideService = require('../rides/FindOneRideService');
const DatabaseManager = require('../database/DatabaseManager');
const AwsLambdaRideApis = require('../rides/aws/AwsLambdaRideApis');
const databaseManager = new DatabaseManager();

const createRideService = new CreateRideService(databaseManager);
const listRidesService = new ListRidesService(databaseManager);
const findOneRideService = new FindOneRideService(databaseManager);

const rides = new AwsLambdaRideApis(createRideService, listRidesService);
const rides = new AwsLambdaRideApis(createRideService, listRidesService, findOneRideService);

module.exports = {
rides: rides
Expand Down
45 changes: 0 additions & 45 deletions backend/src/main/controller/expressApis.js

This file was deleted.

37 changes: 37 additions & 0 deletions backend/src/main/rides/FindOneRideService.js
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;
3 changes: 3 additions & 0 deletions backend/src/main/rides/RideRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class RideRepository {
if(!jsonQuery.includePickupTimeInPast){
where.push('pickupTimeAndDateInUTC >= NOW()')
}
if(jsonQuery.id){
where.push(`id = ${jsonQuery.id}`)
}
if(jsonQuery.driverGenders && jsonQuery.driverGenders.length){
let genders = jsonQuery.driverGenders.map(g => ` driverGender = '${g}'`).join(' or ');
where.push(genders.length === 1 ? genders : `(${genders})`)
Expand Down
12 changes: 11 additions & 1 deletion backend/src/main/rides/aws/AwsLambdaRideApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const decodeJwt = require('../../utils/jwt').decodeJwt;

class AwsLambdaRideApis {
constructor(createRideService,
listRidesService) {
listRidesService,
findOneRideService) {
this.createRideService = createRideService;
this.listRidesService = listRidesService;
this.findOneRideService = findOneRideService;
}

create(event, context, callback) {
Expand All @@ -21,6 +23,14 @@ class AwsLambdaRideApis {
.then(result => callback(null, result))
.catch(result => callback(result));
}

findOne(event, context, callback) {
let loginData = decodeJwt(event);
let pathParams = event.pathParameters || {};
return this.findOneRideService.findOne(pathParams.id, loginData)
.then(result => callback(null, result))
.catch(result => callback(result));
}
}

module.exports = AwsLambdaRideApis;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
50 changes: 50 additions & 0 deletions backend/src/test/expressApis.js
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")
});
10 changes: 5 additions & 5 deletions backend/src/test/rides/CreateRide.integration.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DatabaseManager = require('../../main/database/DatabaseManager');
const CreateRideController = require('../../main/rides/CreateRideService');
const FindRideTestRepository = require('./FindRideTestRepository');
const CreateRideService = require('../../main/rides/CreateRideService');
const FindRideTestRepository = require('./RideTestRepository');
const databaseTestConfig = require('../database/databaseTestConfig');
const RandomUtils = require('../RandomUtils');
const moment = require('moment');
Expand All @@ -9,7 +9,7 @@ const chaiExclude = require('chai-exclude');
const assert = chai.assert;
chai.use(chaiExclude);

let createRideController;
let createRideService;
let rideRequest;
let expectedRide;
let databaseManager;
Expand All @@ -33,7 +33,7 @@ after(async () => {
beforeEach(async () => {
await databaseManager.beginTransaction(connection);

createRideController = new CreateRideController(mockDatabaseManager);
createRideService = new CreateRideService(mockDatabaseManager);
findRideTestRepository = new FindRideTestRepository(databaseManager);
});

Expand Down Expand Up @@ -92,7 +92,7 @@ beforeEach(function setupData() {

describe('SQL', function () {
it('should insert and retrieve ride', async function () {
await createRideController.createRide(rideRequest, loginData);
await createRideService.createRide(rideRequest, loginData);
let storedRide = await findRideTestRepository.findOneByClientEmail(rideRequest.client, connection);

assert.deepEqualExcluding(storedRide, expectedRide, 'id', 'datetime');
Expand Down
126 changes: 126 additions & 0 deletions backend/src/test/rides/FindOneRide.integration.test.js
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;
}
});



2 changes: 1 addition & 1 deletion backend/src/test/rides/ListRides.integration.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DatabaseManager = require('../../main/database/DatabaseManager');
const ListRidesController = require('../../main/rides/ListRidesService');
const FindRideTestRepository = require('./FindRideTestRepository');
const FindRideTestRepository = require('./RideTestRepository');
const databaseTestConfig = require('../database/databaseTestConfig');
const RideEntityBuilder = require('./RideEntityBuilder');
const RideRepository = require('../../main/rides/RideRepository');
Expand Down
Loading

0 comments on commit 7156fb4

Please sign in to comment.