16578 mailout api from mailing list
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import axios from "axios";
|
||||
import CryptoJS from "crypto-js";
|
||||
import _ from "lodash";
|
||||
import { azureHeadersPaged, consoleLogger, getToken } from "../../../actions";
|
||||
|
||||
const WORDKEY = process.env.HASHKEY;
|
||||
const WEBAPI_URL =
|
||||
process.env.RELAY_ROOT ||
|
||||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
||||
|
||||
/**
|
||||
* Generates a hash-secured query string.
|
||||
*/
|
||||
const hashAPIPath = (queryPath) => {
|
||||
const hash = CryptoJS.HmacSHA256(
|
||||
queryPath,
|
||||
CryptoJS.enc.Hex.parse(WORDKEY)
|
||||
).toString(CryptoJS.enc.Hex);
|
||||
return queryPath.includes("?") ? `&hash=${hash}` : `?hash=${hash}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Encrypts a document reference into a secure hash link.
|
||||
*/
|
||||
const encryptDocReference = (documentRef) => {
|
||||
const hash = CryptoJS.HmacSHA256(
|
||||
`documents/download/${documentRef}`,
|
||||
CryptoJS.enc.Hex.parse(WORDKEY)
|
||||
).toString(CryptoJS.enc.Hex);
|
||||
return `/api/documents/download/${documentRef}?hash=${hash}`;
|
||||
};
|
||||
|
||||
export default async function ApiProxy(req, res) {
|
||||
const incidentID = req.query.incidentid;
|
||||
|
||||
if (!incidentID) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "incidentid query parameter is required." });
|
||||
}
|
||||
|
||||
try {
|
||||
const token = await getToken();
|
||||
|
||||
// Date range: today and 7 days ago
|
||||
const now = new Date();
|
||||
const twoWeeksAgo = new Date();
|
||||
twoWeeksAgo.setDate(now.getDate() - 7);
|
||||
|
||||
const nowStr = now.toISOString();
|
||||
const twoWeeksAgoStr = twoWeeksAgo.toISOString();
|
||||
|
||||
// Build query
|
||||
const query =
|
||||
`pinswg_documents?$count=true` +
|
||||
`&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq ${incidentID}` +
|
||||
` and pinswg_documentpublisheddate ne null` +
|
||||
` and pinswg_documentpublisheddate ge ${twoWeeksAgoStr}` +
|
||||
` and pinswg_documentpublisheddate le ${nowStr}` +
|
||||
`&$select=pinswg_isharedocumentlocations,` +
|
||||
`_pinswg_documentids_value,` +
|
||||
`pinswg_isharelabelcasetype,` +
|
||||
`pinswg_isharelabellpaname,` +
|
||||
`pinswg_publishtoweb,` +
|
||||
`pinswg_uploadstatus,` +
|
||||
`pinswg_isharedocumentclassification,` +
|
||||
`pinswg_isharedocumentreference,` +
|
||||
`pinswg_name,` +
|
||||
`pinswg_latestpublishedversion,` +
|
||||
`pinswg_latestpublisheddate,` +
|
||||
`pinswg_documentpublisheddate`;
|
||||
|
||||
const fullUrl = `${WEBAPI_URL}${query}${hashAPIPath(query)}`;
|
||||
|
||||
console.log("Querying documents from:", fullUrl);
|
||||
|
||||
const { data } = await axios.get(
|
||||
fullUrl,
|
||||
azureHeadersPaged(token.access_token)
|
||||
);
|
||||
|
||||
// Add hash download links to each result
|
||||
const resultsWithLinks = data.value.map((doc) => ({
|
||||
...doc,
|
||||
pinswg_hashlink: encryptDocReference(
|
||||
doc.pinswg_isharedocumentreference
|
||||
),
|
||||
}));
|
||||
|
||||
res.status(200).json({ ...data, value: resultsWithLinks });
|
||||
} catch (error) {
|
||||
consoleLogger(error);
|
||||
res.status(500).json({
|
||||
error: "Failed to fetch document details.",
|
||||
details: error.message || error.toString(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user