From 00cb6737791208da8f919b55bf4637e839867a87 Mon Sep 17 00:00:00 2001 From: Patrick Fic <> Date: Tue, 2 Mar 2021 11:59:38 -0800 Subject: [PATCH] Added offset to report server call IO-729 --- client/src/utils/RenderTemplate.js | 84 ++++++++++++++++-------------- jsreport/jsreport.config.json | 5 +- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/client/src/utils/RenderTemplate.js b/client/src/utils/RenderTemplate.js index 99527c885..e5c7b1556 100644 --- a/client/src/utils/RenderTemplate.js +++ b/client/src/utils/RenderTemplate.js @@ -1,12 +1,13 @@ +import { gql } from "@apollo/client"; import { notification } from "antd"; import axios from "axios"; -import { gql } from "@apollo/client"; import jsreport from "jsreport-browser-client-dist"; import { auth } from "../firebase/firebase.utils"; import { setEmailOptions } from "../redux/email/email.actions"; import { store } from "../redux/store"; import client from "../utils/GraphQLClient"; import { TemplateList } from "./TemplateConstants"; +import moment from "moment"; const server = process.env.REACT_APP_REPORTS_SERVER_URL; jsreport.serverUrl = server; @@ -36,6 +37,7 @@ export default async function RenderTemplate( ...templateObject.context, headerpath: `/${bodyshop.imexshopid}/header.html`, bodyshop: bodyshop, + offset: moment().utcOffset(), }, }; try { @@ -59,23 +61,46 @@ export async function RenderTemplates( ) { //Query assets that match the template name. Must be in format <>.query let templateAndData = []; - let proms = []; - templateObjects.forEach((template) => { - proms.push( - (async () => { - let { contextData, useShopSpecificTemplate } = await fetchContextData( - template - ); - templateAndData.push({ - templateObject: template, - contextData, - useShopSpecificTemplate, - }); - })() - ); - }); - await Promise.all(proms); - let rootTemplate = templateAndData.shift(); + + console.log("About to await all promises."); + await Promise.all( + templateObjects.map(async (template) => { + console.log("executing for", template); + let { contextData, useShopSpecificTemplate } = await fetchContextData( + template + ); + templateAndData.push({ + templateObject: template, + contextData, + useShopSpecificTemplate, + }); + }) + ); + console.log("All promises awaited.", templateAndData); + let rootTemplate = templateAndData[0]; + const pdfOps = []; + + for (var i = 1; i < templateAndData.length; i++) { + pdfOps.push({ + template: { + name: templateAndData[i].useShopSpecificTemplate + ? `/${bodyshop.imexshopid}/${templateAndData[i].templateObject.name}` + : `/${templateAndData[i].templateObject.name}`, + ...(renderAsHtml ? {} : { recipe: "chrome-pdf" }), + }, + data: { + ...templateAndData[i].contextData, + ...templateAndData[i].templateObject.variables, + ...templateAndData[i].templateObject.context, + headerpath: `/${bodyshop.imexshopid}/header.html`, + bodyshop: bodyshop, + offset: moment().utcOffset(), + }, + type: "append", + mergeWholeDocument: true, + renderForEveryPage: true, + }); + } let reportRequest = { template: { @@ -83,26 +108,7 @@ export async function RenderTemplates( ? `/${bodyshop.imexshopid}/${rootTemplate.templateObject.name}` : `/${rootTemplate.templateObject.name}`, ...(renderAsHtml ? {} : { recipe: "chrome-pdf" }), - pdfOperations: templateAndData.map((template) => { - return { - template: { - name: template.useShopSpecificTemplate - ? `/${bodyshop.imexshopid}/${template.templateObject.name}` - : `/${template.templateObject.name}`, - ...(renderAsHtml ? {} : { recipe: "chrome-pdf" }), - }, - data: { - ...template.contextData, - ...template.templateObject.variables, - ...template.templateObject.context, - headerpath: `/${bodyshop.imexshopid}/header.html`, - bodyshop: bodyshop, - }, - type: "append", - mergeWholeDocument: true, - renderForEveryPage: true, - }; - }), + pdfOperations: pdfOps, }, data: { ...rootTemplate.contextData, @@ -110,6 +116,7 @@ export async function RenderTemplates( ...rootTemplate.templateObject.context, headerpath: `/${bodyshop.imexshopid}/header.html`, bodyshop: bodyshop, + offset: moment().utcOffset(), }, }; @@ -149,6 +156,7 @@ export const GenerateDocuments = async (templates) => { }; const fetchContextData = async (templateObject) => { + console.log("Fetching context data", templateObject); jsreport.headers["Authorization"] = "Bearer " + (await auth.currentUser.getIdToken()); diff --git a/jsreport/jsreport.config.json b/jsreport/jsreport.config.json index 01d2eb3a6..216126086 100644 --- a/jsreport/jsreport.config.json +++ b/jsreport/jsreport.config.json @@ -54,12 +54,15 @@ }, "enabled": true }, + "child-templates": { + "parallelLimit": 10 + }, "sample-template": { "createSamples": true }, "scripts": { + "allowedModules": "*", "strategy": "http-server" } } } -