Merged PR 2260: refactor(representations): complete slice-based refactor of representations flow

## Representations Refactor — Behaviour-Preserving Structural Improvements

This PR delivers a full refactor of the representations flow, improving structure, readability, and maintainability while preserving all existing behaviour.

The work was completed using a controlled, slice-based approach with strict guardrails and regression validation at each step. No changes have been made to user journeys, payloads, routing, or EN/CY behaviour.

The result is a cleaner, more maintainable codebase with reduced coupling and clearer separation of concerns, ready for future enhancements without increased risk.

---

## What Was Done

The refactor was delivered incrementally across the following slices:

- **R1** — Representation entry logic extraction
- **R2** — Page loader separation (SSR/data orchestration)
- **R3** — Journey step resolution extraction
- **R4** — Flow shell decomposition
- **R5** — Representation elements normalisation
- **R6** — Data/service layer cleanup
- **R7** — Summary rendering proof slice
- **R8** — Submission/finalisation boundary isolation
- **R9** — Summary rollout (Batch 1)

Each slice:
- was isolated to a single concern
- followed strict guardrails
- was validated before merge

Full detail is available in:
`context/representations-refactor-tracker.md`

---

## Key Improvements

- Reduced coupling across the representations journey
- Separated data loading, orchestration, and rendering concerns
- Simplified complex conditional logic into testable helpers
- Standardised summary rendering using shared primitives (`SummaryCard`, `SummaryRow`)
- Isolated submission/finalisation sequencing into explicit boundaries
- Improved overall readability and maintainability

---

## Behaviour Preservation

This refactor does **not** change:

- User journeys (APP / IP / Agent / LPA)
- Route and query behaviour
- Payload contracts and API interactions
- Redux state shape and usage
- Validation rules and messaging
- EN/CY behaviour
- File upload / PDF / email sequencing
- Linked-case logic

All changes are structural only.

---

## Validation

### Automated

- `npm run lint` — passed (warnings only, no new errors)
- `npm run test:reps` — passed (7/7)

### Manual

Validated end-to-end across:

- APP
- IP
- Agent
- LPA

Including:

- representation creation
- editing/resuming representations
- submission flow
- confirmation/completion behaviour
- summary rendering across case types
- EN/CY parity

---

## Risk Management

The refactor targeted several high-risk areas:

- Case summary entry logic
- Representation submission/finalisation sequencing
- Dual-mode entry (new vs existing representation)

Risk was controlled through:

- small, incremental slices
- one branch per slice
- regression validation per slice
- strict behaviour-preservation guardrails
- controlled rollout for summary rendering changes

---

## Reviewer Guidance

Suggested areas to focus on:

