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 #271

Merged
merged 1 commit into from
Nov 5, 2018
Merged
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.removeColumn('Requests', 'latitude');
queryInterface.removeColumn('Requests', 'longitude');
}
};
4 changes: 3 additions & 1 deletion models/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
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
39 changes: 33 additions & 6 deletions views/utilities.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -276,6 +282,7 @@ module.exports = {
// check again here).

saveRequestData: function(requestData) {
var savedRequest;
return db.Request.create({
name: requestData.name,
source: requestData.source,
Expand All @@ -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;
});
},

Expand Down