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

CVSL-2107 Tidying up vary approval caseload #1058

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
129 changes: 28 additions & 101 deletions server/routes/approvingVariations/handlers/varyApproveList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { Request, Response } from 'express'

import VaryApproveListRoutes from './varyApproveList'
import CaseloadService from '../../../services/lists/caseloadService'
import LicenceStatus from '../../../enumeration/licenceStatus'
import LicenceType from '../../../enumeration/licenceType'
import type { DeliusRecord } from '../../../@types/managedCase'
import type { CvlPrisoner } from '../../../@types/licenceApiClientTypes'

const caseloadService = new CaseloadService(null, null) as jest.Mocked<CaseloadService>
jest.mock('../../../services/lists/caseloadService')

const aCase = {
licenceId: 1,
licenceType: LicenceType.AP,
variationRequestDate: '01 May 2022',
releaseDate: '01 May 2022',
name: 'Bob Smith',
crnNumber: 'X12345',
probationPractitioner: {
name: 'Walter White',
},
}

describe('Route Handlers - Variation approval list', () => {
const handler = new VaryApproveListRoutes(caseloadService)
let req: Request
Expand All @@ -28,44 +37,6 @@ describe('Route Handlers - Variation approval list', () => {
},
},
} as unknown as Response

caseloadService.getVaryApproverCaseload.mockResolvedValue([
{
licences: [
{
id: 1,
type: LicenceType.AP,
status: LicenceStatus.VARIATION_SUBMITTED,
dateCreated: '01/05/2022 10:15',
isDueToBeReleasedInTheNextTwoWorkingDays: true,
releaseDate: null,
},
],
cvlFields: {
licenceType: 'AP',
hardStopDate: '03/01/2023',
hardStopWarningDate: '01/01/2023',
isInHardStopPeriod: true,
isDueForEarlyRelease: true,
isDueToBeReleasedInTheNextTwoWorkingDays: true,
isEligibleForEarlyRelease: false,
},
nomisRecord: {
firstName: 'Bob',
lastName: 'Smith',
prisonerNumber: 'A1234AA',
releaseDate: '2022-05-01',
} as CvlPrisoner,
deliusRecord: {
otherIds: {
crn: 'X12345',
},
} as DeliusRecord,
probationPractitioner: {
name: 'Walter White',
},
},
])
})

