<commit_before>var env = process.env.NODE_ENV || 'development',
    config = require('../../config/config')[env],
    Firebase = require('firebase'),
    geolocator = require('../util/geolocator'),
    request = require('request'),
    moment = require('moment'),
    User = require('../models/User'),
    Request = require('../models/Request'),
    Trip = require('../models/Trip'),
    ObjectId = require('mongoose').Types.ObjectId,
    // 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');

function listAll(req, res) {
    var id, requests,
        uuid = req.params.uuid,
        userRequests = [];

    // return requests for a particular user
    User.findOne({ _id: uuid })
    .where('active')
    .equals(true)
    .exec(function (err, user) {
        if (err) {
            return res.status(400).json({ message: 'An error occured with this request. Please try again later.', error: err });
        }

        if (!user) {
            return res.status(404).json({ message: 'User not found.' });
        }

        Request.find({ user: new ObjectId(uuid) })
        .where('active')
        .equals(true)
        .exec(function (err, requests) {
            if (err) {
                return res.status(400).json({ message: 'An error occured with this request. Please try again later.', error: err });
            }

            if (!requests.length) {
                return res.status(404).json({ message: 'This user has no requests available.' });                
            }

            return res.status(200).json({ message: 'Successfully retrieved requests for this user.', response: requests });
        });
    });
}

function createRequest(req, res) {
    var i, user, params, product, products,
        uid = req.params.uuid,
        data = req.body;

    if (!data || !data.destination || !data.startTime || !data.productType || !data.location) {
        res.status(400).json({ error: 'Invalid request!' });
    }

    geolocator(data.location, function (err, coords) {
        if (err) {
            return res.status(400).json({ error: err });
        }

        data.location = {
            address     : coords.results[0].formatted_address,
            latitude    : coords.results[0].geometry.location.lat,
            longitude   : coords.results[0].geometry.location.lng
        };

        usersRef.child(uid).once('value', function (snap) {
            if (!snap.val()) { return res.status(400).json({ error: 'Invalid user id' }); }

            data.uid = uid;

            user = snap.val();
            params = {
                url: config.uber.base_uber_url + 'products',
                qs: {
                    server_token    : config.uber.server_token,
                    latitude        : data.location.latitude,
                    longitude       : data.location.longitude
                }
            };

            request.get(params, function (err, status, body) {
                if (err) { return res.status(400).json({ error: err }); }

                body = JSON.parse(body);

                products = body.products;

                for (i in products) {
                    if (products[i].display_name == data.productType) {
                        product = products[i];
                    }
                }

                data.product = {
                    id: product.product_id,
                    type: data.productType
                };

                delete data.productType;
                
                geolocator(data.destination, function (err, coords) {
                    if (err) { return res.status(400).json({ error: 'Unable to calculate co-ordinates for your destination address!' }); }

                    data.destination = {
                        address     : coords.results[0].formatted_address,
                        latitude    : coords.results[0].geometry.location.lat,
                        longitude   : coords.results[0].geometry.location.lng
                    };

                    // Set the params object for making a call to Uber's Estimate endpoint
                    params.url = config.uber.base_uber_url + 'requests/estimate';
                    params.headers = {
                        'Authorization': 'Bearer ' + user.accessToken,
                        'Content-Type': 'application/json'
                    };

                    params.body = JSON.stringify({
                        product_id          : data.product.id,
                        start_latitude      : data.location.latitude,
                        start_longitude     : data.location.longitude,
                        end_latitude        : data.destination.latitude,
                        end_longitude       : data.destination.longitude
                    });

                    params.qs = {};

                        // Get the estimated duration for the requested trip
                    request.post(params, function (err, status, body)  {
                        if (err) { res.status(400).json({ error: err }); }

                        if (!body) {
                            return res.status(400).json({ error: 'Unable to calculate estimates! Please, try again later!' });
                        }

                        body = JSON.parse(body);
                        // console.log('\n\nPushing to firebase...\n--------------------------\n', body);

                        if (!body.trip || !body.pickup_estimate) {
                            return res.status(400).json({ error: 'Unable to get estimates! Please, try again later!' });
                        }

                        var duration_estimate = body.trip.duration_estimate,
                            pickup_estimate = body.pickup_estimate,
                            time = moment(data.startTime).subtract(15, 'minutes').format(),
                            reminder = moment(time).subtract(duration_estimate, 'seconds').format();

                        data.estimates = {
                            duration: duration_estimate,
                            reminder: reminder
                        };

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

                        var newRequestRef = requestsRef.push();
                        var request_id = newRequestRef.key();

                        newRequestRef.set(data, function (err) {
                            if (err) {
                                return res.status(400).json({ message: 'Unable to save data to firebase!', error: err });
                            }

                            data.id = request_id;
                            
                            return res.json({ response: data });
                        });
                    });
                });
            });
        });
    });
}
    
