IO-1417 Resolve custom templates printing.
This commit is contained in:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user