Plug in domain logic processing into building API

This commit is contained in:
Maciej Ziarkowski 2019-12-03 13:51:16 +00:00
parent 3ea505359a
commit adb620fc77
3 changed files with 30 additions and 7 deletions

View File

@ -8,6 +8,8 @@ import db from '../../db';
import { tileCache } from '../../tiles/rendererDefinition';
import { BoundingBox } from '../../tiles/types';
import { processBuildingUpdate } from './domainLogic/processBuildingUpdate';
// data type note: PostgreSQL bigint (64-bit) is handled as string in JavaScript, because of
// JavaScript numerics are 64-bit double, giving only partial coverage.
@ -94,9 +96,9 @@ async function queryBuildingsByReference(key: string, ref: string) {
async function getCurrentBuildingDataById(id: number) {
return db.one(
'SELECT * FROM buildings WHERE building_id = $1',
[id]
);
'SELECT * FROM buildings WHERE building_id = $1',
[id]
);
}
async function getBuildingById(id: number) {
@ -155,13 +157,15 @@ async function getBuildingUPRNsById(id: number) {
async function saveBuilding(buildingId: number, building: any, userId: string) { // TODO add proper building type
try {
return await updateBuildingData(buildingId, userId, async () => {
const processedBuilding = await processBuildingUpdate(building);
// remove read-only fields from consideration
delete building.building_id;
delete building.revision_id;
delete building.geometry_id;
delete processedBuilding.building_id;
delete processedBuilding.revision_id;
delete processedBuilding.geometry_id;
// return whitelisted fields to update
return pickAttributesToUpdate(building, BUILDING_FIELD_WHITELIST);
return pickAttributesToUpdate(processedBuilding, BUILDING_FIELD_WHITELIST);
});
} catch(error) {
console.error(error);
@ -399,6 +403,10 @@ const BUILDING_FIELD_WHITELIST = new Set([
// 'sust_life_expectancy',
'building_attachment_form',
'date_change_building_use',
'current_landuse_class',
'current_landuse_group',
'current_landuse_order'
]);
/**

View File

@ -0,0 +1,11 @@
import { hasAnyOwnProperty } from '../../../helpers';
import { processCurrentLandUseClassifications } from './currentLandUseClassifications';
export async function processBuildingUpdate<T>(building: T): Promise<T> {
if(hasAnyOwnProperty(building, ['current_landuse_class', 'current_landuse_group', 'current_landuse_order'])) {
building = await processCurrentLandUseClassifications(building);
}
return building;
}

View File

@ -23,3 +23,7 @@ export function parseJsonOrDefault(jsonString: string) {
return null;
}
}
export function hasAnyOwnProperty(obj: {}, keys: string[]) {
return keys.some(k => obj.hasOwnProperty(k));
}