// /** // * @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 CryptoJS from "crypto-js"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; const WORDKEY = process.env.HASHKEY; const WEBAPI_URL = process.env.RELAY_ROOT || "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; const hashAPIPath = (queryPath) => { var hashlink = CryptoJS.HmacSHA256( queryPath, CryptoJS.enc.Hex.parse(WORDKEY) ); hashlink = hashlink.toString(CryptoJS.enc.Hex); //return "&hash=" + hashlink; return (queryPath.indexOf("?") > -1 ? "&hash=" : "?hash=") + hashlink; }; // export default async function ApiProxy(req, res) { // var token = await getToken(); // var data = JSON.stringify(req.body); // var queryUrl = "pinswg_watchlists"; // console.log(data); // var config = { // method: "put", // 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, // }; // return axios(config) // .then(({ data }) => { // res.status(200).json(data); // }) // .catch((error) => { // consoleLogger(Object.assign(err, data)); // res.status(400).json(error); // }); // } const recordExists = async (incidentId, contactId, token) => { const filter = `$filter=pinswg_WatchedCase/incidentid eq ${incidentId} and pinswg_Contact/contactid eq ${contactId}`; const queryUrl = `pinswg_watchlists?${filter}`; const url = WEBAPI_URL + queryUrl + hashAPIPath(queryUrl); try { const res = await axios.get(url, { headers: { Authorization: "Bearer " + token.access_token, Accept: "application/json" } }); if (res.data.value && res.data.value.length > 0) { return res.data.value[0]; // return the existing record } return null; } catch (err) { throw err; } }; export default async function ApiProxy(req, res) { const token = await getToken(); const data = req.body; const incidentId = data["pinswg_WatchedCase@odata.bind"].match(/\(([^)]+)\)/)[1]; const contactId = data["pinswg_Contact@odata.bind"].match(/\(([^)]+)\)/)[1]; const existingRecord = await recordExists(incidentId, contactId, token); let method, queryUrl; if (existingRecord) { method = "patch"; queryUrl = `pinswg_watchlists(${existingRecord.pinswg_watchlistid})`; // adjust with your primary key logical name } else { method = "post"; queryUrl = "pinswg_watchlists"; } const config = { method: method, 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(data) }; return axios(config) .then(({ data }) => res.status(200).json(data)) .catch((err) => { consoleLogger(err); res.status(400).json(err.response?.data || err); }); }