From 1d4318868bb594936d3a5d13dd579fb86d87f20c Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 2 Jun 2017 14:07:30 -0400 Subject: [PATCH] added helper for removed non-coarse layers --- controller/coarse_reverse.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/controller/coarse_reverse.js b/controller/coarse_reverse.js index ea9791cb..d86a0951 100644 --- a/controller/coarse_reverse.js +++ b/controller/coarse_reverse.js @@ -21,6 +21,8 @@ function getMostGranularLayer(results) { }); } +// removing non-coarse layers could leave effective_layers empty, so it's +// important to check for empty layers here function hasResultsAtRequestedLayers(results, layers) { return _.isEmpty(layers) || !_.isEmpty(_.intersection(layers, Object.keys(results))); } @@ -83,6 +85,15 @@ function setup(service, should_execute) { return next(); } + // because coarse reverse is called when non-coarse reverse didn't return + // anything, don't consider non-coarse layers when filtering + const effective_layers = _.without(req.clean.layers, 'venue', 'address', 'street'); + + const centroid = { + lat: req.clean['point.lat'], + lon: req.clean['point.lon'] + }; + service(req, (err, results) => { // if there's an error, log it and bail if (err) { @@ -93,13 +104,13 @@ function setup(service, should_execute) { // find the finest granularity requested const finest_granularity_requested = granularities.findIndex((granularity) => { - return req.clean.layers.indexOf(granularity) !== -1; + return effective_layers.indexOf(granularity) !== -1; }); logger.info(`[controller:coarse_reverse][queryType:pip][result_count:${Object.keys(results).length}]`); // now remove everything from the response that is more granular than the - // most granular layer requested. that is, if req.clean.layers=['county'], + // most granular layer requested. that is, if effective_layers=['county'], // remove neighbourhoods, localities, and localadmins Object.keys(results).forEach((layer) => { if (granularities.indexOf(layer) < finest_granularity_requested) { @@ -110,7 +121,7 @@ function setup(service, should_execute) { res.meta = {}; res.data = []; // synthesize a doc from results if there's a result at the request layer(s) - if (hasResultsAtRequestedLayers(results, _.without(req.clean.layers, 'venue', 'address', 'street'))) { + if (hasResultsAtRequestedLayers(results, effective_layers)) { res.data.push(synthesizeDoc(results)); }