function listOne(req, res) {
    var user, userTrips, trip, requestData,
        uuid = req.params.uuid,
        id = req.params.req_id,
        params = {};

    // Find the user from the database with the user's id
    usersRef.child(uuid).once('value', function (snap) {
        if (!snap.val()) { // If user doesn't exist, return a 401 error
            return res.status(401).json({ error: 'You are not authorized!' });
        }

        // If user exists, assign the object to the user` variable
        user = snap.val();

        // Then, find the request with the request id
        requestsRef.child(id).once('value', function (snap) {
            if (!snap.val()) {
                // If the request doesn't exist, return a 404 error
                return res.status(404).json({ error: 'Request not found!' });
            }

            requestData = snap.val();

            // Check if the request has details for a trip
            tripsRef.child(uuid).once('value', function (snap) {
                if (!snap.val()) { // If it doesn't have a trip, send the requestData object
                    return res.json({ response: requestData });
                }
                
                // If the request has a trip, add it to the requestData object
                userTrips = snap.val();
                trip = userTrips[id];

                if (!trip) { return res.json({ response: requestData }); }
                
                if (trip.status === 'accepted') {
                    requestData.trip = trip;<commit_msg>Updated single request route to replace firebase<commit_after>var env = process.env.NODE_ENV || 'development',
    config = require('../../config/config')[env],
    Firebase = require('firebase'),
    geolocator = require('../util/geolocator'),
    request = require('request'),
    moment = require('moment'),
    User = require('../models/User'),
    Request = require('../models/Request'),
    Trip = require('../models/Trip'),
    ObjectId = require('mongoose').Types.ObjectId,
    // 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');

function listAll(req, res) {
    var id, requests,
        uuid = req.params.uuid,
        userRequests = [];

    // return requests for a particular user
    User.findOne({ _id: uuid })
    .where('active')
    .equals(true)
    .exec(function (err, user) {
        if (err) {
            return res.status(400).json({ message: 'An error occured with this request. Please try again later.', error: err });
        }

        if (!user) {
            return res.status(404).json({ message: 'User not found.' });
        }

        Request.find({ user: new ObjectId(uuid) })
        .where('active')
        .equals(true)
        .exec(function (err, requests) {
            if (err) {
                return res.status(400).json({ message: 'An error occured with this request. Please try again later.', error: err });
            }

            if (!requests.length) {
                return res.status(404).json({ message: 'This user has no requests available.' });                
            }

            return res.status(200).json({ message: 'Successfully retrieved requests for this user.', response: requests });
        });
    });
}

function createRequest(req, res) {
    var i, user, params, product, products,
        uid = req.params.uuid,
        data = req.body;

    if (!data || !data.destination || !data.startTime || !data.productType || !data.location) {
        res.status(400).json({ error: 'Invalid request!' });
    }

    geolocator(data.location, function (err, coords) {
        if (err) {
            return res.status(400).json({ error: err });
        }

        data.location = {
            address     : coords.results[0].formatted_address,
            latitude    : coords.results[0].geometry.location.lat,
            longitude   : coords.results[0].geometry.location.lng
        };

        usersRef.child(uid).once('value', function (snap) {
            if (!snap.val()) { return res.status(400).json({ error: 'Invalid user id' }); }

            data.uid = uid;

            user = snap.val();
            params = {
                url: config.uber.base_uber_url + 'products',
                qs: {
                    server_token    : config.uber.server_token,
                    latitude        : data.location.latitude,
                    longitude       : data.location.longitude
                }
            };

            request.get(params, function (err, status, body) {
                if (err) { return res.status(400).json({ error: err }); }

                body = JSON.parse(body);

                products = body.products;

                for (i in products) {
                    if (products[i].display_name == data.productType) {
                        product = products[i];
                    }
                }

                data.product = {
                    id: product.product_id,
                    type: data.productType
                };

                delete data.productType;
                
                geolocator(data.destination, function (err, coords) {
                    if (err) { return res.status(400).json({ error: 'Unable to calculate co-ordinates for your destination address!' }); }

                    data.destination = {
                        address     : coords.results[0].formatted_address,
                        latitude    : coords.results[0].geometry.location.lat,
                        longitude   : coords.results[0].geometry.location.lng
                    };

                    // Set the params object for making a call to Uber's Estimate endpoint
                    params.url = config.uber.base_uber_url + 'requests/estimate';
                    params.headers = {
                        'Authorization': 'Bearer ' + user.accessToken,
                        'Content-Type': 'application/json'
                    };

                    params.body = JSON.stringify({
                        product_id          : data.product.id,
                        start_latitude      : data.location.latitude,
                        start_longitude     : data.location.longitude,
                        end_latitude        : data.destination.latitude,
                        end_longitude       : data.destination.longitude
                    });

                    params.qs = {};

                        // Get the estimated duration for the requested trip
                    request.post(params, function (err, status, body)  {
                        if (err) { res.status(400).json({ error: err }); }

                        if (!body) {
                            return res.status(400).json({ error: 'Unable to calculate estimates! Please, try again later!' });
                        }

                        body = JSON.parse(body);
                        // console.log('\n\nPushing to firebase...\n--------------------------\n', body);

                        if (!body.trip || !body.pickup_estimate) {
                            return res.status(400).json({ error: 'Unable to get estimates! Please, try again later!' });
                        }

                        var duration_estimate = body.trip.duration_estimate,
                            pickup_estimate = body.pickup_estimate,
                            time = moment(data.startTime).subtract(15, 'minutes').format(),
                            reminder = moment(time).subtract(duration_estimate, 'seconds').format();

                        data.estimates = {
                            duration: duration_estimate,
                            reminder: reminder
                        };

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

                        var newRequestRef = requestsRef.push();
                        var request_id = newRequestRef.key();

                        newRequestRef.set(data, function (err) {
                            if (err) {
                                return res.status(400).json({ message: 'Unable to save data to firebase!', error: err });
                            }

                            data.id = request_id;
                            
                            return res.json({ response: data });
                        });
                    });
                });
            });
        });
    });
}
    
function listOne(req, res) {
    var user, userTrips, trip, requestData,
        uuid = req.params.uuid,
        id = req.params.req_id,
        params = {};

    // Find the user from the database with the user's id
    User.findOne({ _id: uuid })
    .where('active')
    .equals(true)
    .exec(function (err, user) {
        if (err) {
            return res.status(400).json({ message: 'An error occured with this request. Please, try again later.', error: err });
        }

        if (!user) {
            return res.status(404).json({ message: 'This user is not available.' });
        }

        Request.findOne({ _id: id })
        .exec(function (err, request_data) {
            if (err) {
                return res.status(400).json({ message: 'An error occured with this request. Please, try again later.', error: err });
            }

            if (!request_data) {
                return res.status(404).json({ message: 'Request not found.' });
            }
        });
    });
    usersRef.child(uuid).once('value', function (snap) {
        if (!snap.val()) { // If user doesn't exist, return a 401 error
            return res.status(401).json({ error: 'You are not authorized!' });
        }

        // If user exists, assign the object to the user` variable
        user = snap.val();

        // Then, find the request with the request id
        requestsRef.child(id).once('value', function (snap) {
            if (!snap.val()) {
                // If the request doesn't exist, return a 404 error
                return res.status(404).json({ error: 'Request not found!' });
            }

            requestData = snap.val();

            // Check if the request has details for a trip
            tripsRef.child(uuid).once('value', function (snap) {
                if (!snap.val()) { // If it doesn't have a trip, send the requestData object
                    return res.json({ response: requestData });
                }
                
                // If the request has a trip, add it to the requestData object
                userTrips = snap.val();
                trip = userTrips[id];

                if (!trip) { return res.json({ response: requestData }); }
                
                if (trip.status === 'accepted') {
                    requestData.trip = trip;
{'fraction_non_alphanumeric': 0.12119854382525903, 'fraction_numerical': 0.007616914029683562, 'mean_word_length': 1.3788968824940049, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '939827', 'n_tokens_mistral': 4649, 'n_tokens_neox': 4323, 'n_words': 1123}