diff --git a/app/src/api/api.ts b/app/src/api/api.ts index 59471b6b..62e683b1 100644 --- a/app/src/api/api.ts +++ b/app/src/api/api.ts @@ -1,6 +1,7 @@ import bodyParser from 'body-parser'; import express from 'express'; +import autofillController from './controllers/autofillController'; import * as editHistoryController from './controllers/editHistoryController'; import buildingsRouter from './routes/buildingsRouter'; import extractsRouter from './routes/extractsRouter'; @@ -19,6 +20,7 @@ server.use('/users', usersRouter); server.use('/extracts', extractsRouter); server.get('/history', editHistoryController.getGlobalEditHistory); +server.get('/autofill', autofillController.getAutofillOptions); // POST user auth server.post('/login', function (req, res) { diff --git a/app/src/api/controllers/autofillController.ts b/app/src/api/controllers/autofillController.ts new file mode 100644 index 00000000..527979d1 --- /dev/null +++ b/app/src/api/controllers/autofillController.ts @@ -0,0 +1,14 @@ +import asyncController from '../routes/asyncController'; +import * as autofillService from '../services/autofill'; + +const getAutofillOptions = asyncController(async (req, res) => { + const { field_name, field_value } = req.query; + + const options = await autofillService.getAutofillOptions(field_name, field_value); + + res.send(options); +}); + +export default { + getAutofillOptions +}; diff --git a/app/src/api/services/autofill.ts b/app/src/api/services/autofill.ts new file mode 100644 index 00000000..8c296a27 --- /dev/null +++ b/app/src/api/services/autofill.ts @@ -0,0 +1,25 @@ +import db from '../../db'; + +const autofillFunctionMap = { + current_landuse_class: getLanduseClassOptions +}; + +function getLanduseClassOptions(value: string) { + return db.manyOrNone(` + SELECT landuse_id AS id, description as value, similarity(description, $1) AS similarity + FROM reference_tables.building_landuse_class + WHERE description % $1 + ORDER BY similarity DESC, description + `, [value] + ); +} + +export function getAutofillOptions(fieldName: string, fieldValue: any) { + const optionsFn = autofillFunctionMap[fieldName]; + + if (optionsFn == undefined) { + throw new Error(`Autofill options not available for field '${fieldName}'`); + } + + return optionsFn(fieldValue); +}