Skip to content

Modified trips route and trip schema to reflect changes #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 70 additions & 83 deletions lib/controllers/trips.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@ var env = process.env.NODE_ENV || 'development',
_ = require('lodash'),
request = require('request'),
moment = require('moment'),
// set firebase root ref and child refs
root = new Firebase(config.firebase.rootRefUrl),
usersRef = root.child('users'),
tripsRef = root.child('trips'),
requestsRef = root.child('requests');
User = require('../models/users'),
Trip = require('../models/trips'),
Request = require('../models/requests'),
ObjectId = require('mongoose').Types.ObjectId;


function listAll(req, res) { // route controller to return all trips for a user
var trips,
uuid = req.params.uuid;

usersRef.child(uuid).once('value', function (snap) { // get the user from the database
if (!snap.val()) { // Return a 404 error if user doesn't exist
return res.status(404).json({ error: 'User does not exist!' });
}
User.findOne({ _id: uuid })
.where('active')
.equals(true)
.exec(function (err, user) {
if (err) { // Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.', error: err });
}

if (!user) { // Return a 404 if user is not found and exit.
return res.status(404).json({ message: 'User not found.' });
}

tripsRef.once('value', function (snap) { // get all the trips in the database
if (!snap.val()) { // Return a 404 error if there are no trips in the database
return res.status(404).json({ error: 'There are no trips available!' });
Trip.find({ uuid: new ObjectId(uuid) }) // Find the trips that has the supplied user id.
.exec(function (err, trips) {
if (err) { // Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.', error: err });
}

// Filter the trips by the user id to get all the trips by that user
trips = _.filter(snap.val(), { 'uuid': uuid });

if (!trips) { // If there are no trips for this user, return a 404 error
return res.status(404).json({ error: 'There are no trips for this user.' });
if (!trips.length) { // Return a 404 if user has no trips available and exit.
return res.status(404).json({ message: 'No trips available for this user.' });
}
// return a 200 with the trips for that user
return res.status(200).json({ response: trips });

// Return the trips object as a response if none of the above was true.
return res.status(200).json({ message: 'Successfully returned trips for this user.', response: trips });
});
});
}
Expand All @@ -49,17 +53,27 @@ function create(req, res) {

request_id = tripBody.request_id;

requestsRef.child(request_id).once('value', function (snap) {
if (!snap.val()) { return res.status(400).json({ error: 'This request could not be completed!\nRequest could not be found.' }); }

requestData = snap.val();
User.findOne({ _id: uuid })
.where('active')
.equals(true)
.exec(function (err, user) {
if (err) { // Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.', error: err });
}

if (requestData.uid !== uuid) { return res.status(400).json({ error: 'This request could not be completed.\nInvalid user.' }); }
if (!user) {
return res.status(400).json({ message: 'Invalid user.' });
}

usersRef.child(uuid).once('value', function (snap) {
if (!snap.val()) { return res.status(400).json({ error: 'This request could not be completed.\nThe user cannot be found!' }); }
Request.findOne({ _id: request_id })
.exec(function (err, requestData) {
if (err) { // Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.', error: err });
}

user = snap.val();
if (!requestData) {// Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.' });
}

params = {
url : config.uber.sandbox_base_url + 'requests',
Expand All @@ -70,50 +84,53 @@ function create(req, res) {
body: JSON.stringify(buildTripObject(requestData))
};

request.post(params, function (err, response, trip) {
if (err) { res.status(400).json({ error: err }); }
request.post(params, function (err, response, tripData) {
if (err) { return res.status(400).json({ message: 'Unable to complete this request. Please, try again later.', error: err }); }

trip = JSON.parse(trip);
if (!tripData) { return res.status(400).json({ error: 'This request could not be completed.\nUnable to create request from Uber API.' }); }

if (!trip) { return res.status(400).json({ error: 'This request could not be completed.\nUnable to create request from Uber API.' }); }
tripData = JSON.parse(tripData);

validateTripDetails(trip);
new Trip(tripData).save(function (err, trip) {
if (err) {
return res.status(400).json({ message: 'Unable to complete this request. Please, try again later.', error: err });
}

trip.created = moment().format();

tripsRef.child(request_id).set(trip, function (err) {
if (err) { return res.status(400).json({ error: err }); }

return res.json({ response: trip });
return res.status(200).json({ message: 'Successfully created trip.', response: trip });
});
});
});
});
}

function listOne(req, res) {
var tripData,
uuid = req.params.uuid,
var uuid = req.params.uuid,
trip_id = req.params.trip_id;

usersRef.child(uuid).once('value', function (snap) { // get the user from the database
if (!snap.val()) { // If the user doesn't exist, return a 404 error
return res.status(404).json({ error: 'This user does not exist.' });
User.findOne({ _id: uuid })
.where('active')
.equals(true)
.exec(function (err, user) {
if (err) { // Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.', error: err });
}

tripsRef.child(trip_id).once('value', function (snap) { // get a trip data using the trip id from the database
if (!snap.val()) { // If the trip doesn't exist, return a 404 error
return res.status(404).json({ error: 'The requested trip could not be found!' });
}
if (!user) { // Return a 404 if user is not found and exit.
return res.status(404).json({ message: 'User not found.' });
}

tripData = snap.val();
Trip.findOne({ _id: trip_id }) // Find the trip that has the supplied id.
.exec(function (err, trip) {
if (err) { // Return an error if computation was incomplete
return res.status(400).json({ message: 'An error occurred while processsing this request. Please, try again later.', error: err });
}

if (tripData.uid !== uuid) { // If the trip user id doesn't match the user id from the route, return a 404 error
return res.status(404).json({ error: 'There is a mismatch in the requested trip.' });
if (!trip) { // Return a 404 if the trip is not available and exit.
return res.status(404).json({ message: 'No trips available for this user.' });
}

// Return a 200 along with the trip data
return res.status(200).json({ response: tripData });
// Return the trip object as a response if none of the above was true.
return res.status(200).json({ message: 'Successfully returned trips for this user.', response: trip });
});
});
}
Expand All @@ -128,36 +145,6 @@ function buildTripObject(requestData) {
};
}

function validateTripDetails(trip) {
if (trip.driver === null) {
trip.driver = {
name : "",
phone_number : "",
picture_url : "",
rating : ""
};
}

if (trip.vehicle === null) {
trip.vehicle = {
make : "",
model : "",
picture_url : "",
license_plate : ""
};
}

if (trip.location === null) {
trip.location = {
bearing : "",
latitude : "",
longitude : ""
};
}

return trip;
}

module.exports = {
createOne : create,
listOne : listOne,
Expand Down
61 changes: 50 additions & 11 deletions lib/models/Trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,64 @@ tripSchema = new mongoose.Schema({
required: true,
index: true
},
'user': {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
'request_id': {
type: String,
required: true
},
'vehicle': {
'make': String,
'model': String,
'picture_url': String,
'plate': String
'make': {
type: String,
default: null
},
'model': {
type: String,
default: null
},
'picture_url': {
type: String,
default: null
},
'plate': {
type: String,
default: null
}
},
'location': {
'bearing': String,
'latitude': Number,
'longitude': Number
'bearing': {
type: String,
default: null
},
'latitude': {
type: Number,
default: null
},
'longitude': {
type: Number,
default: null
}
},
'driver': {
'name': String,
'phone': Number,
'picture_url': String,
'rating': Number
'name': {
type: String,
default: null
},
'phone': {
type: Number,
default: null
},
'picture_url': {
type: String,
default: null
},
'rating': {
type: Number,
default: null
}
},
'eta': Number,
'surge_multiplier': Number,
Expand All @@ -46,6 +84,7 @@ tripSchema.method('toJSON', function () {
id : this._id,
request_id : this.request_id,
request : this.request,
user : this.user,
vehicle : this.vehicle,
location : this.location,
driver : this.driver,
Expand Down