Introduced JS report and refactored some doc generation. IO-585

This commit is contained in:
Patrick Fic
2021-01-06 17:56:46 -08:00
parent 1c967ece2e
commit 2d260dceb8
33 changed files with 311 additions and 676 deletions

View File

@@ -1,43 +1,90 @@
import axios from "axios";
import gql from "graphql-tag";
import { QUERY_TEMPLATES_BY_NAME } from "../graphql/templates.queries";
import jsreport from "jsreport-browser-client-dist";
import client from "../utils/GraphQLClient";
import cleanAxios from "./CleanAxios";
export default async function RenderTemplate(templateObject, bodyshop) {
const { data: templateRecords } = await client.query({
query: QUERY_TEMPLATES_BY_NAME,
variables: { name: templateObject.name },
fetchPolicy: "network-only",
});
const server = "https://reports.bodyshop.app";
jsreport.serverUrl = server;
jsreport.headers["Authorization"] = "Basic " + btoa("admin:admin");
let templateToUse;
export default async function RenderTemplate(
templateObject,
bodyshop,
renderAsHtml = false
) {
//Template Object
// {
// name: TemplateList().parts_order_confirmation.key,
// variables: {
// id: insertResult.data.insert_parts_orders.returning[0].id,
// },
// },
if (templateRecords.templates.length === 1) {
console.log("[ITE] Using OOTB template.");
templateToUse = templateRecords.templates[0];
} else if (templateRecords.templates.length === 2) {
console.log("[ITE] Found custom template.");
templateToUse = templateRecords.templates.filter((t) => !!t.bodyshopid)[0];
console.log("templateToUse", templateToUse);
//Query assets that match the template name. Must be in format <<templateName>>.query
const jsReportQueries = await cleanAxios.get(
`${server}/odata/assets?$filter=name eq '${templateObject.name}.query'`,
{ headers: { Authorization: "Basic " + btoa("admin:admin") } }
);
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
);
useShopSpecificTemplate = true;
} else {
//No template found.Uh oh.
alert("Error: Template key does not exist.");
throw new Error("Template key does not exist.");
//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 { data: contextData } = await client.query({
query: gql(templateToUse.query),
query: gql(templateQueryToExecute),
variables: { ...templateObject.variables },
fetchPolicy: "network-only",
});
const { data } = await axios.post("/render", {
view: templateToUse.html,
context: { ...contextData, bodyshop: bodyshop },
});
return new Promise((resolve, reject) => {
resolve(data);
});
let reportRequest = {
template: {
name: useShopSpecificTemplate
? `/${bodyshop.imexshopid}/${templateObject.name}`
: `/${templateObject.name}`,
...(renderAsHtml ? {} : { recipe: "chrome-pdf" }),
},
data: {
...contextData,
headerpath: `/${bodyshop.imexshopid}/header.html`,
bodyshop: bodyshop,
},
};
const render = await jsreport.renderAsync(reportRequest);
if (!renderAsHtml) {
render.download();
var html =
"<html>" +
"<style>html,body {padding:0;margin:0;} iframe {width:100%;height:100%;border:0}</style>" +
"<body>" +
'<iframe type="application/pdf" src="' +
render.toDataURI() +
'"></iframe>' +
"</body></html>";
displayTemplateInWindowNoprint(html);
} else {
return new Promise((resolve, reject) => {
resolve(render.toString());
});
}
}
export const displayTemplateInWindow = (html) => {