Add simple dynamics API support

This commit is contained in:
Maciej Ziarkowski 2021-03-11 19:15:58 +00:00
parent b93d016a77
commit 28bad44220
5 changed files with 88 additions and 0 deletions

View File

@ -239,4 +239,10 @@ export const dataFieldsConfig = valueType<DataFieldConfig>()({ /* eslint-disable
verify: false, verify: false,
}, },
demolished_buildings: {
edit: true,
verify: false,
asJson: true,
sqlCast: 'jsonb',
},
}); });

View File

@ -4,4 +4,63 @@ import { dataFieldsConfig } from './dataFields';
export const fieldSchemaConfig: { [key in keyof typeof dataFieldsConfig]?: SomeJSONSchema} = { /*eslint-disable @typescript-eslint/camelcase */ export const fieldSchemaConfig: { [key in keyof typeof dataFieldsConfig]?: SomeJSONSchema} = { /*eslint-disable @typescript-eslint/camelcase */
demolished_buildings: {
type: 'array',
items: {
type: 'object',
required: ['year_constructed', 'year_demolished', 'overlap_present', 'links'],
properties: {
year_constructed: {
type: 'object',
required: ['min', 'max'],
additionalProperties: false,
properties: {
min: {
type: 'integer'
},
max: {
type: 'integer'
}
}
},
year_demolished: {
type: 'object',
required: ['min', 'max'],
additionalProperties: false,
properties: {
min: {
type: 'integer'
},
max: {
type: 'integer'
}
}
},
overlap_present: {
type: 'string',
enum: ['1%', '25%', '50%', '75%', '100%']
},
links: {
type: 'array',
items: {
type: 'string'
},
minItems: 1
}
},
additionalProperties: false,
}
} as JSONSchemaType<{
year_constructed: {
min: number;
max: number;
};
year_demolished: {
min: number;
max: number;
}
overlap_present: string;
links: string[];
}[]>,
} as const; } as const;

View File

@ -6,6 +6,9 @@ import { ArgumentError } from '../../errors/general';
import { updateLandUse } from './landUse'; import { updateLandUse } from './landUse';
/**
* Process land use classifications - derive land use order from land use groups
*/
async function processCurrentLandUseClassifications(buildingId: number, buildingUpdate: any): Promise<any> { async function processCurrentLandUseClassifications(buildingId: number, buildingUpdate: any): Promise<any> {
const currentBuildingData = await getBuildingData(buildingId); const currentBuildingData = await getBuildingData(buildingId);
@ -31,10 +34,26 @@ async function processCurrentLandUseClassifications(buildingId: number, building
} }
} }
/**
* Process Dynamics data - sort past buildings by construction date
*/
async function processDynamicsPastBuildings(buildingId: number, buildingUpdate: any): Promise<any> {
buildingUpdate.demolished_buildings = buildingUpdate.demolished_buildings.sort((a, b) => b.year_constructed.min - a.year_constructed.min);
return buildingUpdate;
}
/**
* Define any custom processing logic for specific building attributes
*/
export async function processBuildingUpdate(buildingId: number, buildingUpdate: any): Promise<any> { export async function processBuildingUpdate(buildingId: number, buildingUpdate: any): Promise<any> {
if(hasAnyOwnProperty(buildingUpdate, ['current_landuse_group'])) { if(hasAnyOwnProperty(buildingUpdate, ['current_landuse_group'])) {
buildingUpdate = await processCurrentLandUseClassifications(buildingId, buildingUpdate); buildingUpdate = await processCurrentLandUseClassifications(buildingId, buildingUpdate);
} }
if('demolished_buildings' in buildingUpdate) {
buildingUpdate = await processDynamicsPastBuildings(buildingId, buildingUpdate);
}
return buildingUpdate; return buildingUpdate;
} }

View File

@ -0,0 +1,2 @@
ALTER TABLE buildings
DROP demolished_buildings;

View File

@ -0,0 +1,2 @@
ALTER TABLE buildings
ADD column demolished_buildings JSONB DEFAULT '[]'::JSONB;