Files
pedwfrontend/pages/api/endpoint/getbasicsearchpaged_api.js
T

143 lines
4.4 KiB
JavaScript

/**
* @swagger
* /api/endpoint/getbasicsearchpaged_api:
* get:
* tags: [Search]
* description: Basic search paged
* parameters:
* - name: searchString
* in: query
* required: true
* example: cas-
* schema:
* type: string
* - name: pageNumber
* in: query
* required: true
* example: 2
* 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 _ from "lodash";
import { azureHeadersPagedCustom } from "../../../actions/core/headers";
import { respondError } from "../middleware/apiResponse";
import { relayGet } from "../middleware/relayForwarding";
export default async function ApiProxy(req, res) {
let searchString = req.query.searchString;
const pageNumber = req.query.pageNumber;
const orderby = req.query.orderby;
const fieldSort = req.query.fieldSort;
const showNumberOfRecords = req.query.showNumberOfRecords;
if (typeof searchString !== "string" || searchString.trim().length === 0) {
return respondError(res, {
status: 400,
code: "SEARCH_STRING_REQUIRED",
message: "searchString 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"
});
}
searchString = searchString.replace(/\'/g, "''");
const queryUrl =
"incidents?$select=pinswg_environmentalstatementlocation,pinswg_caseaddress,modifiedon,description,numberofchildincidents,_accountid_value,_customerid_value,_pinswg_associatedlpa_value,_ownerid_value,pinswg_appealcasetype,statuscode,ticketnumber,title, _primarycontactid_value,pinswg_lpareference,pinswg_appellantagent,pinswg_appellantfirstname,pinswg_appellantlastname&$expand=primarycontactid($select=fullname)&$filter=(contains(title, '" +
searchString +
"') or contains(ticketnumber, '" +
searchString +
"') or contains(pinswg_lpareference, '" +
searchString +
"')) and pinswg_appealcasetype ne null " +
(process.env.SHOWSIPS !== "true"
? "and pinswg_appealcasetype ne 846040002 "
: "") +
"and pinswg_publishtoweb eq true&$orderby=" +
orderby +
" " +
fieldSort +
"&$count=true" +
(typeof pageNumber != "undefined"
? "&$skiptoken=" + '<cookie pagenumber="' + pageNumber + '" />'
: "");
const relayPolicy = {
method: "GET",
timeoutMs: 9000,
maxRetries: 2,
retryBaseDelayMs: 150,
retryMaxDelayMs: 1000
};
return relayGet({
queryUrl,
res,
requestOptionsBuilder: (accessToken) =>
azureHeadersPagedCustom(accessToken, showNumberOfRecords),
relayPolicy,
transformData: (data) => {
if (_.has(data, "@odata.nextLink") === true) {
const dataStr = JSON.stringify(data["@odata.nextLink"]);
data["@odata.nextLink"] = dataStr.split("/v8.2/")[1];
}
return data;
},
errorResponse: {
status: 400,
code: "BASIC_SEARCH_PAGED_FETCH_FAILED",
message: "Failed to fetch paged basic search results"
}
});
}