UNPKG

@opengis/bi

Version:

BI data visualization module

116 lines (101 loc) 3.81 kB
import { dataUpdate, pgClients } from '@opengis/fastify-table/utils.js'; import { getWidget, yamlSafe } from '../../../../utils.js'; export default async function widgetEdit({ pg = pgClients.client, body, params }, reply) { const { widget: widgetName, name: dashboardName } = params; const data = body.yml && !body.style ? yamlSafe.load(body.yml) : body; const yml = body.yml ?? yamlSafe.dump(body.data); // console.log(body.data, data, body.yml, yml); if (!widgetName || !dashboardName) { return reply.status(400).send({ error: 'not enough params: dashboard and widget name', code: 400, }); } const { widget_id: widgetId, dashboard_id: dashboardId, widgets } = await pg.query( `select a.widget_id , b.dashboard_id, b.widgets from bi.widget a, bi.dashboard b where $2 in (a.widget_id, a.name) and $1 in (b.dashboard_id, b.name) order by 1,2`, [dashboardName, widgetName]).then(res1 => res1.rows?.[0] || {}); // get Data const templateData = await getWidget({ pg, dashboard: dashboardName, widget: widgetName }); if (!templateData) { return reply.status(404).send({ error: `widget not found ${widgetName}`, code: 404, }); } if (data?.data) { Object.assign(data, { data: Object.keys(data.data || {}) ?.reduce?.((acc, curr) => ({ ...acc, [curr]: data.data[curr] === 'null' ? null : data.data[curr] }), {}) }); } const widgetData = ['style', 'data', 'type', 'title', 'controls', 'query', 'cls', 'x'] .filter(el => data[el]) .reduce((p, el) => ({ ...p, [el]: data[el] }), {}); // get table const tableName = body.table || widgetData.data?.table_name || widgetData.data?.table || widgetData?.table_name || templateData.table; const loadTemplate = pg?.pk?.['admin.doc_template'] ? await pg.query( 'select body from admin.doc_template where doc_type=5 and title=$1', [tableName] ).then(el => el.rows?.[0]?.body) : null; if (!pg || !tableName || !(pg.pk?.[loadTemplate?.table || tableName] || pgClients[widgetData?.data?.db || templateData?.db || 'client']?.pk?.[loadTemplate?.table || tableName])) { return reply.status(400).send({ error: 'bad params: table ' + tableName, code: 400, }); } if (yml) { Object.assign(widgetData, { yml }) } if (widgetData?.data) { Object.assign(widgetData, { data: { ...widgetData?.data || {}, table_name: tableName } }); } Object.assign(widgetData, { table_name: tableName }); // console.log(widgetData); const row = await dataUpdate({ pg, table: 'bi.widget', id: widgetId, data: widgetData, }); const idx = widgets?.findIndex?.(el => el.name === widgetName)?.toString?.() || -1; if (widgetData.hasOwnProperty('table_name') && idx > -1) { widgets[idx].table_name = tableName; widgets[idx].table = tableName; } if (widgetData.hasOwnProperty('data') && idx > -1) { widgets[idx].data = widgetData.data } if (widgetData.hasOwnProperty('yml') && idx > -1) { widgets[idx].yml = widgetData.yml; } if (widgetData.hasOwnProperty('style') && idx > -1) { widgets[idx].style = widgetData.style; } if (widgetData.hasOwnProperty('type') && idx > -1) { widgets[idx].type = widgetData.type; } if (widgetData.data?.type === 'text' && idx > -1) { widgets[idx].data.text = widgetData.data?.text; } if (typeof widgetData?.query === 'string' && idx > -1) { widgets[idx].data.query = widgetData.query; } if (typeof widgetData?.cls === 'string' && idx > -1) { widgets[idx].data.cls = widgetData.cls; } if (typeof widgetData?.x === 'string' && idx > -1) { widgets[idx].data.x = widgetData.x; } await dataUpdate({ pg, table: 'bi.dashboard', id: dashboardId, data: { widgets }, }); return row; }