Skip to content
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

Add lat/long to Request model, rework ID implementation #221

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 6 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ be edited here.
1. Do `cp config/config.json.tmpl config/config.json`. For dev, move on to step 3. For non-dev, edit the `config/config.json`:

* Fill in database usernames and passwords, and
the Mailgun.com API key and sender information that the app will use to send out email notifications. You can modify one of the existing top-level environments listed in `config.json` or set up a whole new environment, e.g., "demo" (e.g., based
on the "test" example).
the Mailgun.com API key and sender information that the app will use to send out email notifications.
You can modify one of the existing top-level environments listed in `config.json`
or set up a whole new environment, e.g., "demo" (e.g., based on the "test" example).

To enable Geocoding of entered addresses, make sure to configure your
Google Maps geocoding API key: `googleMapsApiKey`

(Don't worry if you don't know how to set up a database
username / password; that will be explained in a later step.)
Expand Down
3 changes: 3 additions & 0 deletions config.js.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ exports.systemEmail = '[email protected]';
// of the navigation for the admin section.
exports.signupEnabled = false;

// Configure Geocoding API API key
exports.googleMapsApiKey = 'YOUR_API_KEY_HERE';

// Make up your own key here. More background:
//
// I believe the cryptoKey is used to server-side sign session data
Expand Down
13 changes: 13 additions & 0 deletions migrations/20160806200731-add-lat-long-columns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

module.exports = {
up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Requests', 'latitude', Sequelize.DOUBLE);
queryInterface.addColumn('Requests', 'longitude', Sequelize.DOUBLE);
},

down: function (queryInterface, Sequelize) {
queryInterface.addColumn('Requests', 'latitude');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be dropColumn or some similar Sequelize method?

queryInterface.addColumn('Requests', 'longitude');
}
};
39 changes: 22 additions & 17 deletions models/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@
*/

module.exports = function(sequelize, DataTypes) {
return sequelize.define('Request', {
name: DataTypes.TEXT,
address: DataTypes.TEXT,
sms_raw_address: DataTypes.TEXT,
assigned_rc_region: DataTypes.TEXT,
city: DataTypes.TEXT,
state: DataTypes.TEXT,
zip: DataTypes.TEXT,
sms_raw_zip: DataTypes.TEXT,
phone: DataTypes.TEXT,
sms_raw_phone: DataTypes.TEXT,
email: DataTypes.TEXT,
source: DataTypes.TEXT,
serial: { type: DataTypes.TEXT, unique: true },
status: DataTypes.TEXT
})
}
return sequelize.define('Request', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, we prefer to minimize whitespace changes to make the diff easier to read. I see you just added the lat/long columns, though.

name: DataTypes.TEXT,
address: DataTypes.TEXT,
sms_raw_address: DataTypes.TEXT,
assigned_rc_region: DataTypes.TEXT,
city: DataTypes.TEXT,
state: DataTypes.TEXT,
zip: DataTypes.TEXT,
sms_raw_zip: DataTypes.TEXT,
phone: DataTypes.TEXT,
sms_raw_phone: DataTypes.TEXT,
email: DataTypes.TEXT,
source: DataTypes.TEXT,
serial: {
type: DataTypes.TEXT,
unique: true
},
status: DataTypes.TEXT,
latitude: DataTypes.DOUBLE,
longitude: DataTypes.DOUBLE
});
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"async": "^0.9.x",
"axios": "^0.13.1",
"backbone": "^1.x.x",
"bcrypt": "^0.8.x",
"body-parser": "~1.12.4",
Expand Down Expand Up @@ -49,6 +50,7 @@
"sequelize-cli": "^2.3.1",
"serve-favicon": "~2.2.1",
"serve-static": "^1.9.x",
"shortid": "^2.2.6",
"twilio": "^2.5.1",
"underscore": "^1.x.x"
},
Expand Down
45 changes: 38 additions & 7 deletions views/utilities.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
var axios = require('axios');
var shortid = require('shortid');
var config = require('../config');
var db = require('../models');
var requestData = {};

// Config for Google Maps Geocode API
var mapsApiUrl = 'https://maps.googleapis.com/maps/api/geocode/json';
var mapsApiKey = config.googleMapsApiKey;

var state_abbrevs =
{
"Alabama": "AL",
Expand Down Expand Up @@ -198,7 +205,7 @@ module.exports = {
// "region-date-00000." I think that would be confusing for users.
// We might as well start with 1.
numberOfRequests = numberOfRequests + 1;
var sequenceNumber = module.exports.padWithZeroes(numberOfRequests.toString(), 5);
var sequenceNumber = shortid.generate();
// find fiscal year
var fiscalYear = today.getFullYear();
//account for zero-indexing
Expand Down Expand Up @@ -243,6 +250,8 @@ module.exports = {
// check again here).

saveRequestData: function(requestData) {
var savedRequest;

return db.Request.create({
name: requestData.name,
source: requestData.is_sms,
Expand All @@ -258,12 +267,34 @@ module.exports = {
serial: requestData.serial,
assigned_rc_region: requestData.assigned_rc_region,
status: 'new'
}).catch( function () {
// uniqueness failed; increment serial
var serial_array = requestData.serial.split("-");
var new_serial = module.exports.padWithZeroes((parseInt(serial_array[2]) + 1).toString(), 5);
requestData.serial = serial_array[0] + "-" + serial_array[1] + "-" + new_serial;
return saveRequestData(requestData); //loop until save works
}).then(function(request) {
savedRequest = request;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to me and @kfogel: re-learn about passing vars through the Promise chain.

var query = [
request.address,
request.city,
request.state,
request.zip
].join(' ');
return axios(mapsApiUrl, {
params: {
key: mapsApiKey,
address: query
}
});
}).then(function(geocodeResponse) {
var loc;
try {
loc = geocodeResponse.data.results[0].geometry.location;
savedRequest.latitude = loc.lat;
savedRequest.longitude = loc.lng;
} catch (e) {
console.log('Location not found in response, NBD.');
}

return savedRequest.save();
}).catch(function (error) {
console.log('Error saving ' + error);
throw error;
});
},

Expand Down