Files
pedwfrontend/pages/api/endpoint/getsearchdocumentdetails_api.js
T
2026-06-29 13:08:06 +00:00

88 lines
3.1 KiB
JavaScript

/**
* @swagger
* /api/endpoint/getsearchdocumentdetails_api:
* get:
* tags: [Search,Documents]
* description: Basic search documents details
* parameters:
* - name: incidentid
* in: query
* required: true
* example: 50b533aa-43f1-ec11-aade-00224800be9c
* schema:
* type: string
* responses:
* 200:
* description: Success
*/
import CryptoJS from "crypto-js";
import _ from "lodash";
import { azureHeadersPaged } from "../../../actions/core/headers";
import { hasOwn } from "../../../components/utils";
import { respondError } from "../middleware/apiResponse";
import { relayGet } from "../middleware/relayForwarding";
const WORDKEY = process.env.HASHKEY;
const encryptDocReference = (documentRef) => {
var hashlink = CryptoJS.HmacSHA256(
"documents/download/" + documentRef,
CryptoJS.enc.Hex.parse(WORDKEY)
);
hashlink = hashlink.toString(CryptoJS.enc.Hex);
var hashStr =
"/api/documents/download/" + documentRef + "?hash=" + hashlink;
return hashStr;
};
export default async function ApiProxy(req, res) {
const incidentID = req.query.incidentid;
if (typeof incidentID !== "string" || incidentID.trim().length === 0) {
return respondError(res, {
status: 400,
code: "INCIDENT_ID_REQUIRED",
message: "incidentid is required"
});
}
const queryUrl =
"pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " +
incidentID +
" and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null&$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";
return relayGet({
queryUrl,
res,
requestOptionsBuilder: (accessToken) => azureHeadersPaged(accessToken),
transformData: (data) => {
data.value.forEach(function (element) {
Object.assign(element, {
"pinswg_documentpublisheddate":
element.pinswg_documentpublisheddate != null
? element.pinswg_documentpublisheddate
: element.pinswg_latestpublisheddate
});
element.pinswg_hashlink = encryptDocReference(
element.pinswg_isharedocumentreference
);
});
if (hasOwn(data, "@odata.nextLink") === true) {
const dataStr = JSON.stringify(data["@odata.nextLink"]);
data["@odata.nextLink"] = dataStr.split("/v9.2/")[1];
}
return data;
},
errorResponse: {
status: 400,
code: "SEARCH_DOCUMENT_DETAILS_FETCH_FAILED",
message: "Failed to fetch search document details"
}
});
}