diff --git a/pages/api/endpoint/getsearchdocumentTypes_api.js b/pages/api/endpoint/getsearchdocumentTypes_api.js index 222f3d06..140da38b 100644 --- a/pages/api/endpoint/getsearchdocumentTypes_api.js +++ b/pages/api/endpoint/getsearchdocumentTypes_api.js @@ -17,11 +17,11 @@ */ 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 { hashAPIPath } from "../../../actions/core/hash"; +import { respondError, respondSuccess } from "../middleware/apiResponse"; const WEBAPI_URL = process.env.RELAY_ROOT || @@ -57,37 +57,41 @@ const groupArray = (arr) => { }; export default async function ApiProxy(req, res) { - var incidentID = req.query.incidentid; - var token = await getToken(); + const incidentID = req.query.incidentid; - var queryUrl = - "pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + - incidentID + - " and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null&$select=pinswg_isharedocumentlocations"; + if (typeof incidentID !== "string" || incidentID.trim().length === 0) { + return respondError(res, { + status: 400, + code: "INCIDENT_ID_REQUIRED", + message: "incidentid is required" + }); + } - //console.log("docu: ", WEBAPI_URL + queryUrl + hashAPIPath(queryUrl)); + try { + const token = await getToken(); - return axios - .get( + const queryUrl = + "pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + + incidentID + + " and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null&$select=pinswg_isharedocumentlocations"; + + const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeaders(token.access_token) - ) - .then(({ data }) => { - //delete data["pinswg_documentid"]; - data.value.forEach(function (element) { - delete element["pinswg_documentid"]; - }); + ); - var dataArr = groupArray(data.value); - - // var dataStr; - // _.has(data, "@odata.nextLink") == true && - // ((dataStr = JSON.stringify(data["@odata.nextLink"])), - // (data["@odata.nextLink"] = dataStr.split("/v8.2/")[1])); - res.status(200).json(dataArr); - }) - .catch((error) => { - consoleLogger(error); - res.status(400).json(error); + data.value.forEach(function (element) { + delete element["pinswg_documentid"]; }); + + const dataArr = groupArray(data.value); + return respondSuccess(res, dataArr); + } catch (error) { + consoleLogger(error); + return respondError(res, { + status: 400, + code: "SEARCH_DOCUMENT_TYPES_FETCH_FAILED", + message: "Failed to fetch search document types" + }); + } } diff --git a/pages/api/endpoint/getsearchdocumentdetails_api.js b/pages/api/endpoint/getsearchdocumentdetails_api.js index 66a0aa56..4f924615 100644 --- a/pages/api/endpoint/getsearchdocumentdetails_api.js +++ b/pages/api/endpoint/getsearchdocumentdetails_api.js @@ -23,6 +23,7 @@ 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 WORDKEY = process.env.HASHKEY; @@ -44,41 +45,53 @@ const encryptDocReference = (documentRef) => { }; export default async function ApiProxy(req, res) { - var incidentID = req.query.incidentid; - var token = await getToken(); + const incidentID = req.query.incidentid; - var queryUrl = - "pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + - incidentID + - " and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null&$select=pinswg_isharedocumentlocations,_pinswg_documentids_value,pinswg_isharelabelcasetype,pinswg_isharelabellpaname,pinswg_publishtoweb,pinswg_uploadstatus,pinswg_isharedocumentclassification,pinswg_isharedocumentreference,pinswg_name,pinswg_latestpublishedversion,pinswg_latestpublisheddate,pinswg_documentpublisheddate"; + if (typeof incidentID !== "string" || incidentID.trim().length === 0) { + return respondError(res, { + status: 400, + code: "INCIDENT_ID_REQUIRED", + message: "incidentid is required" + }); + } - //console.log("docu: ", WEBAPI_URL + queryUrl + hashAPIPath(queryUrl)); + try { + const token = await getToken(); - return axios - .get( + const queryUrl = + "pinswg_documents?$count=true&$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + + incidentID + + " and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null&$select=pinswg_isharedocumentlocations,_pinswg_documentids_value,pinswg_isharelabelcasetype,pinswg_isharelabellpaname,pinswg_publishtoweb,pinswg_uploadstatus,pinswg_isharedocumentclassification,pinswg_isharedocumentreference,pinswg_name,pinswg_latestpublishedversion,pinswg_latestpublisheddate,pinswg_documentpublisheddate"; + + const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeadersPaged(token.access_token) - ) - .then(({ data }) => { - data.value.forEach(function (element) { - Object.assign(element, { - "pinswg_documentpublisheddate": - element.pinswg_documentpublisheddate != null - ? element.pinswg_documentpublisheddate - : element.pinswg_latestpublisheddate - }); - element.pinswg_hashlink = encryptDocReference( - element.pinswg_isharedocumentreference - ); + ); + + data.value.forEach(function (element) { + Object.assign(element, { + "pinswg_documentpublisheddate": + element.pinswg_documentpublisheddate != null + ? element.pinswg_documentpublisheddate + : element.pinswg_latestpublisheddate }); - var dataStr; - _.has(data, "@odata.nextLink") == true && - ((dataStr = JSON.stringify(data["@odata.nextLink"])), - (data["@odata.nextLink"] = dataStr.split("/v8.2/")[1])); - res.status(200).json(data); - }) - .catch((error) => { - consoleLogger(error); - res.status(400).json(error); + element.pinswg_hashlink = encryptDocReference( + element.pinswg_isharedocumentreference + ); }); + + 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: "SEARCH_DOCUMENT_DETAILS_FETCH_FAILED", + message: "Failed to fetch search document details" + }); + } } diff --git a/pages/api/endpoint/getsearchdocumentdetailspaged_api.js b/pages/api/endpoint/getsearchdocumentdetailspaged_api.js index 677e405c..8533d6fb 100644 --- a/pages/api/endpoint/getsearchdocumentdetailspaged_api.js +++ b/pages/api/endpoint/getsearchdocumentdetailspaged_api.js @@ -49,6 +49,7 @@ import { azureHeadersPagedCustom } 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 WORDKEY = process.env.HASHKEY; @@ -70,20 +71,53 @@ const encryptDocReference = (documentRef) => { }; export default async function ApiProxy(req, res) { - var pageNumber = req.query.pageNumber; - var incidentID = req.query.incidentid; - var token = await getToken(); + const pageNumber = req.query.pageNumber; + const incidentID = req.query.incidentid; + const orderby = req.query.orderby; + const fieldSort = req.query.fieldSort; + const showNumberOfRecords = req.query.showNumberOfRecords; + const documentType = req.query.documentType || "all"; - var orderby = req.query.orderby; - var fieldSort = req.query.fieldSort; - var showNumberOfRecords = req.query.showNumberOfRecords; - var documentType = req.query.documentType || "all"; + if (typeof incidentID !== "string" || incidentID.trim().length === 0) { + return respondError(res, { + status: 400, + code: "INCIDENT_ID_REQUIRED", + message: "incidentid is required" + }); + } + + if (typeof orderby !== "string" || orderby.trim().length === 0) { + return respondError(res, { + status: 400, + code: "ORDER_BY_REQUIRED", + message: "orderby is required" + }); + } + + if (typeof fieldSort !== "string" || fieldSort.trim().length === 0) { + return respondError(res, { + status: 400, + code: "FIELD_SORT_REQUIRED", + message: "fieldSort is required" + }); + } + + if ( + typeof showNumberOfRecords !== "string" || + showNumberOfRecords.trim().length === 0 + ) { + return respondError(res, { + status: 400, + code: "SHOW_NUMBER_OF_RECORDS_REQUIRED", + message: "showNumberOfRecords is required" + }); + } // (documentType != "all" // ? documentType.indexOf(",")<0?" and pinswg_isharedocumentlocations eq " + documentType : // : "") + - var docTypeQueryString = ""; + let docTypeQueryString = ""; if (documentType != "all") { if (documentType.indexOf(",") >= 0) { @@ -91,8 +125,6 @@ export default async function ApiProxy(req, res) { docTypeQueryString = " and ("; documentTypeArr.forEach(function (item, index) { - console.log(item, index); - if (item != "all") { docTypeQueryString += " pinswg_isharedocumentlocations eq " + item; @@ -118,47 +150,53 @@ export default async function ApiProxy(req, res) { //console.log("has this passed docuemntType:", documentType); //(documentType !="all" && " pinswg_pinswg_isharedocumentlocations eq " + ) - var queryUrl = - "pinswg_documents?$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + - incidentID + - " and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null" + - docTypeQueryString + - "&$select=pinswg_isharedocumentlocations,_pinswg_documentids_value,pinswg_isharelabelcasetype,pinswg_isharelabellpaname,pinswg_publishtoweb,pinswg_uploadstatus,pinswg_isharedocumentclassification,pinswg_isharedocumentreference,pinswg_name,pinswg_latestpublishedversion,pinswg_latestpublisheddate,pinswg_documentpublisheddate&$orderby=" + - orderby + - " " + - fieldSort + - "&$count=true" + - (typeof pageNumber != "undefined" - ? "&$skiptoken=" + ('') - : ""); + try { + const token = await getToken(); - console.log("docu paged 1: ", queryUrl); + const queryUrl = + "pinswg_documents?$filter=pinswg_publishtoweb eq true and _pinswg_documentids_value eq " + + incidentID + + " and pinswg_latestpublishedversion ne null and pinswg_latestpublisheddate ne null" + + docTypeQueryString + + "&$select=pinswg_isharedocumentlocations,_pinswg_documentids_value,pinswg_isharelabelcasetype,pinswg_isharelabellpaname,pinswg_publishtoweb,pinswg_uploadstatus,pinswg_isharedocumentclassification,pinswg_isharedocumentreference,pinswg_name,pinswg_latestpublishedversion,pinswg_latestpublisheddate,pinswg_documentpublisheddate&$orderby=" + + orderby + + " " + + fieldSort + + "&$count=true" + + (typeof pageNumber != "undefined" + ? "&$skiptoken=" + + ('') + : ""); - return axios - .get( + const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeadersPagedCustom(token.access_token, showNumberOfRecords) - ) - .then(({ data }) => { - data.value.forEach(function (element) { - Object.assign(element, { - "pinswg_documentpublisheddate": - element.pinswg_documentpublisheddate != null - ? element.pinswg_documentpublisheddate - : element.pinswg_latestpublisheddate - }); - element.pinswg_hashlink = encryptDocReference( - element.pinswg_isharedocumentreference - ); + ); + + data.value.forEach(function (element) { + Object.assign(element, { + "pinswg_documentpublisheddate": + element.pinswg_documentpublisheddate != null + ? element.pinswg_documentpublisheddate + : element.pinswg_latestpublisheddate }); - var dataStr; - _.has(data, "@odata.nextLink") == true && - ((dataStr = JSON.stringify(data["@odata.nextLink"])), - (data["@odata.nextLink"] = dataStr.split("/v8.2/")[1])); - return res.status(200).json(data); - }) - .catch((error) => { - consoleLogger(error); - res.status(400).json(error); + element.pinswg_hashlink = encryptDocReference( + element.pinswg_isharedocumentreference + ); }); + + 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: "SEARCH_DOCUMENT_DETAILS_PAGED_FETCH_FAILED", + message: "Failed to fetch paged search document details" + }); + } } diff --git a/pages/api/endpoint/getsearchdocumenthistory_api.js b/pages/api/endpoint/getsearchdocumenthistory_api.js index 9c25514d..19893c0a 100644 --- a/pages/api/endpoint/getsearchdocumenthistory_api.js +++ b/pages/api/endpoint/getsearchdocumenthistory_api.js @@ -17,34 +17,45 @@ */ import axios from "axios"; -import CryptoJS from "crypto-js"; 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 documentID = req.query.documentid; - var token = await getToken(); + const documentID = req.query.documentid; - var queryUrl = - "pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " + - documentID; + if (typeof documentID !== "string" || documentID.trim().length === 0) { + return respondError(res, { + status: 400, + code: "DOCUMENT_ID_REQUIRED", + message: "documentid is required" + }); + } - return axios - .get( + try { + const token = await getToken(); + const queryUrl = + "pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " + + documentID; + + const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeadersPaged(token.access_token) - ) - .then(({ data }) => { - res.status(200).json(data); - }) - .catch((error) => { - consoleLogger(error); - res.status(400).json(error); + ); + + return respondSuccess(res, data); + } catch (error) { + consoleLogger(error); + return respondError(res, { + status: 400, + code: "SEARCH_DOCUMENT_HISTORY_FETCH_FAILED", + message: "Failed to fetch search document history" }); + } } diff --git a/pages/api/endpoint/getsearchdocumenthistorypaged_api.js b/pages/api/endpoint/getsearchdocumenthistorypaged_api.js index 1f164445..4ee413c4 100644 --- a/pages/api/endpoint/getsearchdocumenthistorypaged_api.js +++ b/pages/api/endpoint/getsearchdocumenthistorypaged_api.js @@ -43,39 +43,50 @@ */ import axios from "axios"; -import CryptoJS from "crypto-js"; 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 documentID = req.query.documentid; - var pageNumber = req.query.pageNumber; - var token = await getToken(); + const documentID = req.query.documentid; - var queryUrl = - "pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " + - documentID; - // + - // (typeof pageNumber != "undefined" - // ? "&$skiptoken=" + ('') - // : ""); + if (typeof documentID !== "string" || documentID.trim().length === 0) { + return respondError(res, { + status: 400, + code: "DOCUMENT_ID_REQUIRED", + message: "documentid is required" + }); + } - return axios - .get( + try { + const token = await getToken(); + + const queryUrl = + "pinswg_documenthistories?$count=true&$select=_pinswg_documentid_value,pinswg_createddate,pinswg_state,pinswg_fileexists,statecode,pinswg_publisheddate&$filter=_pinswg_documentid_value eq " + + documentID; + // + + // (typeof pageNumber != "undefined" + // ? "&$skiptoken=" + ('') + // : ""); + + const { data } = await axios.get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeadersPaged(token.access_token) - ) - .then(({ data }) => { - res.status(200).json(data); - }) - .catch((error) => { - consoleLogger(error); - res.status(400).json(error); + ); + + return respondSuccess(res, data); + } catch (error) { + consoleLogger(error); + return respondError(res, { + status: 400, + code: "SEARCH_DOCUMENT_HISTORY_PAGED_FETCH_FAILED", + message: "Failed to fetch paged search document history" }); + } } diff --git a/tests/phase21/endpoint-handler-contract.test.cjs b/tests/phase21/endpoint-handler-contract.test.cjs index a85e5ed2..4ef9aae9 100644 --- a/tests/phase21/endpoint-handler-contract.test.cjs +++ b/tests/phase21/endpoint-handler-contract.test.cjs @@ -971,6 +971,368 @@ test("getadvancedsearch catch path returns ADVANCED_SEARCH_FETCH_FAILED", async ); }); +test("getsearchdocumenthistory returns DOCUMENT_ID_REQUIRED when documentid missing", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumenthistory_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + 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, "DOCUMENT_ID_REQUIRED"); +}); + +test("getsearchdocumenthistory catch path returns SEARCH_DOCUMENT_HISTORY_FETCH_FAILED", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumenthistory_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + axios: { + get: async () => { + throw new Error("relay failed"); + } + }, + consoleLogger: () => {} + } + ); + + const req = { query: { documentid: "d1" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual( + res.state.jsonBody.error.code, + "SEARCH_DOCUMENT_HISTORY_FETCH_FAILED" + ); +}); + +test("getsearchdocumenthistory success returns existing data payload contract", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumenthistory_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + axios: { + get: async () => ({ + data: { value: [{ _pinswg_documentid_value: "d1" }] } + }) + }, + consoleLogger: () => {} + } + ); + + const req = { query: { documentid: "d1" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 200); + assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), { + value: [{ _pinswg_documentid_value: "d1" }] + }); +}); + +test("getsearchdocumenthistorypaged returns DOCUMENT_ID_REQUIRED when documentid missing", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumenthistorypaged_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + 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, "DOCUMENT_ID_REQUIRED"); +}); + +test("getsearchdocumenthistorypaged catch path returns SEARCH_DOCUMENT_HISTORY_PAGED_FETCH_FAILED", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumenthistorypaged_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + axios: { + get: async () => { + throw new Error("relay failed"); + } + }, + consoleLogger: () => {} + } + ); + + const req = { query: { documentid: "d1" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual( + res.state.jsonBody.error.code, + "SEARCH_DOCUMENT_HISTORY_PAGED_FETCH_FAILED" + ); +}); + +test("getsearchdocumentdetails returns INCIDENT_ID_REQUIRED when incidentid missing", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumentdetails_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + axios: { get: async () => ({ data: { value: [] } }) }, + consoleLogger: () => {}, + CryptoJS: { + HmacSHA256: () => ({ toString: () => "hash" }), + enc: { Hex: { parse: () => "parsed", stringify: () => "hex" } } + }, + _: { 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, "INCIDENT_ID_REQUIRED"); +}); + +test("getsearchdocumentdetails catch path returns SEARCH_DOCUMENT_DETAILS_FETCH_FAILED", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumentdetails_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPaged: () => ({}), + axios: { + get: async () => { + throw new Error("relay failed"); + } + }, + consoleLogger: () => {}, + CryptoJS: { + HmacSHA256: () => ({ toString: () => "hash" }), + enc: { Hex: { parse: () => "parsed", stringify: () => "hex" } } + }, + _: { has: () => false } + } + ); + + const req = { query: { incidentid: "i1" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual( + res.state.jsonBody.error.code, + "SEARCH_DOCUMENT_DETAILS_FETCH_FAILED" + ); +}); + +test("getsearchdocumentdetailspaged returns INCIDENT_ID_REQUIRED when incidentid missing", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumentdetailspaged_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPagedCustom: () => ({}), + axios: { get: async () => ({ data: { value: [] } }) }, + consoleLogger: () => {}, + CryptoJS: { + HmacSHA256: () => ({ toString: () => "hash" }), + enc: { Hex: { parse: () => "parsed", stringify: () => "hex" } } + }, + _: { has: () => false } + } + ); + + const req = { + query: { + orderby: "createdon", + fieldSort: "asc", + showNumberOfRecords: "10" + } + }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual(res.state.jsonBody.error.code, "INCIDENT_ID_REQUIRED"); +}); + +test("getsearchdocumentdetailspaged catch path returns SEARCH_DOCUMENT_DETAILS_PAGED_FETCH_FAILED", async () => { + const mod = loadModule( + "pages/api/endpoint/getsearchdocumentdetailspaged_api.js", + { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeadersPagedCustom: () => ({}), + axios: { + get: async () => { + throw new Error("relay failed"); + } + }, + consoleLogger: () => {}, + CryptoJS: { + HmacSHA256: () => ({ toString: () => "hash" }), + enc: { Hex: { parse: () => "parsed", stringify: () => "hex" } } + }, + _: { has: () => false } + } + ); + + const req = { + query: { + incidentid: "i1", + orderby: "createdon", + fieldSort: "asc", + showNumberOfRecords: "10" + } + }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual( + res.state.jsonBody.error.code, + "SEARCH_DOCUMENT_DETAILS_PAGED_FETCH_FAILED" + ); +}); + +test("getsearchdocumentTypes returns INCIDENT_ID_REQUIRED when incidentid missing", async () => { + const mod = loadModule("pages/api/endpoint/getsearchdocumentTypes_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, "INCIDENT_ID_REQUIRED"); +}); + +test("getsearchdocumentTypes catch path returns SEARCH_DOCUMENT_TYPES_FETCH_FAILED", async () => { + const mod = loadModule("pages/api/endpoint/getsearchdocumentTypes_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: { incidentid: "i1" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual( + res.state.jsonBody.error.code, + "SEARCH_DOCUMENT_TYPES_FETCH_FAILED" + ); +}); + +test("getsearchdocumentTypes success returns grouped data payload contract", async () => { + const mod = loadModule("pages/api/endpoint/getsearchdocumentTypes_api.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeaders: () => ({}), + axios: { + get: async () => ({ + data: { + value: [ + { + pinswg_isharedocumentlocations: 1, + "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue": + "Plans" + }, + { + pinswg_isharedocumentlocations: 1, + "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue": + "Plans" + }, + { + pinswg_isharedocumentlocations: 2, + "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue": + "Letters" + } + ] + } + }) + }, + consoleLogger: () => {} + }); + + const req = { query: { incidentid: "i1" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 200); + assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), [ + { + pinswg_isharedocumentlocations: 1, + pinswg_isharedocumentlocationsLabel: "Plans", + count: 2 + }, + { + pinswg_isharedocumentlocations: 2, + pinswg_isharedocumentlocationsLabel: "Letters", + count: 1 + } + ]); +}); + const run = async () => { let passed = 0; for (const currentTest of tests) {