diff --git a/pages/api/file/downloadblob.js b/pages/api/file/downloadblob.js index eabd0aa8..f7683731 100644 --- a/pages/api/file/downloadblob.js +++ b/pages/api/file/downloadblob.js @@ -3,6 +3,7 @@ import { downloadFile } from "../../../actions/azurestorage"; import nextConnect from "next-connect"; import { hashAPIPath } from "../../../actions/core/hash"; import middleware from "../middleware/middleware"; +import { respondError } from "../middleware/apiResponse"; const ApiProxy = nextConnect(); @@ -24,7 +25,11 @@ ApiProxy.get(async (req, res) => { typeof checkHash === "undefined" || checkHash.length === 0 ) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "MISSING_REQUIRED_QUERY", + message: "container, casefolderID, blobname and hash are required" + }); } var checkquerypath = @@ -36,24 +41,26 @@ ApiProxy.get(async (req, res) => { blobName.trim(); if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "INVALID_HASH", + message: "Invalid hash" + }); } - if (hashAPIPath(checkquerypath) == "&hash=" + checkHash) { - const bloblocation = - casefolderID + (blobName.indexOf(".json") > 0 ? "/" : "/files/"); + 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); }); async function streamToBuffer(readableStream) { diff --git a/pages/api/file/generateappealpdf.js b/pages/api/file/generateappealpdf.js index 5915501a..e90284c9 100644 --- a/pages/api/file/generateappealpdf.js +++ b/pages/api/file/generateappealpdf.js @@ -104,6 +104,7 @@ import { finalComments_pdf } from "../../../components/pdftemplates/finalComment 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, respondSuccess } from "../middleware/apiResponse"; //const ApiProxy = nextConnect(); // ApiProxy.use(middleware); @@ -132,7 +133,12 @@ export default async function handler(req, res) { typeof casefolderID === "undefined" || String(casefolderID).length === 0 ) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "MISSING_REQUIRED_QUERY", + message: + "hash, appealType, containerID and casefolderID are required" + }); } checkquerypath = @@ -142,7 +148,11 @@ export default async function handler(req, res) { (isDownload ? "&download=true" : ""); if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "INVALID_HASH", + message: "Invalid hash" + }); } // Create Document Component @@ -238,20 +248,28 @@ export default async function handler(req, res) { renderedPDFBuffer, containerID, blobProgress.pinswg_name || blobProgress.caseObj.ticketnumber - ).then((data) => { - 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 res.status(200).json({ - status: "success", - data: data, - path: `/files/${pdfFileName}.pdf` + ) + .then((data) => { + 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(() => { + return respondError(res, { + status: 400, + code: "GENERATE_APPEAL_PDF_FAILED", + message: "Failed to generate appeal PDF" + }); }); - }); } diff --git a/pages/api/file/getbloblist.js b/pages/api/file/getbloblist.js index ff56f48c..da29a655 100644 --- a/pages/api/file/getbloblist.js +++ b/pages/api/file/getbloblist.js @@ -31,6 +31,7 @@ import { hashAPIPath } from "../../../actions/core/hash"; import { getBlobs, getRepsFilesBlobs } from "../../../actions/azurestorage"; +import { respondError, respondSuccess } from "../middleware/apiResponse"; import nextConnect from "next-connect"; import middleware from "../middleware/middleware"; @@ -52,7 +53,11 @@ ApiProxy.get(async (req, res) => { typeof checkHash === "undefined" || checkHash.length === 0 ) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "MISSING_REQUIRED_QUERY", + message: "container, casefolderID and hash are required" + }); } var checkquerypath = @@ -62,20 +67,23 @@ ApiProxy.get(async (req, res) => { casefolderID; if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "INVALID_HASH", + message: "Invalid hash" + }); } - casefolderID.split("/").length > 1 - ? await getRepsFilesBlobs( - containerName, - casefolderID.split("/")[0], - casefolderID.split("/")[1] - ).then((data) => { - return res.status(200).json({ "value": [data] }); - }) - : await getBlobs(containerName, casefolderID).then((data) => { - return res.status(200).json({ "value": [data] }); - }); + const data = + casefolderID.split("/").length > 1 + ? await getRepsFilesBlobs( + containerName, + casefolderID.split("/")[0], + casefolderID.split("/")[1] + ) + : await getBlobs(containerName, casefolderID); + + return respondSuccess(res, { "value": [data] }); }); export const config = { diff --git a/pages/api/file/upload.js b/pages/api/file/upload.js index 1185f174..80038527 100644 --- a/pages/api/file/upload.js +++ b/pages/api/file/upload.js @@ -4,6 +4,7 @@ import { uploadFile } from "../../../actions/azurestorage"; import { hashAPIPath } from "../../../actions/core/hash"; +import { respondError, respondSuccess } from "../middleware/apiResponse"; import nextConnect from "next-connect"; import middleware from "../middleware/middleware"; @@ -15,13 +16,21 @@ ApiProxy.post(async (req, res) => { var checkHash = req.query.hash; if (typeof checkHash === "undefined" || checkHash.length === 0) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "HASH_REQUIRED", + message: "hash is required" + }); } var checkquerypath = "/api/file/upload"; if (hashAPIPath(checkquerypath) != "?hash=" + checkHash) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "INVALID_HASH", + message: "Invalid hash" + }); } const appealData = req.body.appealData; @@ -35,28 +44,28 @@ ApiProxy.post(async (req, res) => { typeof casefolderID === "undefined" || casefolderID.length === 0 ) { - return res.status(400).json(); + return respondError(res, { + status: 400, + code: "MISSING_REQUIRED_BODY", + message: "containerID and casefolderID are required" + }); } - repOrAppeal - ? createRepBlob(appealData, containerID, casefolderID).then((data) => { - // Object.keys(req.files).length > 0 && - // uploadFile(req.files, containerID, casefolderID).then( - // (data) => { - // return res.status(200).json({ data }); - // } - // ); - return res.status(200).json({ data }); - }) - : createBlob(appealData, containerID, casefolderID).then((data) => { - // Object.keys(req.files).length > 0 && - // uploadFile(req.files, containerID, casefolderID).then( - // (data) => { - // return res.status(200).json({ data }); - // } - // ); - return res.status(200).json({ data }); - }); + return ( + repOrAppeal + ? createRepBlob(appealData, containerID, casefolderID) + : createBlob(appealData, containerID, casefolderID) + ) + .then((data) => { + return respondSuccess(res, { data }); + }) + .catch(() => { + return respondError(res, { + status: 400, + code: "UPLOAD_FAILED", + message: "Failed to upload blob" + }); + }); }); export const config = {