From d4daa518d9c5ee06e0f502e026049f947360c2eb Mon Sep 17 00:00:00 2001 From: Kevin Whinnery Date: Sun, 7 Aug 2016 11:04:41 -0500 Subject: [PATCH 1/2] initial WIP --- INSTALL.md | 8 +++- config.js.tmpl | 3 ++ ...=> 20150814140017-rename-region-column.js} | 0 .../20160806200731-add-lat-long-columns.js | 13 +++++++ models/request.js | 39 +++++++++++-------- package.json | 1 + views/utilities.js | 33 +++++++++++++++- 7 files changed, 77 insertions(+), 20 deletions(-) rename migrations/{20150814140017-unnamed-migration.js => 20150814140017-rename-region-column.js} (100%) create mode 100644 migrations/20160806200731-add-lat-long-columns.js diff --git a/INSTALL.md b/INSTALL.md index 2df4018..458fadc 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -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.) diff --git a/config.js.tmpl b/config.js.tmpl index 5a069d6..2829b50 100644 --- a/config.js.tmpl +++ b/config.js.tmpl @@ -28,6 +28,9 @@ exports.systemEmail = 'jrandom@example.com'; // of the navigation for the admin section. exports.signupEnabled = false; +// Configure Geocoding API API key +exports.googleMapsApiKey = 'AIzaSyB9r-02V9oy8iluVuLweHx6IFi_VuUMXVg'; + // Make up your own key here. More background: // // I believe the cryptoKey is used to server-side sign session data diff --git a/migrations/20150814140017-unnamed-migration.js b/migrations/20150814140017-rename-region-column.js similarity index 100% rename from migrations/20150814140017-unnamed-migration.js rename to migrations/20150814140017-rename-region-column.js diff --git a/migrations/20160806200731-add-lat-long-columns.js b/migrations/20160806200731-add-lat-long-columns.js new file mode 100644 index 0000000..5a62525 --- /dev/null +++ b/migrations/20160806200731-add-lat-long-columns.js @@ -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'); + queryInterface.addColumn('Requests', 'longitude'); + } +}; diff --git a/models/request.js b/models/request.js index b5ea13f..17f73c8 100644 --- a/models/request.js +++ b/models/request.js @@ -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', { + 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 + }); +}; diff --git a/package.json b/package.json index bda7a16..e633556 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/views/utilities.js b/views/utilities.js index a658337..8849f11 100644 --- a/views/utilities.js +++ b/views/utilities.js @@ -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", @@ -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, @@ -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; + 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 + */ }); }, From 3dd6ca0e270f3abb5b998aa34e5a0a6c287308b2 Mon Sep 17 00:00:00 2001 From: Kevin Whinnery Date: Sun, 7 Aug 2016 12:01:58 -0500 Subject: [PATCH 2/2] use placeholder value --- config.js.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.js.tmpl b/config.js.tmpl index 2829b50..cadddc3 100644 --- a/config.js.tmpl +++ b/config.js.tmpl @@ -29,7 +29,7 @@ exports.systemEmail = 'jrandom@example.com'; exports.signupEnabled = false; // Configure Geocoding API API key -exports.googleMapsApiKey = 'AIzaSyB9r-02V9oy8iluVuLweHx6IFi_VuUMXVg'; +exports.googleMapsApiKey = 'YOUR_API_KEY_HERE'; // Make up your own key here. More background: //