166 lines
5.7 KiB
JavaScript
166 lines
5.7 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/endpoint/getadvancedsearch_api:
|
|
* get:
|
|
* tags: [Search]
|
|
* description: Advanced search
|
|
* parameters:
|
|
* - name: searchstring
|
|
* description: search string
|
|
* in: query
|
|
* required: false
|
|
* example: {
|
|
* "q":"cas-",
|
|
* "lpa":"744c69f4-48da-eb11-aac5-00224800be9c",
|
|
* "apt":"846040000",
|
|
* "statuscode":"1"}
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Success
|
|
*/
|
|
|
|
import { azureHeadersPaged } from "../../../actions/core/headers";
|
|
import { consoleLogger } from "../../../actions/core/logger";
|
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
|
import { relayGet, relayGetData } from "../middleware/relayForwarding";
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
const encodedSearchString = req.query.searchstring;
|
|
|
|
if (
|
|
typeof encodedSearchString !== "string" ||
|
|
encodedSearchString.trim().length === 0
|
|
) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "SEARCH_STRING_REQUIRED",
|
|
message: "searchstring is required"
|
|
});
|
|
}
|
|
|
|
let searchString;
|
|
try {
|
|
searchString = JSON.parse(decodeURI(encodedSearchString));
|
|
} catch (error) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "INVALID_SEARCH_STRING",
|
|
message: "searchstring must be valid JSON"
|
|
});
|
|
}
|
|
|
|
if (
|
|
!searchString ||
|
|
typeof searchString !== "object" ||
|
|
Object.keys(searchString).length === 0
|
|
) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "SEARCH_STRING_REQUIRED",
|
|
message: "searchstring is required"
|
|
});
|
|
}
|
|
|
|
const toEscapedString = (value) => String(value).replace(/\'/g, "''");
|
|
let queryString = "";
|
|
|
|
queryString +=
|
|
searchString.q != null
|
|
? "(contains(title, '" +
|
|
toEscapedString(searchString.q) +
|
|
"') or contains(ticketnumber,'" +
|
|
toEscapedString(searchString.q) +
|
|
"')) and"
|
|
: "";
|
|
|
|
queryString +=
|
|
searchString.lpaRef != null
|
|
? "(contains(pinswg_lpareference, '" +
|
|
toEscapedString(searchString.lpaRef) +
|
|
"')) and"
|
|
: "";
|
|
|
|
queryString +=
|
|
(searchString.lpa != null || searchString.LPA != null) &&
|
|
searchString.lpa != null
|
|
? " _pinswg_associatedlpa_value eq " + searchString.lpa + " and"
|
|
: "";
|
|
|
|
queryString +=
|
|
searchString.apt != null
|
|
? " pinswg_appealcasetype eq " + searchString.apt + " and"
|
|
: "";
|
|
|
|
queryString +=
|
|
searchString.statuscode != null
|
|
? " statuscode eq " + searchString.statuscode + " and"
|
|
: "";
|
|
|
|
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=" +
|
|
queryString +
|
|
" pinswg_appealcasetype ne null and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true";
|
|
|
|
return relayGet({
|
|
queryUrl,
|
|
res,
|
|
requestOptionsBuilder: (accessToken) => azureHeadersPaged(accessToken),
|
|
transformData: async (data, accessToken) => {
|
|
if (Object.prototype.hasOwnProperty.call(data, "@odata.nextLink")) {
|
|
const dataStr = JSON.stringify(data["@odata.nextLink"]);
|
|
data["@odata.nextLink"] = dataStr.split("/v8.2/")[1];
|
|
}
|
|
|
|
if (searchString.projecttype != null) {
|
|
for (let i = 0; i < data.value.length; i++) {
|
|
const item = data.value[i];
|
|
|
|
try {
|
|
const projectTypeQuery =
|
|
"pinswg_sipses?$filter=_pinswg_sipscase_value eq " +
|
|
item.incidentid +
|
|
" and _pinswg_projecttype_value eq " +
|
|
searchString.projecttype +
|
|
"&$select=_pinswg_projecttype_value";
|
|
|
|
const projectTypeResponse = await relayGetData({
|
|
queryUrl: projectTypeQuery,
|
|
accessToken,
|
|
requestOptionsBuilder: (token) =>
|
|
azureHeadersPaged(token)
|
|
});
|
|
|
|
if (
|
|
projectTypeResponse.data.value.length > 0 &&
|
|
projectTypeResponse.data.value[0]
|
|
._pinswg_projecttype_value != null
|
|
) {
|
|
Object.assign(
|
|
item,
|
|
projectTypeResponse.data.value[0]
|
|
);
|
|
}
|
|
} catch (error) {
|
|
consoleLogger(error);
|
|
}
|
|
}
|
|
|
|
data.value = data.value.filter(
|
|
(item) => item._pinswg_projecttype_value != null
|
|
);
|
|
|
|
data["@odata.count"] = data.value.length;
|
|
}
|
|
|
|
return data;
|
|
},
|
|
errorResponse: {
|
|
status: 400,
|
|
code: "ADVANCED_SEARCH_FETCH_FAILED",
|
|
message: "Failed to fetch advanced search results"
|
|
}
|
|
});
|
|
}
|