TASK22224: harden file/static endpoint contracts and phase21 coverage

This commit is contained in:
2026-03-23 16:40:39 +00:00
parent 78cac2cbb9
commit f8fba6a561
7 changed files with 352 additions and 127 deletions
@@ -1,4 +1,4 @@
import { respondSuccess } from "../middleware/apiResponse";
import { respondError, respondSuccess } from "../middleware/apiResponse";
/**
* @swagger
@@ -13,6 +13,14 @@ import { respondSuccess } from "../middleware/apiResponse";
*/
const ApiResponse = (req, res) => {
if (req.method && req.method !== "GET") {
return respondError(res, {
status: 405,
code: "METHOD_NOT_ALLOWED",
message: "Only GET is supported"
});
}
return respondSuccess(res, {
"value": [
{
+9 -24
View File
@@ -1,29 +1,14 @@
import { respondSuccess } from "../middleware/apiResponse";
import { respondError, respondSuccess } from "../middleware/apiResponse";
// export default async function ApiProxy(req, res) {
// var caseid = req.query.caseid;
// var token = await getToken();
export default function ApiProxy(req, res) {
if (req.method && req.method !== "GET") {
return respondError(res, {
status: 405,
code: "METHOD_NOT_ALLOWED",
message: "Only GET is supported"
});
}
// var queryUrl =
// "pinswg_sipsevents?$filter=_pinswg_sipseventsid_value eq " +
// caseid +
// "&$count=true";
// return axios
// .get(
// WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
// azureHeaders(token.access_token),
// )
// .then(({ data }) => {
// res.status(200).json(data);
// })
// .catch((error) => {
// consoleLogger(error);
// res.status(400).json(error);
// });
// }
export default function ApiProx(req, res) {
const data = {
"@odata.count": 4,
"value": [
+24 -29
View File
@@ -10,10 +10,10 @@ const ApiProxy = nextConnect();
ApiProxy.use(middleware);
ApiProxy.get(async (req, res) => {
var containerName = req.query.container;
var casefolderID = req.query.casefolderID;
var blobName = req.query.blobname;
var checkHash = req.query.hash;
const containerName = req.query.container;
const casefolderID = req.query.casefolderID;
const blobName = req.query.blobname;
const checkHash = req.query.hash;
if (
typeof containerName === "undefined" ||
@@ -32,7 +32,7 @@ ApiProxy.get(async (req, res) => {
});
}
var checkquerypath =
const checkquerypath =
"/api/file/downloadblob?container=" +
containerName +
"&casefolderID=" +
@@ -48,34 +48,29 @@ ApiProxy.get(async (req, res) => {
});
}
const bloblocation =
casefolderID + (blobName.indexOf(".json") > 0 ? "/" : "/files/");
try {
const bloblocation =
casefolderID + (blobName.indexOf(".json") > 0 ? "/" : "/files/");
const downloaded = await downloadFile(
containerName,
bloblocation + decodeURI(blobName)
);
const downloaded = await downloadFile(
containerName,
bloblocation + decodeURI(blobName)
);
res.setHeader(
"content-disposition",
"attachment; filename=" + decodeURI(blobName)
);
return res.status(200).send(downloaded);
res.setHeader(
"content-disposition",
"attachment; filename=" + decodeURI(blobName)
);
return res.status(200).send(downloaded);
} catch (error) {
return respondError(res, {
status: 400,
code: "DOWNLOAD_BLOB_FAILED",
message: "Unable to download blob"
});
}
});
async function streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
});
}
export const config = {
api: {
bodyParser: false
+64 -73
View File
@@ -1,45 +1,17 @@
import {
createContainer,
getContainers,
getBlobs,
createRepBlob,
uploadFile,
uploadPDFAppealFiles,
downloadProgressFile,
getProgressBlobs,
getTempCaseBlob,
createAppealPDFBlob
} from "../../../actions/azurestorage";
import {
getCaseByID,
getPortalModuleDetails,
getAppealPDFDocs
} from "../../../actions/services/caseService";
import { getFormCollectionByID } from "../../../components/utils";
import ReactPDF, {
Document,
Page,
Text,
View,
StyleSheet,
PDFViewer,
pdf
} from "@react-pdf/renderer";
import middleware from "../middleware/middleware";
import nextConnect from "next-connect";
import fs from "fs";
import path from "path";
import { pdf } from "@react-pdf/renderer";
import { getPickLists } from "../../../actions/services/referenceDataService";
import { planningappeals78_pdf } from "../../../components/pdftemplates/planningappeals78_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";
import { respondError } from "../middleware/apiResponse";
const getDetails = (resultsObj, detailsType) => {
const getDetails = (resultsObj) => {
if (!Array.isArray(resultsObj)) {
console.warn("getDetails: resultsObj is not an array");
return Promise.resolve([]);
}
@@ -51,7 +23,6 @@ const getDetails = (resultsObj, detailsType) => {
const form = getFormCollectionByID(appealType);
if (!form) {
console.warn("No form found for appeal type:", appealType);
return null;
}
@@ -64,54 +35,74 @@ const getDetails = (resultsObj, detailsType) => {
export default async function handler(req, res) {
const incidentid = req.body?.docProps;
if (!incidentid) return res.status(400).send("Missing incident ID");
const caseObj = await getCaseByID(incidentid);
if (!Array.isArray(caseObj) || caseObj.length === 0)
return res.status(404).send("Case not found");
const caseDetailsObj = await getDetails(caseObj, "myCases");
const fileList = (await getAppealPDFDocs(incidentid)) || [];
// Inject into the first item in `value` array
if (caseDetailsObj[0]?.value?.[0]) {
caseDetailsObj[0].value[0].pinswg_developmentdescription =
caseObj[0].description;
caseDetailsObj[0].value[0].caseObj = caseObj[0];
caseDetailsObj[0].value[0].filesList = fileList;
if (!incidentid) {
return respondError(res, {
status: 400,
code: "INCIDENT_ID_REQUIRED",
message: "Incident id is required"
});
}
const whichForm = getFormCollectionByID(caseObj[0].pinswg_appealcasetype);
const pickListData = await getPickLists(whichForm.UrlName);
try {
const caseObj = await getCaseByID(incidentid);
if (!Array.isArray(caseObj) || caseObj.length === 0) {
return respondError(res, {
status: 404,
code: "CASE_NOT_FOUND",
message: "Case not found"
});
}
const appealTypeMap = {
846040000: planningappeals78_pdf,
846040004: planningappeals78_pdf
};
const caseDetailsObj = await getDetails(caseObj);
const fileList = (await getAppealPDFDocs(incidentid)) || [];
//console.log(caseDetailsObj[0].value[0]);
if (caseDetailsObj[0]?.value?.[0]) {
caseDetailsObj[0].value[0].pinswg_developmentdescription =
caseObj[0].description;
caseDetailsObj[0].value[0].caseObj = caseObj[0];
caseDetailsObj[0].value[0].filesList = fileList;
}
const MyDocument = (values) => {
const type = caseObj[0].pinswg_appealcasetype;
const renderDocument = appealTypeMap[type] || other_pdf;
return renderDocument(values);
};
const whichForm = getFormCollectionByID(
caseObj[0].pinswg_appealcasetype
);
if (!whichForm?.UrlName) {
return respondError(res, {
status: 400,
code: "FORM_COLLECTION_NOT_FOUND",
message: "Unable to resolve form collection"
});
}
const pickListData = await getPickLists(whichForm.UrlName);
const pdfComponent = MyDocument({
docProps: caseDetailsObj[0].value[0],
pickListData
});
const appealTypeMap = {
846040000: planningappeals78_pdf,
846040004: planningappeals78_pdf
};
const pdfBuffer = await pdf(pdfComponent).toBuffer();
const renderDocument =
appealTypeMap[caseObj[0].pinswg_appealcasetype] || other_pdf;
const pdfComponent = renderDocument({
docProps: caseDetailsObj?.[0]?.value?.[0] || {},
pickListData
});
const repDate = new Date();
const formattedDate = repDate.toISOString().split("T")[0];
const pdfBuffer = await pdf(pdfComponent).toBuffer();
res.setHeader("Content-Type", "application/pdf");
res.setHeader(
"Content-Disposition",
`attachment; filename=${formattedDate}_-_Appeal_Form.pdf`
);
res.send(pdfBuffer);
const repDate = new Date();
const formattedDate = repDate.toISOString().split("T")[0];
res.setHeader("Content-Type", "application/pdf");
res.setHeader(
"Content-Disposition",
`attachment; filename=${formattedDate}_-_Appeal_Form.pdf`
);
return res.send(pdfBuffer);
} catch (error) {
return respondError(res, {
status: 400,
code: "APPEAL_PDF_COPY_GENERATION_FAILED",
message: "Unable to generate appeal PDF copy"
});
}
}