@opengis/bi
Version:
BI data visualization module
100 lines (83 loc) • 2.53 kB
JavaScript
import {
config, pgClients, getMeta, getToken,
} from '@opengis/fastify-table/utils.js';
import datasetForms from '../utils/datasetForms.js';
const inputType = {
text: 'Text',
autocomplete: 'Autocomplete',
select: 'Autocomplete',
date: 'DatePicker',
'yes/no': 'Switcher',
badge: 'Select',
number: 'Number',
tags: 'Tags',
file: 'File',
};
const systemColumns = [
'uid',
'cdate',
'editor_id',
'editor_date',
'files',
];
export default async function datasetForm({
pg = pgClients.client, params = {}, query = {}, user = {},
}) {
if (!user?.uid) {
return { message: 'access restricted', status: 403 };
}
if (!params?.id) {
return { message: 'not enough params: id', status: 404 };
}
const tokenData = await getToken({
uid: user?.uid,
token: params.id,
mode: 'a',
json: 1,
});
if (!tokenData && !config.local && !config.debug) {
return { message: 'token not allow', status: 403 };
}
const {
id, columns, formSetting, table, style,
} = await pg.query(`select dataset_id as id, table_name as table, column_list as columns, style,
form_setting as "formSetting" from bi.dataset where dataset_id=$1`, [tokenData?.dataset || params.id])
.then(el => el.rows?.[0] || {});
if (!id) {
return { message: 'dataset not found', status: 404 };
}
if (datasetForms[id] && !query.nocache) {
return datasetForms[id];
}
const { columns: dbColumns = [] } = await getMeta(table) || {};
const isFilesColumn = dbColumns.find((el) => el.name === 'files' && pg.pgType[el.dataTypeID] === 'text');
const formSchema = formSetting
|| ((columns || dbColumns).filter((col) => !systemColumns.includes(col.name))).reduce((acc, curr) => Object.assign(acc, {
[curr.name]: {
type: inputType[curr?.format || curr?.type || 'text'] || 'Text',
ua: curr?.description || curr?.title || curr?.name,
validators: curr?.is_required ? ['required'] : null,
options: curr?.values?.length ? curr?.values : null,
data: curr?.data,
min: curr?.type || curr?.format === 'number' ? 0 : null,
},
}), {});
Object.assign(formSchema, {
geom: {
type: 'Geom',
ua: 'Геометрія',
height: '500',
geom_edit_controol: [style?.type ? [style.type] : null],
},
});
if (isFilesColumn) {
Object.assign(formSchema, {
files: {
type: 'FileList',
ua: 'Файли',
},
});
}
datasetForms[id] = formSchema;
return formSchema;
}