Skip to content

Commit

Permalink
Remove unused code referencing bed search
Browse files Browse the repository at this point in the history
We also rename BedSearchController to SpaceSearchController to avoid
confusion.
  • Loading branch information
froddd committed Jan 9, 2025
1 parent 5492e9b commit a64b6e3
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import { DeepMocked, createMock } from '@golevelup/ts-jest'
import BookingsController from './bookingsController'

import { PlacementRequestService } from '../../../services'
import {
bedSearchResultFactory,
newPlacementRequestBookingConfirmationFactory,
personFactory,
placementRequestDetailFactory,
} from '../../../testutils/factories'
import { encodeSpaceSearchResult, placementDates } from '../../../utils/match'
import { NewBookingNotMade, type Cas1SpaceSearchResult as SpaceSearchResult } from '../../../@types/shared'
import { NewBookingNotMade } from '../../../@types/shared'

import matchPaths from '../../../paths/match'
import adminPaths from '../../../paths/admin'
Expand All @@ -32,62 +25,6 @@ describe('BookingsController', () => {
bookingsController = new BookingsController(placementRequestService)
})

describe('confirm', () => {
it('should render the confirmation template', async () => {
const person = personFactory.build({ name: 'John Wayne' })
const placementRequestDetail = placementRequestDetailFactory.build({ person })
const bedSearchResult = bedSearchResultFactory.build()

placementRequestService.getPlacementRequest.mockResolvedValue(placementRequestDetail)

const query = {
bedSearchResult: encodeSpaceSearchResult(bedSearchResult as unknown as SpaceSearchResult),
startDate: '2022-01-01',
duration: '4',
}

const params = { id: placementRequestDetail.id }

const requestHandler = bookingsController.confirm()

await requestHandler({ ...request, params, query }, response, next)

expect(response.render).toHaveBeenCalledWith('match/placementRequests/bookings/confirm', {
pageHeading: 'Confirm booking',
placementRequest: placementRequestDetail,
bedSearchResult,
dates: placementDates(query.startDate, query.duration),
})
expect(placementRequestService.getPlacementRequest).toHaveBeenCalledWith(token, placementRequestDetail.id)
})
})

describe('create', () => {
it('should create a booking and render a success page', async () => {
const bookingConfirmation = newPlacementRequestBookingConfirmationFactory.build()

placementRequestService.createBooking.mockResolvedValue(bookingConfirmation)

const body = {
arrivalDate: '2022-01-01',
departureDate: '2022-03-01',
bedId: 'some-other-uuid',
}

const params = { id: 'some-uuid' }

const requestHandler = bookingsController.create()

await requestHandler({ ...request, params, body }, response, next)

expect(response.render).toHaveBeenCalledWith('match/placementRequests/bookings/success', {
pageHeading: 'Your booking is complete',
bookingConfirmation,
})
expect(placementRequestService.createBooking).toHaveBeenCalledWith(token, 'some-uuid', body)
})
})

