Merged in release/2021-10-08 (pull request #234)

release/2021-10-08

Approved-by: Patrick Fic
This commit is contained in:
Patrick Fic
2021-10-01 22:41:36 +00:00

View File

@@ -1,4 +1,4 @@
import { gql } from "@apollo/client"; import { gql, selectHttpOptionsAndBody } from "@apollo/client";
import { notification } from "antd"; import { notification } from "antd";
import axios from "axios"; import axios from "axios";
import jsreport from "jsreport-browser-client-dist"; import jsreport from "jsreport-browser-client-dist";
@@ -195,35 +195,36 @@ export const GenerateDocuments = async (templates) => {
const fetchContextData = async (templateObject) => { const fetchContextData = async (templateObject) => {
console.log("Fetching context data", templateObject); console.log("Fetching context data", templateObject);
const bodyshop = store.getState().user.bodyshop;
jsreport.headers["Authorization"] = jsreport.headers["Authorization"] =
"Bearer " + (await auth.currentUser.getIdToken()); "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( const jsReportQueries = await axios.get(
`${server}/odata/assets?$filter=name eq '${templateObject.name}.query'` `${server}/odata/assets?$filter=name eq '${templateObject.name}.query'`
); );
let templateQueryToExecute, let templateQueryToExecute,
useShopSpecificTemplate = false; useShopSpecificTemplate = false;
if (jsReportQueries.data.value.length === 0) { // let shopSpecificTemplate;
//We have no query to execute. Just render the template.
} else if (jsReportQueries.data.value.length === 1) { if (shopSpecificFolder) {
//We're using the default template. Get the query and execute. let shopSpecificTemplate = jsReportQueries.data.value.find(
templateQueryToExecute = atob(jsReportQueries.data.value[0].content); (f) => f?.folder?.shortid === shopSpecificFolder.shortid
} 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
); );
useShopSpecificTemplate = true; useShopSpecificTemplate = true;
templateQueryToExecute = atob(shopSpecificTemplate.content);
} else { } else {
//We have too many queries to choose from. Throw an error. const generalTemplate = jsReportQueries.data.value.find((f) => !f.folder);
alert( useShopSpecificTemplate = false;
"There are too many queries to choose from. Please ensure there are no conflicting keys." templateQueryToExecute = atob(generalTemplate.content);
);
throw new Error(
"There are too many queries to choose from. Please ensure there are no conflicting keys."
);
} }
let contextData = {}; let contextData = {};
if (templateQueryToExecute) { if (templateQueryToExecute) {
const { data } = await client.query({ const { data } = await client.query({