update error and response handling

This commit is contained in:
2026-03-23 10:28:19 +00:00
parent 26b889d182
commit a67f84a99f
16 changed files with 972 additions and 4727 deletions
+96 -96
View File
@@ -22,149 +22,149 @@
*/
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import { azureHeadersPaged } 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) {
var searchString = req.query.searchstring;
var token = await getToken();
const encodedSearchString = req.query.searchstring;
searchString = _.isEmpty(searchString)
? searchString
: JSON.parse(decodeURI(searchString));
if (
typeof encodedSearchString !== "string" ||
encodedSearchString.trim().length === 0
) {
return respondError(res, {
status: 400,
code: "SEARCH_STRING_REQUIRED",
message: "searchstring is required"
});
}
var queryString = "";
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 +=
_.has(searchString, "q") && searchString.q != null
searchString.q != null
? "(contains(title, '" +
searchString.q.replace(/\'/g, "''") +
toEscapedString(searchString.q) +
"') or contains(ticketnumber,'" +
searchString.q.replace(/\'/g, "''") +
toEscapedString(searchString.q) +
"')) and"
: "";
queryString +=
_.has(searchString, "lpaRef") && searchString.lpaRef != null
searchString.lpaRef != null
? "(contains(pinswg_lpareference, '" +
searchString.lpaRef.replace(/\'/g, "''") +
toEscapedString(searchString.lpaRef) +
"')) and"
: "";
queryString +=
(_.has(searchString, "lpa") || _.has(searchString, "LPA")) &&
(searchString.lpa != null || searchString.LPA != null) &&
searchString.lpa != null
? " _pinswg_associatedlpa_value eq " + searchString.lpa + " and"
: "";
queryString +=
_.has(searchString, "apt") && searchString.apt != null
searchString.apt != null
? " pinswg_appealcasetype eq " + searchString.apt + " and"
: "";
queryString +=
_.has(searchString, "statuscode") && searchString.statuscode != null
searchString.statuscode != null
? " statuscode eq " + searchString.statuscode + " and"
: "";
//console.log(queryString);
var queryUrl =
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";
//console.log(queryString, "\n", queryUrl);
try {
const token = await getToken();
const { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeadersPaged(token.access_token)
);
var apiResponse = _.isEmpty(searchString)
? res.status(400).json()
: axios
.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeadersPaged(token.access_token)
)
.then(async ({ data }) => {
var dataStr;
_.has(data, "@odata.nextLink") == true &&
((dataStr = JSON.stringify(data["@odata.nextLink"])),
(data["@odata.nextLink"] = dataStr.split("/v8.2/")[1]));
if (Object.prototype.hasOwnProperty.call(data, "@odata.nextLink")) {
const dataStr = JSON.stringify(data["@odata.nextLink"]);
data["@odata.nextLink"] = dataStr.split("/v8.2/")[1];
}
if (_.has(searchString, "projecttype")) {
await updateValueArray(data, token);
if (searchString.projecttype != null) {
for (let i = 0; i < data.value.length; i++) {
const item = data.value[i];
async function updateValueArray(data, token) {
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";
try {
// Axios call using the `incidentid` to fetch additional data
const response = await axios.get(
WEBAPI_URL +
"pinswg_sipses?$filter=_pinswg_sipscase_value eq " +
item.incidentid +
" and _pinswg_projecttype_value eq " +
searchString.projecttype +
"&$select=_pinswg_projecttype_value" +
hashAPIPath(
"pinswg_sipses?$filter=_pinswg_sipscase_value eq " +
item.incidentid +
" and _pinswg_projecttype_value eq " +
searchString.projecttype +
"&$select=_pinswg_projecttype_value"
),
azureHeadersPaged(token.access_token)
);
const response = await axios.get(
WEBAPI_URL +
projectTypeQuery +
hashAPIPath(projectTypeQuery),
azureHeadersPaged(token.access_token)
);
// Assuming the response contains the additional data you want to add
// console.log(response.data.value[0]);
console.log(response.data.value);
if (
response.data.value.length > 0 &&
response.data.value[0]._pinswg_projecttype_value != null
) {
Object.assign(item, response.data.value[0]);
}
} catch (error) {
consoleLogger(error);
}
}
if (
response.data.value.length > 0 &&
response.data.value[0]
._pinswg_projecttype_value != null
) {
Object.assign(
item,
response.data.value[0]
); // Update item with new data
} else {
console.log(
`No project type found for incident ID ${item.incidentid}`
);
}
} catch (error) {
console.error(
`Error fetching data for incident ID ${item.incidentid}:`,
error
);
}
}
}
data.value = data.value.filter(
(item) => item._pinswg_projecttype_value != null
);
data.value = data.value.filter(
(item) => item._pinswg_projecttype_value != null
);
data["@odata.count"] = data.value.length;
}
data["@odata.count"] = data.value.length;
// console.log(newDataObj);
}
res.status(200).json(data);
})
.catch((error) => {
consoleLogger(error);
res.status(400).json(error);
});
return apiResponse;
return respondSuccess(res, data);
} catch (error) {
consoleLogger(error);
return respondError(res, {
status: 400,
code: "ADVANCED_SEARCH_FETCH_FAILED",
message: "Failed to fetch advanced search results"
});
}
}