diff --git a/server/model/remandChangeModel.ts b/server/model/remandChangeModel.ts index a0dec668..4fbb42c1 100644 --- a/server/model/remandChangeModel.ts +++ b/server/model/remandChangeModel.ts @@ -1,12 +1,9 @@ -import dayjs from 'dayjs' import { Adjustment } from '../@types/adjustments/adjustmentsTypes' import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes' import { UnusedDeductionCalculationResponse } from '../@types/calculateReleaseDates/calculateReleaseDatesClientTypes' -import { calculateReleaseDatesCheckInformationUrl, offencesForAdjustment } from '../utils/utils' +import { offencesForAdjustment, remandRelatedValidationSummary } from '../utils/utils' export default class RemandChangeModel { - remandRelatedValidationCodes = ['REMAND_OVERLAPS_WITH_REMAND', 'REMAND_OVERLAPS_WITH_SENTENCE'] - constructor( public prisonerDetail: PrisonApiPrisoner, public adjustment: Adjustment, @@ -19,12 +16,6 @@ export default class RemandChangeModel { return offencesForAdjustment(this.adjustment, this.sentencesAndOffences) } - private remandRelatedValidation() { - return (this.calculatedUnusedDeductions?.validationMessages || []).filter(it => - this.remandRelatedValidationCodes.includes(it.code), - ) - } - public showUnusedMessage() { if (this.calculatedUnusedDeductions?.unusedDeductions != null) { const currentUnusedDeductions = this.currentAdjustments @@ -40,35 +31,6 @@ export default class RemandChangeModel { } public remandRelatedValidationSummary() { - const message = this.remandRelatedValidation().length ? this.remandRelatedValidation()[0] : null - if (!message) { - return { - errorList: [] as string[], - } - } - const overlapsWithRemand = message.code === 'REMAND_OVERLAPS_WITH_REMAND' - return { - titleText: overlapsWithRemand - ? 'Remand time cannot overlap' - : 'Remand cannot be applied when a sentence is being served.', - errorList: [ - { - text: `The remand dates from ${dayjs(message.arguments[2]).format('DD MMM YYYY')} to ${dayjs( - message.arguments[3], - ).format('DD MMM YYYY')} overlaps with ${ - overlapsWithRemand ? 'another remand period' : 'a sentence' - } from ${dayjs(message.arguments[0]).format('DD MMM YYYY')} to ${dayjs(message.arguments[1]).format( - 'DD MMM YYYY', - )}`, - }, - ], - subText: { - html: overlapsWithRemand - ? '
To continue, edit the remand days that overlap or Cancel.
' - : `Update the remand dates to continue.
You can view the court case & sentence information in the Calculate release dates service.
`, - }, - } + return remandRelatedValidationSummary(this.calculatedUnusedDeductions.validationMessages) } } diff --git a/server/model/remandReviewModel.ts b/server/model/remandReviewModel.ts index ae3c0240..44e58b03 100644 --- a/server/model/remandReviewModel.ts +++ b/server/model/remandReviewModel.ts @@ -1,13 +1,11 @@ import dayjs from 'dayjs' import { Adjustment } from '../@types/adjustments/adjustmentsTypes' import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes' -import { calculateReleaseDatesCheckInformationUrl, daysBetween } from '../utils/utils' +import { daysBetween, remandRelatedValidationSummary } from '../utils/utils' import ReviewRemandForm from './reviewRemandForm' import { CalculateReleaseDatesValidationMessage } from '../@types/calculateReleaseDates/calculateReleaseDatesClientTypes' export default class RemandReviewModel { - remandRelatedValidationCodes = ['REMAND_OVERLAPS_WITH_REMAND', 'REMAND_OVERLAPS_WITH_SENTENCE'] - adjustmentIds: string[] constructor( @@ -20,43 +18,8 @@ export default class RemandReviewModel { this.adjustmentIds = Object.keys(adjustments) } - private remandRelatedValidation() { - return this.calculateReleaseDatesValidationMessages.filter(it => - this.remandRelatedValidationCodes.includes(it.code), - ) - } - public remandRelatedValidationSummary() { - const message = this.remandRelatedValidation().length ? this.remandRelatedValidation()[0] : null - if (!message) { - return { - errorList: [] as string[], - } - } - const overlapsWithRemand = message.code === 'REMAND_OVERLAPS_WITH_REMAND' - return { - titleText: overlapsWithRemand - ? 'Remand time cannot overlap' - : 'Remand cannot be applied when a sentence is being served.', - errorList: [ - { - text: `The remand dates from ${dayjs(message.arguments[2]).format('DD MMM YYYY')} to ${dayjs( - message.arguments[3], - ).format('DD MMM YYYY')} overlaps with ${ - overlapsWithRemand ? 'another remand period' : 'a sentence' - } from ${dayjs(message.arguments[0]).format('DD MMM YYYY')} to ${dayjs(message.arguments[1]).format( - 'DD MMM YYYY', - )}`, - }, - ], - subText: { - html: overlapsWithRemand - ? 'To continue, edit or remove the remand days that overlap.
' - : `Update the remand dates to continue.
You can view the court case & sentence information in the Calculate release dates service.
`, - }, - } + return remandRelatedValidationSummary(this.calculateReleaseDatesValidationMessages) } public totalDays(): number { diff --git a/server/routes/remandRoutes.test.ts b/server/routes/remandRoutes.test.ts index 2db7ed74..ed74286a 100644 --- a/server/routes/remandRoutes.test.ts +++ b/server/routes/remandRoutes.test.ts @@ -535,10 +535,8 @@ describe('Remand routes tests', () => { `Back`, ) expect(res.text).toContainInOrder([ - ' Remand cannot be applied when a sentence is being served.', - 'The remand dates from 02 Jan 2021 to 02 Feb 2021 overlaps with a sentence from 01 Jan 2021 to 01 Feb 2021', - 'Update the remand dates to continue.', - 'You can view the court case & sentence information in the Calculate release dates service.', + 'Remand cannot be applied when a sentence is being served.', + 'The remand dates from 02 Jan 2021 to 02 Feb 2021 overlaps with the sentence starting on 01 Jan 2021 with a release date of the 01 Feb 2021', ]) expect(res.text).toContain('Review remand details') expect(res.text).toContainInOrder([ @@ -741,10 +739,9 @@ describe('Remand routes tests', () => { .expect('Content-Type', /html/) .expect(res => { expect(res.text).toContainInOrder([ - ' Remand cannot be applied when a sentence is being served.', - 'The remand dates from 02 Jan 2021 to 02 Feb 2021 overlaps with a sentence from 01 Jan 2021 to 01 Feb 2021', + 'Remand cannot be applied when a sentence is being served.', + 'The remand dates from 02 Jan 2021 to 02 Feb 2021 overlaps with the sentence starting on 01 Jan 2021 with a release date of the 01 Feb 2021', 'Update the remand dates to continue.', - 'You can view the court case & sentence information in the Calculate release dates service.', ]) }) }) diff --git a/server/utils/utils.ts b/server/utils/utils.ts index 899f9e6f..a6ed21c5 100644 --- a/server/utils/utils.ts +++ b/server/utils/utils.ts @@ -7,6 +7,7 @@ import { PrisonApiPrisoner, } from '../@types/prisonApi/prisonClientTypes' import { Adjustment } from '../@types/adjustments/adjustmentsTypes' +import { CalculateReleaseDatesValidationMessage } from '../@types/calculateReleaseDates/calculateReleaseDatesClientTypes' const properCase = (word: string): string => word.length >= 1 ? word[0].toUpperCase() + word.toLowerCase().slice(1) : word @@ -116,3 +117,38 @@ export function offencesForAdjustment( }) }) } + +export function remandRelatedValidationSummary(messages: CalculateReleaseDatesValidationMessage[]) { + const remandRelatedValidationCodes = ['REMAND_OVERLAPS_WITH_REMAND', 'REMAND_OVERLAPS_WITH_SENTENCE'] + + const remandRelatedMessages = (messages || []).filter(it => remandRelatedValidationCodes.includes(it.code)) + + const message = remandRelatedMessages.length ? remandRelatedMessages[0] : null + if (!message) { + return { + errorList: [] as string[], + } + } + const overlapsWithRemand = message.code === 'REMAND_OVERLAPS_WITH_REMAND' + return { + titleText: overlapsWithRemand + ? 'Remand time cannot overlap' + : 'Remand cannot be applied when a sentence is being served.', + errorList: [ + { + text: `The remand dates from ${dayjs(message.arguments[2]).format('DD MMM YYYY')} to ${dayjs( + message.arguments[3], + ).format('DD MMM YYYY')} overlaps with ${ + overlapsWithRemand ? 'another remand period from' : 'the sentence starting on' + } ${dayjs(message.arguments[0]).format('DD MMM YYYY')} ${ + overlapsWithRemand ? 'to' : 'with a release date of the' + } ${dayjs(message.arguments[1]).format('DD MMM YYYY')}`, + }, + ], + subText: { + html: overlapsWithRemand + ? 'To continue, edit the remand days that overlap or Cancel.
' + : `Update the remand dates to continue.
`, + }, + } +}