Add autofill backend

This commit is contained in:
Maciej Ziarkowski 2020-01-06 16:15:36 +00:00
parent 35554e37ff
commit aafb81a17b
3 changed files with 41 additions and 0 deletions

View File

@ -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) {

View File

@ -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
};

View File

@ -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);
}