UNPKG

@opengis/bi

Version:

BI data visualization module

59 lines (47 loc) 1.71 kB
import { dataDelete, dataUpdate, pgClients } from '@opengis/fastify-table/utils.js'; export default async function widgetDel({ pg = pgClients.client, params = {} }) { const { widget: widgetName, name: dashboardName } = params; if (!widgetName || !dashboardName) { return { message: 'not enough params: dashboard and widget name', status: 400, }; } const row = await pg.query(`select a.widget_id, b.dashboard_id 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(el => el.rows?.[0] || {}); const { widget_id: widgetId, dashboard_id: dashboardId } = row || {}; if (!widgetId) { return { message: `widget not found ${widgetName}`, status: 404 }; } await dataDelete({ pg, table: 'bi.widget', id: widgetId, }); const currentDashboard = await pg.query('select * from bi.dashboard where $1 in (dashboard_id, name)', [dashboardName]) .then(el => el.rows?.[0] || {}); const body = currentDashboard; if (!currentDashboard) { return { message: `dashboard not found ${dashboardName}`, status: 404 }; } body.panels = Array.isArray(body.panels) && body.panels?.length ? body.panels?.filter((panel) => panel.widget !== widgetName) : undefined; body.widgets = Array.isArray(body.widgets) && body?.widgets?.length ? body.widgets?.filter((widget) => widget.name !== widgetName) : undefined; const res1 = await dataUpdate({ pg, table: 'bi.dashboard', id: dashboardId, data: body, }); return { message: `Deleted widget ${widgetName}`, status: 200, rows: res1, }; }