Files
pedwfrontend/pages/api/file/createappealcompletemessage_api.js
T

188 lines
5.4 KiB
JavaScript

import { updateCrmContact } from "../../../lib/server/updateCrmContact";
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";
import { respondError, respondSuccess } from "../middleware/apiResponse";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
const ApiProxy = nextConnect();
ApiProxy.use(middleware);
const hasValue = (value) =>
typeof value === "string" && value.trim().length > 0;
const isValidContainer = (value) =>
typeof value === "string" &&
value.length >= 3 &&
value.length <= 63 &&
/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(value);
const isValidTempCaseRef = (value) =>
typeof value === "string" &&
value.length <= 100 &&
!value.includes("..") &&
/^[A-Za-z0-9](?:[A-Za-z0-9/-]*[A-Za-z0-9])?$/.test(value);
const forbidden = (res) =>
respondError(res, {
status: 403,
code: "FORBIDDEN",
message: "Request not permitted"
});
ApiProxy.get(async (req, res) => {
const containerName = req.query.container;
const tempCaseRef = req.query.tempcaseref;
const typeofinvolvement = req.query.inv;
const checkHash = req.query.hash;
if (
!hasValue(containerName) ||
!hasValue(tempCaseRef) ||
!hasValue(checkHash)
) {
return respondError(res, {
status: 400,
code: "MISSING_REQUIRED_QUERY",
message: "container, tempcaseref and hash are required"
});
}
if (!isValidContainer(containerName) || !isValidTempCaseRef(tempCaseRef)) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
const session = await getServerSession(req, res, authOptions(req, res));
if (
typeof session?.user?.id !== "string" ||
session.user.id.length === 0 ||
session.user.id !== containerName
) {
return forbidden(res);
}
const hashCandidatePaths = [
"/api/file/createappealcompletemessage_api?container=" +
containerName +
"&tempcaseref=" +
tempCaseRef,
"/api/file/createappealcompletemessage_api?container=" +
encodeURIComponent(containerName) +
"&tempcaseref=" +
encodeURIComponent(tempCaseRef)
];
const isHashValid = hashCandidatePaths.some(
(candidatePath) => hashAPIPath(candidatePath) == "&hash=" + checkHash
);
if (!isHashValid) {
return respondError(res, {
status: 400,
code: "INVALID_HASH",
message: "Invalid hash"
});
}
try {
const progressBlob = await getProgressBlobs(containerName, tempCaseRef);
const blobProgress = await downloadProgressFile(
containerName,
progressBlob.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
);
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
});
await getCaseBlob(containerName, tempCaseRef, caseObj);
let contactId = caseObj["customerid_contact@odata.bind"];
contactId = contactId.match(/\(([^)]+)\)/);
if (typeofinvolvement != 846040000) {
void (async () => {
try {
await updateCrmContact(contactId[1], {
pinswg_typeofinvolvement: 846040001
});
} catch (error) {
consoleLogger(error);
}
})();
}
await createCaseCompleteMessage(containerName, tempCaseRef);
return respondSuccess(res, {
status: "success"
});
} catch (error) {
consoleLogger(error);
return respondError(res, {
status: 400,
code: "CREATE_APPEAL_COMPLETE_MESSAGE_FAILED",
message: "Failed to create appeal complete message"
});
}
});
export const config = {
api: {
bodyParser: false
}
};
export default ApiProxy;