169 lines
5.2 KiB
JavaScript
169 lines
5.2 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/endpoint/getbasicsearch_api:
|
|
* get:
|
|
* tags: [Search]
|
|
* description: Basic search
|
|
* parameters:
|
|
* - name: searchString
|
|
* in: query
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Success
|
|
*/
|
|
|
|
import axios from "axios";
|
|
import CryptoJS from "crypto-js";
|
|
import _ from "lodash";
|
|
import {
|
|
azureHeadersPagedCustom,
|
|
consoleLogger,
|
|
getToken,
|
|
} from "../../../actions";
|
|
import { number } from "prop-types";
|
|
|
|
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;
|
|
};
|
|
|
|
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) {
|
|
var pageNumber = req.query.pageNumber || 1;
|
|
var token = await getToken();
|
|
|
|
var orderby = req.query.orderby || "createdon";
|
|
var fieldSort = req.query.fieldSort || "desc";
|
|
var showNumberOfRecords = req.query.showNumberOfRecords || 10;
|
|
var documentType = req.query.documentType || "all";
|
|
var numberOfWeeks = req.query.numberWeeks || 1;
|
|
|
|
var docTypeQueryString = "";
|
|
|
|
if (documentType != "all") {
|
|
if (documentType.indexOf(",") >= 0) {
|
|
const documentTypeArr = documentType.split(",");
|
|
|
|
docTypeQueryString = " and (";
|
|
documentTypeArr.forEach(function (item, index) {
|
|
console.log(item, index);
|
|
|
|
if (item != "all") {
|
|
docTypeQueryString +=
|
|
" pinswg_isharedocumentlocations eq " + item;
|
|
|
|
docTypeQueryString +=
|
|
index < documentTypeArr.length - 1 ? " or " : "";
|
|
}
|
|
});
|
|
docTypeQueryString += " )";
|
|
} else {
|
|
docTypeQueryString +=
|
|
" and pinswg_isharedocumentlocations eq " + documentType;
|
|
}
|
|
}
|
|
|
|
function daysAgoISO(days) {
|
|
const today = new Date();
|
|
const resultDate = new Date(today);
|
|
resultDate.setUTCDate(today.getUTCDate() - days); // subtract weeks
|
|
resultDate.setUTCHours(0, 0, 0, 0); // reset time to 00:00:00
|
|
return resultDate.toISOString().replace(/\.000Z$/, "Z"); // format as 'YYYY-MM-DDT00:00:00Z'
|
|
}
|
|
|
|
function flattenDocuments(response) {
|
|
return {
|
|
...response,
|
|
value: Array.isArray(response.value)
|
|
? response.value.map((doc) => ({
|
|
...doc,
|
|
ticketnumber:
|
|
doc.pinswg_DocumentIds?.ticketnumber || null,
|
|
}))
|
|
: [],
|
|
};
|
|
}
|
|
|
|
var queryUrl =
|
|
"pinswg_documents?$select=pinswg_name,createdon,pinswg_publishtoweb,_pinswg_documentids_value,pinswg_isharedocumentlocations,pinswg_isharedocumentreference,pinswg_uploadurl,pinswg_uploadstatus,pinswg_origin&$filter=createdon ge " +
|
|
daysAgoISO(numberOfWeeks) +
|
|
docTypeQueryString +
|
|
"&$orderby=" +
|
|
orderby +
|
|
" " +
|
|
fieldSort +
|
|
"&$count=true" +
|
|
"&$expand=pinswg_DocumentIds($select = ticketnumber)" +
|
|
(typeof pageNumber != "undefined"
|
|
? "&$skiptoken=" + '<cookie pagenumber="' + pageNumber + '" />'
|
|
: "");
|
|
|
|
console.log(
|
|
"\n==========================================\n",
|
|
"\nnew docs search ",
|
|
"\n\nQuery url: " + queryUrl,
|
|
"\n\nRelay link: " + WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
|
"\n==========================================\n"
|
|
);
|
|
|
|
var apiResponse = axios
|
|
.get(
|
|
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
|
azureHeadersPagedCustom(token.access_token, showNumberOfRecords)
|
|
)
|
|
.then(({ data }) => {
|
|
let flattened = data.value.map((r) => ({
|
|
...r,
|
|
ticketnumber: r.pinswg_DocumentIds?.ticketnumber || null,
|
|
}));
|
|
|
|
data.value = flattened;
|
|
data.value.forEach(function (element) {
|
|
element.pinswg_hashlink = encryptDocReference(
|
|
element.pinswg_isharedocumentreference
|
|
);
|
|
});
|
|
|
|
var dataStr;
|
|
|
|
_.has(data, "@odata.nextLink") == true &&
|
|
((dataStr = JSON.stringify(data["@odata.nextLink"])),
|
|
(data["@odata.nextLink"] = dataStr.split("/v8.2/")[1]));
|
|
res.status(200).json(data);
|
|
})
|
|
.catch((error) => {
|
|
consoleLogger(error);
|
|
res.status(400).json(error);
|
|
});
|
|
|
|
return apiResponse;
|
|
}
|