Skip to content

Commit

Permalink
Merge pull request #27 from ministryofjustice/CBA-10-dashboard-roles
Browse files Browse the repository at this point in the history
Customise dashboard tiles based on user roles
  • Loading branch information
patrickjfl authored Dec 5, 2024
2 parents 77c3c87 + 29fccfb commit 93d8da3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/controllers/dashboardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sectionsForUser } from '../utils/userUtils'
export default class DashboardController {
index(): RequestHandler {
return (_req: Request, res: Response) => {
const sections = sectionsForUser()
const sections = sectionsForUser(res.locals.user.userRoles)

res.render('dashboard/index', {
pageHeading: 'CAS-2: Bail Accommodation',
Expand Down
21 changes: 20 additions & 1 deletion server/utils/userUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@ import { sectionsForUser, sections } from './userUtils'

describe('userUtils', () => {
describe('sectionsForUser', () => {
it('should return an empty array for a user with no roles', () => {
expect(sectionsForUser([])).toEqual([])
})

it('should return correct sections for a POM', () => {
const expected = [sections.applications, sections.newApplication]
expect(sectionsForUser()).toEqual(expected)
expect(sectionsForUser(['POM'])).toEqual(expected)
})

it('should return the prison dashboard for a Licence CA', () => {
const expected = [sections.applications, sections.newApplication]
expect(sectionsForUser(['LICENCE_CA'])).toEqual(expected)
})

it('should return correct sections for an admin', () => {
const expected = [sections.submittedApplications]
expect(sectionsForUser(['CAS2_ADMIN'])).toEqual(expected)
})

it('should return correct sections for a management information user', () => {
const expected = [sections.managementInformationReports]
expect(sectionsForUser(['CAS2_MI'])).toEqual(expected)
})
})
})
14 changes: 11 additions & 3 deletions server/utils/userUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ export const hasRole = (userRoles: Array<string>, role: string): boolean => {
return userRoles.includes(role)
}

export const sectionsForUser = (): Array<ServiceSection> => {
export const sectionsForUser = (userRoles: Array<string>): Array<ServiceSection> => {
const items = []

items.push(sections.applications)
items.push(sections.newApplication)
if (hasRole(userRoles, 'POM') || hasRole(userRoles, 'LICENCE_CA')) {
items.push(sections.applications)
items.push(sections.newApplication)
}
if (hasRole(userRoles, 'CAS2_ADMIN')) {
items.push(sections.submittedApplications)
}
if (hasRole(userRoles, 'CAS2_MI')) {
items.push(sections.managementInformationReports)
}

return Array.from(new Set(items))
}

0 comments on commit 93d8da3

Please sign in to comment.