Files
pedwfrontend/lib/representation/pageLoaders.js
T
2026-05-14 08:55:49 +00:00

352 lines
11 KiB
JavaScript

import { getSession } from "next-auth/react";
import {
getPersonalAccount,
getPortalLogin
} from "../../actions/services/accountService";
import {
getCase,
getPortalModuleDetails
} from "../../actions/services/caseService";
import { getRepsFromBlob } from "../../actions/services/documentService";
import { getBasicSearch } from "../../actions/services/searchService";
import { consoleLogger } from "../../actions/core/logger";
import {
getFormCollectionByID,
getSearchDetails
} from "../../components/utils";
import {
setAccountDetails,
setContainerID
} from "../../store/accountDetails/action";
import {
setCurrentReference,
setCurrentView,
setFilesForRepresentations,
setRepresentationCapacity
} from "../../store/currentView/action";
import {
setMyRepresentations,
setMyRepresentationsDetails
} from "../../store/myRepresentations/action";
import {
setSearchDetails,
setSearchResults
} from "../../store/searchOutput/action";
import { setSearch } from "../../store/search/action";
import { getRepsFilesBlobs } from "../../actions/azurestorage";
const getDetails = (resultsObj, detailsType) => {
let detailsArr = [];
resultsObj =
detailsType == "mySubmittedReps" ? resultsObj : resultsObj.value;
if (detailsType == "myRepresentations") {
resultsObj.map((searchDetail) => {
detailsArr.push(
getCase(searchDetail["incidentID"]).then((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":
caseID = data.ticketnumber;
break;
default:
caseID = data.pinswg_title;
}
return getPortalModuleDetails(
getFormCollectionByID(data.pinswg_appealcasetype)
.LogicalCollectionName,
caseID
);
})
);
});
}
let detArr = Promise.all(detailsArr);
return detArr;
};
export const loadRepresentationBootstrap = async ({ ctx }) => {
const { query } = ctx;
let thisSession = await getSession(ctx);
let loggedInUser = null;
if (!thisSession) {
consoleLogger({
name: "RepresentationLoaderMissingSession",
message:
"loadRepresentationBootstrap missing session; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
} else {
loggedInUser = await getPortalLogin(thisSession.user.email);
loggedInUser = loggedInUser?.value?.[0]?.contactid || null;
}
if (!loggedInUser) {
consoleLogger({
name: "RepresentationLoaderMissingContact",
message:
"loadRepresentationBootstrap missing CRM contact id; redirecting to signin",
hasSessionEmail: !!thisSession?.user?.email
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
const [accountDetails, myRepresentations] = await Promise.all([
getPersonalAccount(loggedInUser),
getRepsFromBlob(thisSession.user.id)
]);
const myRepresentationsDetails = await getDetails(
myRepresentations,
"myRepresentations"
);
const isLPA =
accountDetails?.[
"pinswg_typeofinvolvement@OData.Community.Display.V1.FormattedValue"
] == "LPA"
? true
: false;
return {
query,
thisSession,
loggedInUser,
accountDetails,
myRepresentations,
myRepresentationsDetails,
isLPA
};
};
export const loadExistingRepresentation = async ({ store, ctx, bootstrap }) => {
const { query } = ctx;
const {
thisSession,
myRepresentations,
myRepresentationsDetails,
accountDetails,
isLPA
} = bootstrap;
const searchResultsObj = await getBasicSearch(query.case);
if (!searchResultsObj?.value?.[0]) {
consoleLogger({
name: "RepresentationExistingMissingSearchResult",
message:
"loadExistingRepresentation missing search result; redirecting to myportal",
hasCaseQuery: !!query?.case
});
return {
redirect: {
destination: "/myportal",
permanent: false
}
};
}
const searchDetailsObj = await getSearchDetails(searchResultsObj);
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setContainerID(thisSession.user.id));
store.dispatch(setMyRepresentations(myRepresentations));
store.dispatch(setMyRepresentationsDetails(myRepresentationsDetails));
function findObjectByKeyValue(arr, key, value) {
return arr.find((obj) => obj[key] === value);
}
const result = findObjectByKeyValue(
myRepresentations?.value || [],
"repfile_name",
query.created
);
if (!result) {
consoleLogger({
name: "RepresentationExistingMissingBlobEntry",
message:
"loadExistingRepresentation missing representation blob entry; redirecting to myportal",
hasCreatedQuery: !!query?.created
});
return {
redirect: {
destination: "/myportal",
permanent: false
}
};
}
store.dispatch(
setCurrentReference({
"ticketnumber":
result.ticketnumber || result.caseRef || result.pinswg_name,
"currentReference": result.caseRef,
"currentType": "myRepresentations",
"incidentid": result.incidentID,
"appealType": result.appealType,
"repDetails": result,
"filesList": result.filesList
})
);
store.dispatch(setRepresentationCapacity(result.representationCapacity));
const repsFileListObj = await getRepsFilesBlobs(
result.containerID || thisSession.user.id,
result.ticketnumber || result.caseRef,
result.repfile_name
);
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setFilesForRepresentations(repsFileListObj));
return {
props: {
containerID: thisSession.user.id,
docsOffline: process.env.DOCAPI_OFFLINE || false
}
};
};
export const loadNewRepresentation = async ({ store, ctx, bootstrap }) => {
const { query } = ctx;
const { thisSession, accountDetails } = bootstrap;
const searchResultsObj = await getBasicSearch(query.case);
if (!searchResultsObj?.value?.[0]) {
consoleLogger({
name: "RepresentationNewMissingSearchResult",
message:
"loadNewRepresentation missing search result; redirecting to myportal",
hasCaseQuery: !!query?.case
});
return {
redirect: {
destination: "/myportal",
permanent: false
}
};
}
const searchDetailsObj = await getSearchDetails(searchResultsObj);
store.dispatch(setContainerID(thisSession.user.id));
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setSearch(query.case));
store.dispatch(
setCurrentView({
"viewName": "My Representations",
"viewKey": "myRepresentations"
})
);
store.dispatch(
setCurrentReference({
"isNRW":
accountDetails?.emailaddress1?.includes(
process.env.NRWDOMAIN
) || false,
"ticketnumber": searchResultsObj?.value?.[0]?.ticketnumber,
"currentReference": searchResultsObj?.value?.[0]?.title,
"currentType": "myRepresentations",
"incidentid": searchResultsObj?.value?.[0]?.incidentid,
"appealType": searchResultsObj?.value?.[0]?.pinswg_appealcasetype,
"specialistProcess":
searchDetailsObj?.[0]?.value?.[0]
?.pinswg_speacialistcaseprocess != null ||
searchDetailsObj?.[0]?.value?.[0]
?.pinswg_specialistcaseprocess != null
? searchDetailsObj?.[0]?.value?.[0]
.pinswg_speacialistcaseprocess ||
searchDetailsObj?.[0]?.value?.[0]
.pinswg_specialistcaseprocess
: ""
})
);
store.dispatch(setAccountDetails(accountDetails));
return {
props: {
containerID: thisSession.user.id,
docsOffline: process.env.DOCAPI_OFFLINE || false
}
};
};
export const loadRepresentationPage = async ({ store, ctx }) => {
const { query } = ctx;
if (!query?.case) {
consoleLogger({
name: "RepresentationLoaderMissingCaseQuery",
message:
"loadRepresentationPage missing case query; redirecting to myportal"
});
return {
redirect: {
destination: "/myportal",
permanent: false
}
};
}
if (
Object.prototype.hasOwnProperty.call(query, "state") &&
!query?.created
) {
consoleLogger({
name: "RepresentationLoaderMissingCreatedQuery",
message:
"loadRepresentationPage missing created query for state flow; redirecting to myportal"
});
return {
redirect: {
destination: "/myportal",
permanent: false
}
};
}
const bootstrap = await loadRepresentationBootstrap({ ctx });
if (bootstrap?.redirect) {
return bootstrap;
}
if (Object.prototype.hasOwnProperty.call(query, "state")) {
return loadExistingRepresentation({ store, ctx, bootstrap });
}
return loadNewRepresentation({ store, ctx, bootstrap });
};