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 1 commit
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.
2. 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 = 'AIzaSyB9r-02V9oy8iluVuLweHx6IFi_VuUMXVg';
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @kwhinnery, I see that you removed this in 3dd6ca0. Are you okay with having this in the history or does this mean you need to change your key?


// 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
});
};
1 change: 1 addition & 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
33 changes: 32 additions & 1 deletion views/utilities.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
var axios = require('axios');
var db = require('./../models');
var config = require('../config');
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 @@ -243,6 +249,7 @@ 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 +265,36 @@ module.exports = {
serial: requestData.serial,
assigned_rc_region: requestData.assigned_rc_region,
status: 'new'
}).catch( function () {
}).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) {
console.log(JSON.stringify(geocodeResponse.data.results[0].geometry, null, 2));
var loc = geocodeResponse.data.results[0].geometry.location;
savedRequest.latitude = loc.latitude;
savedRequest.longitude = loc.longitude;
return savedRequest.save();
}).catch(function (error) {
console.log(error);
throw error;
/*
// 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
*/
});
},

Expand Down