From 9457c984e043747ea96e89f1c2e2fa8a64dfa04b Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Tue, 26 Mar 2024 15:34:09 +0000 Subject: [PATCH 1/7] Updated unused deductions banner --- server/model/adjustmentsHubViewModel.ts | 10 +++- server/routes/adjustmentRoutes.test.ts | 56 ++++++++++++++++++++-- server/routes/adjustmentRoutes.ts | 13 ++--- server/services/unusedDeductionsService.ts | 49 +++++++++++++------ server/views/pages/adjustments/hub.njk | 26 ++++++++-- 5 files changed, 120 insertions(+), 34 deletions(-) diff --git a/server/model/adjustmentsHubViewModel.ts b/server/model/adjustmentsHubViewModel.ts index c28ceaa2..86c5821b 100644 --- a/server/model/adjustmentsHubViewModel.ts +++ b/server/model/adjustmentsHubViewModel.ts @@ -1,5 +1,7 @@ import { Adjustment } from '../@types/adjustments/adjustmentsTypes' import { IdentifyRemandDecision, RemandResult } from '../@types/identifyRemandPeriods/identifyRemandPeriodsTypes' +import config from '../config' +import { UnusedDeductionMessageType } from '../services/unusedDeductionsService' import { calculateReleaseDatesCheckInformationUrl } from '../utils/utils' import adjustmentTypes, { AdjustmentType } from './adjustmentTypes' @@ -13,6 +15,8 @@ export type Message = { export type MessageAction = 'CREATE' | 'REMOVE' | 'UPDATE' | 'REJECTED' | 'VALIDATION' export default class AdjustmentsHubViewModel { + public checkInformationLink: string + constructor( public prisonerNumber: string, public adjustments: Adjustment[], @@ -20,8 +24,10 @@ export default class AdjustmentsHubViewModel { public remandDecision: IdentifyRemandDecision, public roles: string[], public message: Message, - public serviceHasCalculatedUnusedDeductions: boolean, - ) {} + public unusedDeductionMessage: UnusedDeductionMessageType, + ) { + this.checkInformationLink = `${config.services.calculateReleaseDatesUI.url}/calculation/${this.prisonerNumber}/check-information?hasErrors=true` + } public deductions(): AdjustmentType[] { return adjustmentTypes.filter(it => diff --git a/server/routes/adjustmentRoutes.test.ts b/server/routes/adjustmentRoutes.test.ts index c9cb97a3..3cfebc23 100644 --- a/server/routes/adjustmentRoutes.test.ts +++ b/server/routes/adjustmentRoutes.test.ts @@ -129,7 +129,7 @@ describe('Adjustment routes tests', () => { unusedDeductions, ]) identifyRemandPeriodsService.calculateRelevantRemand.mockResolvedValue(remandResult) - unusedDeductionsService.serviceHasCalculatedUnusedDeductions.mockResolvedValue(true) + unusedDeductionsService.getCalculatedUnusedDeductionsMessage.mockResolvedValue('NONE') additionalDaysAwardedService.shouldIntercept.mockResolvedValue({ type: 'NONE', number: 0, @@ -147,14 +147,14 @@ describe('Adjustment routes tests', () => { expect(res.text).toContain('including 10 days unused') }) }) - it('GET /{nomsId} hub unused deductions cannot be calculated', () => { + it('GET /{nomsId} hub unused deductions cannot be calculated because of unsupported sentence type', () => { prisonerService.getStartOfSentenceEnvelope.mockResolvedValue({ earliestExcludingRecalls: new Date(), earliestSentence: new Date(), }) adjustmentsService.findByPerson.mockResolvedValue([remandAdjustment]) identifyRemandPeriodsService.calculateRelevantRemand.mockResolvedValue(remandResult) - unusedDeductionsService.serviceHasCalculatedUnusedDeductions.mockResolvedValue(false) + unusedDeductionsService.getCalculatedUnusedDeductionsMessage.mockResolvedValue('UNSUPPORTED') additionalDaysAwardedService.shouldIntercept.mockResolvedValue({ type: 'NONE', number: 0, @@ -164,7 +164,53 @@ describe('Adjustment routes tests', () => { .get(`/${NOMS_ID}`) .expect('Content-Type', /html/) .expect(res => { - expect(res.text).toContain('Unused remand/tagged bail time cannot be calculated') + expect(res.text).toContain( + 'Some of the details recorded in NOMIS cannot be used for a sentence calculation. This means unused deductions cannot be automatically calculated by this service. To add any unused remand, go to the sentence adjustments screen in NOMIS.', + ) + }) + }) + it('GET /{nomsId} hub unused deductions cannot be calculated because of validation error', () => { + prisonerService.getStartOfSentenceEnvelope.mockResolvedValue({ + earliestExcludingRecalls: new Date(), + earliestSentence: new Date(), + }) + adjustmentsService.findByPerson.mockResolvedValue([remandAdjustment]) + identifyRemandPeriodsService.calculateRelevantRemand.mockResolvedValue(remandResult) + unusedDeductionsService.getCalculatedUnusedDeductionsMessage.mockResolvedValue('VALIDATION') + additionalDaysAwardedService.shouldIntercept.mockResolvedValue({ + type: 'NONE', + number: 0, + anyProspective: false, + }) + return request(app) + .get(`/${NOMS_ID}`) + .expect('Content-Type', /html/) + .expect(res => { + expect(res.text).toContain( + 'Some of the data in NOMIS related to this person is incorrect. This means unused deductions cannot be automatically calculated.', + ) + }) + }) + it('GET /{nomsId} hub unused deductions cannot be calculated because its a nomis adjustment', () => { + prisonerService.getStartOfSentenceEnvelope.mockResolvedValue({ + earliestExcludingRecalls: new Date(), + earliestSentence: new Date(), + }) + adjustmentsService.findByPerson.mockResolvedValue([remandAdjustment]) + identifyRemandPeriodsService.calculateRelevantRemand.mockResolvedValue(remandResult) + unusedDeductionsService.getCalculatedUnusedDeductionsMessage.mockResolvedValue('NOMIS_ADJUSTMENT') + additionalDaysAwardedService.shouldIntercept.mockResolvedValue({ + type: 'NONE', + number: 0, + anyProspective: false, + }) + return request(app) + .get(`/${NOMS_ID}`) + .expect('Content-Type', /html/) + .expect(res => { + expect(res.text).toContain( + 'Existing deductions have been added on NOMIS. This means unused deductions cannot be automatically calculated. To add any unused remand, go to the sentence adjustments screen in NOMIS.', + ) }) }) it('GET /{nomsId} with remand role', () => { @@ -178,7 +224,7 @@ describe('Adjustment routes tests', () => { }) identifyRemandPeriodsService.calculateRelevantRemand.mockResolvedValue(remandResult) additionalDaysAwardedService.shouldIntercept.mockResolvedValue({ type: 'NONE', number: 0, anyProspective: false }) - unusedDeductionsService.serviceHasCalculatedUnusedDeductions.mockResolvedValue(true) + unusedDeductionsService.getCalculatedUnusedDeductionsMessage.mockResolvedValue('NONE') return request(app) .get(`/${NOMS_ID}`) .expect('Content-Type', /html/) diff --git a/server/routes/adjustmentRoutes.ts b/server/routes/adjustmentRoutes.ts index f5109ea0..7aba1259 100644 --- a/server/routes/adjustmentRoutes.ts +++ b/server/routes/adjustmentRoutes.ts @@ -18,7 +18,7 @@ import FullPageError from '../model/FullPageError' import { daysBetween } from '../utils/utils' import RecallModel from '../model/recallModel' import RecallForm from '../model/recallForm' -import UnusedDeductionsService from '../services/unusedDeductionsService' +import UnusedDeductionsService, { UnusedDeductionMessageType } from '../services/unusedDeductionsService' import { Adjustment } from '../@types/adjustments/adjustmentsTypes' export default class AdjustmentRoutes { @@ -54,14 +54,11 @@ export default class AdjustmentRoutes { const message = req.flash('message') const messageExists = message && message[0] - let serviceHasCalculatedUnusedDeductions = true + let unusedDeductionMessage: UnusedDeductionMessageType = 'NONE' if (messageExists) { this.adjustmentsStoreService.clear(req, nomsId) - serviceHasCalculatedUnusedDeductions = await this.unusedDeductionsService.waitUntilUnusedRemandCreated( - nomsId, - token, - ) + unusedDeductionMessage = await this.unusedDeductionsService.waitUntilUnusedRemandCreated(nomsId, token) } const adjustments = await this.adjustmentsService.findByPerson( @@ -71,7 +68,7 @@ export default class AdjustmentRoutes { ) if (!messageExists) { - serviceHasCalculatedUnusedDeductions = await this.unusedDeductionsService.serviceHasCalculatedUnusedDeductions( + unusedDeductionMessage = await this.unusedDeductionsService.getCalculatedUnusedDeductionsMessage( nomsId, adjustments, token, @@ -107,7 +104,7 @@ export default class AdjustmentRoutes { remandDecision, roles, message && message[0] && (JSON.parse(message[0]) as Message), - serviceHasCalculatedUnusedDeductions, + unusedDeductionMessage, ), }) } diff --git a/server/services/unusedDeductionsService.ts b/server/services/unusedDeductionsService.ts index 22a7365b..01809cfc 100644 --- a/server/services/unusedDeductionsService.ts +++ b/server/services/unusedDeductionsService.ts @@ -3,6 +3,8 @@ import { delay } from '../utils/utils' import AdjustmentsService from './adjustmentsService' import CalculateReleaseDatesService from './calculateReleaseDatesService' +export type UnusedDeductionMessageType = 'NOMIS_ADJUSTMENT' | 'VALIDATION' | 'UNSUPPORTED' | 'NONE' + export default class UnusedDeductionsService { private maxTries = 6 // 3 seconds max wait @@ -18,18 +20,18 @@ export default class UnusedDeductionsService { } /* Wait until calclulated unused deductions matches with adjustments database. */ - async waitUntilUnusedRemandCreated(nomsId: string, token: string): Promise { + async waitUntilUnusedRemandCreated(nomsId: string, token: string): Promise { try { let adjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token) const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') if (!deductions.length) { // If there are no deductions then unused deductions doesn't need to be calculated - return true + return 'NONE' } if (this.anyDeductionFromNomis(deductions)) { // won't calculate unused deductions if adjusments are not from DPS. - return false + return 'NOMIS_ADJUSTMENT' } const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( @@ -39,7 +41,15 @@ export default class UnusedDeductionsService { ) if (unusedDeductionsResponse.validationMessages?.length) { - return false + if ( + unusedDeductionsResponse.validationMessages.find( + it => it.type === 'UNSUPPORTED_CALCULATION' || it.type === 'UNSUPPORTED_SENTENCE', + ) + ) { + return 'UNSUPPORTED' + } + + return 'VALIDATION' } const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions @@ -48,36 +58,36 @@ export default class UnusedDeductionsService { if (calculatedUnusedDeducions || calculatedUnusedDeducions === 0) { const dbDeductions = this.getTotalUnusedRemand(adjustments) if (calculatedUnusedDeducions === dbDeductions) { - return true + return 'NONE' } await delay(this.waitBetweenTries) adjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token) // Try again } else { // Unable to calculate unused deductions. - return false + return 'NONE' } } } catch { // Error couldn't calculate unused deductions. } - return false + return 'NONE' /* eslint-enable no-await-in-loop */ } - async serviceHasCalculatedUnusedDeductions( + async getCalculatedUnusedDeductionsMessage( nomsId: string, adjustments: Adjustment[], token: string, - ): Promise { + ): Promise { try { const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') if (!deductions.length) { // If there are no deductions then unused deductions doesn't need to be calculated - return true + return 'NONE' } if (this.anyDeductionFromNomis(deductions)) { - return false + return 'NOMIS_ADJUSTMENT' } const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( nomsId, @@ -86,19 +96,28 @@ export default class UnusedDeductionsService { ) if (unusedDeductionsResponse.validationMessages?.length) { - return false + if ( + unusedDeductionsResponse.validationMessages.find( + it => it.type === 'UNSUPPORTED_CALCULATION' || it.type === 'UNSUPPORTED_SENTENCE', + ) + ) { + return 'UNSUPPORTED' + } + + return 'VALIDATION' } const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions if (calculatedUnusedDeducions || calculatedUnusedDeducions === 0) { const dbDeductions = this.getTotalUnusedRemand(adjustments) if (calculatedUnusedDeducions === dbDeductions) { - return true + return 'NONE' } } - return false + + return 'NONE' } catch { - return false + return 'NONE' } } diff --git a/server/views/pages/adjustments/hub.njk b/server/views/pages/adjustments/hub.njk index 361b1be8..cc0037cf 100644 --- a/server/views/pages/adjustments/hub.njk +++ b/server/views/pages/adjustments/hub.njk @@ -66,17 +66,35 @@ NOMIS, {{ prisoner.firstName | title }} {{ prisoner.lastName | title }} may have {{ model.getTotalDaysRelevantRemand() }} days remand. Review the remand to make sure it is relevant.

- Review + Review {% endif %} - {% if not model.serviceHasCalculatedUnusedDeductions %} + {% if model.unusedDeductionMessage != 'NONE' %}

Unused deductions

- Unused remand/tagged bail time cannot be calculated. Any unused deductions must be entered in NOMIS. + {% if model.unusedDeductionMessage == 'UNSUPPORTED' %} + Some of the details recorded in NOMIS cannot be used for a sentence calculation. This means unused deductions cannot be automatically calculated by this service. To add any unused remand, go to the sentence adjustments screen in NOMIS. + {% elif model.unusedDeductionMessage == 'VALIDATION' %} + Some of the data in NOMIS related to this person is incorrect. This means unused deductions cannot be automatically calculated. +
+ To continue, you must: +
    +
  1. + Review the incorrect details +
  2. +
  3. + Update these details +
  4. +
  5. + Reload this page +
  6. +
+ {% elif model.unusedDeductionMessage == 'NOMIS_ADJUSTMENT' %} + Existing deductions have been added on NOMIS. This means unused deductions cannot be automatically calculated. To add any unused remand, go to the sentence adjustments screen in NOMIS. + {% endif %}
{% endif %} From b95cb812fcee2190733519c0379e04795bd1fe73 Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Tue, 26 Mar 2024 15:49:04 +0000 Subject: [PATCH 2/7] Updated styling --- server/views/pages/adjustments/hub.njk | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/server/views/pages/adjustments/hub.njk b/server/views/pages/adjustments/hub.njk index cc0037cf..eba6ac45 100644 --- a/server/views/pages/adjustments/hub.njk +++ b/server/views/pages/adjustments/hub.njk @@ -70,11 +70,12 @@ {% endif %} +

+ Deductions +

+ {% if model.unusedDeductionMessage != 'NONE' %} -

- Unused deductions -

-
+
{% if model.unusedDeductionMessage == 'UNSUPPORTED' %} Some of the details recorded in NOMIS cannot be used for a sentence calculation. This means unused deductions cannot be automatically calculated by this service. To add any unused remand, go to the sentence adjustments screen in NOMIS. {% elif model.unusedDeductionMessage == 'VALIDATION' %} @@ -98,9 +99,6 @@
{% endif %} -

- Deductions -

{% for adjustmentType in model.deductions() %} {{ adjustmentCard(adjustmentType, model, prisoner.prisonerNumber) }} From 61fad4a22f2c89e9a447560d123c7b8544c93b18 Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Tue, 26 Mar 2024 16:34:23 +0000 Subject: [PATCH 3/7] Refactored code --- server/routes/adjustmentRoutes.test.ts | 22 ++++++++++ server/services/unusedDeductionsService.ts | 47 ++++++++++++---------- server/views/pages/adjustments/hub.njk | 2 + 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/server/routes/adjustmentRoutes.test.ts b/server/routes/adjustmentRoutes.test.ts index 3cfebc23..80fec2e2 100644 --- a/server/routes/adjustmentRoutes.test.ts +++ b/server/routes/adjustmentRoutes.test.ts @@ -213,6 +213,28 @@ describe('Adjustment routes tests', () => { ) }) }) + it('GET /{nomsId} hub unused deductions cannot be calculated because of an exception', () => { + prisonerService.getStartOfSentenceEnvelope.mockResolvedValue({ + earliestExcludingRecalls: new Date(), + earliestSentence: new Date(), + }) + adjustmentsService.findByPerson.mockResolvedValue([remandAdjustment]) + identifyRemandPeriodsService.calculateRelevantRemand.mockResolvedValue(remandResult) + unusedDeductionsService.getCalculatedUnusedDeductionsMessage.mockResolvedValue('UNKNOWN') + additionalDaysAwardedService.shouldIntercept.mockResolvedValue({ + type: 'NONE', + number: 0, + anyProspective: false, + }) + return request(app) + .get(`/${NOMS_ID}`) + .expect('Content-Type', /html/) + .expect(res => { + expect(res.text).toContain( + 'Unused remand/tagged bail time cannot be calculated. Any unused deductions must be entered in NOMIS.', + ) + }) + }) it('GET /{nomsId} with remand role', () => { userInTest = userWithRemandRole adjustmentsService.findByPerson.mockResolvedValue([ diff --git a/server/services/unusedDeductionsService.ts b/server/services/unusedDeductionsService.ts index 01809cfc..1e5d9392 100644 --- a/server/services/unusedDeductionsService.ts +++ b/server/services/unusedDeductionsService.ts @@ -3,7 +3,7 @@ import { delay } from '../utils/utils' import AdjustmentsService from './adjustmentsService' import CalculateReleaseDatesService from './calculateReleaseDatesService' -export type UnusedDeductionMessageType = 'NOMIS_ADJUSTMENT' | 'VALIDATION' | 'UNSUPPORTED' | 'NONE' +export type UnusedDeductionMessageType = 'NOMIS_ADJUSTMENT' | 'VALIDATION' | 'UNSUPPORTED' | 'UNKNOWN' | 'NONE' export default class UnusedDeductionsService { private maxTries = 6 // 3 seconds max wait @@ -24,16 +24,6 @@ export default class UnusedDeductionsService { try { let adjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token) - const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') - if (!deductions.length) { - // If there are no deductions then unused deductions doesn't need to be calculated - return 'NONE' - } - if (this.anyDeductionFromNomis(deductions)) { - // won't calculate unused deductions if adjusments are not from DPS. - return 'NOMIS_ADJUSTMENT' - } - const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( nomsId, adjustments, @@ -53,6 +43,16 @@ export default class UnusedDeductionsService { } const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions + const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') + if (!deductions.length) { + // If there are no deductions then unused deductions doesn't need to be calculated + return 'NONE' + } + if (this.anyDeductionFromNomis(deductions)) { + // won't calculate unused deductions if adjusments are not from DPS. + return 'NOMIS_ADJUSTMENT' + } + /* eslint-disable no-await-in-loop */ for (let i = 0; i < this.maxTries; i += 1) { if (calculatedUnusedDeducions || calculatedUnusedDeducions === 0) { @@ -65,13 +65,15 @@ export default class UnusedDeductionsService { // Try again } else { // Unable to calculate unused deductions. - return 'NONE' + return 'UNKNOWN' } } } catch { // Error couldn't calculate unused deductions. + return 'UNKNOWN' } - return 'NONE' + + return 'UNKNOWN' /* eslint-enable no-await-in-loop */ } @@ -81,14 +83,6 @@ export default class UnusedDeductionsService { token: string, ): Promise { try { - const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') - if (!deductions.length) { - // If there are no deductions then unused deductions doesn't need to be calculated - return 'NONE' - } - if (this.anyDeductionFromNomis(deductions)) { - return 'NOMIS_ADJUSTMENT' - } const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( nomsId, adjustments, @@ -108,6 +102,15 @@ export default class UnusedDeductionsService { } const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions + const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') + if (!deductions.length) { + // If there are no deductions then unused deductions doesn't need to be calculated + return 'NONE' + } + if (this.anyDeductionFromNomis(deductions)) { + return 'NOMIS_ADJUSTMENT' + } + if (calculatedUnusedDeducions || calculatedUnusedDeducions === 0) { const dbDeductions = this.getTotalUnusedRemand(adjustments) if (calculatedUnusedDeducions === dbDeductions) { @@ -117,7 +120,7 @@ export default class UnusedDeductionsService { return 'NONE' } catch { - return 'NONE' + return 'UNKNOWN' } } diff --git a/server/views/pages/adjustments/hub.njk b/server/views/pages/adjustments/hub.njk index eba6ac45..f237110c 100644 --- a/server/views/pages/adjustments/hub.njk +++ b/server/views/pages/adjustments/hub.njk @@ -95,6 +95,8 @@ {% elif model.unusedDeductionMessage == 'NOMIS_ADJUSTMENT' %} Existing deductions have been added on NOMIS. This means unused deductions cannot be automatically calculated. To add any unused remand, go to the sentence adjustments screen in NOMIS. + {% elif model.unusedDeductionMessage == 'UNKNOWN' %} + Unused remand/tagged bail time cannot be calculated. Any unused deductions must be entered in NOMIS. {% endif %}
{% endif %} From b9dcee635adc3f39b3d9f351171dae817c5bad09 Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Tue, 26 Mar 2024 16:38:10 +0000 Subject: [PATCH 4/7] Removed return --- server/services/unusedDeductionsService.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/server/services/unusedDeductionsService.ts b/server/services/unusedDeductionsService.ts index 1e5d9392..2ba0944f 100644 --- a/server/services/unusedDeductionsService.ts +++ b/server/services/unusedDeductionsService.ts @@ -70,7 +70,6 @@ export default class UnusedDeductionsService { } } catch { // Error couldn't calculate unused deductions. - return 'UNKNOWN' } return 'UNKNOWN' From 231ea0c20d676cf06b8f85280d0f4bb8ac816126 Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Tue, 26 Mar 2024 16:40:25 +0000 Subject: [PATCH 5/7] Changed return type --- server/services/unusedDeductionsService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/services/unusedDeductionsService.ts b/server/services/unusedDeductionsService.ts index 2ba0944f..49453ab0 100644 --- a/server/services/unusedDeductionsService.ts +++ b/server/services/unusedDeductionsService.ts @@ -117,7 +117,7 @@ export default class UnusedDeductionsService { } } - return 'NONE' + return 'UNKNOWN' } catch { return 'UNKNOWN' } From c6b2cc04d6e137d7b90af5d9439576ad383a835a Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Wed, 27 Mar 2024 14:06:45 +0000 Subject: [PATCH 6/7] Refactored code --- server/services/unusedDeductionsService.ts | 26 ++++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/server/services/unusedDeductionsService.ts b/server/services/unusedDeductionsService.ts index 49453ab0..020c1470 100644 --- a/server/services/unusedDeductionsService.ts +++ b/server/services/unusedDeductionsService.ts @@ -24,6 +24,12 @@ export default class UnusedDeductionsService { try { let adjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token) + const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') + if (!deductions.length) { + // If there are no deductions then unused deductions doesn't need to be calculated + return 'NONE' + } + const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( nomsId, adjustments, @@ -41,13 +47,8 @@ export default class UnusedDeductionsService { return 'VALIDATION' } - const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions - const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') - if (!deductions.length) { - // If there are no deductions then unused deductions doesn't need to be calculated - return 'NONE' - } + const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions if (this.anyDeductionFromNomis(deductions)) { // won't calculate unused deductions if adjusments are not from DPS. return 'NOMIS_ADJUSTMENT' @@ -88,6 +89,12 @@ export default class UnusedDeductionsService { token, ) + const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') + if (!deductions.length) { + // If there are no deductions then unused deductions doesn't need to be calculated + return 'NONE' + } + if (unusedDeductionsResponse.validationMessages?.length) { if ( unusedDeductionsResponse.validationMessages.find( @@ -99,13 +106,8 @@ export default class UnusedDeductionsService { return 'VALIDATION' } - const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions - const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') - if (!deductions.length) { - // If there are no deductions then unused deductions doesn't need to be calculated - return 'NONE' - } + const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions if (this.anyDeductionFromNomis(deductions)) { return 'NOMIS_ADJUSTMENT' } From 92167718efc6e08389659bdb808df849ebf976a0 Mon Sep 17 00:00:00 2001 From: Pavilion Sahota Date: Wed, 27 Mar 2024 14:18:55 +0000 Subject: [PATCH 7/7] Updated unused deductions service --- server/services/unusedDeductionsService.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/server/services/unusedDeductionsService.ts b/server/services/unusedDeductionsService.ts index 020c1470..acf4da8a 100644 --- a/server/services/unusedDeductionsService.ts +++ b/server/services/unusedDeductionsService.ts @@ -23,7 +23,6 @@ export default class UnusedDeductionsService { async waitUntilUnusedRemandCreated(nomsId: string, token: string): Promise { try { let adjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token) - const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') if (!deductions.length) { // If there are no deductions then unused deductions doesn't need to be calculated @@ -48,12 +47,12 @@ export default class UnusedDeductionsService { return 'VALIDATION' } - const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions if (this.anyDeductionFromNomis(deductions)) { // won't calculate unused deductions if adjusments are not from DPS. return 'NOMIS_ADJUSTMENT' } + const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions /* eslint-disable no-await-in-loop */ for (let i = 0; i < this.maxTries; i += 1) { if (calculatedUnusedDeducions || calculatedUnusedDeducions === 0) { @@ -83,18 +82,18 @@ export default class UnusedDeductionsService { token: string, ): Promise { try { - const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( - nomsId, - adjustments, - token, - ) - const deductions = adjustments.filter(it => it.adjustmentType === 'REMAND' || it.adjustmentType === 'TAGGED_BAIL') if (!deductions.length) { // If there are no deductions then unused deductions doesn't need to be calculated return 'NONE' } + const unusedDeductionsResponse = await this.calculateReleaseDatesService.calculateUnusedDeductions( + nomsId, + adjustments, + token, + ) + if (unusedDeductionsResponse.validationMessages?.length) { if ( unusedDeductionsResponse.validationMessages.find( @@ -107,11 +106,11 @@ export default class UnusedDeductionsService { return 'VALIDATION' } - const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions if (this.anyDeductionFromNomis(deductions)) { return 'NOMIS_ADJUSTMENT' } + const calculatedUnusedDeducions = unusedDeductionsResponse.unusedDeductions if (calculatedUnusedDeducions || calculatedUnusedDeducions === 0) { const dbDeductions = this.getTotalUnusedRemand(adjustments) if (calculatedUnusedDeducions === dbDeductions) {