74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
import axios from "axios";
|
|
import CryptoJS from "crypto-js";
|
|
import _ from "lodash";
|
|
import { getToken, azureHeaders, consoleLogger } from "../../../actions";
|
|
|
|
const WORDKEY = process.env.HASHKEY;
|
|
const WEBAPI_URL =
|
|
process.env.RELAY_ROOT ||
|
|
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
|
|
|
/**
|
|
* Generate hashed query string for secured API access.
|
|
*/
|
|
const hashAPIPath = (queryPath) => {
|
|
const hash = CryptoJS.HmacSHA256(
|
|
queryPath,
|
|
CryptoJS.enc.Hex.parse(WORDKEY)
|
|
).toString(CryptoJS.enc.Hex);
|
|
|
|
return queryPath.includes("?") ? `&hash=${hash}` : `?hash=${hash}`;
|
|
};
|
|
|
|
/**
|
|
* Main proxy handler.
|
|
*/
|
|
export default async function ApiProxy(req, res) {
|
|
const { incidentID } = req.query;
|
|
|
|
if (!incidentID) {
|
|
return res.status(400).json({ error: "incidentID is required" });
|
|
}
|
|
|
|
try {
|
|
const token = await getToken();
|
|
|
|
// Step 1: Get SIPs record linked to incident
|
|
const sipsQuery = `pinswg_sipses?$filter=_pinswg_sipscase_value eq ${incidentID}&$select=pinswg_sipsid`;
|
|
const sipsUrl = `${WEBAPI_URL}${sipsQuery}${hashAPIPath(sipsQuery)}`;
|
|
const sipsResponse = await axios.get(
|
|
sipsUrl,
|
|
azureHeaders(token.access_token)
|
|
);
|
|
|
|
const sipsRecords = _.get(sipsResponse, "data.value", []);
|
|
if (sipsRecords.length === 0) {
|
|
return res
|
|
.status(404)
|
|
.json({ error: "No SIPs record found for this incidentID." });
|
|
}
|
|
|
|
const sipsId = sipsRecords[0].pinswg_sipsid;
|
|
|
|
// Step 2: Get related SIP Events
|
|
const eventsQuery = `pinswg_sipsevents?$count=true&$filter=_pinswg_sipseventsid_value eq ${sipsId}&$select=pinswg_name,pinswg_dateeventrequested,pinswg_typeofevent,createdon`;
|
|
|
|
///&$select=pinswg_name,pinswg_dateeventrequested,pinswg_typeofevent
|
|
const eventsUrl = `${WEBAPI_URL}${eventsQuery}${hashAPIPath(
|
|
eventsQuery
|
|
)}`;
|
|
const eventsResponse = await axios.get(
|
|
eventsUrl,
|
|
azureHeaders(token.access_token)
|
|
);
|
|
|
|
res.status(200).json(eventsResponse.data);
|
|
} catch (error) {
|
|
consoleLogger(error);
|
|
res.status(500).json({
|
|
error: "An error occurred while retrieving data.",
|
|
details: error.message || error.toString(),
|
|
});
|
|
}
|
|
}
|