diff --git a/server/utils/applications/deleteOrphanedData.test.ts b/server/utils/applications/deleteOrphanedData.test.ts index 6159c36..6c0dd14 100644 --- a/server/utils/applications/deleteOrphanedData.test.ts +++ b/server/utils/applications/deleteOrphanedData.test.ts @@ -212,4 +212,58 @@ describe('deleteOrphanedFollowOnAnswers', () => { }) }) }) + + describe('cpp-details and current offences', () => { + describe('when probation supervision is set to no', () => { + const applicationData = { + 'community-supervision-and-current-offences': { + 'community-supervision': { + probationSupervision: 'no', + }, + 'cpp-details': { + cppDetails: { + name: 'A. CPP', + probationRegion: 'some region', + email: 'cpp@moj.gov.uk', + telephone: '012345', + }, + }, + 'current-offence-data': [ + { + titleAndNumber: 'Arson', + offenceCategory: 'Arson', + 'offenceDate-day': '5', + 'offenceDate-month': '6', + 'offenceDate-year': '1940', + sentenceLength: '3 years', + summary: 'summary detail', + outstandingCharges: 'yes', + outstandingChargesDetail: 'outstanding charges detail', + }, + { + titleAndNumber: 'Stalking', + offenceCategory: 'Stalking', + 'offenceDate-day': '6', + 'offenceDate-month': '7', + 'offenceDate-year': '2023', + sentenceLength: '2 months', + summary: 'more summary detail', + outstandingCharges: 'no', + }, + ], + 'current-offences': {}, + }, + } + + it('removes cpp details and current offence data', () => { + expect(deleteOrphanedFollowOnAnswers(applicationData)).toEqual({ + 'community-supervision-and-current-offences': { + 'community-supervision': { + probationSupervision: 'no', + }, + }, + }) + }) + }) + }) }) diff --git a/server/utils/applications/deleteOrphanedData.ts b/server/utils/applications/deleteOrphanedData.ts index c4f005f..15d245d 100644 --- a/server/utils/applications/deleteOrphanedData.ts +++ b/server/utils/applications/deleteOrphanedData.ts @@ -28,6 +28,12 @@ export default function deleteOrphanedFollowOnAnswers(applicationData: AnyValue) } } + const deleteOrphanedCPPAndCurrentOffenceData = () => { + delete applicationData['community-supervision-and-current-offences']['cpp-details'] + delete applicationData['community-supervision-and-current-offences']['current-offence-data'] + delete applicationData['community-supervision-and-current-offences']['current-offences'] + } + const hasOrphanedInformation = ({ taskName, pageName, @@ -90,5 +96,16 @@ export default function deleteOrphanedFollowOnAnswers(applicationData: AnyValue) deleteAddressHistoryInformation() } + if ( + hasOrphanedInformation({ + taskName: 'community-supervision-and-current-offences', + pageName: 'community-supervision', + questionKey: 'probationSupervision', + answerToCheck: 'no', + }) + ) { + deleteOrphanedCPPAndCurrentOffenceData() + } + return applicationData }