Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADJUST1-356 fix to remand overlap message. #140

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions server/model/remandChangeModel.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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
? '<p>To continue, edit the remand days that overlap or Cancel.</p>'
: `<p>Update the remand dates to continue.</p><p>You can view the court case & sentence information in the <a href="${calculateReleaseDatesCheckInformationUrl(
this.prisonerDetail,
)}">Calculate release dates service</a>.</p>`,
},
}
return remandRelatedValidationSummary(this.calculatedUnusedDeductions.validationMessages)
}
}
41 changes: 2 additions & 39 deletions server/model/remandReviewModel.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
? '<p>To continue, edit or remove the remand days that overlap.</p>'
: `<p>Update the remand dates to continue.</p><p>You can view the court case & sentence information in the <a href="${calculateReleaseDatesCheckInformationUrl(
this.prisonerDetail,
)}">Calculate release dates service</a>.</p>`,
},
}
return remandRelatedValidationSummary(this.calculateReleaseDatesValidationMessages)
}

public totalDays(): number {
Expand Down
11 changes: 4 additions & 7 deletions server/routes/remandRoutes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,8 @@ describe('Remand routes tests', () => {
`<a href="/${NOMS_ID}/remand/offences/add/${SESSION_ID}" class="govuk-back-link">Back</a>`,
)
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 <a href="http://localhost:8080/calculation/ABC123/reason">Calculate release dates service</a>.',
'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([
Expand Down Expand Up @@ -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 <a href="http://localhost:8080/calculation/ABC123/reason">Calculate release dates service</a>.',
])
})
})
Expand Down
36 changes: 36 additions & 0 deletions server/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
? '<p>To continue, edit the remand days that overlap or Cancel.</p>'
: `<p>Update the remand dates to continue.</p>`,
},
}
}