Files
pedwfrontend/pages/api/endpoint/createwatchedcases_api.js
T

134 lines
4.0 KiB
JavaScript

// /**
// * @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";
import { hashAPIPath } from "../../../actions/core/hash";
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 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 data = req.body;
const watchedCaseBind = data?.["pinswg_WatchedCase@odata.bind"];
const contactBind = data?.["pinswg_Contact@odata.bind"];
if (
typeof watchedCaseBind !== "string" ||
watchedCaseBind.length === 0 ||
typeof contactBind !== "string" ||
contactBind.length === 0
) {
return res.status(400).json();
}
const watchedCaseMatch = watchedCaseBind.match(/\(([^)]+)\)/);
const contactMatch = contactBind.match(/\(([^)]+)\)/);
if (!watchedCaseMatch || !contactMatch) {
return res.status(400).json();
}
const token = await getToken();
const incidentId = watchedCaseMatch[1];
const contactId = contactMatch[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);
});
}