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"; const WEBAPI_URL = process.env.RELAY_ROOT || "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; export default async function ApiProxy(req, res) { var createCaseBody = req.body; var contactid = req.query.contactid; var appealTypeId = req.query.appealTypeId; var containerName = req.query.containername; var lpaID = req.query.lpaID; var queryUrl = "incidents"; var token = await getToken(); var tempCaseRef = getTempCaseRef(); if ( typeof contactid === "undefined" || contactid.length === 0 || typeof appealTypeId === "undefined" || appealTypeId.length === 0 || typeof containerName === "undefined" || containerName.length === 0 || typeof lpaID === "undefined" || lpaID.length === 0 ) { return respondError(res, { status: 400, code: "MISSING_REQUIRED_QUERY", message: "contactid, appealTypeId, containername and lpaID are required" }); } 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; }