From 1596edc89f56a291d91f67556fdcd5ebb05d9605 Mon Sep 17 00:00:00 2001 From: Ed Davey Date: Thu, 4 Jul 2024 14:14:19 +0100 Subject: [PATCH] Provide link to v2 'Manage (bed)' page to FUTURE_MANAGER only --- server/utils/bedUtils.test.ts | 26 ++++++++++++++++++++++++-- server/utils/bedUtils.ts | 30 ++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/server/utils/bedUtils.test.ts b/server/utils/bedUtils.test.ts index 65576e142c..4a02eea82c 100644 --- a/server/utils/bedUtils.test.ts +++ b/server/utils/bedUtils.test.ts @@ -1,3 +1,4 @@ +import { UserDetails } from '@approved-premises/ui' import paths from '../paths/manage' import { apCharacteristicPairFactory, @@ -13,7 +14,6 @@ import { actionCell, bedActions, bedDetails, - bedLink, bedNameCell, bedTableRows, characteristicsRow, @@ -24,6 +24,8 @@ import { statusCell, statusRow, title, + v1BedLink, + v2BedLink, } from './bedUtils' import { DateFormats } from './dateUtils' @@ -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), }) }) }) diff --git a/server/utils/bedUtils.ts b/server/utils/bedUtils.ts index 262079a13e..f742f0fc40 100644 --- a/server/utils/bedUtils.ts +++ b/server/utils/bedUtils.ts @@ -1,3 +1,5 @@ +import { v1 } from 'uuid' +import { path } from 'static-path' import { BedDetail, BedSummary } from '../@types/shared' import { BedOccupancyBookingEntryUi, @@ -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 => { @@ -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 }, { @@ -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)