/** * @swagger * /api/file/generateappealpdf: * post: * tags: [Azure Storage] * summary: Phase 2 * description: Create reps pdf from text field * consumes: * - application/json * requestBody: * name: PDF * description: PDF content body * required: true * content: * 'application/json': * schema: * type: object * properties: * representationType: * type: string * example: "Questionnaire" * representationQuestion1: * type: string * example: "question 111111" * representationQuestion2: * type: string * example: "question 232222" * representationCapacity: * type: string * example: "yes" * representationDocuments: * type: array * example: [{}] * pinswg_name: * type: string * example: "2023-8-24-1692882853853--REP_Bond" * containerID: * type: string * example: "cljzkgyev0006f9tl4pk6u171" * appealType: * type: integer * example: 846040000 * incidentID: * type: string * example: "50b533aa-43f1-ec11-aade-00224800be9c" * caseRef: * type: string * example: "CAS-00764-L0Q9W2" * parameters: * - name: container * in: query * required: true * example: cljzkgyev0006f9tl4pk6u171 * schema: * type: string * - name: casefolderID * in: query * required: true * example: CAS-00764-L0Q9W2 * schema: * type: string * - name: hash * in: query * required: true * example: 530b67db992ea277a236790be05b2e49bdd9dd02e6d790cc1c25f96cf39687cb * schema: * type: string * * responses: * 200: * description: Success */ import { downloadProgressFile, getProgressBlobs, getTempCaseBlob, createAppealPDFBlob } from "../../../actions/azurestorage"; import { hashAPIPath } from "../../../actions/core/hash"; import ReactPDF from "@react-pdf/renderer"; import { getPickLists } from "../../../actions/services/referenceDataService"; import { planningappeals78_pdf } from "../../../components/pdftemplates/planningappeals78_pdf"; import { other_pdf } from "../../../components/pdftemplates/other_pdf"; import { respondError, respondSuccess } from "../middleware/apiResponse"; //const ApiProxy = nextConnect(); // ApiProxy.use(middleware); // ApiProxy.post(async (req, res) => { export default async function handler(req, res) { const checkHash = req.query.hash; const appealType = req.query.appealType; const isDownload = req.query?.download === "true"; let appealBodyObj = typeof req.body === "string" ? JSON.parse(req.body) : req.body; const containerID = appealBodyObj.containerID; const casefolderID = appealBodyObj.casefolderID; const locale = appealBodyObj.locale || "en"; let checkquerypath = "/api/file/generateappealpdf"; if ( typeof checkHash === "undefined" || checkHash.length === 0 || typeof appealType === "undefined" || String(appealType).length === 0 || typeof containerID === "undefined" || String(containerID).length === 0 || typeof casefolderID === "undefined" || String(casefolderID).length === 0 ) { return respondError(res, { status: 400, code: "MISSING_REQUIRED_QUERY", message: "hash, appealType, containerID and casefolderID are required" }); } checkquerypath = checkquerypath + "?appealType=" + appealType + (isDownload ? "&download=true" : ""); if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) { return respondError(res, { status: 400, code: "INVALID_HASH", message: "Invalid hash" }); } // Create Document Component const MyDocument = (values) => { switch (values.docProps.pinswg_appealcasetype) { case "846040000": return planningappeals78_pdf(values); break; case "846040025": return listedbuildingandconservationareaconsen_pdf(values); break; case "846040004": return planningappeals78_pdf(values); break; case "846040018": return adverts_pdf(values); break; case "846040024": return nonvalidation_pdf(values); break; case "846040005": return enforcement_pdf(values); break; case "846040017": return highhedgestpo_pdf(values); break; default: return other_pdf(values); } }; try { const tempCaseBlob = await getTempCaseBlob(containerID, casefolderID); const progressBlob = await getProgressBlobs(containerID, casefolderID); const blobProgress = await downloadProgressFile( containerID, progressBlob.path, casefolderID ); const uniqueArray = appealBodyObj.filesList.filter( (obj, index, self) => { return index === self.findIndex((t) => t.name === obj.name); } ); Object.assign(blobProgress, { "filesList": uniqueArray, "caseObj": tempCaseBlob }); const pickListData = await getPickLists(appealType); // ReactPDF.render( // , // process.cwd() + `/tmp/${blobProgress.pinswg_name}.pdf` // ); // ReactPDF.renderToStream(); const streamToBuffer = async (readableStream) => { return new Promise((resolve, reject) => { const chunks = []; readableStream.on("data", (chunk) => { chunks.push( Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk) ); }); readableStream.on("end", () => resolve(Buffer.concat(chunks))); readableStream.on("error", reject); }); }; const renderedPDF = await ReactPDF.renderToStream( MyDocument({ docProps: { ...blobProgress, locale }, pickListData: pickListData }) ); const repDate = new Date(); let day = repDate.getDate(); let month = repDate.getMonth() + 1; let year = repDate.getFullYear(); const pdfFileName = year + "-" + ("0" + month).slice(-2) + "-" + ("0" + day).slice(-2) + "_-_Appeal_Form"; const renderedPDFBuffer = await streamToBuffer(renderedPDF); const data = await createAppealPDFBlob( renderedPDFBuffer, containerID, blobProgress.pinswg_name || blobProgress.caseObj.ticketnumber ); if (isDownload) { const filename = `${pdfFileName}.pdf`; res.setHeader("Content-Type", "application/pdf"); res.setHeader( "Content-Disposition", `attachment; filename="${filename}"` ); return res.status(200).send(renderedPDFBuffer); } return respondSuccess(res, { status: "success", data: data, path: `/files/${pdfFileName}.pdf` }); } catch (error) { return respondError(res, { status: 400, code: "GENERATE_APPEAL_PDF_FAILED", message: "Failed to generate appeal PDF" }); } }