import axios from "axios"; import CryptoJS from "crypto-js"; import _ from "lodash"; import { azureHeaders } from "../../../actions/core/headers"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; import { hashAPIPath } from "../../../actions/core/hash"; const WORDKEY = process.env.HASHKEY; const WEBAPI_URL = process.env.RELAY_ROOT || "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; function flattenWatchlistEntry(entry) { const flattened = { ...entry }; if (entry.pinswg_Contact) { flattened.contactid = entry.pinswg_Contact.contactid; flattened.contact_email = entry.pinswg_Contact.emailaddress1; delete flattened.pinswg_Contact; // Optional: remove nested object } return flattened; } export default async function ApiProxy(req, res) { var token = await getToken(); var queryUrl = "pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications ne null&$expand=pinswg_Contact($select=contactid,emailaddress1)&$select=pinswg_emailnotifications,_pinswg_watchedcase_value"; return axios .get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeaders(token.access_token) ) .then(({ data }) => { var dataStr; const flattenedResults = data.value.map(flattenWatchlistEntry); res.status(200).json(flattenedResults); }) .catch((error) => { consoleLogger(error); res.status(400).json(error); }); }