@opengis/fastify-table
Version:
core-plugins
58 lines (44 loc) • 1.87 kB
JavaScript
/* eslint-disable no-param-reassign */
import {
config, handlebars, getToken, getTemplate, getMeta, pgClients,
} from '../../../../utils.js';
import getTableData from './tableData.js';
export default async function cardTabData({
pg = pgClients.client, query = {}, params = {}, user = {},
}, reply) {
const { token } = params;
const { sql } = query;
if (!token) {
return reply.status(400).send('not enough params: token');
}
const time = Date.now();
const { uid } = config?.auth?.disable ? { uid: '1' } : user;
const props = await getToken({
token,
mode: 'w',
uid,
json: 1,
});
if (!props || !props?.table) {
return reply.status(403).send('token is invalid');
}
const tableTemplate = await getTemplate('table', props.table);
const { columns = [] } = tableTemplate?.columns || tableTemplate?.colModel
? { columns: tableTemplate?.columns || tableTemplate?.colModel }
: await getMeta({ pg, table: props.table });
const filter = await handlebars.compile(props.query?.replace?.(/@id/g, '{{id}}') || '1=1')(props);
const { message, rows = [] } = await getTableData({
pg, params: { table: props.table }, user, query: { filter, sql },
});
if (message) return { message };
if (props.columns?.length && rows?.length) {
const allowList = ['id', 'token'].concat(props.columns).map((el) => [el, `${el}_text`]).flat();
rows.forEach((row) => Object.keys(row).forEach((el) => { if (!allowList.includes(el)) delete row[el]; }));
return {
time: Date.now() - time,
rows,
columns: columns.filter((col) => props.columns.includes(col.name)),
};
}
return { time: Date.now() - time, rows, columns };
}