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

Map 1745 UOF bug users unable to access incident details page #743

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
49 changes: 40 additions & 9 deletions server/routes/creatingReports/changePrison.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ const draftReportService = new DraftReportService(
) as jest.Mocked<DraftReportService>

let app
const flash = jest.fn()

beforeEach(() => {
app = appWithAllRoutes({ locationService, draftReportService })
flash.mockReturnValue([])
app = appWithAllRoutes({ locationService, draftReportService }, undefined, false, flash)
locationService.getPrisons.mockResolvedValue([
{
agencyId: 'BXI',
Expand All @@ -32,37 +34,37 @@ afterEach(() => {
jest.resetAllMocks()
})

describe('GET /change-prison', () => {
describe('GET /prison-of-incident', () => {
test('should render content', () => {
return request(app)
.get(`/report/-19/change-prison`)
.get(`/report/-19/prison-of-incident`)
.expect('Content-Type', /html/)
.expect(res => {
expect(res.text).toContain('What prison did the use of force take place in?')
expect(res.text).toContain('Brixton')
})
})
})
describe('POST /change-prison', () => {
describe('POST /prison-of-incident', () => {
it('should redirect to incident-details page', () => {
return request(app)
.post('/report/-19/change-prison')
.post('/report/-19/prison-of-incident')
.send({ agencyId: 'MDI', submit: 'save-and-continue' })
.expect(302)
.expect('Location', '/report/-19/incident-details')
})

it('Not selecting a prison but selecting Continue should redirect to current page', () => {
return request(app)
.post('/report/-19/change-prison')
.post('/report/-19/prison-of-incident')
.send({ submit: 'save-and-continue' })
.expect(302)
.expect('Location', '/report/-19/change-prison')
.expect('Location', '/report/-19/prison-of-incident')
})

it('Selecting a prison followed by Continue should call the location service', () => {
return request(app)
.post('/report/-19/change-prison')
.post('/report/-19/prison-of-incident')
.send({ agencyId: 'MDI', submit: 'save-and-continue' })
.expect(302)
.expect(() => {
Expand All @@ -72,11 +74,40 @@ describe('POST /change-prison', () => {

it('Cancel should not call the location service', () => {
return request(app)
.post('/report/-19/change-prison')
.post('/report/-19/prison-of-incident')
.send({ agencyId: 'MDI', submit: 'cancel' })
.expect(302)
.expect(() => {
expect(draftReportService.updateAgencyId).not.toHaveBeenCalled()
})
})

it('Should process user input', () => {
flash.mockReturnValue([{ plannedUseOfForce: true, authorisedBy: 'the authoriser' }])

return request(app)
.post('/report/-19/prison-of-incident')
.send({ agencyId: 'MDI', submit: 'save-and-continue' })
.expect(302)
.expect(() => {
expect(draftReportService.process).toHaveBeenCalledWith(
{
activeCaseLoadId: 'MDI',
displayName: 'First Last',
firstName: 'first',
isReviewer: false,
lastName: 'last',
token: 'token',
userId: 'id',
username: 'user1',
},
-19,
'incidentDetails',
{ authorisedBy: 'the authoriser', plannedUseOfForce: true, witnesses: undefined },
null
)
expect(draftReportService.updateAgencyId).toHaveBeenCalledWith('MDI', 'user1', -19)
expect(flash).toHaveBeenCalledTimes(1)
})
})
})
15 changes: 15 additions & 0 deletions server/routes/creatingReports/changePrison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ export default class ChangePrisonRoutes {
return res.redirect(req.originalUrl)
}

const userInput = req.flash('userInputForIncidentDetails')

// this will create a new report record if it doesn't exist already
await this.draftReportService.process(
res.locals.user,
parseInt(bookingId, 10),
'incidentDetails',
{
plannedUseOfForce: userInput[0]?.plannedUseOfForce,
authorisedBy: userInput[0]?.authorisedBy,
witnesses: userInput[0]?.witnesses,
},
null
)

await this.draftReportService.updateAgencyId(agencyId, username, Number(bookingId))
}

Expand Down
25 changes: 24 additions & 1 deletion server/routes/creatingReports/incidentDetails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ describe('GET /section/form', () => {
expect(locationService.getIncidentLocations).toBeCalledWith('user1-system-token', 'persisted-agency-id')
})
})
test('should redirect to /prison-of-incident if agencyId is TRN', () => {
draftReportService.getCurrentDraft.mockResolvedValue({ id: '1', agencyId: 'TRN' })
return request(app)
.get(`/report/1/incident-details`)
.expect(302)
.expect('Location', '/report/1/prison-of-incident')
.expect(() => {
expect(locationService.getIncidentLocations).not.toHaveBeenCalled()
})
})

test('should redirect to /prison-of-incident if agencyId is OUT', () => {
draftReportService.getCurrentDraft.mockResolvedValue({ id: '1', agencyId: 'OUT' })
return request(app)
.get(`/report/1/incident-details`)
.expect(302)
.expect('Location', '/report/1/prison-of-incident')
.expect(() => {
expect(locationService.getIncidentLocations).not.toHaveBeenCalled()
})
})
})

describe('POST save and continue /section/form', () => {
Expand All @@ -145,6 +166,7 @@ describe('POST save and continue /section/form', () => {
.expect(302)
.expect('Location', '/report/1/staff-involved')
.expect(() => {
expect(flash).toHaveBeenCalledTimes(2)
expect(draftReportService.process).toBeCalledTimes(1)
expect(draftReportService.process).toBeCalledWith(
user,
Expand Down Expand Up @@ -246,8 +268,9 @@ describe('POST save and return to tasklist', () => {
witnesses: [{ name: 'User bob' }, { name: '' }],
})
.expect(302)
.expect('Location', '/report/1/change-prison')
.expect('Location', '/report/1/prison-of-incident')
.expect(() => {
expect(flash).toHaveBeenCalledTimes(1)
expect(draftReportService.getPotentialDuplicates).not.toBeCalled()
expect(draftReportService.process).toBeCalledTimes(1)
expect(draftReportService.process).toBeCalledWith(
Expand Down
18 changes: 15 additions & 3 deletions server/routes/creatingReports/incidentDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class IncidentDetailsRoutes {

private async getSubmitRedirectLocation(req: Request, bookingId: number, submitType) {
if (submitType === SubmitType.SAVE_AND_CHANGE_PRISON) {
return `/report/${bookingId}/change-prison`
return `/report/${bookingId}/prison-of-incident`
}

if (await this.draftReportService.isDraftComplete(req.user.username, bookingId)) {
Expand Down Expand Up @@ -77,11 +77,18 @@ export default class IncidentDetailsRoutes {

// If report has been created, use persisted agency Id which is robust against offender moving establishments
const prisonId = persistedAgencyId || offenderDetail.agencyId

// if prisoner is currently being transferred or has left the prison, redirect user to select prison where the incident occured
if (prisonId === 'TRN' || prisonId === 'OUT') {
return res.redirect(`/report/${bookingId}/prison-of-incident`)
}

const locations = await this.locationService.getIncidentLocations(token, prisonId)

const { displayName, offenderNo } = offenderDetail

const input = firstItem(req.flash('userInput'))
const input = firstItem(req.flash('userInputForIncidentDetails'))

const pageData = input || form[formName]

const prison = await this.locationService.getPrisonById(token, prisonId)
Expand Down Expand Up @@ -121,9 +128,11 @@ export default class IncidentDetailsRoutes {

const updatedSection = payloadFields

// flashing so data can be accessed in 'change prison'
req.flash('userInputForIncidentDetails', { ...payloadFields, incidentDate }) // merge all fields back together!

if (!isNilOrEmpty(errors)) {
req.flash('errors', errors)
req.flash('userInput', { ...payloadFields, incidentDate }) // merge all fields back together!
return res.redirect(req.originalUrl)
}

Expand All @@ -136,6 +145,9 @@ export default class IncidentDetailsRoutes {
)

if (incidentDate && submitType !== SubmitType.SAVE_AND_CHANGE_PRISON) {
// clear content in flash when not going to 'change the prison' page
req.flash('userInputForIncidentDetails')

const duplicates = await this.draftReportService.getPotentialDuplicates(
parseInt(bookingId, 10),
moment(incidentDate.value),
Expand Down
4 changes: 2 additions & 2 deletions server/routes/creatingReports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export default function Index({
get(reportPath('staff-member-not-found'), addInvolvedStaff.viewStaffMemberNotFound)

const changePrison = new ChangePrisonRoutes(locationService, draftReportService, systemToken)
get(reportPath('change-prison'), changePrison.viewPrisons)
post(reportPath('change-prison'), changePrison.submit)
get(reportPath('prison-of-incident'), changePrison.viewPrisons)
post(reportPath('prison-of-incident'), changePrison.submit)

const whyWasUoFApplied = new WhyWasUoFAppliedRoutes(draftReportService)
get(reportPath('why-was-uof-applied'), whyWasUoFApplied.view())
Expand Down
Loading