162 lines
5.0 KiB
JavaScript
162 lines
5.0 KiB
JavaScript
import axios from "axios";
|
|
import CryptoJS from "crypto-js";
|
|
import _ from "lodash";
|
|
import { azureHeadersPaged } from "../../../actions/core/headers";
|
|
import { consoleLogger } from "../../../actions/core/logger";
|
|
import { getToken } from "../../../actions/core/token";
|
|
import { getCaseBlob, createBlob } from "../../../actions/azurestorage";
|
|
import { getTempCaseRef } from "../../../components/utils";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import { hashAPIPath } from "../../../actions/core/hash";
|
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
|
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "../auth/[...nextauth]";
|
|
|
|
const WEBAPI_URL =
|
|
process.env.RELAY_ROOT ||
|
|
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
|
|
|
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 isValidDraftFolder = (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"
|
|
});
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
let createCaseBody;
|
|
try {
|
|
createCaseBody =
|
|
typeof req.body === "string" ? JSON.parse(req.body) : req.body;
|
|
} catch (_error) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "INVALID_REQUEST",
|
|
message: "Invalid request"
|
|
});
|
|
}
|
|
|
|
var contactid = req.query.contactid;
|
|
var appealTypeId = req.query.appealTypeId;
|
|
var containerName = req.query.containername;
|
|
var lpaID = req.query.lpaID;
|
|
var queryUrl = "incidents";
|
|
|
|
if (
|
|
!hasValue(contactid) ||
|
|
!hasValue(appealTypeId) ||
|
|
!hasValue(containerName) ||
|
|
!hasValue(lpaID)
|
|
) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "MISSING_REQUIRED_QUERY",
|
|
message:
|
|
"contactid, appealTypeId, containername and lpaID are required"
|
|
});
|
|
}
|
|
|
|
if (
|
|
!createCaseBody ||
|
|
typeof createCaseBody !== "object" ||
|
|
Array.isArray(createCaseBody) ||
|
|
!isValidContainer(containerName) ||
|
|
["container", "containerID", "containername"].some(
|
|
(key) => Object.prototype.hasOwnProperty.call(createCaseBody, key)
|
|
) ||
|
|
["container", "containerID"].some((key) =>
|
|
Object.prototype.hasOwnProperty.call(req.query, key)
|
|
)
|
|
) {
|
|
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);
|
|
}
|
|
|
|
var tempCaseRef = getTempCaseRef();
|
|
|
|
if (!isValidDraftFolder(tempCaseRef)) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "INVALID_REQUEST",
|
|
message: "Invalid request"
|
|
});
|
|
}
|
|
|
|
var token = await getToken();
|
|
|
|
var data = {
|
|
"title": tempCaseRef,
|
|
"ticketnumber": tempCaseRef,
|
|
"incidentid": uuidv4(),
|
|
"statecode": 0,
|
|
"statuscode": 1,
|
|
"caseorigincode": 3,
|
|
"servicestage": 1,
|
|
"customerid_contact@odata.bind": "/contacts(" + contactid + ")",
|
|
"pinswg_appealcasetype": appealTypeId,
|
|
"pinswg_AssociatedLPA@odata.bind": "/accounts(" + lpaID + ")"
|
|
};
|
|
var newData = Object.assign(data, createCaseBody);
|
|
|
|
delete newData.lpaTypes;
|
|
delete newData.appealTypes;
|
|
|
|
var newAppealObj = {
|
|
"pinswg_name": tempCaseRef,
|
|
"pinswg_appealcasetype": appealTypeId,
|
|
"_pinswg_associatedlpa_value": lpaID
|
|
};
|
|
|
|
// var config = {
|
|
// method: "post",
|
|
// url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
|
// headers: {
|
|
// "OData-MaxVersion": "4.0",
|
|
// "OData-Version": "4.0",
|
|
// "Accept": "application/json",
|
|
// "Prefer": 'odata.include-annotations="*",return=representation',
|
|
// "Authorization": "Bearer " + token.access_token,
|
|
// "Content-Type": "application/json",
|
|
// },
|
|
// data: JSON.stringify(newData),
|
|
// };
|
|
var apiResponse = _.isEmpty(req.query)
|
|
? respondError(res, {
|
|
status: 400,
|
|
code: "MISSING_REQUIRED_QUERY",
|
|
message: "Query parameters are required"
|
|
})
|
|
: (getCaseBlob(containerName, tempCaseRef, newData),
|
|
createBlob(JSON.stringify(newAppealObj), containerName),
|
|
respondSuccess(res, newData));
|
|
|
|
return apiResponse;
|
|
}
|