import { updateAccount } from "../../../actions/services/accountService"; import { hashAPIPath } from "../../../actions/core/hash"; import { consoleLogger } from "../../../actions/core/logger"; import { createCaseCompleteMessage, getProgressBlobs, downloadProgressFile, createBlob, getCaseBlob } from "../../../actions/azurestorage"; import _ from "lodash"; import nextConnect from "next-connect"; import middleware from "../middleware/middleware"; const ApiProxy = nextConnect(); ApiProxy.use(middleware); ApiProxy.get(async (req, res) => { var containerName = req.query.container; var tempCaseRef = req.query.tempcaseref; var typeofinvolvement = req.query.inv; var checkHash = req.query.hash; if ( typeof containerName === "undefined" || containerName.length === 0 || typeof tempCaseRef === "undefined" || tempCaseRef.length === 0 || typeof checkHash === "undefined" || checkHash.length === 0 ) { return res.status(400).json(); } var checkquerypath = "/api/file/createappealcompletemessage_api?container=" + containerName + "&tempcaseref=" + tempCaseRef; if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) { return res.status(400).json(); } const blobProgress = await getProgressBlobs( containerName, tempCaseRef ).then((data) => { return downloadProgressFile(containerName, data.path, tempCaseRef); }); const caseObj = await downloadProgressFile( containerName, tempCaseRef + "/" + tempCaseRef + "_case.json", tempCaseRef ); Object.assign(blobProgress, { "appealComplete": tempCaseRef }); delete blobProgress["pinswg_name"]; await createBlob(JSON.stringify(blobProgress), containerName, tempCaseRef) .then(() => { //update case.json with lpa ref and description let combinedAddress = blobProgress.pinswg_siteaddressline1 + (!_.isEmpty(blobProgress.pinswg_siteaddressline2) ? ", " + blobProgress.pinswg_siteaddressline2 : "") + (!_.isEmpty(blobProgress.pinswg_siteaddresstown) ? ", " + blobProgress.pinswg_siteaddresstown : ""); combinedAddress = combinedAddress + ", " + blobProgress.pinswg_siteaddresscounty + ", " + blobProgress.pinswg_siteaddresspostcode; Object.assign(caseObj, { "pinswg_lpareference": blobProgress.pinswg_lpaapplicationreference, "description": blobProgress.pinswg_developmentdescription, "pinswg_caseaddress": combinedAddress }); //send update case.json and then create queue message getCaseBlob(containerName, tempCaseRef, caseObj).then(() => { let contactId = caseObj["customerid_contact@odata.bind"]; contactId = contactId.match(/\(([^)]+)\)/); if (typeofinvolvement != 846040000) { updateAccount( contactId[1], { "pinswg_typeofinvolvement": 846040001 }, true ).catch((error) => { consoleLogger(error); }); } createCaseCompleteMessage(containerName, tempCaseRef); return res.status(200).json({ status: "success" }); }); }) .catch((error) => { consoleLogger(error); return res.status(400).json(error); }); }); export const config = { api: { bodyParser: false } }; export default ApiProxy;