describe('bookingNotMade', () => {
it('should render the booking not made confirmation template', async () => {
const placementRequestId = '123'
Expand Down
46 changes: 0 additions & 46 deletions server/controllers/match/placementRequests/bookingsController.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
import type { Request, Response, TypedRequestHandler } from 'express'
import { decodeSpaceSearchResult, placementDates } from '../../../utils/match'
import { PlacementRequestService } from '../../../services'
import matchPaths from '../../../paths/match'
import adminPaths from '../../../paths/admin'

interface ConfirmRequest extends Request {
params: { id: string }
query: { startDate: string; duration: string; bedSearchResult: string }
}

interface CreateRequest extends Request {
params: { id: string }
body: { arrivalDate: string; departureDate: string; bedId: string }
}

export default class BookingsController {
constructor(private readonly placementRequestService: PlacementRequestService) {}

confirm(): TypedRequestHandler<Request, Response> {
return async (req: ConfirmRequest, res: Response) => {
const placementRequest = await this.placementRequestService.getPlacementRequest(req.user.token, req.params.id)
const bedSearchResult = decodeSpaceSearchResult(req.query.bedSearchResult)

res.render('match/placementRequests/bookings/confirm', {
pageHeading: 'Confirm booking',
placementRequest,
bedSearchResult,
dates: placementDates(req.query.startDate, req.query.duration),
})
}
}

create(): TypedRequestHandler<Request, Response> {
return async (req: CreateRequest, res: Response) => {
const newPlacementRequestBooking = {
arrivalDate: req.body.arrivalDate,
departureDate: req.body.departureDate,
bedId: req.body.bedId,
}

const bookingConfirmation = await this.placementRequestService.createBooking(
req.user.token,
req.params.id,
newPlacementRequestBooking,
)

res.render('match/placementRequests/bookings/success', {
pageHeading: 'Your booking is complete',
bookingConfirmation,
})
}
}

bookingNotMade(): TypedRequestHandler<Request, Response> {
return async (req: Request, res: Response) => {
const confirmPath = matchPaths.placementRequests.bookingNotMade.create({ id: req.params.id })
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/match/search/spaceSearchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SpaceService from '../../../services/spaceService'
import { startDateObjFromParams } from '../../../utils/match'
import { objectIfNotEmpty } from '../../../utils/utils'

export default class BedSearchController {
export default class SpaceSearchController {
constructor(
private readonly spaceService: SpaceService,
private readonly placementRequestService: PlacementRequestService,
Expand Down
8 changes: 3 additions & 5 deletions server/paths/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const v2Match = {

const placementRequestsPath = path('/placement-requests')
const placementRequestPath = placementRequestsPath.path(':id')
const placementRequestBookingsPath = placementRequestPath.path('bookings')
const newPlacementRequestPath = placementRequestsPath.path('new')
const bookingNotMadePath = placementRequestPath.path('booking-not-made')

Expand All @@ -32,10 +31,9 @@ export default {
index: placementRequestsPath,
show: placementRequestPath,
create: newPlacementRequestPath,
bookingNotMade: { confirm: bookingNotMadePath.path('confirm'), create: bookingNotMadePath },
bookings: {
confirm: placementRequestBookingsPath.path('confirm'),
create: placementRequestBookingsPath,
bookingNotMade: {
confirm: bookingNotMadePath.path('confirm'),
create: bookingNotMadePath,
},
},
v2Match,
Expand Down
5 changes: 0 additions & 5 deletions server/routes/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ export default function routes(controllers: Controllers, router: Router, service
auditEvent: 'SPACE_SEARCH',
})

get(paths.placementRequests.bookings.confirm.pattern, placementRequestBookingsController.confirm())
post(paths.placementRequests.bookings.create.pattern, placementRequestBookingsController.create(), {
auditEvent: 'CREATE_BOOKING_FROM_PLACEMENT_REQUEST',
})

get(paths.v2Match.placementRequests.spaceBookings.new.pattern, spaceBookingsController.new(), {
auditEvent: 'NEW_SPACE_BOOKING',
})
Expand Down
10 changes: 8 additions & 2 deletions server/testutils/factories/bedDetail.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Factory } from 'fishery'
import { faker } from '@faker-js/faker/locale/en_GB'

import type { BedDetail } from '@approved-premises/api'
import { apCharacteristicPairFactory } from './bedSearchResult'
import { BedDetail, CharacteristicPair } from '@approved-premises/api'
import { placementCriteria } from './placementRequest'

export const apCharacteristicPairFactory = Factory.define<CharacteristicPair>(() => ({
name: faker.lorem.sentence(),
propertyName: faker.helpers.arrayElement(placementCriteria),
premises: faker.company.name(),
}))

export default Factory.define<BedDetail>(() => ({
id: faker.string.uuid(),
Expand Down
54 changes: 0 additions & 54 deletions server/testutils/factories/bedSearchResult.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { faker } from '@faker-js/faker/locale/en_GB'
import type { Cas1PremisesSearchResultSummary, Cas1SpaceCharacteristic } from '@approved-premises/api'
import namedIdFactory from './namedId'
import { sentenceCase } from '../../utils/utils'
import { apCharacteristicPairFactory } from './bedSearchResult'
import { apCharacteristicPairFactory } from './bedDetail'

const characteristics: ReadonlyArray<Cas1SpaceCharacteristic> = [
'acceptsChildSexOffenders',
Expand Down
5 changes: 1 addition & 4 deletions server/testutils/factories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import assessmentTaskFactory from './assessmentTask'
import assessmentFactory from './assessment'
import assessmentSummaryFactory from './assessmentSummary'
import bedSummaryFactory from './bedSummary'
import bedDetailFactory from './bedDetail'
import { apCharacteristicPairFactory, bedSearchResultFactory, bedSearchResultsFactory } from './bedSearchResult'
import bedDetailFactory, { apCharacteristicPairFactory } from './bedDetail'
import bookingAppealTask from './bookingAppealTask'
import bookingSummaryFactory from './bookingSummary'
import bookingFactory from './booking'
Expand Down Expand Up @@ -112,8 +111,6 @@ export {
bedSummaryFactory,
bedDetailFactory,
bookingSummaryFactory,
bedSearchResultFactory,
bedSearchResultsFactory,
bookingAppealTask,
bookingFactory,
bookingExtensionFactory,
Expand Down
22 changes: 1 addition & 21 deletions server/testutils/factories/room.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import { Factory } from 'fishery'
import { faker } from '@faker-js/faker/locale/en_GB'

import type { Bed, Characteristic, CharacteristicPair } from '@approved-premises/api'

export const roomCharacteristicPairFactory = Factory.define<CharacteristicPair>(() => ({
name: faker.helpers.arrayElement([
'acceptsSexOffenders',
'acceptsChildSexOffenders',
'acceptsNonSexualChildOffenders',
'acceptsHateCrimeOffenders',
'isCatered',
'hasWideStepFreeAccess',
'hasWideAccessToCommunalAreas',
'hasStepFreeAccessToCommunalAreas',
'hasWheelChairAccessibleBathrooms',
'hasLift',
'hasTactileFlooring',
'hasBrailleSignage',
'hasHearingLoop',
'additionalRestrictions',
]),
premises: faker.company.name(),
}))
import type { Bed, Characteristic } from '@approved-premises/api'

export const bedFactory = Factory.define<Bed>(() => ({
id: faker.string.uuid(),
Expand Down
28 changes: 0 additions & 28 deletions server/utils/match/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from '../../testutils/factories'
import { DateFormats } from '../dateUtils'
import {
InvalidSpaceSearchDataException,
addressRow,
apManagerDetailsRow,
apTypeLabelsForRadioInput,
Expand All @@ -28,11 +27,9 @@ import {
characteristicsRow,
checkBoxesForCriteria,
confirmationSummaryCardRows,
decodeSpaceSearchResult,
departureDateRow,
desirableCharacteristicsRow,
distanceRow,
encodeSpaceSearchResult,
essentialCharacteristicsRow,
filterOutAPTypes,
filterToSpaceBookingCharacteristics,
Expand Down Expand Up @@ -223,31 +220,6 @@ describe('matchUtils', () => {
})
})

describe('encodeSpaceSearchResult', () => {
it('encodes a space search result to Base64', () => {
const spaceSearchResult = spaceSearchResultFactory.build()

expect(encodeSpaceSearchResult(spaceSearchResult)).toEqual(
Buffer.from(JSON.stringify(spaceSearchResult)).toString('base64'),
)
})
})

describe('decodeSpaceSearchResult', () => {
it('decodes a Base64 encoded space search result', () => {
const spaceSearchResult = spaceSearchResultFactory.build()
const encodedResult = encodeSpaceSearchResult(spaceSearchResult)

expect(decodeSpaceSearchResult(encodedResult)).toEqual(spaceSearchResult)
})

it('throws an error if the object is not a space search result', () => {
const obj = Buffer.from('{"foo":"bar"}').toString('base64')

expect(() => decodeSpaceSearchResult(obj)).toThrowError(InvalidSpaceSearchDataException)
})
})

describe('placementLength', () => {
it('formats the number of days as weeks', () => {
expect(placementLength(16)).toEqual('2 weeks, 2 days')
Expand Down
19 changes: 0 additions & 19 deletions server/utils/match/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ type PlacementDates = {
endDate: string
}

export class InvalidSpaceSearchDataException extends Error {}

export type SearchFilterCategories = 'apType' | 'offenceAndRisk' | 'placementRequirements'

export const mapUiParamsForApi = (query: SpaceSearchParametersUi): SpaceSearchParameters => {
Expand All @@ -63,23 +61,6 @@ export const mapUiParamsForApi = (query: SpaceSearchParametersUi): SpaceSearchPa
}
}

export const encodeSpaceSearchResult = (spaceSearchResult: SpaceSearchResult): string => {
const json = JSON.stringify(spaceSearchResult)

return Buffer.from(json).toString('base64')
}

export const decodeSpaceSearchResult = (string: string): SpaceSearchResult => {
const json = Buffer.from(string, 'base64').toString('utf-8')
const obj = JSON.parse(json)

if ('premises' in obj) {
return obj as SpaceSearchResult
}

throw new InvalidSpaceSearchDataException()
}

export const placementLength = (lengthInDays: number): string => {
return DateFormats.formatDuration(daysToWeeksAndDays(lengthInDays), ['weeks', 'days'])
}
Expand Down
Loading

0 comments on commit a64b6e3

Please sign in to comment.