Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-editable fields to be auto-derived #684

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/src/api/config/dataFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ import { valueType } from '../../helpers';
/** Configuration for a single data field */
export interface DataFieldConfig {
/**
* Allow editing the field?
* Allow editing the field through the API?
*/
edit: boolean;

/**
* Should the editing of the field be allowed - but only when
* the change is a result of an edit of another field, from which this field is derived.
* Example: editing Land Use Group modifies Land Use Order, too, because LU Order is automatically derived from LU Group.
* But Land Use Order itself cannot be modified directly by users.
* Default: false
*/
derivedEdit?: boolean;

/**
* Allow verifying the field value?
* Default: false;
Expand Down Expand Up @@ -236,6 +245,7 @@ export const dataFieldsConfig = valueType<DataFieldConfig>()({ /* eslint-disable
},
current_landuse_order: {
edit: false,
derivedEdit: true,
verify: false,
},

Expand Down
2 changes: 1 addition & 1 deletion app/src/api/dataAccess/building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function insertEditHistoryRevision(

const columnConfigLookup = Object.assign(
{},
...Object.entries(dataFieldsConfig).filter(([, config]) => config.edit).map(([key, {
...Object.entries(dataFieldsConfig).filter(([, config]) => config.edit || config.derivedEdit).map(([key, {
asJson = false,
sqlCast
}]) => ({ [key]: {
Expand Down
9 changes: 9 additions & 0 deletions app/src/api/errors/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export class InvalidOperationError extends UserError {
}
}

export class InvalidFieldError extends UserError {
public fieldName: string;
constructor(message?: string, fieldName?: string) {
super(message);
this.name = 'InvalidFieldError';
this.fieldName = fieldName;
}
}

export class FieldTypeError extends UserError {
public fieldName: string;
constructor(message?: string, fieldName?: string) {
Expand Down
8 changes: 6 additions & 2 deletions app/src/api/services/building/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export async function getBuildingById(id: number) {
}
}

const FIELD_EDIT_WHITELIST = new Set(Object.entries(dataFieldsConfig).filter(([, value]) => value.edit).map(([key]) => key));
/**
* List of fields for which modification is allowed
* (directly by the user, or for fields that are derived from others)
*/
const FINAL_FIELD_EDIT_ALLOWLIST = new Set(Object.entries(dataFieldsConfig).filter(([, value]) => value.edit || value.derivedEdit).map(([key]) => key));

export async function editBuilding(buildingId: number, building: any, userId: string): Promise<object> { // TODO add proper building type
return await updateBuildingData(buildingId, userId, async () => {
Expand All @@ -44,7 +48,7 @@ export async function editBuilding(buildingId: number, building: any, userId: st
delete processedBuilding.geometry_id;

// return whitelisted fields to update
return pickFields(processedBuilding, FIELD_EDIT_WHITELIST);
return pickFields(processedBuilding, FINAL_FIELD_EDIT_ALLOWLIST);
});
}

Expand Down
9 changes: 8 additions & 1 deletion app/src/api/services/domainLogic/validateBuildingUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ import Ajv from 'ajv';
import addFormats from 'ajv-formats';

import { mapObject } from '../../../helpers';
import { FieldTypeError } from '../../errors/general';
import { InvalidFieldError, FieldTypeError } from '../../errors/general';
import { dataFieldsConfig } from '../../config/dataFields';
import { fieldSchemaConfig } from '../../config/fieldSchemaConfig';

const ajv = new Ajv();
addFormats(ajv);

const compiledSchemas = mapObject(fieldSchemaConfig, ([, val]) => ajv.compile(val))

const EXTERNAL_FIELD_EDIT_ALLOWLIST = new Set(Object.entries(dataFieldsConfig).filter(([, value]) => value.edit).map(([key]) => key));

export function validateBuildingUpdate(buildingId: number, building: any) {
for(const field of Object.keys(building)) {
if(!EXTERNAL_FIELD_EDIT_ALLOWLIST.has(field)) {
throw new InvalidFieldError('Field is not editable', field);
}

if(field in compiledSchemas) {
if(!compiledSchemas[field](building[field])) {
throw new FieldTypeError('Invalid format of data sent', field);
Expand Down