|
|
|
@ -1,12 +1,14 @@
|
|
|
|
|
var _ = require('lodash'); |
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
var PARENT_PROPS = require('../helper/placeTypes'); |
|
|
|
|
const _ = require('lodash'); |
|
|
|
|
|
|
|
|
|
var ADDRESS_PROPS = { |
|
|
|
|
'number': 'housenumber', |
|
|
|
|
'zip': 'postalcode', |
|
|
|
|
'street': 'street' |
|
|
|
|
}; |
|
|
|
|
const PARENT_PROPS = require('../helper/placeTypes'); |
|
|
|
|
|
|
|
|
|
const ADDRESS_PROPS = [ |
|
|
|
|
{ name: 'number', newName: 'housenumber' }, |
|
|
|
|
{ name: 'zip', newName: 'postalcode', transform: (value) => { return [value]; } }, |
|
|
|
|
{ name: 'street', newName: 'street' } |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setup() { |
|
|
|
@ -28,22 +30,39 @@ function renamePlacenames(req, res, next) {
|
|
|
|
|
* Rename the fields in one record |
|
|
|
|
*/ |
|
|
|
|
function renameOneRecord(place) { |
|
|
|
|
if (place.address_parts) { |
|
|
|
|
Object.keys(ADDRESS_PROPS).forEach(function (prop) { |
|
|
|
|
place[ADDRESS_PROPS[prop]] = place.address_parts[prop]; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// merge the parent block into the top level object to flatten the structure
|
|
|
|
|
// only copy the properties if they have values
|
|
|
|
|
if (place.parent) { |
|
|
|
|
PARENT_PROPS.forEach(function (prop) { |
|
|
|
|
PARENT_PROPS.forEach( (prop) => { |
|
|
|
|
place[prop] = place.parent[prop]; |
|
|
|
|
place[prop + '_a'] = place.parent[prop + '_a']; |
|
|
|
|
place[prop + '_gid'] = place.parent[prop + '_id']; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// copy the address parts after parent hierarchy in order to prefer
|
|
|
|
|
// the postalcode specified by the original source data
|
|
|
|
|
if (place.address_parts) { |
|
|
|
|
ADDRESS_PROPS.forEach( (prop) => { |
|
|
|
|
renameAddressProperty(place, prop); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return place; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function renameAddressProperty(place, prop) { |
|
|
|
|
if (!place.address_parts.hasOwnProperty(prop.name)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (prop.hasOwnProperty('transform')) { |
|
|
|
|
place[prop.newName] = prop.transform(place.address_parts[prop.name]); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
place[prop.newName] = place.address_parts[prop.name]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.exports = setup; |
|
|
|
|