From c0d04bb448adcab12a84b57cb2a253a9b303da1a Mon Sep 17 00:00:00 2001 From: Ladi Adenusi andela-ladenusi Date: Tue, 2 Aug 2016 12:01:48 +0100 Subject: [PATCH] Modified trips route and trip schema to reflect changes --- lib/controllers/trips.js | 153 ++++++++++++++++++--------------------- lib/models/Trip.js | 61 +++++++++++++--- 2 files changed, 120 insertions(+), 94 deletions(-) diff --git a/lib/controllers/trips.js b/lib/controllers/trips.js index b1d8bfd..3a44864 100644 --- a/lib/controllers/trips.js +++ b/lib/controllers/trips.js @@ -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 }); }); }); } @@ -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', @@ -70,21 +84,19 @@ 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 }); }); }); }); @@ -92,28 +104,33 @@ function create(req, res) { } 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 }); }); }); } @@ -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, diff --git a/lib/models/Trip.js b/lib/models/Trip.js index 729fb00..f6c07b5 100644 --- a/lib/models/Trip.js +++ b/lib/models/Trip.js @@ -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, @@ -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,