TASK22211: normalize basic search endpoint contracts
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
*/
|
||||
|
||||
import axios from "axios";
|
||||
import CryptoJS from "crypto-js";
|
||||
import _ from "lodash";
|
||||
import { azureHeadersPaged } from "../../../actions/core/headers";
|
||||
import { consoleLogger } from "../../../actions/core/logger";
|
||||
@@ -38,82 +37,105 @@ import { getToken } from "../../../actions/core/token";
|
||||
import { getSelectQuery } from "../../../actions/selectQueryTypes";
|
||||
import { getNavigationPropertyByPrimaryAttribute } from "../../../components/utils";
|
||||
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 appealTypeName = req.query.appealTypeName;
|
||||
var primaryIdAttribute = req.query.primaryIdAttribute;
|
||||
var incidentID = req.query.incidentID;
|
||||
const appealTypeName = req.query.appealTypeName;
|
||||
const primaryIdAttribute = req.query.primaryIdAttribute;
|
||||
const incidentID = req.query.incidentID;
|
||||
|
||||
if (
|
||||
!appealTypeName ||
|
||||
!primaryIdAttribute ||
|
||||
!incidentID ||
|
||||
String(appealTypeName).trim().length === 0 ||
|
||||
String(primaryIdAttribute).trim().length === 0 ||
|
||||
String(incidentID).trim().length === 0
|
||||
typeof appealTypeName !== "string" ||
|
||||
appealTypeName.trim().length === 0
|
||||
) {
|
||||
return res.status(400).json();
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "APPEAL_TYPE_NAME_REQUIRED",
|
||||
message: "appealTypeName is required"
|
||||
});
|
||||
}
|
||||
|
||||
var token = await getToken();
|
||||
if (
|
||||
typeof primaryIdAttribute !== "string" ||
|
||||
primaryIdAttribute.trim().length === 0
|
||||
) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "PRIMARY_ID_ATTRIBUTE_REQUIRED",
|
||||
message: "primaryIdAttribute is required"
|
||||
});
|
||||
}
|
||||
|
||||
let navigationProperty =
|
||||
getNavigationPropertyByPrimaryAttribute(
|
||||
primaryIdAttribute
|
||||
).NavigationProperty;
|
||||
if (typeof incidentID !== "string" || incidentID.trim().length === 0) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "INCIDENT_ID_REQUIRED",
|
||||
message: "incidentID is required"
|
||||
});
|
||||
}
|
||||
|
||||
// "?$filter=_" +
|
||||
// (primaryIdAttribute == "pinswg_sipscase"
|
||||
// ? "pinswg_sipscase_value"
|
||||
// : primaryIdAttribute + "s_value ") +
|
||||
// " eq " +
|
||||
// incidentID +
|
||||
try {
|
||||
const token = await getToken();
|
||||
|
||||
var queryUrl =
|
||||
appealTypeName +
|
||||
"?$filter=" +
|
||||
incidentID +
|
||||
"&$count=true" +
|
||||
"&$expand=" +
|
||||
navigationProperty +
|
||||
"($select=ticketnumber)";
|
||||
const navigationProperty =
|
||||
getNavigationPropertyByPrimaryAttribute(
|
||||
primaryIdAttribute
|
||||
).NavigationProperty;
|
||||
|
||||
//" and statuscode eq 1&$count=true";
|
||||
// "?$filter=_" +
|
||||
// (primaryIdAttribute == "pinswg_sipscase"
|
||||
// ? "pinswg_sipscase_value"
|
||||
// : primaryIdAttribute + "s_value ") +
|
||||
// " eq " +
|
||||
// incidentID +
|
||||
|
||||
queryUrl =
|
||||
queryUrl +
|
||||
getSelectQuery(appealTypeName) +
|
||||
",_" +
|
||||
(primaryIdAttribute == "pinswg_sipscase"
|
||||
? "pinswg_sipscase_value"
|
||||
: primaryIdAttribute + "s_value");
|
||||
let queryUrl =
|
||||
appealTypeName +
|
||||
"?$filter=" +
|
||||
incidentID +
|
||||
"&$count=true" +
|
||||
"&$expand=" +
|
||||
navigationProperty +
|
||||
"($select=ticketnumber)";
|
||||
|
||||
return axios
|
||||
.get(
|
||||
//" and statuscode eq 1&$count=true";
|
||||
|
||||
queryUrl =
|
||||
queryUrl +
|
||||
getSelectQuery(appealTypeName) +
|
||||
",_" +
|
||||
(primaryIdAttribute == "pinswg_sipscase"
|
||||
? "pinswg_sipscase_value"
|
||||
: primaryIdAttribute + "s_value");
|
||||
|
||||
const { data } = await axios.get(
|
||||
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
||||
azureHeadersPaged(token.access_token)
|
||||
)
|
||||
.then(({ data }) => {
|
||||
let flattened = data.value.map((r) => ({
|
||||
...r,
|
||||
ticketnumber: r[navigationProperty]?.ticketnumber || null
|
||||
}));
|
||||
);
|
||||
|
||||
data.value = flattened;
|
||||
let flattened = data.value.map((r) => ({
|
||||
...r,
|
||||
ticketnumber: r[navigationProperty]?.ticketnumber || null
|
||||
}));
|
||||
|
||||
var dataStr;
|
||||
_.has(data, "@odata.nextLink") == true &&
|
||||
((dataStr = JSON.stringify(data["@odata.nextLink"])),
|
||||
(data["@odata.nextLink"] = dataStr.split("/v8.2/")[1]));
|
||||
data.value = flattened;
|
||||
|
||||
return res.status(200).json(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
consoleLogger(error);
|
||||
res.status(400).json(error);
|
||||
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_DETAILS_PAGED_FETCH_FAILED",
|
||||
message: "Failed to fetch paged basic search details"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user