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

CMG-16 refactored to middleware #168

Merged
merged 4 commits into from
Feb 21, 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
2 changes: 2 additions & 0 deletions server/@types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SessionAdjustment from '../AdjustmentTypes'
import type { UserDetails } from '../../services/userService'
import { PrisonApiPrisoner } from '../prisonApi/prisonClientTypes'

export default {}

Expand Down Expand Up @@ -29,6 +30,7 @@ export declare global {

interface Locals {
user: Express.User
prisoner: PrisonApiPrisoner
}
}
}
2 changes: 2 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import setUpWebSession from './middleware/setUpWebSession'

import routes from './routes'
import type { Services } from './services'
import populateCurrentPrisoner from './middleware/populateCurrentPrisoner'

export default function createApp(services: Services): express.Application {
const app = express()
Expand All @@ -37,6 +38,7 @@ export default function createApp(services: Services): express.Application {
app.use(authorisationMiddleware())
app.use(setUpCsrf())
app.use(setUpCurrentUser(services))
app.use('/:nomsId', populateCurrentPrisoner(services.prisonerService))

app.use(routes(services))

Expand Down
27 changes: 27 additions & 0 deletions server/middleware/populateCurrentPrisoner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RequestHandler } from 'express'
import PrisonerService from '../services/prisonerService'
import FullPageError from '../model/FullPageError'
import logger from '../../logger'

export default function populateCurrentPrisoner(prisonerService: PrisonerService): RequestHandler {
return async (req, res, next) => {
const { token, caseloads } = res.locals.user
const { nomsId } = req.params

if (token && nomsId) {
try {
const prisoner = await prisonerService.getPrisonerDetail(nomsId, caseloads, token)
if (caseloads.includes(prisoner.agencyId)) {
res.locals.prisoner = prisoner
} else {
throw FullPageError.notInCaseLoadError()
}
} catch (error) {
logger.error(error, `Failed to get prisoner with prisoner number: ${nomsId}`)
return next(error)
}
}

return next()
}
}
5 changes: 2 additions & 3 deletions server/model/adjustmentsHubViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { IdentifyRemandDecision, RemandResult } from '../@types/identifyRemandPeriods/identifyRemandPeriodsTypes'
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { calculateReleaseDatesCheckInformationUrl } from '../utils/utils'
import adjustmentTypes, { AdjustmentType } from './adjustmentTypes'

Expand All @@ -26,7 +25,7 @@ export default class AdjustmentsHubViewModel {
public messageType: AdjustmentType

constructor(
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
public adjustments: Adjustment[],
public relevantRemand: RemandResult,
public remandDecision: IdentifyRemandDecision,
Expand Down Expand Up @@ -92,7 +91,7 @@ export default class AdjustmentsHubViewModel {
}

public calculateReleaseDatesUrl() {
return calculateReleaseDatesCheckInformationUrl(this.prisonerDetail)
return calculateReleaseDatesCheckInformationUrl(this.prisonerNumber)
}

private allDeductionsOnDps() {
Expand Down
2 changes: 0 additions & 2 deletions server/model/recallModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import dayjs from 'dayjs'
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import RecallForm from './recallForm'

export default class RecallModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustments: Adjustment[],
public form: RecallForm,
) {}
Expand Down
3 changes: 1 addition & 2 deletions server/model/remandChangeModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import { UnusedDeductionCalculationResponse } from '../@types/calculateReleaseDates/calculateReleaseDatesClientTypes'
import { offencesForAdjustment, remandRelatedValidationSummary } from '../utils/utils'

export default class RemandChangeModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustment: Adjustment,
private sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
private calculatedUnusedDeductions: UnusedDeductionCalculationResponse,
Expand Down
9 changes: 4 additions & 5 deletions server/model/remandDatesModel.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import SessionAdjustment from '../@types/AdjustmentTypes'
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import RemandDatesForm from './remandDatesForm'

export default class RemandDatesModel {
constructor(
public id: string,
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
public adjustments: SessionAdjustment[],
public form: RemandDatesForm,
public addOrEdit: string = null,
) {}

public backlink(): string {
if (this.addOrEdit === 'edit') return `/${this.prisonerDetail.offenderNo}/remand/edit/${this.id}`
if (this.addOrEdit === 'edit') return `/${this.prisonerNumber}/remand/edit/${this.id}`
if (this.adjustments.length > 1 || this.adjustments[0].complete) {
return `/${this.prisonerDetail.offenderNo}/remand/review`
return `/${this.prisonerNumber}/remand/review`
}
return `/${this.prisonerDetail.offenderNo}`
return `/${this.prisonerNumber}`
}
}
10 changes: 5 additions & 5 deletions server/model/remandReviewModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import { daysBetween, offencesForAdjustment, remandRelatedValidationSummary } from '../utils/utils'
import ReviewRemandForm from './reviewRemandForm'
import { CalculateReleaseDatesValidationMessage } from '../@types/calculateReleaseDates/calculateReleaseDatesClientTypes'
Expand All @@ -9,7 +9,7 @@ export default class RemandReviewModel {
adjustmentIds: string[]

constructor(
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
public adjustments: Record<string, Adjustment>,
private sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
private calculateReleaseDatesValidationMessages: CalculateReleaseDatesValidationMessage[],
Expand All @@ -29,7 +29,7 @@ export default class RemandReviewModel {
}

public backlink(): string {
return `/${this.prisonerDetail.offenderNo}/remand/offences/add/${this.adjustmentIds[0]}`
return `/${this.prisonerNumber}/remand/offences/add/${this.adjustmentIds[0]}`
}

public adjustmentSummary(id: string) {
Expand All @@ -49,7 +49,7 @@ export default class RemandReviewModel {
actions: {
items: [
{
href: `/${this.prisonerDetail.offenderNo}/remand/dates/add/${id}`,
href: `/${this.prisonerNumber}/remand/dates/add/${id}`,
text: 'Edit',
visuallyHiddenText: 'remand',
},
Expand All @@ -75,7 +75,7 @@ export default class RemandReviewModel {
actions: {
items: [
{
href: `/${this.prisonerDetail.offenderNo}/remand/offences/add/${id}`,
href: `/${this.prisonerNumber}/remand/offences/add/${id}`,
text: 'Edit',
visuallyHiddenText: 'remand',
},
Expand Down
2 changes: 0 additions & 2 deletions server/model/remandSaveModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import dayjs from 'dayjs'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { daysBetween } from '../utils/utils'

export default class RemandSaveModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustments: Adjustment[],
public unusedDeductions: number,
) {}
Expand Down
14 changes: 5 additions & 9 deletions server/model/remandSelectOffencesModel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import SessionAdjustment from '../@types/AdjustmentTypes'
import {
PrisonApiOffence,
PrisonApiOffenderSentenceAndOffences,
PrisonApiPrisoner,
} from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffence, PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import PrisonerService from '../services/prisonerService'
import { daysBetween, groupBy } from '../utils/utils'
import RemandOffencesForm from './remandOffencesForm'
Expand All @@ -13,7 +9,7 @@ export default class RemandSelectOffencesModel {

constructor(
public id: string,
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
public adjustment: SessionAdjustment,
public form: RemandOffencesForm,
sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
Expand All @@ -23,11 +19,11 @@ export default class RemandSelectOffencesModel {
}

public backlink(): string {
if (this.addOrEdit === 'edit') return `/${this.prisonerDetail.offenderNo}/remand/edit/${this.id}`
if (this.addOrEdit === 'edit') return `/${this.prisonerNumber}/remand/edit/${this.id}`
if (this.adjustment.complete) {
return `/${this.prisonerDetail.offenderNo}/remand/review`
return `/${this.prisonerNumber}/remand/review`
}
return `/${this.prisonerDetail.offenderNo}/remand/dates/add/${this.id}`
return `/${this.prisonerNumber}/remand/dates/add/${this.id}`
}

public days(): number {
Expand Down
3 changes: 1 addition & 2 deletions server/model/remandViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import dayjs from 'dayjs'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import { offencesForAdjustment } from '../utils/utils'

export default class RemandViewModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustments: Adjustment[],
private sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
) {}
Expand Down
2 changes: 0 additions & 2 deletions server/model/removeModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { AdjustmentType } from './adjustmentTypes'
import ReviewModel from './reviewModel'

export default class RemoveModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustment: Adjustment,
public adjustmentType: AdjustmentType,
) {}
Expand Down
2 changes: 0 additions & 2 deletions server/model/reviewAndSubmitAdaViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import dayjs from 'dayjs'
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { Adjustment, EditableAdjustment } from '../@types/adjustments/adjustmentsTypes'

export default class ReviewAndSubmitAdaViewModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustments: EditableAdjustment[],
public existingAdjustments: Adjustment[],
public quashedAdjustments: Adjustment[],
Expand Down
6 changes: 1 addition & 5 deletions server/model/reviewModel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import dayjs from 'dayjs'
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { Adjustment, EditableAdjustment } from '../@types/adjustments/adjustmentsTypes'
import adjustmentTypes, { AdjustmentType } from './adjustmentTypes'
import ualType from './ualType'
import { daysBetween } from '../utils/utils'

export default class ReviewModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public adjustment: EditableAdjustment,
) {}
constructor(public adjustment: EditableAdjustment) {}

public adjustmentType(): AdjustmentType {
return adjustmentTypes.find(it => it.value === this.adjustment.adjustmentType)
Expand Down
7 changes: 3 additions & 4 deletions server/model/taggedBailDaysModel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import TaggedBailDaysForm from './taggedBailDaysForm'
import SessionAdjustment from '../@types/AdjustmentTypes'

export default class TaggedBailDaysModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
private addOrEdit: string,
private id: string,
public form: TaggedBailDaysForm,
Expand All @@ -13,8 +12,8 @@ export default class TaggedBailDaysModel {

public backlink(): string {
if (this.adjustment.complete) {
return `/${this.prisonerDetail.offenderNo}/tagged-bail/review/${this.addOrEdit}/${this.id}`
return `/${this.prisonerNumber}/tagged-bail/review/${this.addOrEdit}/${this.id}`
}
return `/${this.prisonerDetail.offenderNo}/tagged-bail/select-case/${this.addOrEdit}/${this.id}`
return `/${this.prisonerNumber}/tagged-bail/select-case/${this.addOrEdit}/${this.id}`
}
}
6 changes: 3 additions & 3 deletions server/model/taggedBailRemoveModel.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import { AdjustmentType } from './adjustmentTypes'
import { dateToString } from '../utils/utils'

export default class TaggedBailRemoveModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
public adjustment: Adjustment,
public adjustmentType: AdjustmentType,
public sentenceAndOffence: PrisonApiOffenderSentenceAndOffences,
public showUnusedMessage: boolean,
) {}

public backlink(): string {
return `/${this.prisonerDetail.offenderNo}`
return `/${this.prisonerNumber}`
}

public rows() {
Expand Down
6 changes: 3 additions & 3 deletions server/model/taggedBailReviewModel.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import SessionAdjustment from '../@types/AdjustmentTypes'
import { dateToString, getMostRecentSentenceAndOffence } from '../utils/utils'

export default class TaggedBailReviewModel {
constructor(
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
private addOrEdit: string,
public id: string,
private sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
public adjustment: SessionAdjustment,
) {}

public backlink(): string {
return `/${this.prisonerDetail.offenderNo}/tagged-bail/days/${this.addOrEdit}/${this.id}`
return `/${this.prisonerNumber}/tagged-bail/days/${this.addOrEdit}/${this.id}`
}

public getCaseDetails() {
Expand Down
12 changes: 6 additions & 6 deletions server/model/taggedBailSelectCaseModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrisonApiOffenderSentenceAndOffences, PrisonApiPrisoner } from '../@types/prisonApi/prisonClientTypes'
import { PrisonApiOffenderSentenceAndOffences } from '../@types/prisonApi/prisonClientTypes'
import SessionAdjustment from '../@types/AdjustmentTypes'
import { getActiveSentencesByCaseSequence, SentencesByCaseSequence } from '../utils/utils'

Expand All @@ -8,7 +8,7 @@ export default class TaggedBailSelectCaseModel {
private sentencesByCaseSequence: SentencesByCaseSequence[]

constructor(
public prisonerDetail: PrisonApiPrisoner,
public prisonerNumber: string,
private sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
private addOrEdit: string,
private id: string,
Expand All @@ -19,9 +19,9 @@ export default class TaggedBailSelectCaseModel {

public backlink(): string {
if (this.adjustment.complete) {
return `/${this.prisonerDetail.offenderNo}/tagged-bail/review/${this.addOrEdit}/${this.id}`
return `/${this.prisonerNumber}/tagged-bail/review/${this.addOrEdit}/${this.id}`
}
return `/${this.prisonerDetail.offenderNo}`
return `/${this.prisonerNumber}`
}

// returns the sentence data for each unique case sequence; i.e. the record that has the earliest sentence date when multiple ones exist
Expand All @@ -31,8 +31,8 @@ export default class TaggedBailSelectCaseModel {
...it.sentences.sort((a, b) => new Date(a.sentenceDate).getTime() - new Date(b.sentenceDate).getTime())[0],
selected: this.adjustment.taggedBail?.caseSequence === it.caseSequence,
selectCaseHref: this.adjustment.complete
? `/${this.prisonerDetail.offenderNo}/tagged-bail/review/${this.addOrEdit}/${this.id}?caseSequence=${it.caseSequence}`
: `/${this.prisonerDetail.offenderNo}/tagged-bail/days/${this.addOrEdit}/${this.id}?caseSequence=${it.caseSequence}`,
? `/${this.prisonerNumber}/tagged-bail/review/${this.addOrEdit}/${this.id}?caseSequence=${it.caseSequence}`
: `/${this.prisonerNumber}/tagged-bail/days/${this.addOrEdit}/${this.id}?caseSequence=${it.caseSequence}`,
}
})
}
Expand Down
Loading