Skip to content

Commit

Permalink
Provide link to v2 'Manage (bed)' page to FUTURE_MANAGER only
Browse files Browse the repository at this point in the history
  • Loading branch information
edavey committed Jul 4, 2024
1 parent 0ad1c86 commit 1596edc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
26 changes: 24 additions & 2 deletions server/utils/bedUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserDetails } from '@approved-premises/ui'
import paths from '../paths/manage'
import {
apCharacteristicPairFactory,
Expand All @@ -13,7 +14,6 @@ import {
actionCell,
bedActions,
bedDetails,
bedLink,
bedNameCell,
bedTableRows,
characteristicsRow,
Expand All @@ -24,6 +24,8 @@ import {
statusCell,
statusRow,
title,
v1BedLink,
v2BedLink,
} from './bedUtils'
import { DateFormats } from './dateUtils'

Expand Down Expand Up @@ -65,12 +67,32 @@ describe('bedUtils', () => {
})

describe('actionCell', () => {
describe('when the user has the FUTURE_MANAGER role', () => {
const user = userDetailsFactory.build({ roles: ['future_manager'] })

it('returns a "V2" link to manage the room', () => {
expect(actionCell(bed, premisesId, user)).toEqual({
html: v2BedLink(bed, premisesId),
})
})
})

describe('when the user does NOT have the FUTURE_MANAGER role', () => {
const user = userDetailsFactory.build({ roles: ['manager'] })

it('returns a "V1" link to manage the room', () => {
expect(actionCell(bed, premisesId, user)).toEqual({
html: bedLink(bed, premisesId),
html: v1BedLink(bed, premisesId),
})
})
})

describe('when no user is given', () => {
const nullUser: UserDetails = null

it('returns a "V1" link to manage the room', () => {
expect(actionCell(bed, premisesId, nullUser)).toEqual({
html: v1BedLink(bed, premisesId),
})
})
})
Expand Down
30 changes: 26 additions & 4 deletions server/utils/bedUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { v1 } from 'uuid'
import { path } from 'static-path'
import { BedDetail, BedSummary } from '../@types/shared'
import {
BedOccupancyBookingEntryUi,
Expand All @@ -24,8 +26,8 @@ export const roomNameCell = (item: { roomName: string }): TableCell => ({ text:

export const statusCell = (bed: BedSummary): TableCell => ({ text: sentenceCase(bed.status) })

export const actionCell = (bed: BedSummary, premisesId: string, _user?: UserDetails): TableCell => ({
html: bedLink(bed, premisesId),
export const actionCell = (bed: BedSummary, premisesId: string, user?: UserDetails): TableCell => ({
html: bedLinkForUser(bed, premisesId, user),
})

export const bedDetails = (bed: BedDetail): Array<SummaryListItem> => {
Expand Down Expand Up @@ -66,8 +68,15 @@ export const bedActions = (bed: BedDetail, premisesId: string) => {
}
}

export const bedLink = (bed: BedSummary, premisesId: string): string =>
linkTo(
const bedLinkForUser = (bed: BedSummary, premisesId: string, user?: UserDetails): string => {
if (user && user.roles?.includes('future_manager')) {
return v2BedLink(bed, premisesId)
}
return v1BedLink(bed, premisesId)
}

export const v1BedLink = (bed: BedSummary, premisesId: string): string => {
return linkTo(
paths.premises.beds.show,
{ bedId: bed.id, premisesId },
{
Expand All @@ -76,6 +85,19 @@ export const bedLink = (bed: BedSummary, premisesId: string): string =>
attributes: { 'data-cy-bedId': bed.id },
},
)
}

export const v2BedLink = (bed: BedSummary, premisesId: string): string => {
return linkTo(
paths.v2Manage.premises.beds.show,
{ bedId: bed.id, premisesId },
{
text: 'Manage',
hiddenText: `bed ${bed.name}`,
attributes: { 'data-cy-bedId': bed.id },
},
)
}

export const encodeOverbooking = (overbooking: BedOccupancyOverbookingEntryUi): string => {
const json = JSON.stringify(overbooking)
Expand Down

0 comments on commit 1596edc

Please sign in to comment.