Files
pedwfrontend/pages/myportal/advancedsearchresults.js
2026-05-14 08:55:49 +00:00

253 lines
8.5 KiB
JavaScript

import useTranslation from "next-translate/useTranslation";
import Head from "next/head";
import { useRouter } from "next/router";
import { connect } from "react-redux";
import { consoleLogger, getIP } from "../../actions/core/logger";
import {
getPersonalAccount,
getPortalLogin
} from "../../actions/services/accountService";
import { getPortalModuleDetails } from "../../actions/services/caseService";
import { getAdvancedSearch } from "../../actions/services/searchService";
import { getWatchedCases } from "../../actions/services/portalService";
import Breadcrumbs from "../../components/breadcrumbs";
import CookieBanner from "../../components/cookieBanner";
import Footer from "../../components/footer";
import Header from "../../components/header";
import SearchResults from "../../components/searchresults";
import {
getFormCollectionByID,
getSearchDetails
} from "../../components/utils";
import {
setSearchDetails,
setSearchResults
} from "../../store/searchOutput/action";
import {
setAccountDetails,
setContainerID,
setLoggedInUserId
} from "../../store/accountDetails/action";
import { setShowReps } from "../../store/currentView/action";
import { setSearch } from "../../store/search/action";
import { getSession } from "next-auth/react";
import TimeOut from "../../components/timeout";
import { wrapper } from "../../store/store";
import {
setWatchedCases,
setWatchedCasesDetails
} from "../../store/watchedCases/action";
import ServiceBanner from "../../components/myportal/servicebanner";
const Home = (props) => {
const { footerLinks, pages, watchedCases } = props;
let { t, lang } = useTranslation();
const router = useRouter();
const { appealtypes } = router.query;
return (
<div>
<Head>
<title>
{t("search:page-title")} - {t("common:service-name")}
</title>
</Head>
<div id="page_wrapper">
<CookieBanner />
<Header />
<div className="govuk-width-container">
<Breadcrumbs slug={appealtypes} />
<ServiceBanner props={props} />
<SearchResults
searchString={props.search.searchString}
searchResultsObj={props.searchResultsObj}
advancedSearch={true}
watchedCases={watchedCases}
myportal={true}
/>
</div>
<Footer footerLinks={footerLinks} ticketnumber={props} />
</div>
<TimeOut />
</div>
);
};
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (ctx) => {
const { query, req } = ctx;
getIP(req);
console.log("query-", query);
//console.log("search array:", Object.entries(query));
const showLoginCheck = process.env.SHOWLOGIN || false;
let thisSession = await getSession(ctx);
if (!thisSession) {
consoleLogger({
name: "MyPortalAdvancedSearchResultsAuthGuard",
reasonCode: "NO_SESSION",
message:
"advancedsearchresults loader missing session; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!thisSession?.user?.email || !thisSession?.user?.id) {
consoleLogger({
name: "MyPortalAdvancedSearchResultsAuthGuard",
reasonCode: "NO_SESSION_USER",
message:
"advancedsearchresults loader missing session user identity; redirecting to signin",
hasSessionUserEmail: !!thisSession?.user?.email,
hasSessionUserId: !!thisSession?.user?.id
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
let loggedInUser;
try {
const loggedInUserLookup = await getPortalLogin(
thisSession.user.email
);
loggedInUser = loggedInUserLookup?.value?.[0]?.contactid;
} catch (error) {
consoleLogger({
name: "MyPortalAdvancedSearchResultsAuthGuard",
reasonCode: "UPSTREAM_FAILURE",
message:
"advancedsearchresults loader portal login lookup failed; redirecting to signin",
error: error?.message
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!loggedInUser) {
consoleLogger({
name: "MyPortalAdvancedSearchResultsAuthGuard",
reasonCode: "CONTACT_LOOKUP_FAILED",
message:
"advancedsearchresults loader missing CRM contact id; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
try {
thisSession != false &&
store.dispatch(setContainerID(thisSession.user.id));
const [accountDetails, watchedCases] = await Promise.all([
getPersonalAccount(loggedInUser),
getWatchedCases(loggedInUser)
]);
console.log(accountDetails);
const watchedCasesDetails = await getDetails(
watchedCases,
"myWatchedCases"
);
const showLoginCheck = process.env.SHOWLOGIN || false;
const showReps = process.env.SHOWREPRESENTATIONS || false;
store.dispatch(setShowReps(showReps, showLoginCheck));
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setSearch(Object.entries(query)));
store.dispatch(setLoggedInUserId(loggedInUser));
} catch (error) {
consoleLogger({
name: "MyPortalAdvancedSearchResultsAuthGuard",
reasonCode: "UPSTREAM_FAILURE",
message:
"advancedsearchresults loader dependency failure; redirecting to signin",
error: error?.message
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
return {
props: {
showLoginCheck: showLoginCheck
}
};
}
);
const getDetails = (resultsObj, detailsType) => {
let detailsArr = [];
resultsObj = resultsObj.value;
const detailsObj = resultsObj.map((searchDetail) => {
if (searchDetail.pinswg_appealcasetype == null) {
console.log(
detailsType != "myWatchedCases"
? searchDetail.ticketnumber
: searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
);
} else {
detailsArr.push(
getPortalModuleDetails(
getFormCollectionByID(searchDetail.pinswg_appealcasetype)
.LogicalCollectionName,
detailsType != "myWatchedCases"
? searchDetail.ticketnumber
: searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
)
);
}
});
return Promise.all(detailsArr);
};
const mapStateToProps = (state) => {
return {
accountDetails: state.accountDetails,
currentView: state.currentView,
search: state.search,
searchResultsObj: state.searchResultsObj,
formData: state.formData,
appealType: state.appealType,
form: state.form,
myCases: state.myCases,
watchedCases: state.watchedCases,
myRepresentations: state.myRepresentations,
awaitingSubmission: state.awaitingSubmission
};
};
export default connect(mapStateToProps)(Home);