/** * @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 } from "../../../actions/core/headers"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; import { hashAPIPath } from "../../../actions/core/hash"; import { respondError, respondSuccess } from "../middleware/apiResponse"; const WORDKEY = process.env.HASHKEY; const WEBAPI_URL = process.env.RELAY_ROOT || "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; 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 || 1; const token = await getToken(); const orderby = req.query.orderby || "createdon"; const fieldSort = req.query.fieldSort || "desc"; const showNumberOfRecords = req.query.showNumberOfRecords || 10; const documentType = req.query.documentType || "all"; const documentOrigin = req.query.documentOrigin || "all"; const numberOfWeeks = req.query.numberWeeks || 1; let docTypeQueryString = ""; let docOriginQueryString = ""; 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; } } if (documentOrigin != "all") { if (documentOrigin.indexOf(",") >= 0) { const documentOriginArr = documentOrigin.split(","); docOriginQueryString = " and ("; documentOriginArr.forEach(function (item, index) { console.log(item, index); if (item != "all") { docOriginQueryString += " pinswg_origin eq " + item; docOriginQueryString += index < documentOriginArr.length - 1 ? " or " : ""; } }); docOriginQueryString += " )"; } else { docOriginQueryString += " and pinswg_origin eq " + documentOrigin; } } 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' } const 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 + docOriginQueryString + "&$orderby=" + orderby + " " + fieldSort + "&$count=true" + "&$expand=pinswg_DocumentIds($select = ticketnumber,pinswg_publishtoweb)" + (typeof pageNumber != "undefined" ? "&$skiptoken=" + '' : ""); console.log( "\n==========================================\n", "\nnew docs search---- ", "\n\nQuery url: " + queryUrl, "\n\nRelay link: " + WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), "\n==========================================\n" ); try { const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeadersPagedCustom(token.access_token, showNumberOfRecords) ); const flattened = data.value.map((r) => ({ ...r, ticketnumber: r.pinswg_DocumentIds?.ticketnumber || null, publishtoweb: r.pinswg_DocumentIds?.pinswg_publishtoweb || null })); data.value = flattened; data.value.forEach(function (element) { element.pinswg_hashlink = encryptDocReference( element.pinswg_isharedocumentreference ); }); let dataStr; _.has(data, "@odata.nextLink") == true && ((dataStr = JSON.stringify(data["@odata.nextLink"])), (data["@odata.nextLink"] = dataStr.split("/v8.2/")[1])); return respondSuccess(res, data); } catch (error) { consoleLogger(error); return respondError(res, { status: 400, code: "ADMIN_LATEST_DOCS_FETCH_FAILED", message: "Failed to fetch latest documents" }); } }