import { getSession, 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 { createContainerProxy, getAwaitingSubmissionFromBlob, getCase, getIP, getMyCases, getMyLPACases, getPersonalAccount, getPortalLogin, getPortalModuleDetails, getRepsFromBlob, getWatchedCases, } from "../../actions"; import Breadcrumbs from "../../components/breadcrumbs"; import CookieBanner from "../../components/cookieBanner"; import Footer from "../../components/footer"; import Header from "../../components/header"; import MyPortal from "../../components/myportal"; import { getFormCollectionByID } from "../../components/utils"; import { setAccountDetails, setContainerID, setLoggedInUserId, } from "../../store/accountDetails/action"; import { setAwaitingSubmissionDetails, setAwaitingSubmissionFromBlob, } from "../../store/awaitingSubmission/action"; import { setShowReps, setLocale } from "../../store/currentView/action"; import { setMyCases, setMyCasesDetails } from "../../store/myCases/action"; import { setMyRepresentations, setMyRepresentationsDetails, setMySubmittedReps, setMySubmittedRepsDetails, } from "../../store/myRepresentations/action"; import { wrapper } from "../../store/store"; import { setWatchedCases, setWatchedCasesDetails, } from "../../store/watchedCases/action"; import pLimit from "p-limit"; const Home = (props) => { const { footerLinks, myCases, myRepresentations, watchedCases, awaitingSubmission, accountDetails, showFileUpload, showLoginCheck, currentView, mySubmittedReps, } = props; const { t } = useTranslation(); const router = useRouter(); const { locale, query } = router; const { appealtypes } = query; const { data: session } = useSession(); return (
{t("myportal:page-title")} - {t("common:service-name")} <>
); }; export const getServerSideProps = wrapper.getServerSideProps( (store) => async (ctx) => { const { query, req, locale } = ctx; getIP(req); const cookies = req.cookies || {}; const thisSession = await getSession(ctx); if (!thisSession) { return { redirect: { destination: "/auth/signin", permanent: false, }, }; } const loggedInUserResponse = await getPortalLogin( thisSession.user.email ); const contacts = loggedInUserResponse?.value || []; if (contacts.length > 1) { return { redirect: { destination: locale === "cy" ? "/cy/manylionpellach" : "/furtherdetails", permanent: false, }, }; } const loggedInUser = contacts[0]?.contactid; await createContainerProxy(thisSession.user.id); const accountDetails = await getPersonalAccount(loggedInUser); const lpaId = accountDetails[ "pinswg_contact_associatedlpa@OData.Community.Display.V1.FormattedValue" ]; const isLPA = accountDetails.pinswg_typeofinvolvement === 846040012; // Fetch cases, reps, watched cases, awaiting submission concurrently const [ myCases, myRepresentations, watchedCases, awaitingSubmissionFromBlob, ] = await Promise.all([ isLPA ? getMyLPACases(lpaId) : getMyCases(loggedInUser), getRepsFromBlob(thisSession.user.id), getWatchedCases(loggedInUser), getAwaitingSubmissionFromBlob(thisSession.user.id), ]); // Set locale to store store.dispatch(setLocale(locale)); // Determine preferred locale (cookie overrides) let preferredLocale = accountDetails.pinswg_preferredlanguage === 846040000 ? "cy" : "en"; if (cookies.pedw_locale) { preferredLocale = cookies.pedw_locale; } if (preferredLocale !== locale) { return { redirect: { destination: `/${preferredLocale}/myportal`, permanent: false, locale: preferredLocale, }, }; } const showLoginCheck = Boolean(process.env.SHOWLOGIN); const showReps = Boolean(process.env.SHOWREPRESENTATIONS); store.dispatch(setShowReps(showReps, showLoginCheck)); // Filter watched cases where reps not submitted const filteredWatchedCases = { "@odata.count": watchedCases.value.filter( (c) => c.pinswg_representationsubmitted == null ).length, value: watchedCases.value.filter( (c) => c.pinswg_representationsubmitted == null ), }; // Get submitted reps from watched cases const mySubmittedReps = watchedCases.value.filter((el) => { return el.pinswg_representationsubmitted != null; }); if (accountDetails.errorCode) { return { redirect: { destination: "/myportal/error", permanent: false, }, }; } // Helper function for fetching details with concurrency limit const getDetails = async (resultsObj, detailsType) => { const limitRequests = pLimit(5); const arr = []; resultsObj = detailsType === "mySubmittedReps" ? resultsObj : resultsObj.value; const tasks = resultsObj.map((item) => limitRequests(async () => { try { let caseID = ""; switch (detailsType) { case "myCases": caseID = item.pinswg_title; 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) return; const collectionName = getFormCollectionByID( item.pinswg_appealcasetype ).LogicalCollectionName; const details = await getPortalModuleDetails( collectionName, caseID ); arr.push(details); } catch (err) { console.error("Error fetching details:", err); } }) ); await Promise.all(tasks); return arr; }; const [ myCasesDetails, watchedCasesDetails, mySubmittedRepsDetails, myRepresentationsDetails, ] = await Promise.all([ getDetails(myCases, "myCases"), getDetails(filteredWatchedCases, "myWatchedCases"), getDetails(mySubmittedReps, "mySubmittedReps"), getDetails(myRepresentations, "myRepresentations"), ]); store.dispatch(setAccountDetails(accountDetails)); store.dispatch(setLoggedInUserId(loggedInUser)); store.dispatch(setMyCases(myCases)); store.dispatch(setMyCasesDetails(myCasesDetails)); store.dispatch(setMyRepresentations(myRepresentations)); store.dispatch(setMyRepresentationsDetails(myRepresentationsDetails)); store.dispatch(setWatchedCases(filteredWatchedCases)); store.dispatch(setWatchedCasesDetails(watchedCasesDetails)); store.dispatch(setMySubmittedReps(mySubmittedReps)); store.dispatch(setMySubmittedRepsDetails(mySubmittedRepsDetails)); store.dispatch( setAwaitingSubmissionFromBlob(awaitingSubmissionFromBlob) ); store.dispatch( setAwaitingSubmissionDetails(awaitingSubmissionFromBlob) ); store.dispatch(setContainerID(thisSession.user.id)); return { props: { showFileUpload: Boolean(process.env.SHOWFILEUPLOAD), containerID: thisSession.user.id, showLoginCheck, }, }; } ); const mapStateToProps = (state) => ({ 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, mySubmittedReps: state.myRepresentations.mySubmittedReps, awaitingSubmission: state.awaitingSubmission, }); export default connect(mapStateToProps)(Home);