263 lines
7.6 KiB
JavaScript
263 lines
7.6 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/file/generatepdf:
|
|
* 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 {
|
|
createContainer,
|
|
getContainers,
|
|
getBlobs,
|
|
createRepBlob,
|
|
uploadFile,
|
|
uploadPDFRepFiles,
|
|
} from "../../../actions/azurestorage";
|
|
import { hashAPIPath } from "../../../actions";
|
|
|
|
import ReactPDF, {
|
|
Document,
|
|
Page,
|
|
Text,
|
|
View,
|
|
StyleSheet,
|
|
PDFViewer,
|
|
} from "@react-pdf/renderer";
|
|
import middleware from "../middleware/middleware";
|
|
import nextConnect from "next-connect";
|
|
import fs from "fs";
|
|
import { lpaQuestionnaire_pdf } from "../../../components/pdftemplates/lpaQuestionnaire_pdf";
|
|
import { finalComments_pdf } from "../../../components/pdftemplates/finalComments_pdf";
|
|
import { statement_pdf } from "../../../components/pdftemplates/statement_pdf";
|
|
import { writtenStatement_pdf } from "../../../components/pdftemplates/writtenStatement_pdf";
|
|
import { other_pdf } from "../../../components/pdftemplates/other_pdf";
|
|
|
|
// const ApiProxy = nextConnect();
|
|
// ApiProxy.use(middleware);
|
|
|
|
// ApiProxy.post(async (req, res) => {
|
|
|
|
export default function handler(req, res) {
|
|
var checkHash = req.query.hash;
|
|
|
|
let reqBodyobj = JSON.parse(req.body);
|
|
|
|
console.log(
|
|
"//////////////////////\nreqBodyobj" +
|
|
req +
|
|
"\n//////////////////////\n"
|
|
);
|
|
console.log(
|
|
"//////////////////////\nreqBodyobj" +
|
|
req.body +
|
|
"\n//////////////////////\n"
|
|
);
|
|
|
|
var containerID = reqBodyobj.containerID;
|
|
var casefolderID = reqBodyobj.casefolderID;
|
|
var caseRef = reqBodyobj.caseRef;
|
|
var representationType = reqBodyobj.representationType;
|
|
var repRaiser = reqBodyobj.lastname;
|
|
//console.log("there are files:", Object.keys(req.files).length);
|
|
|
|
var checkquerypath = "/api/file/generatepdf";
|
|
|
|
//console.log("-------", checkHash);
|
|
// console.log(hashAPIPath(checkquerypath) == "?hash=" + checkHash);
|
|
//console.log(casefolderID, caseRef);
|
|
// Create Document Component
|
|
|
|
const MyDocument = (values) => {
|
|
switch (values.docProps.representationType) {
|
|
case "Questionnaire":
|
|
return lpaQuestionnaire_pdf(values);
|
|
break;
|
|
case "Final comments":
|
|
return finalComments_pdf(values);
|
|
break;
|
|
case "Statement":
|
|
return statement_pdf(values);
|
|
break;
|
|
case "Written statement":
|
|
return writtenStatement_pdf(values);
|
|
break;
|
|
case "Other":
|
|
return other_pdf(values);
|
|
break;
|
|
default:
|
|
return other_pdf(values);
|
|
}
|
|
};
|
|
|
|
let repCapacity = "";
|
|
switch (reqBodyobj.representationCapacity) {
|
|
case "Appellant":
|
|
repCapacity = "APPELLANT";
|
|
break;
|
|
case "Agent":
|
|
repCapacity = "AGENT";
|
|
break;
|
|
case "Interested Party/Person":
|
|
repCapacity = "IP";
|
|
break;
|
|
case "Land Owner":
|
|
repCapacity = "LO";
|
|
break;
|
|
case "LPA":
|
|
repCapacity = "LPA";
|
|
break;
|
|
|
|
default:
|
|
// code block
|
|
}
|
|
|
|
let repType = "";
|
|
switch (reqBodyobj.representationType) {
|
|
case "Statement":
|
|
repType = "STATE";
|
|
break;
|
|
case "Statement of common ground":
|
|
repType = "SCG";
|
|
break;
|
|
case "Written statement":
|
|
repType = "WS";
|
|
break;
|
|
case "Written statement of evidence":
|
|
repType = "WSE";
|
|
break;
|
|
case "Questionnaire":
|
|
repType = "QUEST";
|
|
break;
|
|
case "Final comments":
|
|
repType = "FC";
|
|
break;
|
|
|
|
case "Other":
|
|
repType = "OTHER";
|
|
break;
|
|
|
|
default:
|
|
// code block
|
|
}
|
|
|
|
const repDate = new Date();
|
|
|
|
let day = ("0" + repDate.getDate()).slice(-2);
|
|
let month = ("0" + (repDate.getMonth() + 1)).slice(-2);
|
|
let year = repDate.getFullYear();
|
|
let time =
|
|
("0" + repDate.getHours()).slice(-2) +
|
|
"" +
|
|
("0" + repDate.getMinutes()).slice(-2);
|
|
|
|
let repDateStamp = "";
|
|
repDateStamp = year + "" + month + day + "-" + time;
|
|
|
|
console.log(repDateStamp);
|
|
//if (hashAPIPath(checkquerypath) == "?hash=" + checkHash) {
|
|
// //createContainer(containerID).then((containerName) => {
|
|
|
|
// console.log("does this get folder name:", containerID, casefolderID);
|
|
|
|
console.log(
|
|
"///////////////////////////////////\n file created: \n" +
|
|
`tmp/${caseRef}_rep_${repDateStamp}_${repCapacity}_${repRaiser}_${repType}.pdf` +
|
|
"\n/////////////////////////"
|
|
);
|
|
|
|
createRepBlob(req.body, containerID, caseRef);
|
|
|
|
ReactPDF.render(
|
|
<MyDocument docProps={reqBodyobj} />,
|
|
`tmp/${caseRef}_rep_${repDateStamp}_${repCapacity}_${repRaiser}_${repType}.pdf`
|
|
);
|
|
|
|
uploadPDFRepFiles(
|
|
`tmp/${caseRef}_rep_${repDateStamp}_${repCapacity}_${repRaiser}_${repType}.pdf`,
|
|
containerID,
|
|
caseRef,
|
|
repType
|
|
);
|
|
|
|
// to do upload file to storage account
|
|
|
|
// then remove from local folder
|
|
|
|
// fs.unlinkSync( `tmp/${caseRef}_rep_${repDateStamp}_${repCapacity}_${repRaiser}_${repType}.pdf`,);
|
|
|
|
return res.status(200).json({
|
|
data: "success",
|
|
path: `tmp/${caseRef}_rep_${repDateStamp}_${repCapacity}_${repRaiser}_${repType}.pdf`,
|
|
});
|
|
// } else {
|
|
// return res.status(400).json();
|
|
// }
|
|
}
|