@opengis/bi
Version:
BI data visualization module
62 lines (51 loc) • 1.65 kB
JavaScript
import { dataUpdate, pgClients, getPGAsync } from "@opengis/fastify-table/utils.js";
export default async function dashboardEdit({
pg = pgClients.client, params = {}, body = {},
}, reply) {
const { name: dashboardName } = params;
const { panels, widgets, table_name: tableName } = body;
if (body?.db) {
try {
const pg1 = await getPGAsync(body.db);
await pg1.query('select 1');
} catch (err) {
return reply.status(400).send('Некоректна база даних');
}
}
if (!dashboardName) {
return reply.status(400).send('not enough params: name');
}
if (!tableName) {
return reply.status(400).send('not enough params: table_name');
}
const row = await pg.query('select dashboard_id, widgets, panels from bi.dashboard where $1 in (dashboard_id, name)', [dashboardName])
.then(el => el.rows?.[0] || {});
const { dashboard_id: dashboardId } = row;
if (!dashboardId) {
return reply.status(400).send('dashboard not found');
}
if (panels?.length && !widgets?.length) {
(row.widgets || []).forEach((el) => {
const { title } = panels?.find?.(item => item.widget === el.name) || {};
if (el.data) {
// console.log('el.title', el.title, 'title', title);
el.data.title = title;
}
});
Object.assign(body, { widgets: row.widgets });
}
const res = await dataUpdate({
pg,
table: 'bi.dashboard',
id: dashboardId,
data: body,
});
if (!Object.keys(res)?.length) {
return reply.status(404).send('not found data');
}
return {
message: `updated ${dashboardName}`,
status: 200,
rows: res,
};
}