import _ from "lodash"; 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, getAwaitingSubmission, 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 { setAwaitingSubmission, setAwaitingSubmissionDetails, setAwaitingSubmissionFromBlob, } from "../../store/awaitingSubmission/action"; import { setShowReps, setLocale, setFilesForRepresentation, } 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, pages, myCases, myRepresentations, watchedCases, awaitingSubmission, accountDetails, showFileUpload, showLoginCheck, currentView, mySubmittedReps, } = props; let { t, lang } = useTranslation(); // const mySubmittedReps = myRepresentations.mySubmittedReps; const router = useRouter(); const { locale } = router; const { appealtypes } = router.query; const { data: session } = useSession(); return (
{t("myportal:page-title")} - {t("common:service-name")} <> {/* If they have a Twitter Handle, include the meta details below */}
); }; export const getServerSideProps = wrapper.getServerSideProps( (store) => async (ctx) => { const { query, req, res } = ctx; getIP(req); console.log("The query", query); const { cookies } = req; let loggedInUserCookie = cookies.pinsUser; let thisSession = await getSession(ctx); let loggedInUser = {}; if (!thisSession) { console.log("not has sesssion......."); return { redirect: { destination: "/auth/signin", permanent: false, }, }; } else { console.log(" has sesssion......."); [loggedInUser] = await Promise.all([ await getPortalLogin(thisSession.user.email), ]); //grabs first contact on this list //jump out page for duplicates.... console.log( "Number of contacts with this email:", loggedInUser.value.length ); if (loggedInUser.value.length > 1) { return { redirect: { destination: ctx.locale == "cy" ? "/cy/manylionpellach" : "/furtherdetails", permanent: false, }, }; } loggedInUser = loggedInUser.value[0].contactid; //console.log("nextAuth Session:", thisSession); } //Create Storage container for logged in user await createContainerProxy(thisSession.user.id); const accountDetails = await getPersonalAccount(loggedInUser); //console.log("----------", accountDetails); const lpaid = accountDetails[ "pinswg_contact_associatedlpa@OData.Community.Display.V1.FormattedValue" ]; const [ myCases, myRepresentations, watchedCases, //awaitingSubmission, awaitingSubmissionFromBlob, ] = await Promise.all([ accountDetails.pinswg_typeofinvolvement == 846040012 ? await getMyLPACases(lpaid) : await getMyCases(loggedInUser), await getRepsFromBlob(thisSession.user.id), await getWatchedCases(loggedInUser), //await getAwaitingSubmission(loggedInUser), await getAwaitingSubmissionFromBlob(thisSession.user.id), ]); //console.log( // "/////////////////////////\n ", // awaitingSubmissionFromBlob.value, // awaitingSubmissionFromBlob.case // ); console.log( "preferred language:", accountDetails.pinswg_preferredlanguage ); console.log( "locale :", accountDetails.pinswg_preferredlanguage == "846040000" ? "cy" : "en" ); console.log("req locale :", ctx.locale); console.log("cookie locale :", cookies.pedw_locale); store.dispatch(setLocale(ctx.locale)); let preferredLocale = accountDetails.pinswg_preferredlanguage == "846040000" ? "cy" : "en"; // added to redirect if not the users preferred language cookies.hasOwnProperty("pedw_locale") && (preferredLocale = cookies.pedw_locale); if (preferredLocale != ctx.locale) { return { redirect: { destination: `/${preferredLocale}`, permanent: false, }, }; } const showLoginCheck = process.env.SHOWLOGIN || false; const showMapCheck = process.env.SHOWMAPS || false; const showReps = process.env.SHOWREPRESENTATIONS || false; store.dispatch(setShowReps(showReps, showLoginCheck)); let showWatchedCases = (submittedArr) => { const required = submittedArr.value.filter((el) => { return el.pinswg_representationsubmitted == null; }); let newObj = {}; return Object.assign(newObj, { "@odata.count": required.length, "value": required, }); }; let filteredWatchedCases = showWatchedCases(watchedCases); let showSubmittedReps = (submittedArr) => { submittedArr = submittedArr.value; const required = submittedArr.filter((el) => { let IsSubmittedRep = _.has(el, "pinswg_representationsubmitted") ? el.pinswg_representationsubmitted != null && el.pinswg_representationsubmitted : ""; return IsSubmittedRep; }); return required; }; const mySubmittedReps = showSubmittedReps(watchedCases); if (_.has(accountDetails, "errorCode") != false) { return { redirect: { destination: "/myportal/error", permanent: false, }, }; } else { const [ myCasesDetails, watchedCasesDetails, mySubmittedRepsDetails, myRepresentationsDetails, ] = await Promise.all([ await getDetails(myCases, "myCases"), await getDetails(filteredWatchedCases, "myWatchedCases"), await getDetails(mySubmittedReps, "mySubmittedReps"), await getDetails(myRepresentations, "myRepresentations"), ]); // console.log( // "=================================\nmySubmittedRepsDetails:", // mySubmittedRepsDetails // ); 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(setAwaitingSubmission(awaitingSubmission)); store.dispatch( setAwaitingSubmissionFromBlob(awaitingSubmissionFromBlob) ); store.dispatch( setAwaitingSubmissionDetails(awaitingSubmissionFromBlob) ); store.dispatch(setContainerID(thisSession.user.id)); return { props: { showFileUpload: process.env.SHOWFILEUPLOAD, containerID: thisSession.user.id, showLoginCheck: process.env.SHOWLOGIN, }, }; } } ); // const getDetails = async (resultsObj, detailsType) => { // let detailsArr = []; // const limitRequests = pLimit(1); // Limit to 5 concurrent requests // resultsObj = // detailsType == "mySubmittedReps" ? resultsObj : resultsObj.value; // if (detailsType == "myRepresentations") { // limitRequests(async () => { // resultsObj.map((searchDetail, index) => { // detailsArr.push( // getCase(searchDetail["incidentID"]) // .then(async (data) => { // let caseID = ""; // switch (detailsType) { // case "myCases": // caseID = data.pinswg_title; // break; // case "myWatchedCases": // case "mySubmittedReps": // caseID = // data[ // "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" // ]; // break; // case "awaitingSubmission": // case "myRepresentations": // try { // caseID = data.ticketnumber; // } catch { // console.log("missing:", data); // } // break; // default: // caseID = data.pinswg_title; // } // return await getPortalModuleDetails( // getFormCollectionByID( // data.pinswg_appealcasetype // ).LogicalCollectionName, // caseID // ).catch((error) => { // console.log( // "=============================\ngetPortalModuleDetails error", // error // ); // }); // }) // .catch((error) => { // console.log( // "=============================\ngetCase error", // error // ); // }) // ); // }); // }); // } // //debugger; // await Promise.all( // resultsObj.map(async (searchDetail, index) => { // limitRequests(async () => { // try { // let caseID = ""; // switch (detailsType) { // case "myCases": // caseID = searchDetail.pinswg_title; // break; // case "myWatchedCases": // case "mySubmittedReps": // caseID = // searchDetail[ // "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" // ]; // break; // case "awaitingSubmission": // caseID = searchDetail.ticketnumber; // break; // case "myRepresentations": // caseID = searchDetail.casereference; // break; // default: // caseID = searchDetail.pinswg_title; // } // console.log(detailsType, " case id : ", caseID); // searchDetail.pinswg_appealcasetype != null && // detailsArr.push( // getPortalModuleDetails( // getFormCollectionByID( // searchDetail.pinswg_appealcasetype // ).LogicalCollectionName, // caseID // ) // ); // } catch (error) { // console.log("Error processing case:", error); // } // }); // }) // ); // let detArr = Promise.all(detailsArr); // // console.log(detArr); // return detArr; // }; const getDetails = async (resultsObj, detailsType) => { let detailsArr = []; const limitRequests = pLimit(5); // Allow up to 5 concurrent requests // Adjust resultsObj based on detailsType resultsObj = detailsType === "mySubmittedReps" ? resultsObj : resultsObj.value; const getDetailsForCase = async (searchDetail) => { try { let caseID = ""; // Determine the case ID based on the detailsType switch (detailsType) { case "myCases": caseID = searchDetail.pinswg_title; break; case "myWatchedCases": case "mySubmittedReps": caseID = searchDetail[ "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" ]; break; case "awaitingSubmission": caseID = searchDetail.ticketnumber; break; case "myRepresentations": caseID = searchDetail.casereference; break; default: caseID = searchDetail.pinswg_title; } console.log(detailsType, " case id: ", caseID); // If the appeal case type is valid, fetch the portal module details if (searchDetail.pinswg_appealcasetype != null) { const formCollectionName = getFormCollectionByID( searchDetail.pinswg_appealcasetype ).LogicalCollectionName; const details = await getPortalModuleDetails( formCollectionName, caseID ); detailsArr.push(details); } } catch (error) { console.log("Error processing case:", error); } }; // Use limitRequests to manage concurrency const promises = resultsObj.map((searchDetail) => { return limitRequests(() => getDetailsForCase(searchDetail)); }); // Wait for all the requests to complete await Promise.all(promises); return detailsArr; }; const mapStateToProps = (state) => { //console.log(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, mySubmittedReps: state.myRepresentations.mySubmittedReps, awaitingSubmission: state.awaitingSubmission, }; }; export default connect(mapStateToProps)(Home);