import axios from "axios"; import { azureHeaders } from "../../../actions/core/headers"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; import { hashAPIPath } from "../../../actions/core/hash"; import { respondError, respondSuccess } from "../middleware/apiResponse"; 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) { const token = await getToken(); const queryUrl = "pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications ne null&$expand=pinswg_Contact($select=contactid,emailaddress1)&$select=pinswg_emailnotifications,_pinswg_watchedcase_value"; try { const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeaders(token.access_token) ); const flattenedResults = data.value.map(flattenWatchlistEntry); return respondSuccess(res, flattenedResults); } catch (error) { consoleLogger(error); return respondError(res, { status: 400, code: "MAILING_LIST_FETCH_FAILED", message: "Failed to fetch mailing list" }); } }