-
Notifications
You must be signed in to change notification settings - Fork 20
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
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'); | ||
queryInterface.addColumn('Requests', 'longitude'); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
}); | ||
}; |
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", | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
}, | ||
|
||
|
There was a problem hiding this comment.
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?