-
Notifications
You must be signed in to change notification settings - Fork 0
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-530 unused deductions banner make reason specific #259
Changes from 2 commits
9457c98
b95cb81
61fad4a
b9dcee6
231ea0c
c6b2cc0
9216771
ebe2e42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<boolean> { | ||
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 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the calculated unused deductions don't match whats in the database. There isn't really a message for this. |
||
} | ||
} | ||
} catch { | ||
// Error couldn't calculate unused deductions. | ||
} | ||
return false | ||
return 'NONE' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where there was an exception somewhere. Not sure it should be 'NONE' |
||
/* eslint-enable no-await-in-loop */ | ||
} | ||
|
||
async serviceHasCalculatedUnusedDeductions( | ||
async getCalculatedUnusedDeductionsMessage( | ||
nomsId: string, | ||
adjustments: Adjustment[], | ||
token: string, | ||
): Promise<boolean> { | ||
): Promise<UnusedDeductionMessageType> { | ||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was meant to move to after the validation check |
||
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' | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was meant to move to after the validation check