import axios from "axios"; import CryptoJS from "crypto-js"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; 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 data = JSON.stringify(req.body); var appealObj = req.query.appealObj; var incidentID = req.query.incident; var updateFormCollection = req.query.updateFormCollection; var queryUrl = updateFormCollection + "(" + appealObj + ")"; if ( typeof updateFormCollection === "undefined" || updateFormCollection.length === 0 || typeof appealObj === "undefined" || appealObj.length === 0 || typeof incidentID === "undefined" || incidentID.length === 0 || typeof req.body === "undefined" || req.body === null ) { return respondError(res, { status: 400, code: "MISSING_REQUIRED_QUERY", message: "updateFormCollection, appealObj, incident and body are required" }); } var token = await getToken(); var config = { method: "patch", 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: data }; //console.log("update case:", data); return axios(config) .then(({ data }) => { return respondSuccess(res, data); }) .catch((error) => { consoleLogger(error); return respondError(res, { status: 400, code: "UPDATE_CASE_FAILED", message: "Failed to update case" }); }); }