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) => {

View File

@@ -14,97 +14,5 @@ export const TemplateList = (type, object) => {
drivingId: "job",
key: "estimate_detail",
},
all_job_notes: {
title: i18n.t("printcenter.jobs.all_job_notes"),
description: "All jobs Notes including Private",
drivingId: "job",
key: "all_job_notes",
},
fippa_authorization: {
title: i18n.t("printcenter.jobs.fippa_authorization"),
description: "FIPPA Work Auth",
drivingId: "job",
key: "fippa_authorization",
},
casl_authorization: {
title: i18n.t("printcenter.jobs.casl_authorization"),
description: "CASL Auth",
drivingId: "job",
key: "casl_authorization",
},
window_tag: {
title: i18n.t("printcenter.jobs.window_tag"),
description: "Window Tag",
drivingId: "job",
key: "window_tag",
},
cover_sheet: {
title: i18n.t("printcenter.jobs.cover_sheet"),
description: "Cover Sheet",
drivingId: "job",
key: "cover_sheet",
},
time_tickets_by_employee: {
title: "Time Tickets by Employee",
description: "Time tickets for employee with date range",
drivingId: "employee",
key: "time_tickets_by_employee",
},
//Non Completed Items
appointment_reminder: {
title: "Appointment Reminder",
description:
"Sent to a customer as a reminder of an upcoming appointment.",
drivingId: "appointment",
key: "appointment_reminder",
subject: `Appointment Reminder`,
},
appointment_confirmation: {
title: "Appointment Confirmation",
description:
"Sent to a customer as a Confirmation of an upcoming appointment.",
drivingId: "appointment",
key: "appointment_confirmation",
},
parts_order_confirmation: {
title: "Parts Order Confirmation",
description: "Parts order template including part details",
drivingId: "partsorder",
key: "parts_order_confirmation",
subject: `Parts Order for ${object}`,
},
cover_sheet_landscape: {
title: "Cover Sheet - Landscape",
description: "Cover sheet landscape",
drivingId: "job",
key: "cover_sheet_landscape",
},
cover_sheet_portrait: {
title: "Cover Sheet - portrait",
description: "Cover sheet portrait",
drivingId: "job",
key: "cover_sheet_portrait",
},
parts_return_confirmation: {
title: "Parts Return Confirmation",
description: "Parts Return template including part details",
drivingId: "partsorder",
key: "parts_return_confirmation",
},
csi_invitation: {
title: "Customer Survey Invitation",
description: "Customer Survey Invitation",
drivingId: "csi",
key: "csi_invitation",
},
payment_receipt: {
title: "Payment Receipt",
description: "Receipt of payment for customer",
drivingId: "payment",
key: "payment_receipt",
},
};
};