Skip to content

Commit

Permalink
feat(override): allow addendum data to override imported place data (#…
Browse files Browse the repository at this point in the history
…1494)

* proof of concept for having addendums that override indexed fields

* feat(override): allow addendum data to override imported place data

* use _.has
  • Loading branch information
blackmad authored Oct 14, 2020
1 parent a8de5f0 commit b57bbd4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
41 changes: 41 additions & 0 deletions middleware/applyOverrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const _ = require('lodash');
const logger = require('pelias-logger').get('api');
const codec = require('pelias-model').codec;

function setup() {
return applyOverrides;
}

function applyOverrides(req, res, next) {
// do nothing if no result data set
if (!res || !res.data) {
return next();
}

res.data = res.data.map(overrideOneRecord);

next();
}

/*
* Rename the fields in one record
*/
function overrideOneRecord(place) {
if (_.has(place, 'addendum.override')) {
try {
const overrideData = codec.decode(place.addendum.override);
place = _.merge(place, overrideData);
} catch (err) {
logger.error('Invalid addendum override json string:', place.addendum);
}

delete place.addendum.override;

if (_.isEmpty(place.addendum)) {
delete place.addendum;
}
}
return place;
}

module.exports = setup;
5 changes: 5 additions & 0 deletions routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ function addRoutes(app, peliasConfig) {
middleware.confidenceScoreFallback(),
middleware.interpolate(interpolationService, interpolationShouldExecute, interpolationConfiguration),
middleware.sortResponseData(sorting, predicates.hasAdminOnlyResults),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
Expand All @@ -251,6 +252,7 @@ function addRoutes(app, peliasConfig) {
middleware.confidenceScore(peliasConfig.api),
middleware.confidenceScoreFallback(),
middleware.interpolate(interpolationService, interpolationShouldExecute, interpolationConfiguration),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
Expand All @@ -269,6 +271,7 @@ function addRoutes(app, peliasConfig) {
controllers.search(peliasConfig, esclient, queries.autocomplete, not(hasResponseDataOrRequestErrors)),
middleware.distance('focus.point.'),
middleware.confidenceScore(peliasConfig.api),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
Expand All @@ -290,6 +293,7 @@ function addRoutes(app, peliasConfig) {
// reverse confidence scoring depends on distance from origin
// so it must be calculated first
middleware.confidenceScoreReverse(),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
Expand All @@ -310,6 +314,7 @@ function addRoutes(app, peliasConfig) {
// reverse confidence scoring depends on distance from origin
// so it must be calculated first
middleware.confidenceScoreReverse(),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
Expand Down
114 changes: 114 additions & 0 deletions test/unit/middleware/applyOverrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const applyOverrides = require('../../../middleware/applyOverrides')();
const codec = require('pelias-model').codec;

const proxyquire = require('proxyquire').noCallThru();

module.exports.tests = {};

module.exports.tests.applyOverrides = function(test, common) {
test('valid override data with no other addendum', function(t) {
const res = {
data: [
{
name: 'aliased name to throw out',
addendum: {
override: codec.encode({ name: 'Override Name' }),
}
}
]
};

const expected = {
data: [
{
name: 'Override Name'
}
]
};

applyOverrides({}, res, function () {
t.deepEquals(res, expected, 'valid override data');
t.end();
});
});

test('valid override data with other addendum data', function(t) {
const res = {
data: [
{
name: 'aliased name to throw out',
addendum: {
override: codec.encode({ name: 'Override Name' }),
other_data: 1234
}
}
]
};

const expected = {
data: [
{
name: 'Override Name',
addendum: {
other_data: 1234
}
}
]
};

applyOverrides({}, res, function () {
t.deepEquals(res, expected, 'valid override data');
t.end();
});
});

test('invalid override data', function(t) {
const res = {
data: [
{
name: 'aliased name to throw out',
addendum: {
override: 'garbage json'
}
}
]
};

const expected = {
data: [
{
name: 'aliased name to throw out'
}
]
};

const proxiedApplyOverrides = proxyquire('../../../middleware/applyOverrides', {
'pelias-logger': {
get: () => {
return {
error: (msg1, msg2) => {
t.equals(msg1, 'Invalid addendum override json string:');
t.deepEquals(msg2, { override: 'garbage json' });
}
};

}
}
})();

proxiedApplyOverrides({}, res, function () {
t.deepEquals(res, expected, 'invalid override data');
t.end();
});
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] applyOverrides: ' + name, testFunction);
}

for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
2 changes: 1 addition & 1 deletion test/unit/middleware/parseBBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const proxyquire = require('proxyquire').noCallThru();

module.exports.tests = {};

module.exports.tests.computeDistance = function(test, common) {
module.exports.tests.parseBBox = function(test, common) {
test('valid bounding_box json', function(t) {
var res = {
data: [
Expand Down
1 change: 1 addition & 0 deletions test/unit/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var tests = [
require('./helper/unicode'),
require('./middleware/access_log'),
require('./middleware/accuracy'),
require('./middleware/applyOverrides'),
require('./middleware/assignLabels'),
require('./middleware/confidenceScore'),
require('./middleware/confidenceScoreFallback'),
Expand Down

0 comments on commit b57bbd4

Please sign in to comment.