TASK22211: normalize form and publication endpoint contracts

This commit is contained in:
2026-03-23 11:47:18 +00:00
parent b59f13a45d
commit bcf03a65f9
6 changed files with 380 additions and 107 deletions
@@ -1,41 +1,72 @@
import axios from "axios"; import axios from "axios";
import CryptoJS from "crypto-js";
import { azureHeadersNoOdata } from "../../../actions/core/headers"; import { azureHeadersNoOdata } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token"; import { getToken } from "../../../actions/core/token";
import { hashAPIPath } from "../../../actions/core/hash"; import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
const WEBAPI_URL = const WEBAPI_URL =
process.env.RELAY_ROOT || process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
export default async function ApiProxy(req, res) { export default async function ApiProxy(req, res) {
var appealTypeName = req.query.appealTypeName; const appealTypeName = req.query.appealTypeName;
var primaryIdAttribute = req.query.primaryIdAttribute; const primaryIdAttribute = req.query.primaryIdAttribute;
var incidentID = req.query.incidentID; const incidentID = req.query.incidentID;
var token = await getToken();
var queryUrl = if (
appealTypeName + typeof appealTypeName !== "string" ||
"?$filter=_" + appealTypeName.trim().length === 0
primaryIdAttribute + ) {
"s_value eq " + return respondError(res, {
incidentID + status: 400,
"&$count=true"; code: "APPEAL_TYPE_NAME_REQUIRED",
message: "appealTypeName is required"
});
}
queryUrl = queryUrl; //+ getSelectQuery(appealTypeName); if (
typeof primaryIdAttribute !== "string" ||
primaryIdAttribute.trim().length === 0
) {
return respondError(res, {
status: 400,
code: "PRIMARY_ID_ATTRIBUTE_REQUIRED",
message: "primaryIdAttribute is required"
});
}
//console.log("///////////\nquery: ", queryUrl, "<<<<end query"); if (typeof incidentID !== "string" || incidentID.trim().length === 0) {
return respondError(res, {
status: 400,
code: "INCIDENT_ID_REQUIRED",
message: "incidentID is required"
});
}
return axios try {
.get( const token = await getToken();
const queryUrl =
appealTypeName +
"?$filter=_" +
primaryIdAttribute +
"s_value eq " +
incidentID +
"&$count=true";
const { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeadersNoOdata(token.access_token) azureHeadersNoOdata(token.access_token)
) );
.then(({ data }) => {
res.status(200).json(data); return respondSuccess(res, data);
}) } catch (error) {
.catch((error) => { consoleLogger(error);
consoleLogger(error); return respondError(res, {
res.status(400).json(error); status: 400,
code: "BASIC_PART_SAVED_DETAILS_FETCH_FAILED",
message: "Failed to fetch basic part-saved details"
}); });
}
} }
+21 -25
View File
@@ -1,44 +1,40 @@
import axios from "axios"; import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash"; import _ from "lodash";
import { azureHeaders } from "../../../actions/core/headers"; import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger"; import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token"; import { getToken } from "../../../actions/core/token";
import { hashAPIPath } from "../../../actions/core/hash"; import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
const WEBAPI_URL = const WEBAPI_URL =
process.env.RELAY_ROOT || process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
export default async function ApiProxy(req, res) { export default async function ApiProxy(req, res) {
var token = await getToken(); try {
const token = await getToken();
var queryUrl = const queryUrl =
"incidents?$select=pinswg_environmentalstatementlocation,pinswg_appealcasetype,_customerid_value,_pinswg_associatedlpa_value,_ownerid_value,statuscode,ticketnumber,title &$filter=(pinswg_appealcasetype eq 846040011 or pinswg_appealcasetype eq 846040002) and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true"; "incidents?$select=pinswg_environmentalstatementlocation,pinswg_appealcasetype,_customerid_value,_pinswg_associatedlpa_value,_ownerid_value,statuscode,ticketnumber,title &$filter=(pinswg_appealcasetype eq 846040011 or pinswg_appealcasetype eq 846040002) and pinswg_publishtoweb eq true&$orderby=createdon desc&$count=true";
function renameJsonKey(jsonObj, oldKey, newKey) { const { data } = await axios.get(
if (jsonObj.hasOwnProperty(oldKey)) {
jsonObj[newKey] = jsonObj[oldKey]; // Add new key with the same value
delete jsonObj[oldKey]; // Delete old key
}
return jsonObj;
}
return axios
.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeaders(token.access_token) azureHeaders(token.access_token)
) );
.then(({ data }) => {
var dataStr;
_.has(data, "@odata.nextLink") == true && let dataStr;
((dataStr = JSON.stringify(data["@odata.nextLink"])),
(data["@odata.nextLink"] = dataStr.split("/v8.2/")[1])); _.has(data, "@odata.nextLink") === true &&
res.status(200).json(data); ((dataStr = JSON.stringify(data["@odata.nextLink"])),
}) (data["@odata.nextLink"] = dataStr.split("/v8.2/")[1]));
.catch((error) => {
consoleLogger(error); return respondSuccess(res, data);
res.status(400).json(error); } catch (error) {
consoleLogger(error);
return respondError(res, {
status: 400,
code: "DNS_LIST_FETCH_FAILED",
message: "Failed to fetch DNS list"
}); });
}
} }
+28 -16
View File
@@ -1,33 +1,45 @@
import axios from "axios"; import axios from "axios";
import CryptoJS from "crypto-js";
import { azureHeaders } from "../../../actions/core/headers"; import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger"; import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token"; import { getToken } from "../../../actions/core/token";
import { hashAPIPath } from "../../../actions/core/hash"; import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
const WEBAPI_URL = const WEBAPI_URL =
process.env.RELAY_ROOT || process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
export default async function ApiProxy(req, res) { export default async function ApiProxy(req, res) {
var whichForm = req.query.whichForm; const whichForm = req.query.whichForm;
var token = await getToken();
var queryUrl = if (typeof whichForm !== "string" || whichForm.trim().length === 0) {
"systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode eq 'pinswg_" + return respondError(res, {
whichForm + status: 400,
"' and type eq 2)&$count=true&$top=201"; code: "WHICH_FORM_REQUIRED",
message: "whichForm is required"
});
}
return axios try {
.get( const token = await getToken();
const queryUrl =
"systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode eq 'pinswg_" +
whichForm +
"' and type eq 2)&$count=true&$top=201";
const { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeaders(token.access_token) azureHeaders(token.access_token)
) );
.then(({ data }) => {
res.status(200).json(data); return respondSuccess(res, data);
}) } catch (error) {
.catch((error) => { consoleLogger(error);
consoleLogger(error); return respondError(res, {
res.status(400).json(error); status: 400,
code: "FORM_DATA_FETCH_FAILED",
message: "Failed to fetch form data"
}); });
}
} }
+28 -27
View File
@@ -16,48 +16,49 @@
*/ */
import axios from "axios"; import axios from "axios";
import CryptoJS from "crypto-js";
import { azureHeaders } from "../../../actions/core/headers"; import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger"; import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token"; import { getToken } from "../../../actions/core/token";
import { hashAPIPath } from "../../../actions/core/hash"; import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
const WEBAPI_URL = const WEBAPI_URL =
process.env.RELAY_ROOT || process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
export default async function ApiProxy(req, res) { export default async function ApiProxy(req, res) {
var searchString = req.query.searchString; const searchString = req.query.searchString;
var token = await getToken();
searchString = searchString.replace(/\'/g, "''"); if (typeof searchString !== "string" || searchString.trim().length === 0) {
return respondError(res, {
status: 400,
code: "SEARCH_STRING_REQUIRED",
message: "searchString is required"
});
}
var queryUrl = try {
"incidents?$select=pinswg_environmentalstatementlocation,pinswg_publishtoweb,ticketnumber,title, _primarycontactid_value&$filter=incidentid eq " + const token = await getToken();
searchString +
" and pinswg_appealcasetype ne null &$orderby=createdon desc&$count=true";
console.log( const escapedSearchString = searchString.split("'").join("''");
"\n==========================================\n",
"basic search: " + queryUrl,
"\n\n",
"basic search relay link: " +
WEBAPI_URL +
queryUrl +
hashAPIPath(queryUrl),
"\n==========================================\n"
);
return axios const queryUrl =
.get( "incidents?$select=pinswg_environmentalstatementlocation,pinswg_publishtoweb,ticketnumber,title, _primarycontactid_value&$filter=incidentid eq " +
escapedSearchString +
" and pinswg_appealcasetype ne null &$orderby=createdon desc&$count=true";
const { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeaders(token.access_token) azureHeaders(token.access_token)
) );
.then(({ data }) => {
res.status(200).json(data); return respondSuccess(res, data);
}) } catch (error) {
.catch((error) => { consoleLogger(error);
consoleLogger(error); return respondError(res, {
res.status(400).json(error); status: 400,
code: "IS_PUBLISHED_FETCH_FAILED",
message: "Failed to fetch published state"
}); });
}
} }
+28 -16
View File
@@ -1,33 +1,45 @@
import axios from "axios"; import axios from "axios";
import CryptoJS from "crypto-js";
import { azureHeaders } from "../../../actions/core/headers"; import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger"; import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token"; import { getToken } from "../../../actions/core/token";
import { hashAPIPath } from "../../../actions/core/hash"; import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
const WEBAPI_URL = const WEBAPI_URL =
process.env.RELAY_ROOT || process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
export default async function ApiProxy(req, res) { export default async function ApiProxy(req, res) {
var caseid = req.query.caseid; const caseid = req.query.caseid;
var token = await getToken();
var queryUrl = if (typeof caseid !== "string" || caseid.trim().length === 0) {
"pinswg_sipsevents?$filter=_pinswg_sipseventsid_value eq " + return respondError(res, {
caseid + status: 400,
"&$count=true"; code: "CASE_ID_REQUIRED",
message: "caseid is required"
});
}
return axios try {
.get( const token = await getToken();
const queryUrl =
"pinswg_sipsevents?$filter=_pinswg_sipseventsid_value eq " +
caseid +
"&$count=true";
const { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeaders(token.access_token) azureHeaders(token.access_token)
) );
.then(({ data }) => {
res.status(200).json(data); return respondSuccess(res, data);
}) } catch (error) {
.catch((error) => { consoleLogger(error);
consoleLogger(error); return respondError(res, {
res.status(400).json(error); status: 400,
code: "SIPS_EVENTS_FETCH_FAILED",
message: "Failed to fetch SIPS events"
}); });
}
} }
@@ -2732,6 +2732,227 @@ test("getlinkedcases catch path returns LINKED_CASES_FETCH_FAILED", async () =>
); );
}); });
test("getformdata returns WHICH_FORM_REQUIRED when whichForm missing", async () => {
const mod = loadModule("pages/api/endpoint/getformdata_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: { get: async () => ({ data: { value: [] } }) },
consoleLogger: () => {}
});
const req = { query: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(res.state.jsonBody.error.code, "WHICH_FORM_REQUIRED");
});
test("getformdata catch path returns FORM_DATA_FETCH_FAILED", async () => {
const mod = loadModule("pages/api/endpoint/getformdata_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: {
get: async () => {
throw new Error("relay failed");
}
},
consoleLogger: () => {}
});
const req = { query: { whichForm: "planningappeals78" } };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(res.state.jsonBody.error.code, "FORM_DATA_FETCH_FAILED");
});
test("getispublishedbyid returns SEARCH_STRING_REQUIRED when searchString missing", async () => {
const mod = loadModule("pages/api/endpoint/getispublishedbyid_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: { get: async () => ({ data: { value: [] } }) },
consoleLogger: () => {}
});
const req = { query: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(res.state.jsonBody.error.code, "SEARCH_STRING_REQUIRED");
});
test("getispublishedbyid catch path returns IS_PUBLISHED_FETCH_FAILED", async () => {
const mod = loadModule("pages/api/endpoint/getispublishedbyid_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: {
get: async () => {
throw new Error("relay failed");
}
},
consoleLogger: () => {}
});
const req = { query: { searchString: "incident-guid" } };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(
res.state.jsonBody.error.code,
"IS_PUBLISHED_FETCH_FAILED"
);
});
test("getdnslist catch path returns DNS_LIST_FETCH_FAILED", async () => {
const mod = loadModule("pages/api/endpoint/getdnslist_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: {
get: async () => {
throw new Error("relay failed");
}
},
consoleLogger: () => {},
_: { has: () => false }
});
const req = { query: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(res.state.jsonBody.error.code, "DNS_LIST_FETCH_FAILED");
});
test("getsipsevents returns CASE_ID_REQUIRED when caseid missing", async () => {
const mod = loadModule("pages/api/endpoint/getsipsevents_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: { get: async () => ({ data: { value: [] } }) },
consoleLogger: () => {}
});
const req = { query: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(res.state.jsonBody.error.code, "CASE_ID_REQUIRED");
});
test("getsipsevents catch path returns SIPS_EVENTS_FETCH_FAILED", async () => {
const mod = loadModule("pages/api/endpoint/getsipsevents_api.js", {
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: {
get: async () => {
throw new Error("relay failed");
}
},
consoleLogger: () => {}
});
const req = { query: { caseid: "case-guid" } };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(
res.state.jsonBody.error.code,
"SIPS_EVENTS_FETCH_FAILED"
);
});
test("getbasicpartsaveddetails returns APPEAL_TYPE_NAME_REQUIRED when appealTypeName missing", async () => {
const mod = loadModule(
"pages/api/endpoint/getbasicpartsaveddetails_api.js",
{
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeadersNoOdata: () => ({}),
axios: { get: async () => ({ data: { value: [] } }) },
consoleLogger: () => {}
}
);
const req = {
query: {
primaryIdAttribute: "pinswg_planningappeals78id",
incidentID: "i1"
}
};
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(
res.state.jsonBody.error.code,
"APPEAL_TYPE_NAME_REQUIRED"
);
});
test("getbasicpartsaveddetails catch path returns BASIC_PART_SAVED_DETAILS_FETCH_FAILED", async () => {
const mod = loadModule(
"pages/api/endpoint/getbasicpartsaveddetails_api.js",
{
respondError: respondErrorMock,
respondSuccess: respondSuccessMock,
getToken: async () => ({ access_token: "token" }),
hashAPIPath: () => "&hash=expected",
azureHeadersNoOdata: () => ({}),
axios: {
get: async () => {
throw new Error("relay failed");
}
},
consoleLogger: () => {}
}
);
const req = {
query: {
appealTypeName: "pinswg_planningappeals78s",
primaryIdAttribute: "pinswg_planningappeals78id",
incidentID: "i1"
}
};
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(
res.state.jsonBody.error.code,
"BASIC_PART_SAVED_DETAILS_FETCH_FAILED"
);
});
const run = async () => { const run = async () => {
let passed = 0; let passed = 0;
for (const currentTest of tests) { for (const currentTest of tests) {