From 6e28afda6755425a15dee2bdabcd9dd76dccc973 Mon Sep 17 00:00:00 2001 From: Patrick Fic <> Date: Fri, 1 Oct 2021 15:41:17 -0700 Subject: [PATCH] IO-1417 Resolve custom templates printing. --- client/src/utils/RenderTemplate.js | 35 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/client/src/utils/RenderTemplate.js b/client/src/utils/RenderTemplate.js index 78452b1b6..ad0545762 100644 --- a/client/src/utils/RenderTemplate.js +++ b/client/src/utils/RenderTemplate.js @@ -1,4 +1,4 @@ -import { gql } from "@apollo/client"; +import { gql, selectHttpOptionsAndBody } from "@apollo/client"; import { notification } from "antd"; import axios from "axios"; import jsreport from "jsreport-browser-client-dist"; @@ -195,35 +195,36 @@ export const GenerateDocuments = async (templates) => { const fetchContextData = async (templateObject) => { console.log("Fetching context data", templateObject); + const bodyshop = store.getState().user.bodyshop; + jsreport.headers["Authorization"] = "Bearer " + (await auth.currentUser.getIdToken()); + const folders = await axios.get(`${server}/odata/folders`); + const shopSpecificFolder = folders.data.value.find( + (f) => f.name === bodyshop.imexshopid + ); + const jsReportQueries = await axios.get( `${server}/odata/assets?$filter=name eq '${templateObject.name}.query'` ); let templateQueryToExecute, useShopSpecificTemplate = false; - if (jsReportQueries.data.value.length === 0) { - //We have no query to execute. Just render the template. - } else if (jsReportQueries.data.value.length === 1) { - //We're using the default template. Get the query and execute. - templateQueryToExecute = atob(jsReportQueries.data.value[0].content); - } else if (jsReportQueries.data.value.length === 2) { - //There's a custom template. Use that query instead and execute. We find it because it has a parent folder. - templateQueryToExecute = atob( - jsReportQueries.data.value.filter((v) => !!v.folder)[0].content + // let shopSpecificTemplate; + + if (shopSpecificFolder) { + let shopSpecificTemplate = jsReportQueries.data.value.find( + (f) => f?.folder?.shortid === shopSpecificFolder.shortid ); useShopSpecificTemplate = true; + templateQueryToExecute = atob(shopSpecificTemplate.content); } else { - //We have too many queries to choose from. Throw an error. - alert( - "There are too many queries to choose from. Please ensure there are no conflicting keys." - ); - throw new Error( - "There are too many queries to choose from. Please ensure there are no conflicting keys." - ); + const generalTemplate = jsReportQueries.data.value.find((f) => !f.folder); + useShopSpecificTemplate = false; + templateQueryToExecute = atob(generalTemplate.content); } + let contextData = {}; if (templateQueryToExecute) { const { data } = await client.query({