- End-to-end representation journey (create → submit → complete)
- S...
This commit is contained in:
Robert Bond
2026-04-20 13:09:07 +00:00
parent c8b08a04a6
commit 37a81522d5
52 changed files with 9357 additions and 8201 deletions
@@ -0,0 +1,93 @@
import { JSONPath as jsonpath } from "jsonpath-plus";
export const resolveInitialCaseDataSources = ({
hasState,
isEditState,
currentType,
myRepresentations,
searchDetailsObj,
searchResultsObj,
watchedCasesDetails
}) => {
if (hasState) {
return {
"caseDetailsSource": isEditState
? myRepresentations
: searchDetailsObj,
"caseResultsSource": isEditState
? myRepresentations
: searchResultsObj
};
}
return {
"caseDetailsSource":
currentType === "watchedCases"
? watchedCasesDetails
: searchDetailsObj,
"caseResultsSource":
currentType === "watchedCases"
? watchedCasesDetails
: searchResultsObj
};
};
export const findResultsObjForCurrentReference = ({
currentReference,
caseResultsSource
}) => {
return jsonpath({
path: `$..[?(@ && @.ticketnumber=="${currentReference}")]`,
"json": caseResultsSource,
"eval": true
})[0];
};
export const findDetailsObjForView = ({
hasState,
isEditState,
queryCreated,
ticketnumber,
caseDetailsSource
}) => {
if (hasState && isEditState) {
return jsonpath({
path: `$..[?(@ && @.repfile_name=="${queryCreated}")]`,
"json": caseDetailsSource,
"eval": true
})[0];
}
return jsonpath({
path: `$..[?(@ && @.ticketnumber=="${ticketnumber}")]`,
"json": caseDetailsSource,
"eval": true
})[0];
};
export const resolveSubmitCaseDetailsSource = ({
isEditState,
currentType,
myRepresentations,
watchedCasesDetails,
searchDetailsObj
}) => {
if (isEditState) {
return myRepresentations;
}
return currentType === "watchedCases"
? watchedCasesDetails
: searchDetailsObj;
};
export const findDetailsObjForSubmit = ({
ticketnumber,
caseDetailsSource
}) => {
return jsonpath({
path: `$..[?(@ && @.ticketnumber=="${ticketnumber}")]`,
"json": caseDetailsSource,
"eval": true
})[0];
};
@@ -0,0 +1,126 @@
export const buildRepresentationUpdateBody = ({
formattedValues,
setRepFileName
}) => {
let updateBody = setRepFileName(formattedValues);
const sanitizedBody = Object.entries(updateBody).reduce(
(acc, [key, val]) => {
if (val !== null && val !== undefined && !key.startsWith("_")) {
acc[key] = val;
}
return acc;
},
{}
);
const stringified = JSON.stringify(sanitizedBody)
.replace(/:"Yes"/g, ":true")
.replace(/:"No"/g, ":false");
updateBody = JSON.parse(stringified);
return updateBody;
};
export const buildSubmitEnrichedValues = ({
formValues,
accountDetails,
appealType,
caseReferenceObj,
detailsObj,
resultsObj,
representationCapacity,
locale
}) => {
const buildSiteAddress = () => {
const {
pinswg_siteaddressline1,
pinswg_siteaddressline2,
pinswg_siteaddresstown,
pinswg_siteaddresspostcode
} = detailsObj;
return [
pinswg_siteaddressline1,
pinswg_siteaddressline2,
pinswg_siteaddresstown,
pinswg_siteaddresspostcode
]
.filter(Boolean)
.join(" ");
};
return {
...formValues,
containerID: accountDetails.containerID,
appealType: appealType || caseReferenceObj.repDetails.appealType,
incidentID:
caseReferenceObj.incidentid ||
caseReferenceObj.repDetails.incidentid,
caseRef: caseReferenceObj.currentReference,
casereference: caseReferenceObj.currentReference,
pinswg_questionnaireduedate: detailsObj.pinswg_questionnaireduedate,
pinswg_endofrepresentationperiod:
detailsObj.pinswg_endofrepresentationperiod,
pinswg_applicationacceptedasvalid:
detailsObj.pinswg_applicationacceptedasvalid,
pinswg_statementduedate: detailsObj.pinswg_statementduedate,
pinswg_statementsduedate: detailsObj.pinswg_statementsduedate,
pinswg_finalcommentsduedate: detailsObj.pinswg_finalcommentsduedate,
pinswg_name: caseReferenceObj.currentReference,
firstname: accountDetails.firstname,
lastname: accountDetails.lastname,
emailAddress: accountDetails.emailaddress1,
siteAddress: buildSiteAddress(),
pinswg_siteaddressline1: detailsObj.pinswg_siteaddressline1,
pinswg_siteaddressline2: detailsObj.pinswg_siteaddressline2,
pinswg_siteaddresstown: detailsObj.pinswg_siteaddresstown,
pinswg_siteaddresspostcode: detailsObj.pinswg_siteaddresspostcode,
pinswg_lpareference: resultsObj?.pinswg_lpareference,
_pinswg_appellant_value: detailsObj._pinswg_appellant_value,
"_pinswg_appellant_value@OData.Community.Display.V1.FormattedValue":
detailsObj[
"_pinswg_appellant_value@OData.Community.Display.V1.FormattedValue"
],
_pinswg_localplanningauthority_value:
detailsObj._pinswg_localplanningauthority_value ||
detailsObj._pinswg_associatedlpa_value,
"_pinswg_localplanningauthority_value@OData.Community.Display.V1.FormattedValue":
detailsObj[
"_pinswg_localplanningauthority_value@OData.Community.Display.V1.FormattedValue"
] ||
detailsObj[
"_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue"
],
representationCapacity,
locale
};
};
export const runFinalisationSequence = ({
values,
containerID,
currentReference,
setFinaliseAppealProcess,
generateRepPDF,
updateLinks,
uploadRepresentationFiles,
setRepresentationSubmit,
setRepresentationSubmitConfirmation
}) => {
setFinaliseAppealProcess(true);
return generateRepPDF(values, containerID, currentReference).then(
(data) => {
if (data.status === "success") {
const updatedValues = updateLinks(values);
uploadRepresentationFiles(updatedValues);
setRepresentationSubmit(true);
setRepresentationSubmitConfirmation(true);
}
return data;
}
);
};
@@ -0,0 +1,73 @@
export const resolveJourneyStageFlags = ({
representationSubmit,
representationSubmitConfirmation
}) => {
const showCheckStage =
representationSubmit === true || representationSubmit === "true";
const showCompletionStage =
representationSubmitConfirmation === true ||
representationSubmitConfirmation === "true";
return {
showFormStage: !showCheckStage,
showCheckStage,
showCompletionStage
};
};
export const resolveRepresentationControlKey = ({
isLPA,
representationCapacity,
repDetailsCapacity,
appealType,
normalizeCapacity
}) => {
if (isLPA && (!representationCapacity || representationCapacity === "")) {
return {
controlKey: "lpa-capacity-selection",
normalizedCapacity: ""
};
}
const rawCapacity = representationCapacity || repDetailsCapacity || "false";
const normalizedCapacity = normalizeCapacity(rawCapacity.toLowerCase());
return {
controlKey: `capacity-${normalizedCapacity}`,
normalizedCapacity
};
};
export const resolveSubmitTransition = ({
representationType,
showQuestionnaireSection,
questionnaireCount
}) => {
if (representationType !== "Questionnaire") {
return "goToCheck";
}
if (
questionnaireCount !== 0 &&
showQuestionnaireSection < questionnaireCount
) {
return "advanceQuestionnaire";
}
return "goToCheck";
};
export const getQuestionnaireNextSection = (
currentSection,
questionnaireCount
) => {
if (questionnaireCount !== 0 && currentSection < questionnaireCount) {
return currentSection + 1;
}
return currentSection;
};
export const getQuestionnairePreviousSection = (currentSection) => {
return currentSection > 1 ? currentSection - 1 : currentSection;
};