<commit_before>'use strict';

var util = require('util');

var debug = require('../../debug');
var dbman = require('../../dbman');
var emailer = require('../../emailer');

var log = debug.getLogger({ prefix: '[route.jobsite]-  ' });
var jobsites = dbman.getCollection('jobsites');
var zipsCollection = dbman.getCollection('zips');

var ObjectId = dbman.getObjectId();

module.exports = {
    request: {
        get: function (req, res) {
            res.render('jobsite-request', {
                title: 'Job Site Request'
            });
        },

        post: function (req, res) {
            var record = req.body;
            var query = { _id: '1z2i3p4s5'};
            zipsCollection.findOne(query, function(err, zipDoc) {
                if(err || !zipDoc){
                    log('POST: Could not get zip codes');
                    res.send('Error', 400);
                } else {
                    // verify the zip code
                    var zipArray = zipDoc.zips;
                    if (zipArray.indexOf(record.zip) === -1) {
                        var zipString = zipArray.join(', ');
                        res.send('Currently accepted zip codes are: ' + zipString, 400);
                    } else {
                        log('POST: saving job site request data');
                        // insert record
                        var options = { w: 1 };
                        
                        record.formattedAddress = util.format('%s %s, %s %s',
                            record.address, record.city, record.state, record.zip);

                        jobsites.insert(record, options, function (err) {
                            if (err) {
                                log('POST: Error inserting record:\n\n%s\n\n', err);
                                res.send('Error', 400);
                            } else {
                                emailer.send({
                                    to: record.email,
                                    subject: 'Job Request Confirmation',
                                    template: 'jobsite-request',
                                    locals: { user: record }
                                }, function (err) {
                                    if (err) res.send('Error', 400);
                                    else res.send('ok', 200);
                                });
                            }
                        });
                    }
                }
            });
        },

        staff : {
            post: function(req, res){
                var record = req.body;
                var query = { _id: '1z2i3p4s5'};
                zipsCollection.findOne(query, function(err, zipDoc) {
                    if (err || !zipDoc) {
                        log('Could not get Zip Codes');
                        res.send('Staff', 400);
                    } else {
                        // verify the zip code
                        var zipArray = zipDoc.zips;
                        if (zipArray.indexOf(record.zip) === -1) {
                            var zipString = zipArray.join(', ');
                            res.send('Currently accepted zip codes are: ' + zipString, 400);
                        } else {
                            //insert record
                            var options = { w: 1 };
                            
                            record.formattedAddress = util.format('%s %s, %s %s',
                                record.address, record.city, record.state, record.zip);

                            jobsites.insert(record, options, function (err, records) {
                                if (err) {
                                    log('POST: Error inserting record:\n\n%s\n\n', err);
                                    res.send('Staff', 400);
                                } else {
                                    emailer.send({
                                        to: record.email,
                                        subject: 'Job Request Confirmation',
                                        template: 'jobsite-request',
                                        locals: { user: record }
                                    }, function (err) {
                                        if (err) res.send('Staff', 400);
                                        else res.send({id: records[0]._id}, 200);
                                    });
                                }
                            });
                        }
                    }
                });
            }
        },

        success: function (req, res) {
            res.render('hero-unit', {
                title: 'Jobsite Request Submitted',
                header: 'Jobsite Request Submitted',
                message: 'You should receive an e-mail confirmation verifying that your submission was successfully received.'
            });
        },

        failure: function (req, res) {
            res.render('hero-unit', {
                title: 'Submission Failed',
                header: 'Sorry!',
                message: 'There was a problem submitting your jobsite request. Please try again later.'
            });
        }
    },

    evaluation: {
        get: function (req, res) {
            var id = req.params.id;
            jobsites.findOne({_id: new ObjectId(id)}, function(err, record){
                if(err){
                    log('JOBSITE.EVALUATION.GET: Error getting jobsite: %s', err);
                    res.render('hero-unit', {<commit_msg>Refactoring jobsite route, adds geocoding<commit_after>'use strict';

var util = require('util');

var debug = require('../../debug');
var dbman = require('../../dbman');
var emailer = require('../../emailer');
var geocoder = require('../../geocoder');

var log = debug.getLogger({ prefix: '[route.jobsite]-  ' });
var jobsites = dbman.getCollection('jobsites');
var zipsCollection = dbman.getCollection('zips');

var ObjectId = dbman.getObjectId();

function addJobsite (req, res, callback) {
    var record = req.body;
    var query = { _id: '1z2i3p4s5'};
    zipsCollection.findOne(query, function(err, zipDoc) {
        if(err || !zipDoc){
            log('POST: Could not get zip codes');
            res.send('Error', 400);
        } else {
            // verify the zip code
            var zipArray = zipDoc.zips;
            if (zipArray.indexOf(record.zip) === -1) {
                var zipString = zipArray.join(', ');
                res.send('Currently accepted zip codes are: ' + zipString, 400);
            } else {
                log('POST: saving job site request data');
                // insert record
                var options = { w: 1 };
                
                var query = util.format('%s %s, %s %s',
                    record.address, record.city, record.state, record.zip);

                geocoder.send(query, function (response) {
                    var result = response.results[0];
                    record.location = result.geometry.location;
                    record.formattedAddress = result.formatted_address;

                    jobsites.insert(record, options, function (err, records) {
                        if (err) {
                            log('POST: Error inserting record:\n\n%s\n\n', err);
                            res.send('Error', 400);
                        } else {
                            if (typeof callback === 'function') callback(records[0]);
                        }
                    });
                });
            }
        }
    });
}

module.exports = {
    request: {
        get: function (req, res) {
            res.render('jobsite-request', {
                title: 'Job Site Request'
            });
        },

        post: function (req, res) {
            addJobsite(req, res, function (record) {
                emailer.send({
                    to: record.email,
                    subject: 'Job Request Confirmation',
                    template: 'jobsite-request',
                    locals: { user: record }
                }, function (err) {
                    if (err) res.send('Error', 400);
                    else res.send('ok', 200);
                });
            });
        },

        staff: {
            post: function(req, res) {
                addJobsite(req, res, function (record) {
                    emailer.send({
                        to: record.email,
                        subject: 'Job Request Confirmation',
                        template: 'jobsite-request',
                        locals: { user: record }
                    }, function (err) {
                        if (err) res.send('Staff', 400);
                        else res.send({ id: record._id }, 200);
                    });
                });
            }
        },

        success: function (req, res) {
            res.render('hero-unit', {
                title: 'Jobsite Request Submitted',
                header: 'Jobsite Request Submitted',
                message: 'You should receive an e-mail confirmation verifying that your submission was successfully received.'
            });
        },

        failure: function (req, res) {
            res.render('hero-unit', {
                title: 'Submission Failed',
                header: 'Sorry!',
                message: 'There was a problem submitting your jobsite request. Please try again later.'
            });
        }
    },

    evaluation: {
        get: function (req, res) {
            var id = req.params.id;
            jobsites.findOne({_id: new ObjectId(id)}, function(err, record){
                if(err){
                    log('JOBSITE.EVALUATION.GET: Error getting jobsite: %s', err);
                    res.render('hero-unit', {
{'fraction_non_alphanumeric': 0.1168553074334659, 'fraction_numerical': 0.007647598654022637, 'mean_word_length': 1.0609371716747216, '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': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5085686', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2286, 'n_words': 586}