diff --git a/controller/placeholder.js b/controller/placeholder.js index c3f38c54..7df2756d 100644 --- a/controller/placeholder.js +++ b/controller/placeholder.js @@ -50,38 +50,37 @@ function synthesizeDocs(result) { if (_.isEmpty(result.lineage)) { // there are no hierarchies so just return what's been assembled so far - const esDoc = doc.toESDocument(); - esDoc.data._id = esDoc._id; - esDoc.data._type = esDoc._type; + return buildESDoc(doc); - return esDoc.data; + } + + result.lineage.map((hierarchy) => { + Object.keys(hierarchy) + .filter(doc.isSupportedParent) + .filter((placetype) => { return !_.isEmpty(_.trim(hierarchy[placetype].name)); } ) + .forEach((placetype) => { + if (hierarchy[placetype].hasOwnProperty('abbr') && placetype === 'country') { + doc.setAlpha3(hierarchy[placetype].abbr); + } + + doc.addParent( + placetype, + hierarchy[placetype].name, + hierarchy[placetype].id.toString(), + hierarchy[placetype].abbr); - } else { - result.lineage.map((hierarchy) => { - Object.keys(hierarchy) - .filter(doc.isSupportedParent) - .filter((placetype) => { return !_.isEmpty(_.trim(hierarchy[placetype].name)); } ) - .forEach((placetype) => { - if (hierarchy[placetype].hasOwnProperty('abbr') && placetype === 'country') { - doc.setAlpha3(hierarchy[placetype].abbr); - } - - doc.addParent( - placetype, - hierarchy[placetype].name, - hierarchy[placetype].id.toString(), - hierarchy[placetype].abbr); - - }); }); + }); - const esDoc = doc.toESDocument(); - esDoc.data._id = esDoc._id; - esDoc.data._type = esDoc._type; + return buildESDoc(doc); - return esDoc.data; +} - } +function buildESDoc(doc) { + const esDoc = doc.toESDocument(); + esDoc.data._id = esDoc._id; + esDoc.data._type = esDoc._type; + return esDoc.data; } function setup(placeholderService, should_execute) { @@ -112,7 +111,8 @@ function setup(placeholderService, should_execute) { // filter that passes only documents that match on boundary.country // passed everything if req.clean['boundary.country'] is not found const matchesBoundaryCountry = (doc) => { - return doc.alpha3 === req.clean['boundary.country']; + return doc.parent.country_a && + doc.parent.country_a.indexOf(req.clean['boundary.country']) > -1; }; const countryFilter = _.has(req, ['clean', 'boundary.country']) ? matchesBoundaryCountry : _.constant(true); diff --git a/test/unit/controller/placeholder.js b/test/unit/controller/placeholder.js index d6b7e627..f9540398 100644 --- a/test/unit/controller/placeholder.js +++ b/test/unit/controller/placeholder.js @@ -212,57 +212,6 @@ module.exports.tests.success = (test, common) => { const expected_res = { meta: {}, data: [ - { - _id: '123', - _type: 'neighbourhood', - layer: 'neighbourhood', - source: 'whosonfirst', - source_id: '123', - center_point: { - lat: 14.141414, - lon: 41.414141 - }, - bounding_box: '{"min_lat":12.121212,"max_lat":13.131313,"min_lon":21.212121,"max_lon":31.313131}', - name: { - 'default': 'name 1' - }, - phrase: { - 'default': 'name 1' - }, - alpha3: 'ABC', - parent: { - neighbourhood: ['neighbourhood name 1'], - neighbourhood_id: ['10'], - neighbourhood_a: [null], - borough: ['borough name 1'], - borough_id: ['9'], - borough_a: [null], - locality: ['locality name 1'], - locality_id: ['8'], - locality_a: [null], - localadmin: ['localadmin name 1'], - localadmin_id: ['7'], - localadmin_a: [null], - county: ['county name 1'], - county_id: ['6'], - county_a: [null], - macrocounty: ['macrocounty name 1'], - macrocounty_id: ['5'], - macrocounty_a: [null], - region: ['region name 1'], - region_id: ['4'], - region_a: [null], - macroregion: ['macroregion name 1'], - macroregion_id: ['3'], - macroregion_a: [null], - dependency: ['dependency name 1'], - dependency_id: ['2'], - dependency_a: [null], - country: ['country name 1'], - country_id: ['1'], - country_a: ['ABC'] - } - }, { _id: '123', _type: 'neighbourhood', @@ -282,36 +231,36 @@ module.exports.tests.success = (test, common) => { }, alpha3: 'XYZ', parent: { - neighbourhood: ['neighbourhood name 2'], - neighbourhood_id: ['20'], - neighbourhood_a: ['neighbourhood abbr 2'], - borough: ['borough name 2'], - borough_id: ['19'], - borough_a: ['borough abbr 2'], - locality: ['locality name 2'], - locality_id: ['18'], - locality_a: ['locality abbr 2'], - localadmin: ['localadmin name 2'], - localadmin_id: ['17'], - localadmin_a: ['localadmin abbr 2'], - county: ['county name 2'], - county_id: ['16'], - county_a: ['county abbr 2'], - macrocounty: ['macrocounty name 2'], - macrocounty_id: ['15'], - macrocounty_a: ['macrocounty abbr 2'], - region: ['region name 2'], - region_id: ['14'], - region_a: ['region abbr 2'], - macroregion: ['macroregion name 2'], - macroregion_id: ['13'], - macroregion_a: ['macroregion abbr 2'], - dependency: ['dependency name 2'], - dependency_id: ['12'], - dependency_a: ['dependency abbr 2'], - country: ['country name 2'], - country_id: ['11'], - country_a: ['XYZ'] + neighbourhood: ['neighbourhood name 1', 'neighbourhood name 2'], + neighbourhood_id: ['10', '20'], + neighbourhood_a: [null, 'neighbourhood abbr 2'], + borough: ['borough name 1', 'borough name 2'], + borough_id: ['9', '19'], + borough_a: [null, 'borough abbr 2'], + locality: ['locality name 1', 'locality name 2'], + locality_id: ['8', '18'], + locality_a: [null, 'locality abbr 2'], + localadmin: ['localadmin name 1', 'localadmin name 2'], + localadmin_id: ['7', '17'], + localadmin_a: [null, 'localadmin abbr 2'], + county: ['county name 1', 'county name 2'], + county_id: ['6', '16'], + county_a: [null, 'county abbr 2'], + macrocounty: ['macrocounty name 1', 'macrocounty name 2'], + macrocounty_id: ['5', '15'], + macrocounty_a: [null, 'macrocounty abbr 2'], + region: ['region name 1', 'region name 2'], + region_id: ['4', '14'], + region_a: [null, 'region abbr 2'], + macroregion: ['macroregion name 1', 'macroregion name 2'], + macroregion_id: ['3', '13'], + macroregion_a: [null, 'macroregion abbr 2'], + dependency: ['dependency name 1', 'dependency name 2'], + dependency_id: ['2', '12'], + dependency_a: [null, 'dependency abbr 2'], + country: ['country name 1', 'country name 2'], + country_id: ['1', '11'], + country_a: ['ABC', 'XYZ'] } }, { @@ -839,11 +788,11 @@ module.exports.tests.success = (test, common) => { phrase: { 'default': 'name 1' }, - alpha3: 'ABC', + alpha3: 'DEF', parent: { - country: ['country name 1'], - country_id: ['1'], - country_a: ['ABC'] + country: ['country name 1', 'country name 2'], + country_id: ['1', '2'], + country_a: ['ABC', 'DEF'] } }, {