afterEach(() => {
Expand All @@ -74,32 +45,10 @@ describe('Route Handlers - Variation approval list', () => {

describe('GET', () => {
it('should render list of variations for approval', async () => {
await handler.GET(req, res)
expect(caseloadService.getVaryApproverCaseload).toHaveBeenCalledWith(res.locals.user)
expect(res.render).toHaveBeenCalledWith('pages/vary-approve/cases', {
caseload: [
{
licenceId: 1,
name: 'Bob Smith',
crnNumber: 'X12345',
licenceType: 'AP',
releaseDate: '01 May 2022',
variationRequestDate: '01 May 2022',
probationPractitioner: {
name: 'Walter White',
},
},
],
regionCases: false,
search: undefined,
})
})

it('should successfully search by name', async () => {
req.query.search = 'bob'
caseloadService.getVaryApproverCaseload.mockResolvedValue([aCase])

await handler.GET(req, res)

expect(caseloadService.getVaryApproverCaseload).toHaveBeenCalledWith(res.locals.user, undefined)
expect(res.render).toHaveBeenCalledWith('pages/vary-approve/cases', {
caseload: [
{
Expand All @@ -115,15 +64,16 @@ describe('Route Handlers - Variation approval list', () => {
},
],
regionCases: false,
search: 'bob',
search: undefined,
})
})

it('should successfully search by crn number', async () => {
req.query.search = 'X12345'
it('should render list of variations for region for approval', async () => {
req.query.view = 'region'
caseloadService.getVaryApproverCaseloadByRegion.mockResolvedValue([aCase])

await handler.GET(req, res)

expect(caseloadService.getVaryApproverCaseloadByRegion).toHaveBeenCalledWith(res.locals.user, undefined)
expect(res.render).toHaveBeenCalledWith('pages/vary-approve/cases', {
caseload: [
{
Expand All @@ -138,45 +88,22 @@ describe('Route Handlers - Variation approval list', () => {
},
},
],
regionCases: false,
search: 'X12345',
regionCases: true,
search: undefined,
})
})

it('should successfully search by probation practitioner', async () => {
req.query.search = 'white'

it('should pass search request through for approval search', async () => {
req.query.search = 'bob'
await handler.GET(req, res)

expect(res.render).toHaveBeenCalledWith('pages/vary-approve/cases', {
caseload: [
{
licenceId: 1,
name: 'Bob Smith',
crnNumber: 'X12345',
licenceType: 'AP',
releaseDate: '01 May 2022',
variationRequestDate: '01 May 2022',
probationPractitioner: {
name: 'Walter White',
},
},
],
regionCases: false,
search: 'white',
})
expect(caseloadService.getVaryApproverCaseload).toHaveBeenCalledWith(res.locals.user, 'bob')
})

it('should return empty caseload if search does not match', async () => {
req.query.search = 'XXX'

it('should pass search request through for approval region search', async () => {
req.query.view = 'region'
req.query.search = 'bob'
await handler.GET(req, res)

expect(res.render).toHaveBeenCalledWith('pages/vary-approve/cases', {
caseload: [],
search: 'XXX',
regionCases: false,
})
expect(caseloadService.getVaryApproverCaseloadByRegion).toHaveBeenCalledWith(res.locals.user, 'bob')
})
})
})
48 changes: 4 additions & 44 deletions server/routes/approvingVariations/handlers/varyApproveList.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Request, Response } from 'express'
import moment from 'moment'
import _ from 'lodash'
import { format } from 'date-fns'
import CaseloadService from '../../../services/lists/caseloadService'
import { convertToTitleCase, parseCvlDateTime, parseIsoDate } from '../../../utils/utils'

export default class VaryApproveListRoutes {
constructor(private readonly caseloadService: CaseloadService) {}
Expand All @@ -13,46 +9,10 @@ export default class VaryApproveListRoutes {
const regionCases = req.query?.view === 'region'
const { user } = res.locals

const cases = regionCases
? await this.caseloadService.getVaryApproverCaseloadByRegion(user)
: await this.caseloadService.getVaryApproverCaseload(user)
const caseload = regionCases
? await this.caseloadService.getVaryApproverCaseloadByRegion(user, search)
: await this.caseloadService.getVaryApproverCaseload(user, search)

const caseloadViewModel = cases
.map(c => {
const licence = _.head(c.licences)

const releaseDate = c.nomisRecord.releaseDate
? format(parseIsoDate(c.nomisRecord.releaseDate), 'dd MMM yyyy')
: null

const variationRequestDate = licence.dateCreated
? format(parseCvlDateTime(licence.dateCreated, { withSeconds: false }), 'dd MMMM yyyy')
: null

return {
licenceId: licence.id,
name: convertToTitleCase(`${c.nomisRecord.firstName} ${c.nomisRecord.lastName}`.trim()),
crnNumber: c.deliusRecord.otherIds.crn,
licenceType: licence.type,
variationRequestDate,
releaseDate,
probationPractitioner: c.probationPractitioner,
}
})
.filter(c => {
const searchString = search?.toLowerCase().trim()
if (!searchString) return true
return (
c.crnNumber?.toLowerCase().includes(searchString) ||
c.name.toLowerCase().includes(searchString) ||
c.probationPractitioner?.name.toLowerCase().includes(searchString)
)
})
.sort((a, b) => {
const crd1 = moment(a.releaseDate, 'DD MMM YYYY').unix()
const crd2 = moment(b.releaseDate, 'DD MMM YYYY').unix()
return crd1 - crd2
})
res.render('pages/vary-approve/cases', { caseload: caseloadViewModel, search, regionCases })
res.render('pages/vary-approve/cases', { caseload, search, regionCases })
}
}
Loading