From 653f6e0ebb27e51312fed5d5a4ba660c8d1e2f28 Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Fri, 9 Feb 2024 13:21:09 -0500 Subject: [PATCH] - Merge release Signed-off-by: Dave Richer --- client/src/utils/RenderTemplate.js | 21 ++++++++++++--------- client/src/utils/graphQLmodifier.js | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/client/src/utils/RenderTemplate.js b/client/src/utils/RenderTemplate.js index 64b714c69..72645bdac 100644 --- a/client/src/utils/RenderTemplate.js +++ b/client/src/utils/RenderTemplate.js @@ -1,5 +1,4 @@ import {gql} from "@apollo/client"; -import {parse, print} from "graphql"; import jsreport from "@jsreport/browser-client"; import {notification} from "antd"; import axios from "axios"; @@ -10,7 +9,7 @@ import {store} from "../redux/store"; import client from "../utils/GraphQLClient"; import cleanAxios from "./CleanAxios"; import {TemplateList} from "./TemplateConstants"; -import {applyFilters, applySorters, wrapFiltersInAnd} from "./graphQLmodifier"; +import {applyFilters, applySorters, parseQuery, printQuery, wrapFiltersInAnd} from "./graphQLmodifier"; const server = process.env.REACT_APP_REPORTS_SERVER_URL; @@ -25,7 +24,6 @@ export default async function RenderTemplate( renderAsExcel = false, renderAsText = false ) { - console.log(' RENDER TEMPLATE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') if (window.jsr3) { jsreport.serverUrl = "https://reports3.test.imex.online/"; } @@ -391,13 +389,13 @@ const fetchContextData = async (templateObject, jsrAuth) => { templateQueryToExecute = atob(generalTemplate.content); } - // TODO: REPORT UPDATE - //TemplateQueryToExecute needs to get modified based on sorters/filters set by user from modal. - console.log(' RENDER TEMPLATE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') - console.dir(templateQueryToExecute); + console.log('Template Object'); console.dir(templateObject); + console.log('Unmodified Query'); + console.dir(templateQueryToExecute); + // We have no template filters or sorters, so we can just execute the query and return the data if ((!templateObject?.filters && !templateObject?.filters?.length && !templateObject?.sorters && !templateObject?.sorters?.length)) { console.log('No filters or sorters'); let contextData = {}; @@ -412,7 +410,9 @@ const fetchContextData = async (templateObject, jsrAuth) => { return {contextData, useShopSpecificTemplate}; } - const ast = parse(templateQueryToExecute); + // Parse the query and apply the filters and sorters + const ast = parseQuery(templateQueryToExecute); + let filterFields = []; if (templateObject?.filters && templateObject?.filters?.length) { @@ -420,12 +420,15 @@ const fetchContextData = async (templateObject, jsrAuth) => { applyFilters(ast, templateObject.filters, filterFields); wrapFiltersInAnd(ast, filterFields); } + if (templateObject?.sorters && templateObject?.sorters?.length) { console.log('Applying sorters') applySorters(ast, templateObject.sorters); } - const finalQuery = print(ast); + const finalQuery = printQuery(ast); + + console.log('Modified Query'); console.log(finalQuery); let contextData = {}; diff --git a/client/src/utils/graphQLmodifier.js b/client/src/utils/graphQLmodifier.js index 2041d61ce..99e7014fe 100644 --- a/client/src/utils/graphQLmodifier.js +++ b/client/src/utils/graphQLmodifier.js @@ -1,7 +1,24 @@ -import {Kind, visit} from "graphql"; +import {Kind, visit, parse, print} from "graphql"; /* eslint-disable no-loop-func */ +/** + * Parse a GraphQL query into an AST + * @param query + * @returns {DocumentNode} + */ +export function parseQuery(query) { + return parse(query); +} + +/** + * Print an AST back into a GraphQL query + * @param query + * @returns {string} + */ +export function printQuery(query) { + return print(query); +} /** * Apply sorters to the AST * @param ast