From c2d9792b8f2e6fbb8b5dcf986c251ff4a21c263e Mon Sep 17 00:00:00 2001 From: rrozek Date: Fri, 1 Mar 2024 19:59:59 +0100 Subject: [PATCH] add default options and value for dependent/dynamic filters --- .../dashboard/dashboard.controller.ts | 34 +++++++++++++++++++ src/components/dashboard/dashboard.service.ts | 1 + 2 files changed, 35 insertions(+) diff --git a/src/components/dashboard/dashboard.controller.ts b/src/components/dashboard/dashboard.controller.ts index 4573ae9..4a77390 100644 --- a/src/components/dashboard/dashboard.controller.ts +++ b/src/components/dashboard/dashboard.controller.ts @@ -70,6 +70,40 @@ const getAllDashboards = async (req: Request, res: Response) => { const readDashboard = async (req: Request, res: Response) => { try { const dashboard = await read(req.params.id); + for (const filter of dashboard.filters) { + if ('queryId' in filter) { + const dynamicFilter = filter as + | IDashboardFilterDynamic + | IDashboardFilterDependent; + const filterValues = []; + if ('params' in dynamicFilter && dynamicFilter.params.length > 0) { + for (const param of dynamicFilter.params) { + filterValues.push({ name: param, value: [' '] }); + } + } + const queryPayload = prepareFilterQueryPayload( + dashboard, + filter as IDashboardFilterDynamic | IDashboardFilterDependent, + filterValues, + ); + logger.info( + `getFilterData queryPayload: ${JSON.stringify(queryPayload)}`, + ); + const url = `${API_BASE_URL}/execute-query`; + const response = await axios.post(url, queryPayload); + const data = await response.data; + logger.info(`getFilterData response: ${JSON.stringify(data)}`); + // Update options field if necessary + if (data && data.data && Array.isArray(data.data)) { + // Update options field if necessary + filter.options = data.data.map((item: any) => ({ + label: item.value, + value: item.value, + })); + } + } + } + res .status(httpStatus.OK) .send({ message: 'Dashboard Read', output: dashboard }); diff --git a/src/components/dashboard/dashboard.service.ts b/src/components/dashboard/dashboard.service.ts index df52f4d..3347448 100644 --- a/src/components/dashboard/dashboard.service.ts +++ b/src/components/dashboard/dashboard.service.ts @@ -38,6 +38,7 @@ const read = async (id: string): Promise => { if (!dashboard) { throw new Error(`Dashboard with id ${id} not found`); } + return dashboard as IDashboard; } catch (error) { logger.error('Error in fetching dashboard:', error);