Skip to content

Commit

Permalink
Get maxTotalVisitors into session and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpmcgowan committed Apr 16, 2024
1 parent 3a107e2 commit 368b942
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 43 deletions.
1 change: 1 addition & 0 deletions server/@types/bapv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export type VisitsReviewListItem = {

export interface Prison extends PrisonName {
policyNoticeDaysMin: number
maxTotalVisitors: number
}

export type FilterField = { id: string; label: string; items: { label: string; value: string; checked: boolean }[] }
32 changes: 26 additions & 6 deletions server/@types/orchestration-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,10 @@ export interface components {
visitTimeSlot: components['schemas']['SessionTimeSlotDto']
}
PageVisitDto: {
/** Format: int32 */
totalPages?: number
/** Format: int64 */
totalElements?: number
first?: boolean
last?: boolean
/** Format: int32 */
totalPages?: number
/** Format: int32 */
size?: number
content?: components['schemas']['VisitDto'][]
Expand All @@ -645,16 +643,18 @@ export interface components {
/** Format: int32 */
numberOfElements?: number
pageable?: components['schemas']['PageableObject']
first?: boolean
last?: boolean
empty?: boolean
}
PageableObject: {
/** Format: int64 */
offset?: number
sort?: components['schemas']['SortObject'][]
/** Format: int32 */
pageSize?: number
/** Format: int32 */
pageNumber?: number
/** Format: int32 */
pageSize?: number
paged?: boolean
unpaged?: boolean
}
Expand Down Expand Up @@ -1082,6 +1082,26 @@ export interface components {
* @example 28
*/
policyNoticeDaysMax: number
/**
* Format: int32
* @description Max number of total visitors
*/
maxTotalVisitors: number
/**
* Format: int32
* @description Max number of adults
*/
maxAdultVisitors: number
/**
* Format: int32
* @description Max number of children
*/
maxChildVisitors: number
/**
* Format: int32
* @description Age of adults in years
*/
adultAgeYears: number
/** @description exclude dates */
excludeDates: string[]
}
Expand Down
34 changes: 25 additions & 9 deletions server/middleware/populateSelectedEstablishment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const supportedPrisonsService = createMockSupportedPrisonsService()

const supportedPrisons = TestData.supportedPrisons()
supportedPrisonsService.getSupportedPrisons.mockResolvedValue(supportedPrisons)
supportedPrisonsService.getPolicyNoticeDaysMin.mockResolvedValue(2)
supportedPrisonsService.getPrisonConfig.mockResolvedValue({ maxTotalVisitors: 6, policyNoticeDaysMin: 2 })

let req: Request
const res = {
Expand All @@ -36,7 +36,7 @@ describe('populateSelectedEstablishment', () => {

res.locals = {
selectedEstablishment: <Prison>undefined,
user: <Express.User>{ activeCaseLoadId: 'HEI' },
user: <Express.User>{ activeCaseLoadId: 'HEI', username: 'user1' },
}
})

Expand All @@ -51,14 +51,15 @@ describe('populateSelectedEstablishment', () => {
const expectedEstablishment: Prison = {
prisonId: 'BLI',
prisonName: supportedPrisons.BLI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

await populateSelectedEstablishment(supportedPrisonsService)(req, res, next)
await new Promise(process.nextTick)

expect(supportedPrisonsService.getSupportedPrisons).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getPolicyNoticeDaysMin).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getSupportedPrisons).toHaveBeenCalledWith('user1')
expect(supportedPrisonsService.getPrisonConfig).toHaveBeenCalledWith('user1', 'BLI')
expect(req.session.selectedEstablishment).toStrictEqual(expectedEstablishment)
expect(res.locals.selectedEstablishment).toStrictEqual(expectedEstablishment)
expect(next).toHaveBeenCalled()
Expand All @@ -69,7 +70,7 @@ describe('populateSelectedEstablishment', () => {

await populateSelectedEstablishment(supportedPrisonsService)(req, res, next)

expect(supportedPrisonsService.getSupportedPrisons).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getSupportedPrisons).toHaveBeenCalledWith('user1')
expect(res.redirect).toHaveBeenCalledWith('/change-establishment')
expect(req.session.selectedEstablishment).toBe(undefined)
expect(res.locals.selectedEstablishment).toBe(undefined)
Expand All @@ -81,7 +82,7 @@ describe('populateSelectedEstablishment', () => {

await populateSelectedEstablishment(supportedPrisonsService)(req, res, next)

expect(supportedPrisonsService.getSupportedPrisons).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getSupportedPrisons).toHaveBeenCalledWith('user1')
expect(req.session.selectedEstablishment).toBe(undefined)
expect(res.locals.selectedEstablishment).toBe(undefined)
expect(res.redirect).toHaveBeenCalledWith('/change-establishment')
Expand All @@ -103,7 +104,12 @@ describe('populateSelectedEstablishment', () => {

describe('when establishment already set in session', () => {
it('should populate res.locals with selected establishment without prisons lookup', async () => {
req.session.selectedEstablishment = { prisonId: 'HEI', prisonName: supportedPrisons.HEI, policyNoticeDaysMin: 2 }
req.session.selectedEstablishment = {
prisonId: 'HEI',
prisonName: supportedPrisons.HEI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

await populateSelectedEstablishment(supportedPrisonsService)(req, res, next)

Expand All @@ -114,7 +120,12 @@ describe('populateSelectedEstablishment', () => {

it('should populate res.locals with already selected establishment without prisons lookup if active caseload changes', async () => {
res.locals.user.activeCaseLoadId = 'BLI'
req.session.selectedEstablishment = { prisonId: 'HEI', prisonName: supportedPrisons.HEI, policyNoticeDaysMin: 2 }
req.session.selectedEstablishment = {
prisonId: 'HEI',
prisonName: supportedPrisons.HEI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

await populateSelectedEstablishment(supportedPrisonsService)(req, res, next)

Expand All @@ -126,7 +137,12 @@ describe('populateSelectedEstablishment', () => {
it('should make no changes and not redirect if request path is /change-establishment', async () => {
req.path = '/change-establishment'
res.locals.user.activeCaseLoadId = 'BLI'
req.session.selectedEstablishment = { prisonId: 'HEI', prisonName: supportedPrisons.HEI, policyNoticeDaysMin: 2 }
req.session.selectedEstablishment = {
prisonId: 'HEI',
prisonName: supportedPrisons.HEI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

await populateSelectedEstablishment(supportedPrisonsService)(req, res, next)

Expand Down
3 changes: 2 additions & 1 deletion server/middleware/populateSelectedEstablishment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export default function populateSelectedEstablishment(
return res.redirect('/change-establishment')
}

const policyNoticeDaysMin = await supportedPrisonsService.getPolicyNoticeDaysMin(
const { maxTotalVisitors, policyNoticeDaysMin } = await supportedPrisonsService.getPrisonConfig(
res.locals.user.username,
activeCaseLoadId,
)

req.session.selectedEstablishment = {
prisonId: activeCaseLoadId,
prisonName: supportedPrisons[activeCaseLoadId],
maxTotalVisitors,
policyNoticeDaysMin,
}
}
Expand Down
21 changes: 18 additions & 3 deletions server/middleware/sessionCheckMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ describe('sessionCheckMiddleware', () => {
save: jest.fn(),
touch: jest.fn(),
cookie: new Cookie(),
selectedEstablishment: { prisonId, prisonName: supportedPrisons[prisonId], policyNoticeDaysMin: 2 },
selectedEstablishment: {
prisonId,
prisonName: supportedPrisons[prisonId],
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
},
},
}
mockResponse = {
Expand All @@ -77,7 +82,12 @@ describe('sessionCheckMiddleware', () => {
})

it('should redirect to the start page if prisonId in originalVisitSlot (set for update journey) does not match selected establishment', () => {
req.session.selectedEstablishment = { prisonId: 'BLI', prisonName: supportedPrisons.BLI, policyNoticeDaysMin: 2 }
req.session.selectedEstablishment = {
prisonId: 'BLI',
prisonName: supportedPrisons.BLI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}
req.session.visitSessionData = { originalVisitSlot: visitSlot } as VisitSessionData

sessionCheckMiddleware({ stage: 1 })(req as Request, mockResponse as Response, next)
Expand Down Expand Up @@ -242,7 +252,12 @@ describe('sessionCheckMiddleware', () => {
})

it('should redirect to the start page if prisonId in visitSlot does not match selected establishment', () => {
req.session.selectedEstablishment = { prisonId: 'BLI', prisonName: supportedPrisons.BLI, policyNoticeDaysMin: 2 }
req.session.selectedEstablishment = {
prisonId: 'BLI',
prisonName: supportedPrisons.BLI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}
req.session.visitSessionData = {
applicationReference: 'aaa-bbb-ccc',
prisoner: prisonerData,
Expand Down
40 changes: 30 additions & 10 deletions server/routes/changeEstablishment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const supportedPrisons = TestData.supportedPrisons()

beforeEach(() => {
supportedPrisonsService.getSupportedPrisons.mockResolvedValue(supportedPrisons)
supportedPrisonsService.getPolicyNoticeDaysMin.mockResolvedValue(2)
supportedPrisonsService.getPrisonConfig.mockResolvedValue({ maxTotalVisitors: 6, policyNoticeDaysMin: 2 })

userService.getUser.mockResolvedValue(user as UserDetails)
userService.getActiveCaseLoadId.mockResolvedValue('XYZ') // assume user coming with an unsupported case load
Expand Down Expand Up @@ -142,7 +142,12 @@ describe('POST /change-establishment', () => {
beforeEach(() => {
jest.spyOn(visitorUtils, 'clearSession')

selectedEstablishment = { prisonId: 'BLI', prisonName: supportedPrisons.BLI, policyNoticeDaysMin: 2 }
selectedEstablishment = {
prisonId: 'BLI',
prisonName: supportedPrisons.BLI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}
sessionData = { selectedEstablishment } as SessionData
userService.getUserCaseLoadIds.mockResolvedValue(TestData.supportedPrisonIds())

Expand All @@ -164,7 +169,7 @@ describe('POST /change-establishment', () => {
{ location: 'body', msg: 'No prison selected', path: 'establishment', type: 'field', value: '' },
])
expect(visitorUtils.clearSession).toHaveBeenCalledTimes(0)
expect(supportedPrisonsService.getPolicyNoticeDaysMin).not.toHaveBeenCalled()
expect(supportedPrisonsService.getPrisonConfig).not.toHaveBeenCalled()
expect(auditService.changeEstablishment).toHaveBeenCalledTimes(0)
expect(userService.setActiveCaseLoad).not.toHaveBeenCalled()
})
Expand All @@ -182,14 +187,19 @@ describe('POST /change-establishment', () => {
{ location: 'body', msg: 'No prison selected', path: 'establishment', type: 'field', value: 'HEX' },
])
expect(visitorUtils.clearSession).toHaveBeenCalledTimes(0)
expect(supportedPrisonsService.getPolicyNoticeDaysMin).not.toHaveBeenCalled()
expect(supportedPrisonsService.getPrisonConfig).not.toHaveBeenCalled()
expect(auditService.changeEstablishment).toHaveBeenCalledTimes(0)
expect(userService.setActiveCaseLoad).not.toHaveBeenCalled()
})
})

it('should clear session, set selected establishment and redirect to home page', () => {
const newEstablishment: Prison = { prisonId: 'HEI', prisonName: supportedPrisons.HEI, policyNoticeDaysMin: 2 }
const newEstablishment: Prison = {
prisonId: 'HEI',
prisonName: supportedPrisons.HEI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

return request(app)
.post(`/change-establishment`)
Expand All @@ -199,7 +209,7 @@ describe('POST /change-establishment', () => {
.expect(() => {
expect(sessionData.selectedEstablishment).toStrictEqual(newEstablishment)
expect(visitorUtils.clearSession).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getPolicyNoticeDaysMin).toHaveBeenCalledWith('user1', 'HEI')
expect(supportedPrisonsService.getPrisonConfig).toHaveBeenCalledWith('user1', 'HEI')
expect(auditService.changeEstablishment).toHaveBeenCalledWith({
previousEstablishment: 'BLI',
newEstablishment: 'HEI',
Expand All @@ -211,7 +221,12 @@ describe('POST /change-establishment', () => {
})

it('should clear session, set selected establishment and redirect to / not the set referrer', () => {
const newEstablishment: Prison = { prisonId: 'HEI', prisonName: supportedPrisons.HEI, policyNoticeDaysMin: 2 }
const newEstablishment: Prison = {
prisonId: 'HEI',
prisonName: supportedPrisons.HEI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

return request(app)
.post(`/change-establishment?referrer=//search/prisoner/`)
Expand All @@ -221,14 +236,19 @@ describe('POST /change-establishment', () => {
.expect(() => {
expect(sessionData.selectedEstablishment).toStrictEqual(newEstablishment)
expect(visitorUtils.clearSession).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getPolicyNoticeDaysMin).toHaveBeenCalledWith('user1', 'HEI')
expect(supportedPrisonsService.getPrisonConfig).toHaveBeenCalledWith('user1', 'HEI')
expect(auditService.changeEstablishment).toHaveBeenCalledTimes(1)
expect(userService.setActiveCaseLoad).toHaveBeenCalledWith('HEI', 'user1')
})
})

it('should redirect to valid page when passed in querystring', () => {
const newEstablishment: Prison = { prisonId: 'HEI', prisonName: supportedPrisons.HEI, policyNoticeDaysMin: 2 }
const newEstablishment: Prison = {
prisonId: 'HEI',
prisonName: supportedPrisons.HEI,
maxTotalVisitors: 6,
policyNoticeDaysMin: 2,
}

return request(app)
.post(`/change-establishment?referrer=/search/prisoner/`)
Expand All @@ -238,7 +258,7 @@ describe('POST /change-establishment', () => {
.expect(() => {
expect(sessionData.selectedEstablishment).toStrictEqual(newEstablishment)
expect(visitorUtils.clearSession).toHaveBeenCalledTimes(1)
expect(supportedPrisonsService.getPolicyNoticeDaysMin).toHaveBeenCalledWith('user1', 'HEI')
expect(supportedPrisonsService.getPrisonConfig).toHaveBeenCalledWith('user1', 'HEI')
expect(auditService.changeEstablishment).toHaveBeenCalledTimes(1)
expect(userService.setActiveCaseLoad).toHaveBeenCalledWith('HEI', 'user1')
})
Expand Down
3 changes: 2 additions & 1 deletion server/routes/changeEstablishment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function routes({ auditService, supportedPrisonsService, userServ

clearSession(req)

const policyNoticeDaysMin = await supportedPrisonsService.getPolicyNoticeDaysMin(
const { maxTotalVisitors, policyNoticeDaysMin } = await supportedPrisonsService.getPrisonConfig(
res.locals.user.username,
req.body.establishment,
)
Expand All @@ -66,6 +66,7 @@ export default function routes({ auditService, supportedPrisonsService, userServ
const newEstablishment: Prison = {
prisonId: req.body.establishment,
prisonName: availablePrisons[req.body.establishment],
maxTotalVisitors,
policyNoticeDaysMin,
}

Expand Down
7 changes: 5 additions & 2 deletions server/routes/testutils/appSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ class MockSupportedPrisonsService extends SupportedPrisonsService {
return TestData.supportedPrisons()
}

async getPolicyNoticeDaysMin(_username: string, _prisonCode: string): Promise<number> {
return 2
async getPrisonConfig(
_username: string,
_prisonCode: string,
): Promise<{ maxTotalVisitors: number; policyNoticeDaysMin: number }> {
return { maxTotalVisitors: 6, policyNoticeDaysMin: 2 }
}
}

Expand Down
8 changes: 8 additions & 0 deletions server/routes/testutils/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,21 @@ export default class TestData {
active = true,
policyNoticeDaysMax = 28,
policyNoticeDaysMin = 3,
maxTotalVisitors = 6,
maxAdultVisitors = 3,
maxChildVisitors = 3,
adultAgeYears = 18,
excludeDates = [],
}: Partial<PrisonDto> = {}): PrisonDto =>
({
code,
active,
policyNoticeDaysMax,
policyNoticeDaysMin,
maxTotalVisitors,
maxAdultVisitors,
maxChildVisitors,
adultAgeYears,
excludeDates,
}) as PrisonDto

Expand Down
Loading

0 comments on commit 368b942

Please sign in to comment.