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
+22 -15
View File
@@ -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) => {
+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,
+3 -3
View File
@@ -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: {
@@ -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, "<<<<end query");
console.log("query: ", queryUrl, "<<<<end query");
return axios
.get(
+30 -27
View File
@@ -124,38 +124,41 @@ export const getServerSideProps = wrapper.getServerSideProps(
"public, s-maxage=10, stale-while-revalidate=59"
);
let searchResultsObj = await getBasicDNSSearch();
// let searchResultsObj = await getBasicDNSSearch();
searchResultsObj =
searchResultsObj.status == 502
? {
value: [],
errorCode: searchResultsObj.status,
errorMsg: searchResultsObj.statusText,
}
: searchResultsObj;
// searchResultsObj =
// searchResultsObj.status == 502
// ? {
// value: [],
// errorCode: searchResultsObj.status,
// errorMsg: searchResultsObj.statusText,
// }
// : searchResultsObj;
const showLoginCheck = process.env.SHOWLOGIN || false;
const showMapCheck = process.env.SHOWMAPS || false;
if (_.has(searchResultsObj, "status") == true) {
return {
redirect: {
//destination: "/dns-not-found",
destination: "/error",
permanent: false,
},
};
} else {
const searchDetailsObj = await getSearchDetails(
searchResultsObj
);
const dnsCoords = await getDNSCoords();
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setSearch("DNS"));
store.dispatch(setDNSCoords(dnsCoords));
}
// if (_.has(searchResultsObj, "status") == true) {
// return {
// redirect: {
// //destination: "/dns-not-found",
// destination: "/error",
// permanent: false,
// },
// };
// } else {
// // const searchDetailsObj = await getSearchDetails(
// // searchResultsObj
// // );
// const dnsCoords = await getDNSCoords();
// // store.dispatch(setSearchResults(searchResultsObj));
// // store.dispatch(setSearchDetails(searchDetailsObj));
// store.dispatch(setSearch("DNS"));
// store.dispatch(setDNSCoords(dnsCoords));
// }
const dnsCoords = await getDNSCoords();
store.dispatch(setSearch("DNS"));
store.dispatch(setDNSCoords(dnsCoords));
return {
props: {
+5 -10
View File
@@ -107,17 +107,14 @@ export const getServerSideProps = wrapper.getServerSideProps(
loggedInUser = loggedInUser.value[0].contactid;
const [accountDetails, searchResultsObj, watchedCases] =
await Promise.all([
await getPersonalAccount(loggedInUser),
await getAdvancedSearch(query),
await getWatchedCases(loggedInUser),
]);
const [accountDetails, watchedCases] = await Promise.all([
await getPersonalAccount(loggedInUser),
await getWatchedCases(loggedInUser),
]);
console.log(accountDetails);
const [searchDetailsObj, watchedCasesDetails] = await Promise.all([
await getSearchDetails(searchResultsObj),
const [watchedCasesDetails] = await Promise.all([
await getDetails(watchedCases, "myWatchedCases"),
]);
@@ -126,8 +123,6 @@ export const getServerSideProps = wrapper.getServerSideProps(
store.dispatch(setShowReps(showReps, showLoginCheck));
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setSearch(Object.entries(query)));
+15 -18
View File
@@ -161,31 +161,28 @@ export const getServerSideProps = wrapper.getServerSideProps(
},
};
} else {
let [accountDetails, searchResultsObj, watchedCases] =
await Promise.all([
await getPersonalAccount(loggedInUser),
await getBasicDNSSearch(),
await getWatchedCases(loggedInUser),
]);
let [accountDetails, watchedCases] = await Promise.all([
await getPersonalAccount(loggedInUser),
await getWatchedCases(loggedInUser),
]);
searchResultsObj =
searchResultsObj.status == 502
? {
value: [],
errorCode: searchResultsObj.status,
errorMsg: searchResultsObj.statusText,
}
: searchResultsObj;
// searchResultsObj =
// searchResultsObj.status == 502
// ? {
// value: [],
// errorCode: searchResultsObj.status,
// errorMsg: searchResultsObj.statusText,
// }
// : searchResultsObj;
const showMapCheck = process.env.SHOWMAPS || false;
const [searchDetailsObj, watchedCasesDetails] = await Promise.all([
await getSearchDetails(searchResultsObj),
const [watchedCasesDetails] = await Promise.all([
await getDetails(watchedCases, "myWatchedCases"),
]);
const dnsCoords = await getDNSCoords();
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
// store.dispatch(setSearchResults(searchResultsObj));
// store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setSearch("DNS"));
store.dispatch(setDNSCoords(dnsCoords));
store.dispatch(setWatchedCases(watchedCases));
+5 -19
View File
@@ -100,20 +100,10 @@ export const getServerSideProps = wrapper.getServerSideProps(
},
};
} else {
let [searchResultsObj, loggedInUser] = await Promise.all([
await getBasicSearch(query.q),
let [loggedInUser] = await Promise.all([
await getPortalLogin(thisSession.user.email),
]);
searchResultsObj =
searchResultsObj.status == 502
? {
value: [],
errorCode: searchResultsObj.status,
errorMsg: searchResultsObj.statusText,
}
: searchResultsObj;
//console.log("sssss", searchResultsObj);
loggedInUser = loggedInUser.value[0].contactid;
@@ -122,15 +112,11 @@ export const getServerSideProps = wrapper.getServerSideProps(
//console.log("sssss", loggedInUser);
const [accountDetails, searchDetailsObj, watchedCasesDetails] =
await Promise.all([
await getPersonalAccount(loggedInUser),
await getSearchDetails(searchResultsObj),
await getDetails(watchedCases, "myWatchedCases"),
]);
const [accountDetails, watchedCasesDetails] = await Promise.all([
await getPersonalAccount(loggedInUser),
await getDetails(watchedCases, "myWatchedCases"),
]);
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setSearch(query.q));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
+24 -23
View File
@@ -132,33 +132,34 @@ export const getServerSideProps = wrapper.getServerSideProps(
const showReps = process.env.SHOWREPRESENTATIONS || false;
store.dispatch(setShowReps(showReps, showLoginCheck));
let searchResultsObj = await getBasicSearch(query.q);
// let searchResultsObj = await getBasicSearch(query.q);
searchResultsObj =
searchResultsObj.status == 502
? {
value: [],
errorCode: searchResultsObj.status,
errorMsg: searchResultsObj.statusText,
}
: searchResultsObj;
// searchResultsObj =
// searchResultsObj.status == 502
// ? {
// value: [],
// errorCode: searchResultsObj.status,
// errorMsg: searchResultsObj.statusText,
// }
// : searchResultsObj;
if (_.has(searchResultsObj, "status") == true) {
return {
redirect: {
//destination: "/dns-not-found",
destination: "/error",
permanent: false,
},
};
} else {
const searchDetailsObj = await getSearchDetails(searchResultsObj);
// if (_.has(searchResultsObj, "status") == true) {
// return {
// redirect: {
// //destination: "/dns-not-found",
// destination: "/error",
// permanent: false,
// },
// };
// } else {
// // const searchDetailsObj = await getSearchDetails(searchResultsObj);
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setSearch(query.q));
}
// store.dispatch(setSearchResults(searchResultsObj));
// // store.dispatch(setSearchDetails(searchDetailsObj));
// store.dispatch(setSearch(query.q));
// }
store.dispatch(setSearch(query.q));
return {
props: {
isLinkedCase: isLinked,