import { useSession } from "next-auth/react"; import useTranslation from "next-translate/useTranslation"; import Head from "next/head"; import { useRouter } from "next/router"; import { connect } from "react-redux"; import Breadcrumbs from "../../components/breadcrumbs"; import CookieBanner from "../../components/cookieBanner"; import Footer from "../../components/footer"; import Header from "../../components/header"; import ViewAll from "../../components/myportal/viewall"; import NoSessionWarning from "../../components/nosession"; import TimeOut from "../../components/timeout"; import ServiceBanner from "../../components/myportal/servicebanner"; import { getSession } from "next-auth/react"; import pLimit from "p-limit"; import { getPortalLogin } from "../../actions/services/accountService"; import { getCrmPersonalAccount } from "../../lib/server/getCrmPersonalAccount"; import { consoleLogger } from "../../actions/core/logger"; import { getPortalModuleDetails } from "../../actions/services/caseService"; import { getAwaitingSubmissionFromBlob, getRepsFromBlob } from "../../actions/services/documentService"; import { getMyCases, getMyLPACases, getWatchedCases } from "../../actions/services/portalService"; import { getBasicSearch } from "../../actions/services/searchService"; import { splitWatchedCasesBySubmissionState } from "../../lib/domain/dashboard-policy"; import { getFormCollectionByID } from "../../components/utils"; import { setAccountDetails, setContainerID, setLoggedInUserId } from "../../store/accountDetails/action"; import { setAwaitingSubmissionDetails, setAwaitingSubmissionFromBlob } from "../../store/awaitingSubmission/action"; import { setCurrentView } from "../../store/currentView/action"; import { setMyCases, setMyCasesDetails } from "../../store/myCases/action"; import { setMyRepresentations, setMyRepresentationsDetails, setMySubmittedReps, setMySubmittedRepsDetails } from "../../store/myRepresentations/action"; import { setWatchedCases, setWatchedCasesDetails } from "../../store/watchedCases/action"; import { setSearchResults } from "../../store/searchOutput/action"; import { setSearch } from "../../store/search/action"; import { wrapper } from "../../store/store"; const Home = (props) => { const { footerLinks, pages } = props; let { t, lang } = useTranslation(); const router = useRouter(); const { locale } = router; const { appealtypes } = router.query; const { data: session, status } = useSession(); if (status === "loading") { return ; } if (status === "unauthenticated") { router.push("/auth/signin"); return ; } const titleKeys = { "myCases": t("common:mycases-viewall-title"), "awaitingSubmissionDetails": t( "common:awaitingsubmissiondetails-viewall-title" ), "myRepresentations": t("common:myrepresentations-viewall-title"), "watchedCases": t("common:watchedcases-viewall-title"), "mySubmittedReps": t("common:mysubmittedreps-viewall-title") }; function whichTitle(key) { return t(titleKeys[key]); } return (
{whichTitle(router.query?.viewKey)} -{" "} {t("common:service-name")}
); }; export const getServerSideProps = wrapper.getServerSideProps( (store) => async (ctx) => { const { query } = ctx; // The View All component uses router.query.key. const viewKey = Array.isArray(query.key) ? query.key[0] : query.key; console.log("[ViewAll SSR] Query:", ctx.query); console.log("[ViewAll SSR] Selected viewKey:", viewKey); const validViewKeys = [ "myCases", "watchedCases", "mySubmittedReps", "myRepresentations", "awaitingSubmissionDetails" ]; if (!validViewKeys.includes(viewKey)) { return { redirect: { destination: "/myportal", permanent: false } }; } // Authenticate before retrieving portal data. const thisSession = await getSession(ctx); if (!thisSession?.user?.email) { return { redirect: { destination: "/auth/signin", permanent: false } }; } const portalLogin = await getPortalLogin(thisSession.user.email); const loggedInUser = portalLogin?.value?.[0]?.contactid; if (!loggedInUser) { return { redirect: { destination: "/auth/signin", permanent: false } }; } let accountDetails; try { accountDetails = await getCrmPersonalAccount(loggedInUser); } catch (error) { consoleLogger({ name: "MyPortalViewAllAccountLookup", reasonCode: "UPSTREAM_FAILURE", message: "myportal viewall personal account lookup failed", error: error?.message }); return { redirect: { destination: "/myportal/error", permanent: false } }; } if (!accountDetails || accountDetails.errorCode) { return { redirect: { destination: "/myportal/error", permanent: false } }; } // Preserve the LPA logic from MyPortal index. const isLPA = accountDetails.pinswg_typeofinvolvement === 846040012; const lpaId = accountDetails[ "pinswg_contact_associatedlpa@OData.Community.Display.V1.FormattedValue" ]; // Full-list detail enrichment. // No dashboard limit is applied here. const getDetails = async (resultsObj, detailsType) => { const limitRequests = pLimit(5); const list = Array.isArray(resultsObj) ? resultsObj : (resultsObj?.value ?? []); const tasks = list.map((item) => limitRequests(async () => { try { let caseID = ""; switch (detailsType) { case "myCases": caseID = item.ticketnumber; break; case "myWatchedCases": case "mySubmittedReps": caseID = item[ "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" ]; break; case "awaitingSubmission": caseID = item.ticketnumber; break; case "myRepresentations": caseID = item.casereference; break; default: caseID = item.pinswg_title; } if (!item.pinswg_appealcasetype || !caseID) { return null; } const formCollection = getFormCollectionByID( item.pinswg_appealcasetype ); if (!formCollection?.LogicalCollectionName) { console.error( "Missing collection metadata for appeal type:", item.pinswg_appealcasetype ); return null; } return ( (await getPortalModuleDetails( formCollection.LogicalCollectionName, caseID )) || {} ); } catch (err) { console.error("Error fetching View All details:", err); return null; } }) ); const details = await Promise.all(tasks); // Retain the original result ordering. return details.filter(Boolean); }; // Shared account state. store.dispatch(setAccountDetails(accountDetails)); store.dispatch(setLoggedInUserId(loggedInUser)); store.dispatch(setContainerID(thisSession.user.id)); // Ensure the rendered View All category agrees // with the category loaded by this request. store.dispatch( setCurrentView({ viewKey, viewName: viewKey }) ); switch (viewKey) { case "myCases": { const myCases = isLPA ? await getMyLPACases(lpaId) : await getMyCases(loggedInUser); const myCasesDetails = await getDetails(myCases, "myCases"); store.dispatch(setMyCases(myCases)); store.dispatch(setMyCasesDetails(myCasesDetails)); break; } case "watchedCases": { const watchedCases = await getWatchedCases(loggedInUser); const { watchedCases: filteredWatchedCases } = splitWatchedCasesBySubmissionState( watchedCases.value ?? [] ); const watchedCasesDetails = await getDetails( filteredWatchedCases, "myWatchedCases" ); store.dispatch(setWatchedCases(filteredWatchedCases)); store.dispatch(setWatchedCasesDetails(watchedCasesDetails)); break; } case "mySubmittedReps": { const watchedCases = await getWatchedCases(loggedInUser); const { submittedRepresentations: mySubmittedReps } = splitWatchedCasesBySubmissionState( watchedCases.value ?? [] ); const mySubmittedRepsDetails = await getDetails( mySubmittedReps, "mySubmittedReps" ); store.dispatch(setMySubmittedReps(mySubmittedReps)); store.dispatch( setMySubmittedRepsDetails(mySubmittedRepsDetails) ); break; } case "myRepresentations": { const myRepresentations = await getRepsFromBlob( thisSession.user.id, { headers: { Cookie: ctx.req?.headers?.cookie || "" } } ); // The existing View All component reads // representation display data directly // from myRepresentations.value. store.dispatch(setMyRepresentations(myRepresentations)); // Preserve the existing Redux detail // collection contract. const myRepresentationsDetails = await getDetails( myRepresentations, "myRepresentations" ); store.dispatch( setMyRepresentationsDetails(myRepresentationsDetails) ); break; } case "awaitingSubmissionDetails": { const awaitingSubmissionFromBlob = await getAwaitingSubmissionFromBlob(thisSession.user.id); // Awaiting submission already has its // own Blob-backed data structure. // Preserve the dashboard's existing // Redux dispatches. store.dispatch( setAwaitingSubmissionFromBlob(awaitingSubmissionFromBlob) ); store.dispatch( setAwaitingSubmissionDetails(awaitingSubmissionFromBlob) ); break; } } // Preserve the original View All search state. const searchResultsObj = (await getBasicSearch("View All")) || null; store.dispatch(setSearchResults(searchResultsObj)); store.dispatch(setSearch("View All")); return { props: {} }; } ); const mapStateToProps = (state) => { return { accountDetails: state.accountDetails, 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, currentView: state.currentView, mySubmittedReps: state.myRepresentations.mySubmittedReps }; }; export default connect(mapStateToProps)(Home);