/** * @swagger * /api/endpoint/createmywatchedcases_api: * post: * tags: [Portal,Case] * summary: Phase 2 * description: Create my watched cases * responses: * 200: * description: Success */ import axios from "axios"; 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) { if ( !req.body || typeof req.body.contactid === "undefined" || req.body.contactid.length === 0 || typeof req.body.incidentid === "undefined" || req.body.incidentid.length === 0 ) { return respondError(res, { status: 400, code: "MISSING_REQUIRED_BODY", message: "contactid and incidentid are required" }); } const token = await getToken(); const contactid = req.body.contactid; const queryUrl = "incidents(" + req.body.incidentid + ")/pinswg_incident_contact_case_involvement/$ref"; const crmUrl = "https://" + process.env.CRMURL; const data = { "@odata.id": crmUrl + "/api/data/v8.2/contacts(" + contactid + ")" }; const config = { method: "post", url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), headers: { "OData-MaxVersion": "4.0", "OData-Version": "4.0", "Accept": "application/json", "If-None-Match": "null", "Prefer": 'odata.include-annotations="*",return=representation', "Authorization": "Bearer " + token.access_token, "Content-Type": "application/json" }, data: data }; try { const { data: responseData } = await axios(config); return respondSuccess(res, responseData); } catch (error) { const errorStatus = error?.status || error?.response?.status; if (errorStatus == 412) { return respondSuccess(res, { record: "exists" }); } consoleLogger(Object.assign(error, data)); return respondError(res, { status: 400, code: "CREATE_CASE_INVOLVEMENT_FAILED", message: "Failed to create case involvement" }); } }