/** * @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"; import { getPersonalAccount } from "../../../actions"; 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 incidentid = req.body.incidentid; const queryUrl = "pinswg_contactinvolvements"; const userInfo = await getPersonalAccount(contactid); const crmUrl = "https://" + process.env.CRMURL; const crmVersion = process.env.CRMURL_VERSION; var typeofinvolvement = { "appellant": 846040001, "interestedparty": 846040061, "agent": 846040000, "LPA": 846040012 }[req.body.involvement] || 846040061; const data = { "pinswg_email": userInfo.emailaddress1, "pinswg_lastname": userInfo.lastname, "pinswg_firstname": userInfo.firstname, "pinswg_typeofinvolvement": typeofinvolvement, "pinswg_Case@odata.bind": "/incidents(" + incidentid + ")", "pinswg_Contact@odata.bind": "/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_REP_INVOLVEMENT_FAILED", message: "Failed to create representation involvement" }); } }