TASK22211: normalize basic search endpoint contracts
This commit is contained in:
@@ -30,80 +30,114 @@
|
||||
*/
|
||||
|
||||
import axios from "axios";
|
||||
import CryptoJS from "crypto-js";
|
||||
import { azureHeaders } from "../../../actions/core/headers";
|
||||
import { consoleLogger } from "../../../actions/core/logger";
|
||||
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;
|
||||
var token = await getToken();
|
||||
const appealTypeName = req.query.appealTypeName;
|
||||
const primaryIdAttribute = req.query.primaryIdAttribute;
|
||||
const incidentID = req.query.incidentID;
|
||||
|
||||
let navigationProperty =
|
||||
getNavigationPropertyByPrimaryAttribute(
|
||||
primaryIdAttribute
|
||||
).NavigationProperty;
|
||||
if (
|
||||
typeof appealTypeName !== "string" ||
|
||||
appealTypeName.trim().length === 0
|
||||
) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "APPEAL_TYPE_NAME_REQUIRED",
|
||||
message: "appealTypeName is required"
|
||||
});
|
||||
}
|
||||
|
||||
var queryUrl =
|
||||
appealTypeName +
|
||||
"?$filter=_" +
|
||||
(primaryIdAttribute == "pinswg_sipscase"
|
||||
? "pinswg_sipscase_value"
|
||||
: primaryIdAttribute + "s_value ") +
|
||||
" eq " +
|
||||
incidentID +
|
||||
"&$count=true" +
|
||||
"&$expand=" +
|
||||
navigationProperty +
|
||||
"($select=ticketnumber)";
|
||||
if (
|
||||
typeof primaryIdAttribute !== "string" ||
|
||||
primaryIdAttribute.trim().length === 0
|
||||
) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "PRIMARY_ID_ATTRIBUTE_REQUIRED",
|
||||
message: "primaryIdAttribute is required"
|
||||
});
|
||||
}
|
||||
|
||||
//" and statuscode eq 1&$count=true";
|
||||
if (typeof incidentID !== "string" || incidentID.trim().length === 0) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "INCIDENT_ID_REQUIRED",
|
||||
message: "incidentID is required"
|
||||
});
|
||||
}
|
||||
|
||||
// queryUrl =
|
||||
// queryUrl +
|
||||
// getSelectQuery(appealTypeName) +
|
||||
// ",_" +
|
||||
// (primaryIdAttribute == "pinswg_sipscase"
|
||||
// ? "pinswg_sipscase_value"
|
||||
// : primaryIdAttribute + "s_value");
|
||||
try {
|
||||
const token = await getToken();
|
||||
|
||||
queryUrl = queryUrl + getSelectQuery(appealTypeName);
|
||||
const navigationProperty =
|
||||
getNavigationPropertyByPrimaryAttribute(
|
||||
primaryIdAttribute
|
||||
).NavigationProperty;
|
||||
|
||||
// console.log(
|
||||
// "\n==========================================\n",
|
||||
// "\nSearch Details query ",
|
||||
// "\nAppealType: " + appealTypeName,
|
||||
// "\nIncident ID: " + incidentID,
|
||||
// "\n\nQuery url: " + queryUrl,
|
||||
// "\n==========================================\n"
|
||||
// );
|
||||
let queryUrl =
|
||||
appealTypeName +
|
||||
"?$filter=_" +
|
||||
(primaryIdAttribute == "pinswg_sipscase"
|
||||
? "pinswg_sipscase_value"
|
||||
: primaryIdAttribute + "s_value ") +
|
||||
" eq " +
|
||||
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");
|
||||
|
||||
queryUrl = queryUrl + getSelectQuery(appealTypeName);
|
||||
|
||||
// console.log(
|
||||
// "\n==========================================\n",
|
||||
// "\nSearch Details query ",
|
||||
// "\nAppealType: " + appealTypeName,
|
||||
// "\nIncident ID: " + incidentID,
|
||||
// "\n\nQuery url: " + queryUrl,
|
||||
// "\n==========================================\n"
|
||||
// );
|
||||
|
||||
const { data } = await axios.get(
|
||||
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
||||
azureHeaders(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
|
||||
}));
|
||||
|
||||
res.status(200).json(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
consoleLogger(error);
|
||||
res.status(400).json(error);
|
||||
data.value = flattened;
|
||||
|
||||
return respondSuccess(res, data);
|
||||
} catch (error) {
|
||||
consoleLogger(error);
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "BASIC_SEARCH_DETAILS_FETCH_FAILED",
|
||||
message: "Failed to fetch basic search details"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user