From 909ddd3e72e64d82c894a48e83d6ee981a4fa001 Mon Sep 17 00:00:00 2001 From: Frank Duncan Date: Thu, 21 Feb 2019 13:13:28 +0000 Subject: [PATCH] Link SelectedCountins and chapters in model. This also required updating the code that assigns requests to regions to look at a counties chapters region code, and eagerly loading that information from the database. Issue #277: Add County, Chapter Code and Chapter Name --- models/index.js | 2 ++ models/selectedCounties.js | 5 ++++- views/index.js | 2 +- views/sms/index.js | 2 +- views/utilities.js | 1 + 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/models/index.js b/models/index.js index 15c2718..39ca631 100644 --- a/models/index.js +++ b/models/index.js @@ -82,6 +82,8 @@ db.activeRegion.belongsToMany( } ); +db.SelectedCounties.belongsTo(db.chapter, {foreignKey:'chapter_code', targetKey:'code'}); + db.sequelize.sync(options); // TODO: This is a temporary kludge. These shouldn't really be part diff --git a/models/selectedCounties.js b/models/selectedCounties.js index 6582073..ac42a49 100644 --- a/models/selectedCounties.js +++ b/models/selectedCounties.js @@ -18,7 +18,10 @@ module.exports = function(sequelize, DataTypes) { return sequelize.define('SelectedCounties', { - region: DataTypes.TEXT, + chapter_code: { + field: 'chapter', + type: DataTypes.TEXT + }, state: DataTypes.TEXT, county: DataTypes.TEXT }) diff --git a/views/index.js b/views/index.js index 6bfeb67..f87267f 100644 --- a/views/index.js +++ b/views/index.js @@ -23,7 +23,7 @@ exports.saveRequest = function(req, res) { return utils.findCountyFromAddress(address, zip_for_lookup); }).then( function(county_id){ if (county_id){ - region_code = county_id.region; + region_code = county_id.chapter.region; } else { region_code = 'XXXX'; diff --git a/views/sms/index.js b/views/sms/index.js index 76caf3d..8f7ce45 100644 --- a/views/sms/index.js +++ b/views/sms/index.js @@ -209,7 +209,7 @@ exports.respond = function(req, res) { return save_utils.findCountyFromAddress(address, zip); }).then( function(county_id){ if (county_id){ - region_code = county_id.region; + region_code = county_id.chapter.region; } else { region_code = 'XXXX'; diff --git a/views/utilities.js b/views/utilities.js index 75c73a5..e86b410 100644 --- a/views/utilities.js +++ b/views/utilities.js @@ -375,6 +375,7 @@ module.exports = { requestData.stateFromZip = address['state']; return db.SelectedCounties.findOne({ + include: [ db.chapter ], where: { // Use the PostgreSQL "ILIKE" (case-insensitive LIKE) // operator so that internal inconsistencies in the