diff --git a/actions/index.js b/actions/index.js index fd04e4a8..826c15f0 100644 --- a/actions/index.js +++ b/actions/index.js @@ -532,23 +532,30 @@ export const getBasicSearchDetailsPaged = async ( appealTypeName, caseReference, primaryIdAttribute, - incidentID + incidentIDs ) => { - //console.log("check appealtype:", appealType, caseReference); + if (!incidentIDs || incidentIDs.length === 0) return []; - return axios - .get( - "/api/endpoint/getbasicsearchdetailspaged_api?appealTypeName=" + - appealTypeName + - "&primaryIdAttribute=" + - primaryIdAttribute + - "&incidentID=" + - incidentID - ) - .then((res) => res.data) - .catch((error) => { - consoleLogger(error); - }); + // Build OData filter correctly + const filter = incidentIDs + .map((id) => `_${primaryIdAttribute}s_value eq ${id}`) + .join(" or "); + + const url = `/api/endpoint/getbasicsearchdetailspaged_api?appealTypeName=${appealTypeName}&primaryIdAttribute=${primaryIdAttribute}&incidentID=${encodeURIComponent( + filter + )}`; + + // console.log( + // `Fetching ${incidentIDs.length} incidents for appeal type: ${appealTypeName}` + // ); + + try { + const res = await axios.get(url); + return res.data.value || []; // ensure returning an array + } catch (error) { + consoleLogger(error); + return []; + } }; export const getSearchDocumentDetails = (incidentID) => { diff --git a/actions/selectQueryTypes.js b/actions/selectQueryTypes.js index 936ad6a8..1c4cdb3c 100644 --- a/actions/selectQueryTypes.js +++ b/actions/selectQueryTypes.js @@ -4,7 +4,7 @@ export const getSelectQuery = (appealTypeName) => { return "&$select=pinswg_speacialistcaseprocess,modifiedon,_pinswg_appellant_value,pinswg_name,pinswg_siteaddresscounty,pinswg_siteaddressline1,pinswg_siteaddressline2,pinswg_siteaddresspostcode,pinswg_siteaddresstown,_pinswg_associatedlpa_value,pinswg_casedecisiondate,pinswg_dateeventrequested,pinswg_decision,pinswg_procedure,pinswg_questionnaireduedate,statuscode,pinswg_startdateoftheevent,pinswg_typeofevent,pinswg_startdate,pinswg_otherpartiesstatement,pinswg_finalcommentsduedate,pinswg_proofsofevidencewrittenstatementsofeviden,pinswg_speacialistcaseprocess,pinswg_statementduedate"; break; case "pinswg_callinss77s": - return "&$select=modifiedon,_pinswg_appellant_value,pinswg_name,pinswg_siteaddresscounty,pinswg_siteaddressline1,pinswg_siteaddressline2,pinswg_siteaddresspostcode,pinswg_siteaddresstown,_pinswg_associatedlpa_value,pinswg_casedecisiondate,pinswg_dateeventrequested,pinswg_decision,pinswg_finalcommentsduedate,pinswg_procedure,pinswg_proofsofevidencewrittenstatementsofeviden,pinswg_questionnaireduedate,pinswg_startdate,statuscode,pinswg_dateexportedtoiss,pinswg_startdateofevent,pinswg_typeofevent,pinswg_statementsduedate"; + return "&$select=modifiedon,_pinswg_appellant_value,pinswg_name,pinswg_siteaddresscounty,pinswg_siteaddressline1,pinswg_siteaddressline2,pinswg_siteaddresspostcode,pinswg_siteaddresstown,_pinswg_associatedlpa_value,pinswg_casedecisiondate,pinswg_dateeventrequested,pinswg_decision,pinswg_finalcommentsduedate,pinswg_procedure,pinswg_proofsofevidencewrittenstatementsofeviden,pinswg_questionnaireduedate,pinswg_startdate,statuscode,pinswg_dateexportedtoiss,pinswg_startdateofevent,pinswg_typeofevent,pinswg_statementsduedate,pinswg_otherpartiesstatement"; break; case "pinswg_commonlands": return "&$select=modifiedon,_pinswg_appellant_value,pinswg_name,pinswg_siteaddresscounty,pinswg_siteaddressline1,pinswg_siteaddressline2,pinswg_siteaddresspostcode,pinswg_siteaddresstown,_pinswg_associatedlpa_value,pinswg_casedecisiondate,pinswg_dateeventrequested,pinswg_decision,pinswg_finalcommentsduedate,pinswg_otherpartiesstatement,pinswg_procedure,pinswg_proofsofevidencewrittenstatementsofeviden,pinswg_questionnaireduedate,pinswg_relevantauthorityname,pinswg_startdate,statuscode,pinswg_starttimeoftheevent,pinswg_typeofevent,pinswg_statementsduedate"; diff --git a/components/admin/tabs/appeals.js b/components/admin/tabs/appeals.js index ce39a05f..bfa2115e 100644 --- a/components/admin/tabs/appeals.js +++ b/components/admin/tabs/appeals.js @@ -221,7 +221,7 @@ function AppealsTab(props) { resultsArr.map((item, key) => { let detailsObj = jsonpath({ path: - '$..value[?(@ && @.ticketnumber=="' + + '$..[?(@ && @.ticketnumber=="' + item.ticketnumber + '")]', json: searchDetailsObj, diff --git a/components/case/summary.js b/components/case/summary.js index 5e340ebd..a01ae25d 100644 --- a/components/case/summary.js +++ b/components/case/summary.js @@ -129,7 +129,7 @@ const CaseSummary = (props) => { }); }); - setCaseInvolvment(incidentid, loggedInUser); + //setCaseInvolvment(incidentid, loggedInUser); }; const isWatchedCase = (whichIncident) => { diff --git a/components/search/dnssearchresults.js b/components/search/dnssearchresults.js index 14c9f218..2e385fda 100644 --- a/components/search/dnssearchresults.js +++ b/components/search/dnssearchresults.js @@ -303,7 +303,7 @@ const DNSSearchResults = (props) => { resultsArr.map((item, key) => { let detailsObj = jsonpath({ path: - '$..value[?(@ && @.ticketnumber=="' + + '$..[?(@ && @.ticketnumber=="' + item.ticketnumber + '")]', json: searchDetailsObj, @@ -798,7 +798,14 @@ const DNSSearchResults = (props) => { )} - {resultsArr.length > 0 ? ( + + {!searchLoaded ? ( +
+
+

Loading...

+
+
+ ) : resultsArr.length > 0 ? ( ResultsView(resultsArr) ) : (
@@ -807,6 +814,16 @@ const DNSSearchResults = (props) => {
)} + + {/* {resultsArr.length > 0 ? ( + ResultsView(resultsArr) + ) : ( +
+
+

{t("search:searchresults-no-records-label")}

+
+
+ )} */} ); }; diff --git a/components/search/searchresults.js b/components/search/searchresults.js index 2656f674..70483a40 100644 --- a/components/search/searchresults.js +++ b/components/search/searchresults.js @@ -244,7 +244,7 @@ const SearchResults = (props) => { resultsArr.map((item, key) => { let detailsObj = jsonpath({ path: - '$..value[?(@ && @.ticketnumber=="' + + '$..[?(@ && @.ticketnumber=="' + item.ticketnumber + '")]', json: searchDetailsObj, diff --git a/components/utils/index.js b/components/utils/index.js index eaef542b..07831bcb 100644 --- a/components/utils/index.js +++ b/components/utils/index.js @@ -179,49 +179,146 @@ export const getPartSavedDetails = (searchResultsObj) => { * Get the details of a case from the appeal type * @param object searchResultsObj */ -export const getSearchDetailsPaged = (searchResultsObj) => { - let detailsArr = []; - searchResultsObj = searchResultsObj.value; - const detailsObj = searchResultsObj.map((searchDetail, index) => { - if (searchDetail.pinswg_appealcasetype == null) { - console.log(searchDetail.title); - } else { - let formMeta = getFormCollectionByID( - searchDetail.pinswg_appealcasetype - ); - formMeta?.LogicalCollectionName && - formMeta?.LogicalCollectionName != "pinswg_sipses" && - detailsArr.push( - getBasicSearchDetailsPaged( - formMeta.LogicalCollectionName, - searchDetail.title, - formMeta.PrimaryIdAttribute, - searchDetail.incidentid - ) - ); - } - }); - return Promise.all(detailsArr); -}; - -// export const absoluteUrl = (req, setLocalhost) => { -// var protocol = "https:"; -// var host = req -// ? req.headers["x-forwarded-host"] || req.headers["host"] -// : window.location.host; - -// if (host.indexOf("localhost") > -1) { -// if (setLocalhost) host = setLocalhost; -// protocol = "http:"; -// } - -// return { -// protocol: protocol, -// host: host, -// origin: protocol + "//" + host, -// }; +// export const getSearchDetailsPaged = (searchResultsObj) => { +// let detailsArr = []; +// searchResultsObj = searchResultsObj.value; +// const detailsObj = searchResultsObj.map((searchDetail, index) => { +// if (searchDetail.pinswg_appealcasetype == null) { +// console.log(searchDetail.title); +// } else { +// let formMeta = getFormCollectionByID( +// searchDetail.pinswg_appealcasetype +// ); +// formMeta?.LogicalCollectionName && +// formMeta?.LogicalCollectionName != "pinswg_sipses" && +// detailsArr.push( +// getBasicSearchDetailsPaged( +// formMeta.LogicalCollectionName, +// searchDetail.title, +// formMeta.PrimaryIdAttribute, +// searchDetail.incidentid +// ) +// ); +// } +// }); +// return Promise.all(detailsArr); // }; +// export const getSearchDetailsPaged = async (searchResultsObj) => { +// const searchResults = searchResultsObj.value; + +// //Group incidents by appeal type +// const groupedByAppealType = {}; + +// searchResults.forEach((incident) => { +// const appealType = incident.pinswg_appealcasetype; +// if (!appealType) return; + +// const formMeta = getFormCollectionByID(appealType); +// if ( +// !formMeta?.LogicalCollectionName || +// formMeta.LogicalCollectionName === "pinswg_sipses" +// ) +// return; + +// const key = formMeta.LogicalCollectionName; +// if (!groupedByAppealType[key]) groupedByAppealType[key] = []; +// groupedByAppealType[key].push({ +// incidentID: incident.incidentid, +// caseReference: incident.title, +// primaryIdAttribute: formMeta.PrimaryIdAttribute, +// }); +// }); + +// // Make all API calls in parallel per appeal type +// const allDetails = await Promise.all( +// Object.entries(groupedByAppealType).map( +// async ([appealTypeName, incidents]) => { +// console.log( +// `Fetching details for appeal type: ${appealTypeName}, incidents: ${incidents.length}` +// ); + +// // Fully parallel for each incident +// const results = await Promise.all( +// incidents.map((i) => +// getBasicSearchDetailsPaged( +// appealTypeName, +// i.caseReference, +// i.primaryIdAttribute, +// i.incidentID +// ).catch((err) => { +// console.error( +// `Error fetching incident ${i.caseReference}:`, +// err +// ); +// return null; // continue other calls even if one fails +// }) +// ) +// ); + +// return results.filter((r) => r); // remove nulls from failed calls +// } +// ) +// ); + +// //Flatten all results into a single array +// return allDetails.flat(); +// }; + +export const getSearchDetailsPaged = async (searchResultsObj) => { + searchResultsObj = searchResultsObj.value; + + // Group incidents by appeal type + const groupedByAppealType = searchResultsObj.reduce((acc, detail) => { + const appealType = detail.pinswg_appealcasetype; + if (!appealType) { + console.log("No appeal type for:", detail.title); + return acc; + } + + if (!acc[appealType]) acc[appealType] = []; + acc[appealType].push({ + incidentID: detail.incidentid, + title: detail.title, + }); + return acc; + }, {}); + + const allDetails = []; + + // For each appeal type, call getBasicSearchDetailsPaged once with all incident IDs + for (const [appealType, incidents] of Object.entries(groupedByAppealType)) { + const incidentIDs = incidents.map((i) => i.incidentID); + + // Get the form meta for this appeal type to access primaryIdAttribute + const formMeta = getFormCollectionByID(appealType); + + if (!formMeta || !formMeta.PrimaryIdAttribute) { + console.log(`Missing form metadata for appeal type: ${appealType}`); + continue; + } + + // console.log( + // `Fetching ${incidentIDs.length} incidents for appeal type: ${appealType} with primaryIdAttribute: ${formMeta.PrimaryIdAttribute}` + // ); + + try { + const details = await getBasicSearchDetailsPaged( + formMeta.LogicalCollectionName, // appealTypeName + null, // caseReference is no longer used in batch + formMeta.PrimaryIdAttribute, // primaryIdAttribute + incidentIDs // pass array of incidentIDs + ); + + allDetails.push(...details); + } catch (err) { + consoleLogger(err); + } + } + + return allDetails; +}; + export const getProgressObj = ( formObjXML, titleList, diff --git a/pages/advancedsearchresults.js b/pages/advancedsearchresults.js index 05848822..f2135ea9 100644 --- a/pages/advancedsearchresults.js +++ b/pages/advancedsearchresults.js @@ -124,8 +124,8 @@ export const getServerSideProps = wrapper.getServerSideProps( // "=============================== Advanced Search results:", // searchResultsObj // ); - const searchDetailsObj = - (await getSearchDetails(searchResultsObj)) || null; + // const searchDetailsObj = + // (await getSearchDetails(searchResultsObj)) || null; // console.log( // "=============================== Advanced Search results details:", // searchDetailsObj @@ -136,7 +136,7 @@ export const getServerSideProps = wrapper.getServerSideProps( store.dispatch(setShowReps(showReps, showLoginCheck)); store.dispatch(setSearchResults(searchResultsObj)); - store.dispatch(setSearchDetails(searchDetailsObj)); + // store.dispatch(setSearchDetails(searchDetailsObj)); store.dispatch(setSearch(Object.entries(query))); return { props: { diff --git a/pages/api/endpoint/getbasicsearchdetailspaged_api.js b/pages/api/endpoint/getbasicsearchdetailspaged_api.js index b2acaa78..08977c5c 100644 --- a/pages/api/endpoint/getbasicsearchdetailspaged_api.js +++ b/pages/api/endpoint/getbasicsearchdetailspaged_api.js @@ -66,9 +66,7 @@ export default async function ApiProxy(req, res) { var queryUrl = appealTypeName + - "?$filter=_" + - primaryIdAttribute + - "s_value eq " + + "?$filter=" + incidentID + "&$count=true" + "&$expand=" + @@ -79,7 +77,7 @@ export default async function ApiProxy(req, res) { queryUrl = queryUrl + getSelectQuery(appealTypeName); - //console.log("query: ", queryUrl, "<<<