refactor(api): migrate batch 12 remaining relay-get candidates
This commit is contained in:
@@ -21,16 +21,10 @@
|
||||
* description: Success
|
||||
*/
|
||||
|
||||
import axios from "axios";
|
||||
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/";
|
||||
import { relayGet, relayGetData } from "../middleware/relayForwarding";
|
||||
|
||||
export default async function ApiProxy(req, res) {
|
||||
const encodedSearchString = req.query.searchstring;
|
||||
@@ -109,62 +103,63 @@ export default async function ApiProxy(req, res) {
|
||||
queryString +
|
||||
" pinswg_appealcasetype ne null and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true";
|
||||
|
||||
try {
|
||||
const token = await getToken();
|
||||
const { data } = await axios.get(
|
||||
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
||||
azureHeadersPaged(token.access_token)
|
||||
);
|
||||
|
||||
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 response = await axios.get(
|
||||
WEBAPI_URL +
|
||||
projectTypeQuery +
|
||||
hashAPIPath(projectTypeQuery),
|
||||
azureHeadersPaged(token.access_token)
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
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];
|
||||
}
|
||||
|
||||
data.value = data.value.filter(
|
||||
(item) => item._pinswg_projecttype_value != null
|
||||
);
|
||||
if (searchString.projecttype != null) {
|
||||
for (let i = 0; i < data.value.length; i++) {
|
||||
const item = data.value[i];
|
||||
|
||||
data["@odata.count"] = data.value.length;
|
||||
}
|
||||
try {
|
||||
const projectTypeQuery =
|
||||
"pinswg_sipses?$filter=_pinswg_sipscase_value eq " +
|
||||
item.incidentid +
|
||||
" and _pinswg_projecttype_value eq " +
|
||||
searchString.projecttype +
|
||||
"&$select=_pinswg_projecttype_value";
|
||||
|
||||
return respondSuccess(res, data);
|
||||
} catch (error) {
|
||||
consoleLogger(error);
|
||||
return respondError(res, {
|
||||
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"
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user