Files
pedwfrontend/pages/api/email/getmailinglist.js
T

59 lines
1.7 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/";
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;
};
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);
});
}