From cd9dd23cbf6cfb5324f07c0b12d4e396ac8047b8 Mon Sep 17 00:00:00 2001 From: Kevin Whinnery Date: Sun, 7 Aug 2016 11:04:41 -0500 Subject: [PATCH] Loopup and add lat/long to the database for reqs. Added a lookup on google maps API for requests coming in, which requires an API key to fulfill. Also fixes an infinite loop and changes the id creation mechanism. --- INSTALL.md | 8 +++- config.js.tmpl | 3 ++ ...=> 20150814140017-rename-region-column.js} | 0 .../20160806200731-add-lat-long-columns.js | 13 +++++++ models/request.js | 4 +- package.json | 1 + views/utilities.js | 39 ++++++++++++++++--- 7 files changed, 59 insertions(+), 9 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 c3e50e7..ab58451 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -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.) diff --git a/config.js.tmpl b/config.js.tmpl index 5a069d6..cadddc3 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 = 'YOUR_API_KEY_HERE'; + // 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..56ea4a2 --- /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.removeColumn('Requests', 'latitude'); + queryInterface.removeColumn('Requests', 'longitude'); + } +}; diff --git a/models/request.js b/models/request.js index b2ea810..9b96a60 100644 --- a/models/request.js +++ b/models/request.js @@ -31,7 +31,9 @@ module.exports = function(sequelize, DataTypes) { email: DataTypes.TEXT, source: DataTypes.TEXT, serial: { type: DataTypes.TEXT, unique: true }, - status: DataTypes.TEXT + status: DataTypes.TEXT, + latitude: DataTypes.DOUBLE, + longitude: DataTypes.DOUBLE }, { classMethods: { associate: function(models) { 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 ac0468f..f4b381d 100644 --- a/views/utilities.js +++ b/views/utilities.js @@ -1,6 +1,12 @@ var db = require('../models'); +var axios = require('axios'); +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", @@ -276,6 +282,7 @@ module.exports = { // check again here). saveRequestData: function(requestData) { + var savedRequest; return db.Request.create({ name: requestData.name, source: requestData.source, @@ -291,12 +298,32 @@ 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; + var query = [ + request.address, + request.city, + request.state, + request.zip + ].join(' '); + return axios(mapsApiUrl, { + params: { + key: mapsApiKey, + address: query + } + }); + }).then(function(geocodeResponse) { + try { + var 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); + throw error; }); },