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

144 lines
4.7 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 axios from "axios";
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 WEBAPI_URL =
process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
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"
});
}
try {
const token = await getToken();
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 { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeadersPagedCustom(token.access_token, showNumberOfRecords)
);
if (_.has(data, "@odata.nextLink") === true) {
const 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: "BASIC_SEARCH_PAGED_FETCH_FAILED",
message: "Failed to fetch paged basic search results"
});
}
}