Skip to content

Commit

Permalink
ADJUST1-547 Refactoring unused deductions service. (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldlharper authored Apr 3, 2024
1 parent 708d754 commit 5134bd6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 68 deletions.
18 changes: 7 additions & 11 deletions server/routes/adjustmentRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, { UnusedDeductionMessageType } from '../services/unusedDeductionsService'
import UnusedDeductionsService from '../services/unusedDeductionsService'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'

export default class AdjustmentRoutes {
Expand Down Expand Up @@ -54,26 +54,22 @@ export default class AdjustmentRoutes {

const message = req.flash('message')
const messageExists = message && message[0]
let unusedDeductionMessage: UnusedDeductionMessageType = 'NONE'
if (messageExists) {
this.adjustmentsStoreService.clear(req, nomsId)

unusedDeductionMessage = await this.unusedDeductionsService.waitUntilUnusedRemandCreated(nomsId, token)
}

const adjustments = await this.adjustmentsService.findByPerson(
nomsId,
startOfSentenceEnvelope.earliestSentence,
token,
)
const unusedDeductionMessage = await this.unusedDeductionsService.getCalculatedUnusedDeductionsMessage(
nomsId,
adjustments,
!!messageExists, // retry if this page is loaded as a result of adjustment change. Wait for unused deductions to match.
token,
)

if (!messageExists) {
unusedDeductionMessage = await this.unusedDeductionsService.getCalculatedUnusedDeductionsMessage(
nomsId,
adjustments,
token,
)
}
if (!messageExists) {
const intercept = await this.additionalDaysAwardedService.shouldIntercept(
req,
Expand Down
71 changes: 14 additions & 57 deletions server/services/unusedDeductionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,10 @@ export default class UnusedDeductionsService {
return deductions.some(it => !it.remand?.chargeId?.length && !it.taggedBail?.caseSequence)
}

/* Wait until calclulated unused deductions matches with adjustments database. */
async waitUntilUnusedRemandCreated(nomsId: string, token: string): Promise<UnusedDeductionMessageType> {
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,
token,
)

if (unusedDeductionsResponse.validationMessages?.length) {
if (
unusedDeductionsResponse.validationMessages.find(
it => it.type === 'UNSUPPORTED_CALCULATION' || it.type === 'UNSUPPORTED_SENTENCE',
)
) {
return 'UNSUPPORTED'
}

return 'VALIDATION'
}

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) {
const dbDeductions = this.getTotalUnusedRemand(adjustments)
if (calculatedUnusedDeducions === dbDeductions) {
return 'NONE'
}
await delay(this.waitBetweenTries)
adjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token)
// Try again
} else {
// Unable to calculate unused deductions.
return 'UNKNOWN'
}
}
} catch {
// Error couldn't calculate unused deductions.
}

return 'UNKNOWN'
/* eslint-enable no-await-in-loop */
}

async getCalculatedUnusedDeductionsMessage(
nomsId: string,
adjustments: Adjustment[],
retry: boolean,
token: string,
): Promise<UnusedDeductionMessageType> {
try {
Expand Down Expand Up @@ -116,6 +60,19 @@ export default class UnusedDeductionsService {
if (calculatedUnusedDeducions === dbDeductions) {
return 'NONE'
}
if (retry) {
/* eslint-disable no-await-in-loop */
for (let i = 0; i < this.maxTries; i += 1) {
await delay(this.waitBetweenTries)
const retryAdjustments = await this.adjustmentsService.findByPersonOutsideSentenceEnvelope(nomsId, token)
const retryDeductions = this.getTotalUnusedRemand(retryAdjustments)
if (calculatedUnusedDeducions === retryDeductions) {
return 'NONE'
}
// Try again
}
/* eslint-enable no-await-in-loop */
}
}

return 'UNKNOWN'
Expand Down

0 comments on commit 5134bd6

Please sign in to comment.