updated serching to reduce CRM calls for performance and stability

This commit is contained in:
2025-10-28 11:21:38 +00:00
parent df199cff53
commit e54a0b2253
12 changed files with 265 additions and 164 deletions
+1 -1
View File
@@ -221,7 +221,7 @@ function AppealsTab(props) {
resultsArr.map((item, key) => {
let detailsObj = jsonpath({
path:
'$..value[?(@ && @.ticketnumber=="' +
'$..[?(@ && @.ticketnumber=="' +
item.ticketnumber +
'")]',
json: searchDetailsObj,
+19 -2
View File
@@ -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) => {
</div>
</div>
)}
{resultsArr.length > 0 ? (
{!searchLoaded ? (
<div className="govuk-grid-row">
<div className="govuk-grid-column-two-thirds">
<h3>Loading...</h3>
</div>
</div>
) : resultsArr.length > 0 ? (
ResultsView(resultsArr)
) : (
<div className="govuk-grid-row">
@@ -807,6 +814,16 @@ const DNSSearchResults = (props) => {
</div>
</div>
)}
{/* {resultsArr.length > 0 ? (
ResultsView(resultsArr)
) : (
<div className="govuk-grid-row">
<div className="govuk-grid-column-two-thirds">
<h3>{t("search:searchresults-no-records-label")}</h3>
</div>
</div>
)} */}
</>
);
};
+1 -1
View File
@@ -244,7 +244,7 @@ const SearchResults = (props) => {
resultsArr.map((item, key) => {
let detailsObj = jsonpath({
path:
'$..value[?(@ && @.ticketnumber=="' +
'$..[?(@ && @.ticketnumber=="' +
item.ticketnumber +
'")]',
json: searchDetailsObj,
+138 -41
View File
@@ -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,