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-224 Adding ADA after awarded. #81

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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: 21 additions & 21 deletions server/services/additionalDaysAwardedService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export default class AdditionalDaysAwardedService {
const allAdaAdjustments = (await new AdjustmentsClient(token).findByPerson(nomsId)).filter(
it => it.adjustmentType === 'ADDITIONAL_DAYS_AWARDED',
)
const existingAdasWithChargeIds = allAdaAdjustments.filter(it => it.additionalDaysAwarded)
const systemToken = await this.hmppsAuthClient.getSystemClientToken(username)
const adjudicationClient = new AdjudicationClient(systemToken)
const adjudications: AdjudicationSearchResponse = await adjudicationClient.getAdjudications(nomsId)
Expand All @@ -120,19 +119,19 @@ export default class AdditionalDaysAwardedService {
const adas: Ada[] = this.getAdas(individualAdjudications, startOfSentenceEnvelope)

const awardedOrPending: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'AWARDED_OR_PENDING')
let { awarded, awaitingApproval } = this.filterAdasByMatchingAdjustment(awardedOrPending, existingAdasWithChargeIds)
let { awarded, awaitingApproval } = this.filterAdasByMatchingAdjustment(awardedOrPending, allAdaAdjustments)

const suspended: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'SUSPENDED')
const totalSuspended: number = this.getTotalDays(suspended)

const allQuashed: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'QUASHED')
const quashed = this.filterQuashedAdasByMatchingChargeIds(allQuashed, existingAdasWithChargeIds)
const quashed = this.filterQuashedAdasByMatchingChargeIds(allQuashed, allAdaAdjustments)
const totalQuashed: number = this.getTotalDays(quashed)

const allProspective: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'PROSPECTIVE')
const { awarded: prospectiveAwarded, awaitingApproval: prospective } = this.filterAdasByMatchingAdjustment(
allProspective,
existingAdasWithChargeIds,
allAdaAdjustments,
)

awarded = awarded.concat(prospectiveAwarded)
Expand Down Expand Up @@ -167,7 +166,6 @@ export default class AdditionalDaysAwardedService {
const allAdaAdjustments = (await new AdjustmentsClient(token).findByPerson(nomsId)).filter(
it => it.adjustmentType === 'ADDITIONAL_DAYS_AWARDED',
)
const existingAdasWithChargeIds = allAdaAdjustments.filter(it => it.additionalDaysAwarded)
const systemToken = await this.hmppsAuthClient.getSystemClientToken(username)
const adjudicationClient = new AdjudicationClient(systemToken)
const adjudications: AdjudicationSearchResponse = await adjudicationClient.getAdjudications(nomsId)
Expand All @@ -179,10 +177,7 @@ export default class AdditionalDaysAwardedService {
const adas: Ada[] = this.getAdas(individualAdjudications, startOfSentenceEnvelope)

const allProspective: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'PROSPECTIVE')
const { awaitingApproval: prospective } = this.filterAdasByMatchingAdjustment(
allProspective,
existingAdasWithChargeIds,
)
const { awaitingApproval: prospective } = this.filterAdasByMatchingAdjustment(allProspective, allAdaAdjustments)
const totalProspective: number = this.getTotalDays(prospective)

return {
Expand All @@ -199,7 +194,9 @@ export default class AdditionalDaysAwardedService {
adas: AdasByDateCharged[],
adjustments: Adjustment[],
): AdasByDateCharged[] {
const chargeIds = adjustments.flatMap(it => it.additionalDaysAwarded.adjudicationId)
const chargeIds = adjustments
.filter(it => it.additionalDaysAwarded)
.flatMap(it => it.additionalDaysAwarded.adjudicationId)
return adas
.filter(adaByDate => {
return adaByDate.charges.some(it => chargeIds.includes(it.chargeNumber))
Expand All @@ -218,6 +215,14 @@ export default class AdditionalDaysAwardedService {
awaitingApproval: AdasByDateCharged[]
}

if (adjustments.some(it => !it.additionalDaysAwarded)) {
// An ADA has been created in NOMIS, Revert everything to pending approval
result.awaitingApproval = adas.map(it => {
return { ...it, status: 'PENDING APPROVAL' }
})
return result
}

adas.forEach(it => {
const adjustment = adjustments.find(adj => this.adjustmentMatchesAdjudication(it, adj))
if (adjustment) {
Expand Down Expand Up @@ -426,18 +431,14 @@ export default class AdditionalDaysAwardedService {
return adjudicationClient.getAdjudication(prisonerDetail.offenderNo, it.adjudicationNumber)
}),
)
const existingAdasWithChargeIds = allAdaAdjustments.filter(it => it.additionalDaysAwarded)
const adas: Ada[] = this.getAdas(individualAdjudications, startOfSentenceEnvelope)

const awardedOrPending: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'AWARDED_OR_PENDING')
const { awaitingApproval } = this.filterAdasByMatchingAdjustment(awardedOrPending, existingAdasWithChargeIds)
const { awaitingApproval } = this.filterAdasByMatchingAdjustment(awardedOrPending, allAdaAdjustments)
const allProspective: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'PROSPECTIVE')
const { awaitingApproval: prospective } = this.filterAdasByMatchingAdjustment(
allProspective,
existingAdasWithChargeIds,
)
const { awaitingApproval: prospective } = this.filterAdasByMatchingAdjustment(allProspective, allAdaAdjustments)
const allQuashed: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'QUASHED')
const quashed = this.filterQuashedAdasByMatchingChargeIds(allQuashed, existingAdasWithChargeIds)
const quashed = this.filterQuashedAdasByMatchingChargeIds(allQuashed, allAdaAdjustments)

return this.shouldInterceptLogic(
req,
Expand Down Expand Up @@ -520,17 +521,16 @@ export default class AdditionalDaysAwardedService {
return adjudicationClient.getAdjudication(prisonerDetail.offenderNo, it.adjudicationNumber)
}),
)
const existingAdasWithChargeIds = allAdaAdjustments.filter(it => it.additionalDaysAwarded)
const adas: Ada[] = this.getAdas(individualAdjudications, startOfSentenceEnvelope)

const awardedOrPending: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'AWARDED_OR_PENDING')

let { awarded, awaitingApproval } = this.filterAdasByMatchingAdjustment(awardedOrPending, existingAdasWithChargeIds)
let { awarded, awaitingApproval } = this.filterAdasByMatchingAdjustment(awardedOrPending, allAdaAdjustments)

const allProspective: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'PROSPECTIVE')
const { awarded: prospectiveAwarded, awaitingApproval: prospective } = this.filterAdasByMatchingAdjustment(
allProspective,
existingAdasWithChargeIds,
allAdaAdjustments,
)

awarded = awarded.concat(prospectiveAwarded)
Expand All @@ -545,7 +545,7 @@ export default class AdditionalDaysAwardedService {
awaitingApproval = awaitingApproval.concat(selectedProspectiveAdas)

const allQuashed: AdasByDateCharged[] = this.getAdasByDateCharged(adas, 'QUASHED')
const quashed = this.filterQuashedAdasByMatchingChargeIds(allQuashed, existingAdasWithChargeIds)
const quashed = this.filterQuashedAdasByMatchingChargeIds(allQuashed, allAdaAdjustments)

return {
awarded,
Expand Down