192 lines
6.2 KiB
JavaScript
192 lines
6.2 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/endpoint/getsearchdocumentdetailspaged_api:
|
|
* get:
|
|
* tags: [Search,]
|
|
* description: Basic search documents details paged
|
|
* parameters:
|
|
* - name: incidentid
|
|
* in: query
|
|
* required: true
|
|
* example: 50b533aa-43f1-ec11-aade-00224800be9c
|
|
* schema:
|
|
* type: string
|
|
* - name: pageNumber
|
|
* in: query
|
|
* required: true
|
|
* example: 1
|
|
* schema:
|
|
* type: string
|
|
* - name: orderby
|
|
* in: query
|
|
* required: true
|
|
* example: createdon
|
|
* schema:
|
|
* type: string
|
|
* - name: fieldSort
|
|
* in: query
|
|
* required: true
|
|
* description: asc or desc
|
|
* example: asc
|
|
* schema:
|
|
* type: string
|
|
* - name: showNumberOfRecords
|
|
* in: query
|
|
* required: true
|
|
* description: number of records to return per page
|
|
* example: 10
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Success
|
|
*/
|
|
|
|
import CryptoJS from "crypto-js";
|
|
import { azureHeadersPagedCustom } 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 pageNumber = req.query.pageNumber;
|
|
const incidentID = req.query.incidentid;
|
|
const orderby = req.query.orderby;
|
|
const fieldSort = req.query.fieldSort;
|
|
const showNumberOfRecords = req.query.showNumberOfRecords;
|
|
const documentType = req.query.documentType || "all";
|
|
|
|
if (typeof incidentID !== "string" || incidentID.trim().length === 0) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "INCIDENT_ID_REQUIRED",
|
|
message: "incidentid is required"
|
|
});
|
|
}
|
|
|
|
if (typeof orderby !== "string" || orderby.trim().length === 0) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "ORDER_BY_REQUIRED",
|
|
message: "orderby is required"
|
|
});
|
|
}
|
|
|
|
if (typeof fieldSort !== "string" || fieldSort.trim().length === 0) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "FIELD_SORT_REQUIRED",
|
|
message: "fieldSort is required"
|
|
});
|
|
}
|
|
|
|
if (
|
|
typeof showNumberOfRecords !== "string" ||
|
|
showNumberOfRecords.trim().length === 0
|
|
) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "SHOW_NUMBER_OF_RECORDS_REQUIRED",
|
|
message: "showNumberOfRecords is required"
|
|
});
|
|
}
|
|
|
|
// (documentType != "all"
|
|
// ? documentType.indexOf(",")<0?" and pinswg_isharedocumentlocations eq " + documentType :
|
|
// : "") +
|
|
|
|
let docTypeQueryString = "";
|
|
|
|
if (documentType != "all") {
|
|
if (documentType.indexOf(",") >= 0) {
|
|
const documentTypeArr = documentType.split(",");
|
|
|
|
docTypeQueryString = " and (";
|
|
documentTypeArr.forEach(function (item, index) {
|
|
if (item != "all") {
|
|
docTypeQueryString +=
|
|
" pinswg_isharedocumentlocations eq " + item;
|
|
|
|
docTypeQueryString +=
|
|
index < documentTypeArr.length - 1 ? " or " : "";
|
|
}
|
|
});
|
|
docTypeQueryString += " )";
|
|
} else {
|
|
docTypeQueryString +=
|
|
" and pinswg_isharedocumentlocations eq " + documentType;
|
|
}
|
|
}
|
|
|
|
// console.log(
|
|
// "documentType:",
|
|
// documentType,
|
|
// "docTypeQueryString:",
|
|
// docTypeQueryString
|
|
// );
|
|
|
|
//console.log("has this passed docuemntType:", documentType);
|
|
//(documentType !="all" && " pinswg_pinswg_isharedocumentlocations eq " + )
|
|
|
|
const queryUrl =
|
|
"pinswg_documents?$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " +
|
|
incidentID +
|
|
" and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null" +
|
|
docTypeQueryString +
|
|
"&$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&$orderby=" +
|
|
orderby +
|
|
" " +
|
|
fieldSort +
|
|
"&$count=true" +
|
|
(typeof pageNumber != "undefined"
|
|
? "&$skiptoken=" + ('<cookie pagenumber="' + pageNumber + '" />')
|
|
: "");
|
|
|
|
return relayGet({
|
|
queryUrl,
|
|
res,
|
|
requestOptionsBuilder: (accessToken) =>
|
|
azureHeadersPagedCustom(accessToken, showNumberOfRecords),
|
|
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_PAGED_FETCH_FAILED",
|
|
message: "Failed to fetch paged search document details"
|
|
}
|
|
});
|
|
}
|