Skip to content

Commit

Permalink
PI-2152: Default to team when 1 team and add links when no team (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 authored May 7, 2024
1 parent b66895d commit ae2f445
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 55 deletions.
2 changes: 1 addition & 1 deletion integration_tests/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Page, { PageElement } from './page'

export default class IndexPage extends Page {
constructor() {
super('Manage a Supervision')
super('Your cases')
}

headerUserName = (): PageElement => cy.get('[data-qa=header-user-name]')
Expand Down
1 change: 1 addition & 0 deletions server/@types/express/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export declare global {
interface MasData {
provider?: string
team?: string
teamCount: number
}
}
}
4 changes: 4 additions & 0 deletions server/routes/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import TierApiClient from '../data/tierApiClient'
export default function caseRoutes(router: Router, { hmppsAuthClient }: Services) {
const get = (path: string | string[], handler: RequestHandler) => router.get(path, asyncMiddleware(handler))

get('/', async (_req, res, _next) => {
res.redirect('/case')
})

get('/case/:crn', async (req, res, _next) => {
const { crn } = req.params
const token = await hmppsAuthClient.getSystemClientToken(res.locals.user.username)
Expand Down
50 changes: 34 additions & 16 deletions server/routes/caseload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,32 @@ export default function caseloadRoutes(router: Router, { hmppsAuthClient }: Serv
const masClient = new MasApiClient(token)

if (req.session.mas === undefined || req.session.mas.team === undefined) {
req.session.mas = {}
await auditService.sendAuditMessage({
action: 'VIEW_MAS_TEAMS',
who: res.locals.user.username,
subjectId: res.locals.user.username,
subjectType: 'USER',
correlationId: v4(),
service: 'hmpps-manage-a-supervision-ui',
})
const userTeams = await masClient.getUserTeams(res.locals.user.username)
res.render('pages/caseload/select-team', {
userTeams,
})
req.session.mas = { teamCount: userTeams.teams.length }
switch (userTeams.teams.length) {
case 1: {
req.session.mas.team = userTeams.teams[0].code
res.redirect(`/team/case`)
break
}
case 0: {
res.redirect(`/team/case`)
break
}
default: {
await auditService.sendAuditMessage({
action: 'VIEW_MAS_TEAMS',
who: res.locals.user.username,
subjectId: res.locals.user.username,
subjectType: 'USER',
correlationId: v4(),
service: 'hmpps-manage-a-supervision-ui',
})
res.render('pages/caseload/select-team', {
userTeams,
})
}
}
} else {
res.redirect(`/team/case`)
}
Expand Down Expand Up @@ -80,10 +93,11 @@ export default function caseloadRoutes(router: Router, { hmppsAuthClient }: Serv
get('/team/case', async (req, res, _next) => {
const token = await hmppsAuthClient.getSystemClientToken(res.locals.user.username)
const masClient = new MasApiClient(token)
if (req.session.mas === undefined || req.session.mas.team === undefined) {
if (req.session.mas === undefined || (req.session.mas.teamCount > 0 && req.session.mas.team === undefined)) {
res.redirect('/teams')
} else {
const teamCode = req.session.mas.team
const { teamCount } = req.session.mas
await auditService.sendAuditMessage({
action: 'VIEW_MAS_CASELOAD_TEAM',
who: res.locals.user.username,
Expand All @@ -94,7 +108,10 @@ export default function caseloadRoutes(router: Router, { hmppsAuthClient }: Serv
})
const pageNum: number = req.query.page ? Number.parseInt(req.query.page as string, 10) : 1
const currentNavSection = 'teamCases'
const caseload = await masClient.getTeamCaseload(teamCode, (pageNum - 1).toString())
const caseload =
teamCount > 0
? await masClient.getTeamCaseload(teamCode, (pageNum - 1).toString())
: { totalPages: 0, totalElements: 0, pageSize: 0 }

const pagination: Pagination = getPaginationLinks(
req.query.page ? Number.parseInt(req.query.page as string, 10) : 1,
Expand All @@ -107,14 +124,14 @@ export default function caseloadRoutes(router: Router, { hmppsAuthClient }: Serv
pagination,
caseload,
currentNavSection,
teamCount,
})
}
})

post('/team/case', async (req, res, _next) => {
const token = await hmppsAuthClient.getSystemClientToken(res.locals.user.username)
const masClient = new MasApiClient(token)
req.session.mas.team = req.body['team-code']
const errorMessages: ErrorMessages = {}
if (req.body['team-code'] == null) {
logger.info('Team not selected')
Expand All @@ -125,7 +142,8 @@ export default function caseloadRoutes(router: Router, { hmppsAuthClient }: Serv
userTeams,
})
} else {
res.redirect(`/team/case`)
req.session.mas.team = req.body['team-code']
res.redirect(`/teams`)
}
})
}
24 changes: 0 additions & 24 deletions server/routes/index.test.ts

This file was deleted.

2 changes: 0 additions & 2 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Router } from 'express'

import type { Services } from '../services'

import startRoutes from './start'
import searchRoutes from './search'
import caseRoutes from './case'
import personalDetailRoutes from './personalDetails'
Expand All @@ -15,7 +14,6 @@ import caseloadRoutes from './caseload'

export default function routes(services: Services): Router {
const router = Router()
startRoutes(router)
searchRoutes(router, services)
caseRoutes(router, services)
personalDetailRoutes(router, services)
Expand Down
10 changes: 0 additions & 10 deletions server/routes/start.ts

This file was deleted.

13 changes: 11 additions & 2 deletions server/views/pages/caseload/minimal-cases.njk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<p data-qa="change-team">
Showing:
<span class="govuk-body govuk-!-font-weight-bold" data-qa="caseload-names">{{ caseload.team.description }}</span>
<a class="govuk-link govuk-!-margin-left-3" href="/change-team" data-qa="change-team-link">Change team</a>
{% if teamCount > 1 %}
<a class="govuk-link govuk-!-margin-left-3" href="/change-team" data-qa="change-team-link">Change team</a>
{% endif %}
</p>
</div>
{% endif %}
Expand Down Expand Up @@ -48,6 +50,13 @@
{{ govukPagination({ previous: { href: pagination.prev }, next: { href: pagination.next }, items: pagination.items }) }}
{% endif %}
{% else %}
<p>No cases to show</p>
{% if currentNavSection == 'teamCases' and teamCount == 0 %}
<p>You are not currently assigned to any team</p>
{% elseif currentNavSection == 'teamCases' and teamCount > 0 %}
<p>{{ caseload.team.description }} has not been allocated to any cases</p>
{% elseif currentNavSection == 'yourCases' %}
<p>You have not been allocated to any cases</p>
{% endif %}
<p><a href="/search" target="_blank">Find a person on probation</a></br>
{% endif %}
{% endblock %}

0 comments on commit ae2f445

Please sign in to comment.