From f26393925a12f1475044aeecc0db02e827f0c6f0 Mon Sep 17 00:00:00 2001 From: Rob Booth Date: Mon, 30 Dec 2024 09:28:26 +0000 Subject: [PATCH] Implement logic to obtain out of service bed information Amended existing findOutOfServiceBedIds query, renamed to findOutOfServiceBedIdForDates to fetch by optionally provided date Incorporate oosbeds into the premise day summary --- .../controller/cas1/Cas1PremisesController.kt | 54 +++-- .../jpa/entity/Cas1OutOfServiceBedEntity.kt | 9 +- .../cas1/Cas1OutOfServiceBedService.kt | 35 +++- .../cas1/Cas1OutOfServiceBedSummaryService.kt | 43 ++++ .../Cas1OutOfServiceBedSummaryTransformer.kt | 25 +++ .../cas1/Cas1PremisesDayTransformer.kt | 10 +- src/main/resources/static/cas1-schemas.yml | 2 + .../static/codegen/built-cas1-api-spec.yml | 2 + .../integration/cas1/Cas1PremisesTest.kt | 196 +++++++++++++++++- .../cas1/Cas1OutOfServiceBedServiceTest.kt | 16 +- .../Cas1OutOfServiceBedSummaryServiceTest.kt | 94 +++++++++ ...s1OutOfServiceBedSummaryTransformerTest.kt | 87 ++++++++ .../cas1/Cas1PremisesDayTransformerTest.kt | 47 ++--- 13 files changed, 538 insertions(+), 82 deletions(-) create mode 100644 src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedSummaryService.kt create mode 100644 src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1OutOfServiceBedSummaryTransformer.kt create mode 100644 src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedSummaryServiceTest.kt create mode 100644 src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1OutOfServiceBedSummaryTransformerTest.kt diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt index 7ce6a82853..a0eaa995ff 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt @@ -14,8 +14,10 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SortDirection import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesGender import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserPermission import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserAccessService +import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1OutOfServiceBedSummaryService import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1SpaceBookingDaySummaryService +import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1OutOfServiceBedSummaryTransformer import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1PremiseCapacitySummaryTransformer import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1PremisesDayTransformer import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1PremisesTransformer @@ -31,6 +33,8 @@ class Cas1PremisesController( val cas1PremiseCapacityTransformer: Cas1PremiseCapacitySummaryTransformer, private val cas1PremisesDayTransformer: Cas1PremisesDayTransformer, private val cas1SpaceBookingDaySummaryService: Cas1SpaceBookingDaySummaryService, + private val cas1OutOfServiceBedSummaryService: Cas1OutOfServiceBedSummaryService, + private val cas1OutOfServiceBedSummaryTransformer: Cas1OutOfServiceBedSummaryTransformer, ) : PremisesCas1Delegate { override fun getPremisesById(premisesId: UUID): ResponseEntity { @@ -96,30 +100,42 @@ class Cas1PremisesController( bookingsSortDirection: SortDirection?, bookingsSortBy: Cas1SpaceBookingDaySummarySortField?, ): ResponseEntity { - userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_SPACE_BOOKING_VIEW) - - val premiseSummaryInfo = cas1PremisesService.getPremisesSummary(premisesId) - val premiseCapacity = cas1PremisesService.getPremiseCapacity(premisesId, date, date, null) - val premisesCapacitySummary = cas1PremiseCapacityTransformer.toCas1PremiseCapacitySummary( - premiseSummaryInfo = extractEntityFromCasResult(premiseSummaryInfo), - premiseCapacity = extractEntityFromCasResult(premiseCapacity), - ) + userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW) - val spaceBookingDaySummaries = extractEntityFromCasResult( - cas1SpaceBookingDaySummaryService.getBookingDaySummaries( - premisesId = premisesId, - date = date, - bookingsCriteriaFilter = bookingsCriteriaFilter, - bookingsSortBy = bookingsSortBy ?: Cas1SpaceBookingDaySummarySortField.PERSON_NAME, - bookingsSortDirection = bookingsSortDirection ?: SortDirection.desc, - ), + val premiseSummaryInfo = extractEntityFromCasResult( + cas1PremisesService.getPremisesSummary(premisesId), ) return ResponseEntity.ok().body( cas1PremisesDayTransformer.toCas1PremisesDaySummary( - date, - premisesCapacitySummary, - spaceBookingDaySummaries, + date = date, + premisesCapacity = cas1PremiseCapacityTransformer.toCas1PremiseCapacitySummary( + premiseSummaryInfo = premiseSummaryInfo, + premiseCapacity = extractEntityFromCasResult( + cas1PremisesService.getPremiseCapacity( + premisesId = premisesId, + startDate = date, + endDate = date, + null, + ), + ), + ).capacity.first(), + spaceBookings = extractEntityFromCasResult( + cas1SpaceBookingDaySummaryService.getBookingDaySummaries( + premisesId = premisesId, + date = date, + bookingsCriteriaFilter = bookingsCriteriaFilter, + bookingsSortBy = bookingsSortBy ?: Cas1SpaceBookingDaySummarySortField.PERSON_NAME, + bookingsSortDirection = bookingsSortDirection ?: SortDirection.desc, + ), + ), + outOfServiceBeds = extractEntityFromCasResult( + cas1OutOfServiceBedSummaryService.getOutOfServiceBedSummaries( + premisesId = premisesId, + apAreaId = premiseSummaryInfo.entity.probationRegion.apArea!!.id, + date = date, + ), + ).map(cas1OutOfServiceBedSummaryTransformer::toCas1OutOfServiceBedSummary), ), ) } diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/Cas1OutOfServiceBedEntity.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/Cas1OutOfServiceBedEntity.kt index 678655774d..41cd58a7d4 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/Cas1OutOfServiceBedEntity.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/Cas1OutOfServiceBedEntity.kt @@ -52,9 +52,9 @@ interface Cas1OutOfServiceBedRepository : JpaRepository= CURRENT_DATE) AND - (FALSE = :excludeCurrent OR CURRENT_DATE NOT BETWEEN dd.start_date AND dd.end_date) AND - (FALSE = :excludeFuture OR dd.start_date <= CURRENT_DATE) AND + (FALSE = :excludePast OR dd.end_date >= :date) AND + (FALSE = :excludeCurrent OR :date NOT BETWEEN dd.start_date AND dd.end_date) AND + (FALSE = :excludeFuture OR dd.start_date <= :date) AND (oosb_cancellations IS NULL) """ } @@ -70,12 +70,13 @@ interface Cas1OutOfServiceBedRepository : JpaRepository diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedService.kt index 84fd25be32..5bb8b33b1b 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedService.kt @@ -257,10 +257,11 @@ class Cas1OutOfServiceBedService( return success(cancellationEntity) } - fun getOutOfServiceBeds( + fun getOutOfServiceBedsForDate( temporality: Set, premisesId: UUID?, apAreaId: UUID?, + date: LocalDate, pageCriteria: PageCriteria, ): Pair, PaginationMetadata?> { val sortFieldString = when (pageCriteria.sortBy) { @@ -277,13 +278,14 @@ class Cas1OutOfServiceBedService( val excludeCurrent = !temporality.contains(Temporality.current) val excludeFuture = !temporality.contains(Temporality.future) - val page = outOfServiceBedRepository.findOutOfServiceBedIds( - premisesId, - apAreaId, - excludePast, - excludeCurrent, - excludeFuture, - getPageableOrAllPages(pageCriteria.withSortBy(sortFieldString), unsafe = true), + val page = outOfServiceBedRepository.findOutOfServiceBedIdsForDate( + premisesId = premisesId, + apAreaId = apAreaId, + excludePast = excludePast, + excludeCurrent = excludeCurrent, + excludeFuture = excludeFuture, + date = date, + pageable = getPageableOrAllPages(pageCriteria.withSortBy(sortFieldString), unsafe = true), ) val outOfServiceBeds = outOfServiceBedRepository.findAllByIdOrdered(page.content.map(UUID::fromString)) @@ -291,15 +293,30 @@ class Cas1OutOfServiceBedService( return Pair(outOfServiceBeds, getMetadata(page, pageCriteria)) } + fun getOutOfServiceBeds( + temporality: Set, + premisesId: UUID?, + apAreaId: UUID?, + pageCriteria: PageCriteria, + ): Pair, PaginationMetadata?> = + getOutOfServiceBedsForDate( + temporality = temporality, + premisesId = premisesId, + apAreaId = apAreaId, + date = LocalDate.now(), + pageCriteria = pageCriteria, + ) + fun getActiveOutOfServiceBedsForPremisesId(premisesId: UUID) = outOfServiceBedRepository.findAllActiveForPremisesId(premisesId) fun getCurrentOutOfServiceBedsCountForPremisesId(premisesId: UUID): Int { - return outOfServiceBedRepository.findOutOfServiceBedIds( + return outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = premisesId, apAreaId = null, excludePast = true, excludeCurrent = false, excludeFuture = true, + date = LocalDate.now(), pageable = Pageable.unpaged(), ).size } diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedSummaryService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedSummaryService.kt new file mode 100644 index 0000000000..b6a531e76c --- /dev/null +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1OutOfServiceBedSummaryService.kt @@ -0,0 +1,43 @@ +package uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1 + +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1OutOfServiceBedSortField +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SortDirection +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Temporality +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1OutOfServiceBedEntity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult +import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.PageCriteria +import java.time.LocalDate +import java.util.UUID + +@Service +class Cas1OutOfServiceBedSummaryService( + private val cas1PremisesService: Cas1PremisesService, + private val cas1OutOfServiceBedService: Cas1OutOfServiceBedService, +) { + + fun getOutOfServiceBedSummaries( + premisesId: UUID, + apAreaId: UUID, + date: LocalDate, + ): CasResult> { + if (cas1PremisesService.findPremiseById(premisesId) == null) return CasResult.NotFound("premises", premisesId.toString()) + + val (outOfServiceBeds) = cas1OutOfServiceBedService.getOutOfServiceBedsForDate( + temporality = setOf(Temporality.current), + premisesId = premisesId, + date = date, + apAreaId = apAreaId, + pageCriteria = PageCriteria( + sortBy = Cas1OutOfServiceBedSortField.startDate, + sortDirection = SortDirection.asc, + page = 1, + perPage = 1000, + ), + ) + + return CasResult.Success( + outOfServiceBeds, + ) + } +} diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1OutOfServiceBedSummaryTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1OutOfServiceBedSummaryTransformer.kt new file mode 100644 index 0000000000..e2d11063bf --- /dev/null +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1OutOfServiceBedSummaryTransformer.kt @@ -0,0 +1,25 @@ +package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1 + +import org.springframework.stereotype.Component +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1OutOfServiceBedSummary +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceCharacteristic +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1OutOfServiceBedEntity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicEntity + +@Component +class Cas1OutOfServiceBedSummaryTransformer( + private val cas1OutOfServiceBedReasonTransformer: Cas1OutOfServiceBedReasonTransformer, +) { + + fun toCas1OutOfServiceBedSummary(jpa: Cas1OutOfServiceBedEntity) = Cas1OutOfServiceBedSummary( + id = jpa.id, + roomName = jpa.bed.room.name, + startDate = jpa.startDate, + endDate = jpa.endDate, + reason = cas1OutOfServiceBedReasonTransformer.transformJpaToApi(jpa.reason), + characteristics = jpa.bed.room.characteristics.map { it.asCas1SpaceCharacteristic() }, + ) + + private fun CharacteristicEntity.asCas1SpaceCharacteristic() = + Cas1SpaceCharacteristic.entries.first { it.value == this.propertyName } +} diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesDayTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesDayTransformer.kt index c436fce496..723cf199f3 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesDayTransformer.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesDayTransformer.kt @@ -1,7 +1,8 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1 import org.springframework.stereotype.Component -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1OutOfServiceBedSummary +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacityForDay import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesDaySummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingDaySummary import java.time.LocalDate @@ -11,15 +12,16 @@ class Cas1PremisesDayTransformer { fun toCas1PremisesDaySummary( date: LocalDate, - premisesCapacity: Cas1PremiseCapacity, + premisesCapacity: Cas1PremiseCapacityForDay, spaceBookings: List, + outOfServiceBeds: List, ) = Cas1PremisesDaySummary( forDate = date, previousDate = date.minusDays(1), nextDate = date.plusDays(1), - capacity = premisesCapacity.capacity.first(), + capacity = premisesCapacity, spaceBookings = spaceBookings, - outOfServiceBeds = emptyList(), + outOfServiceBeds = outOfServiceBeds, ) } diff --git a/src/main/resources/static/cas1-schemas.yml b/src/main/resources/static/cas1-schemas.yml index 601b99b2c1..00fddec610 100644 --- a/src/main/resources/static/cas1-schemas.yml +++ b/src/main/resources/static/cas1-schemas.yml @@ -837,6 +837,8 @@ components: id: type: string format: uuid + roomName: + type: string startDate: type: string format: date diff --git a/src/main/resources/static/codegen/built-cas1-api-spec.yml b/src/main/resources/static/codegen/built-cas1-api-spec.yml index 92de22fe3a..0f84f60265 100644 --- a/src/main/resources/static/codegen/built-cas1-api-spec.yml +++ b/src/main/resources/static/codegen/built-cas1-api-spec.yml @@ -6887,6 +6887,8 @@ components: id: type: string format: uuid + roomName: + type: string startDate: type: string format: date diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt index f961b42d28..b169883681 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt @@ -26,7 +26,9 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApAreaEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesApplicationEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesGender +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1OutOfServiceBedEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingEntity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicRepository.Constants.CAS1_PROPERTY_NAME_ARSON_SUITABLE import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicRepository.Constants.CAS1_PROPERTY_NAME_ENSUITE import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicRepository.Constants.CAS1_PROPERTY_NAME_SINGLE_ROOM @@ -37,7 +39,9 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserRole.CAS1 import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.deliuscontext.CaseSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.asCaseSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.bodyAsListOfObjects +import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.roundNanosToMillisToAccountForLossOfPrecisionInPostgres import java.time.LocalDate +import java.time.OffsetDateTime import java.util.UUID class Cas1PremisesTest : IntegrationTestBase() { @@ -467,13 +471,19 @@ class Cas1PremisesTest : IntegrationTestBase() { lateinit var offenderA: CaseSummary lateinit var offenderB: CaseSummary lateinit var offenderOffline: CaseSummary + lateinit var outOfServiceBed: Cas1OutOfServiceBedEntity + lateinit var outOfServiceBedEndingToday: Cas1OutOfServiceBedEntity + lateinit var outOfServiceBedTodayOnly: Cas1OutOfServiceBedEntity + lateinit var isGroundFloorCharacteristic: CharacteristicEntity + lateinit var isArsonSuitableCharacteristic: CharacteristicEntity + lateinit var hasEnSuiteCharacteristic: CharacteristicEntity val summaryDate = LocalDate.now() val tierA = "A2" val tierB = "B3" val releaseTypeToBeDetermined = "TBD" - @SuppressWarnings("UnusedPrivateProperty") + @SuppressWarnings("UnusedPrivateProperty", "LongMethod") @BeforeAll fun setupTestData() { val region = givenAProbationRegion( @@ -611,6 +621,137 @@ class Cas1PremisesTest : IntegrationTestBase() { } apDeliusContextAddListCaseSummaryToBulkResponse(listOf(offenderA, offenderB, offenderOffline)) + + hasEnSuiteCharacteristic = characteristicEntityFactory.produceAndPersist { + withId(UUID.fromString("138f63aa-7e51-4581-8d71-dfdf9b85bbd9")) + withPropertyName("hasEnSuite") + withName("hasEnSuite") + withModelScope("room") + withServiceScope("approved-premises") + } + + isArsonSuitableCharacteristic = characteristicEntityFactory.produceAndPersist { + withId(UUID.fromString("a4ba038b-f762-4f19-ae94-2e308637a5ed")) + withPropertyName("isArsonSuitable") + withName("isArsonSuitable") + withModelScope("room") + withServiceScope("approved-premises") + } + + isGroundFloorCharacteristic = characteristicEntityFactory.produceAndPersist { + withId(UUID.fromString("83377a71-6cda-4f83-90ca-32513f401500")) + withPropertyName("isGroundFloor") + withName("isGroundFloor") + withModelScope("room") + withServiceScope("approved-premises") + } + + val bed1 = bedEntityFactory.produceAndPersist { + withRoom( + roomEntityFactory.produceAndPersist { + withCharacteristics(mutableListOf(hasEnSuiteCharacteristic)) + withPremises(premises) + }, + ) + } + + val bed2 = bedEntityFactory.produceAndPersist { + withRoom( + roomEntityFactory.produceAndPersist { + withCharacteristics(mutableListOf(hasEnSuiteCharacteristic, isArsonSuitableCharacteristic)) + withPremises(premises) + }, + ) + } + + val bed3 = bedEntityFactory.produceAndPersist { + withRoom( + roomEntityFactory.produceAndPersist { + withCharacteristics(mutableListOf(hasEnSuiteCharacteristic, isArsonSuitableCharacteristic, isGroundFloorCharacteristic)) + withPremises(premises) + }, + ) + } + + outOfServiceBed = cas1OutOfServiceBedEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withBed(bed1) + }.apply { + this.revisionHistory += cas1OutOfServiceBedRevisionEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withCreatedBy(user) + withOutOfServiceBed(this@apply) + withStartDate(LocalDate.now().minusDays(20)) + withEndDate(LocalDate.now().plusDays(20)) + withYieldedReason { + cas1OutOfServiceBedReasonEntityFactory.produceAndPersist() + } + } + } + + outOfServiceBedEndingToday = cas1OutOfServiceBedEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withBed(bed2) + }.apply { + this.revisionHistory += cas1OutOfServiceBedRevisionEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withCreatedBy(user) + withOutOfServiceBed(this@apply) + withStartDate(LocalDate.now().minusDays(10)) + withEndDate(LocalDate.now()) + withYieldedReason { + cas1OutOfServiceBedReasonEntityFactory.produceAndPersist() + } + } + } + + outOfServiceBedTodayOnly = cas1OutOfServiceBedEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withBed(bed3) + }.apply { + this.revisionHistory += cas1OutOfServiceBedRevisionEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withCreatedBy(user) + withOutOfServiceBed(this@apply) + withStartDate(LocalDate.now()) + withEndDate(LocalDate.now()) + withYieldedReason { + cas1OutOfServiceBedReasonEntityFactory.produceAndPersist() + } + } + } + + val expiredOutOfServiceBed = cas1OutOfServiceBedEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withBed(bed1) + }.apply { + this.revisionHistory += cas1OutOfServiceBedRevisionEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withCreatedBy(user) + withOutOfServiceBed(this@apply) + withStartDate(LocalDate.now().minusDays(20)) + withEndDate(LocalDate.now().minusDays(1)) + withYieldedReason { + cas1OutOfServiceBedReasonEntityFactory.produceAndPersist() + } + } + } + + val upcomingOutOfServiceBed = cas1OutOfServiceBedEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withBed(bed1) + }.apply { + this.revisionHistory += cas1OutOfServiceBedRevisionEntityFactory.produceAndPersist { + withCreatedAt(OffsetDateTime.now().roundNanosToMillisToAccountForLossOfPrecisionInPostgres()) + withCreatedBy(user) + withOutOfServiceBed(this@apply) + withStartDate(LocalDate.now().plusDays(1)) + withEndDate(LocalDate.now().plusDays(10)) + withYieldedReason { + cas1OutOfServiceBedReasonEntityFactory.produceAndPersist() + } + } + } } @Test @@ -653,7 +794,8 @@ class Cas1PremisesTest : IntegrationTestBase() { assertThat(summaries.nextDate).isEqualTo(summaryDate.plusDays(1)) assertThat(summaries.previousDate).isEqualTo(summaryDate.minusDays(1)) assertThat(summaries.spaceBookings.size).isEqualTo(3) - assertThat(summaries.outOfServiceBeds).isEmpty() + assertThat(summaries.spaceBookings.size).isEqualTo(3) + assertThat(summaries.outOfServiceBeds.size).isEqualTo(3) val summaryBookingOffline = summaries.spaceBookings[0] assertThat(summaryBookingOffline.id).isEqualTo(spaceBookingOfflineApplication.id) @@ -670,9 +812,10 @@ class Cas1PremisesTest : IntegrationTestBase() { assertThat(summaryBooking1.canonicalArrivalDate).isEqualTo(spaceBookingLate.canonicalArrivalDate) assertThat(summaryBooking1.canonicalDepartureDate).isEqualTo(spaceBookingLate.canonicalDepartureDate) assertThat(summaryBooking1.essentialCharacteristics.size).isEqualTo(3) - assertThat(summaryBooking1.essentialCharacteristics[0].value).isEqualTo(CAS1_PROPERTY_NAME_ARSON_SUITABLE) - assertThat(summaryBooking1.essentialCharacteristics[1].value).isEqualTo(CAS1_PROPERTY_NAME_ENSUITE) - assertThat(summaryBooking1.essentialCharacteristics[2].value).isEqualTo(CAS1_PROPERTY_NAME_SINGLE_ROOM) + val bookingCharacteristics = summaryBooking1.essentialCharacteristics.map { it.value } + assertThat(bookingCharacteristics).contains(CAS1_PROPERTY_NAME_SINGLE_ROOM) + assertThat(bookingCharacteristics).contains(CAS1_PROPERTY_NAME_ENSUITE) + assertThat(bookingCharacteristics).contains(CAS1_PROPERTY_NAME_ARSON_SUITABLE) assertThat(summaryBooking1.releaseType).isEqualTo(releaseTypeToBeDetermined) val summaryBooking2 = summaries.spaceBookings[2] @@ -704,10 +847,43 @@ class Cas1PremisesTest : IntegrationTestBase() { val capacity = summaries.capacity assertThat(capacity.date).isEqualTo(summaryDate) - assertThat(capacity.totalBedCount).isEqualTo(5) + assertThat(capacity.totalBedCount).isEqualTo(8) assertThat(capacity.availableBedCount).isEqualTo(5) assertThat(capacity.bookingCount).isEqualTo(3) assertThat(capacity.characteristicAvailability.count()).isEqualTo(6) + + val oosBedSummary = summaries.outOfServiceBeds[0] + assertThat(oosBedSummary.id).isEqualTo(outOfServiceBed.id) + assertThat(oosBedSummary.startDate).isEqualTo(outOfServiceBed.startDate) + assertThat(oosBedSummary.endDate).isEqualTo(outOfServiceBed.endDate) + assertThat(oosBedSummary.roomName).isEqualTo(outOfServiceBed.bed.room.name) + assertThat(oosBedSummary.reason.id).isEqualTo(outOfServiceBed.reason.id) + assertThat(oosBedSummary.reason.name).isEqualTo(outOfServiceBed.reason.name) + assertThat(oosBedSummary.characteristics.size).isEqualTo(1) + assertThat(oosBedSummary.characteristics[0].name).isEqualTo(hasEnSuiteCharacteristic.name) + + val oosBedEndingToday = summaries.outOfServiceBeds[1] + assertThat(oosBedEndingToday.id).isEqualTo(outOfServiceBedEndingToday.id) + assertThat(oosBedEndingToday.startDate).isEqualTo(outOfServiceBedEndingToday.startDate) + assertThat(oosBedEndingToday.endDate).isEqualTo(outOfServiceBedEndingToday.endDate) + assertThat(oosBedEndingToday.roomName).isEqualTo(outOfServiceBedEndingToday.bed.room.name) + assertThat(oosBedEndingToday.reason.id).isEqualTo(outOfServiceBedEndingToday.reason.id) + assertThat(oosBedEndingToday.reason.name).isEqualTo(outOfServiceBedEndingToday.reason.name) + assertThat(oosBedEndingToday.characteristics.size).isEqualTo(2) + assertThat(oosBedEndingToday.characteristics[0].name).isEqualTo(hasEnSuiteCharacteristic.name) + assertThat(oosBedEndingToday.characteristics[1].name).isEqualTo(isArsonSuitableCharacteristic.name) + + val oosBedTodayOnly = summaries.outOfServiceBeds[2] + assertThat(oosBedTodayOnly.id).isEqualTo(outOfServiceBedTodayOnly.id) + assertThat(oosBedTodayOnly.startDate).isEqualTo(outOfServiceBedTodayOnly.startDate) + assertThat(oosBedTodayOnly.endDate).isEqualTo(outOfServiceBedTodayOnly.endDate) + assertThat(oosBedTodayOnly.roomName).isEqualTo(outOfServiceBedTodayOnly.bed.room.name) + assertThat(oosBedTodayOnly.reason.id).isEqualTo(outOfServiceBedTodayOnly.reason.id) + assertThat(oosBedTodayOnly.reason.name).isEqualTo(outOfServiceBedTodayOnly.reason.name) + assertThat(oosBedTodayOnly.characteristics.size).isEqualTo(3) + assertThat(oosBedTodayOnly.characteristics[0].name).isEqualTo(hasEnSuiteCharacteristic.name) + assertThat(oosBedTodayOnly.characteristics[1].name).isEqualTo(isArsonSuitableCharacteristic.name) + assertThat(oosBedTodayOnly.characteristics[2].name).isEqualTo(isGroundFloorCharacteristic.name) } @Test @@ -727,7 +903,11 @@ class Cas1PremisesTest : IntegrationTestBase() { assertThat(summary.previousDate).isEqualTo(summaryDate.minusDays(1)) assertThat(summary.spaceBookings.size).isEqualTo(1) val summaryBooking = summary.spaceBookings[0] - assertThat(summaryBooking.essentialCharacteristics[1].value).isEqualTo(CAS1_PROPERTY_NAME_ENSUITE) + assertThat(summaryBooking.essentialCharacteristics.size).isEqualTo(3) + assertThat(summaryBooking.essentialCharacteristics[0].value).isEqualTo(CAS1_PROPERTY_NAME_ENSUITE) + assertThat(summaryBooking.essentialCharacteristics[1].value).isEqualTo(CAS1_PROPERTY_NAME_ARSON_SUITABLE) + assertThat(summaryBooking.essentialCharacteristics[2].value).isEqualTo(CAS1_PROPERTY_NAME_SINGLE_ROOM) + assertThat(summary.outOfServiceBeds.size).isEqualTo(3) } @Test @@ -746,7 +926,7 @@ class Cas1PremisesTest : IntegrationTestBase() { assertThat(summary.nextDate).isEqualTo(summaryDate.plusDays(1)) assertThat(summary.previousDate).isEqualTo(summaryDate.minusDays(1)) assertThat(summary.spaceBookings).isEmpty() - assertThat(summary.outOfServiceBeds).isEmpty() + assertThat(summary.outOfServiceBeds.size).isEqualTo(3) } @Test diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedServiceTest.kt index dcf01f445a..48f6a4dac2 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedServiceTest.kt @@ -575,7 +575,7 @@ class Cas1OutOfServiceBedServiceTest { val expectedPageable = getPageableOrAllPages(expectedSortFieldString, sortDirection, page = null, pageSize = null, unsafe = true) every { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = any(), apAreaId = any(), excludePast = any(), @@ -595,7 +595,7 @@ class Cas1OutOfServiceBedServiceTest { ) verify(exactly = 1) { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = null, apAreaId = null, excludePast = false, @@ -610,7 +610,7 @@ class Cas1OutOfServiceBedServiceTest { @ParameterizedTest fun `Filters correctly according to the temporality`(temporality: List) { every { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = any(), apAreaId = any(), excludePast = any(), @@ -630,7 +630,7 @@ class Cas1OutOfServiceBedServiceTest { ) verify(exactly = 1) { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = null, apAreaId = null, excludePast = !temporality.contains(Temporality.past), @@ -644,7 +644,7 @@ class Cas1OutOfServiceBedServiceTest { @Test fun `Filters correctly according to the premises ID`() { every { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = any(), apAreaId = any(), excludePast = any(), @@ -666,7 +666,7 @@ class Cas1OutOfServiceBedServiceTest { ) verify(exactly = 1) { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = expectedId, apAreaId = null, excludePast = false, @@ -680,7 +680,7 @@ class Cas1OutOfServiceBedServiceTest { @Test fun `Filters correctly according to the AP area ID`() { every { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = any(), apAreaId = any(), excludePast = any(), @@ -702,7 +702,7 @@ class Cas1OutOfServiceBedServiceTest { ) verify(exactly = 1) { - outOfServiceBedRepository.findOutOfServiceBedIds( + outOfServiceBedRepository.findOutOfServiceBedIdsForDate( premisesId = null, apAreaId = expectedId, excludePast = false, diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedSummaryServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedSummaryServiceTest.kt new file mode 100644 index 0000000000..3fe90c8274 --- /dev/null +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1OutOfServiceBedSummaryServiceTest.kt @@ -0,0 +1,94 @@ +package uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1 + +import io.mockk.every +import io.mockk.junit5.MockKExtension +import io.mockk.mockk +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApAreaEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.Cas1OutOfServiceBedEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.LocalAuthorityAreaEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ProbationRegionEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1OutOfServiceBedEntity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PaginationMetadata +import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult +import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1OutOfServiceBedService +import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1OutOfServiceBedSummaryService +import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService +import java.time.LocalDate +import java.util.UUID + +@ExtendWith(MockKExtension::class) +class Cas1OutOfServiceBedSummaryServiceTest { + + private val cas1PremisesService = mockk() + private val cas1OutOfServiceBedService = mockk() + + private val service = Cas1OutOfServiceBedSummaryService( + cas1PremisesService, + cas1OutOfServiceBedService, + ) + + @Nested + inner class GetOutOfServiceBedSummaries { + + @Test + fun `returns not found error if premises with the given Id does not exist`() { + every { cas1PremisesService.findPremiseById(any()) } returns null + + val result = service.getOutOfServiceBedSummaries( + UUID.randomUUID(), + UUID.randomUUID(), + LocalDate.now(), + ) + + assertThat(result).isInstanceOf(CasResult.NotFound::class.java) + assertThat((result as CasResult.NotFound).entityType).isEqualTo("premises") + } + } + + @Test + fun `returns out of service bed summaries when out of service beds exist`() { + val apArea = ApAreaEntityFactory() + .produce() + val probationRegion = ProbationRegionEntityFactory() + .withApArea(apArea) + .produce() + val premises = ApprovedPremisesEntityFactory() + .withLocalAuthorityArea(LocalAuthorityAreaEntityFactory().produce()) + .withProbationRegion(probationRegion) + .produce() + val date = LocalDate.now() + + val outOfServiceBed1 = Cas1OutOfServiceBedEntityFactory() + .withBed { + withRoom { + withPremises( + ApprovedPremisesEntityFactory() + .withDefaults() + .produce(), + ) + } + } + .produce() + + every { cas1PremisesService.findPremiseById(premises.id) } returns premises + every { cas1OutOfServiceBedService.getOutOfServiceBedsForDate(any(), premises.id, apArea.id, date, any()) } returns + Pair(listOf(outOfServiceBed1), PaginationMetadata(0, 0, 0, 0)) + + val result = service.getOutOfServiceBedSummaries( + premisesId = premises.id, + apAreaId = apArea.id, + date = LocalDate.now(), + ) + + assertThat(result).isInstanceOf(CasResult.Success::class.java) + result as CasResult.Success + val outOfServiceBedSummaries = result.value + assertThat(outOfServiceBedSummaries.size).isEqualTo(1) + assertThat(result.value[0]).isInstanceOf(Cas1OutOfServiceBedEntity::class.java) + } +} diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1OutOfServiceBedSummaryTransformerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1OutOfServiceBedSummaryTransformerTest.kt new file mode 100644 index 0000000000..fdb4c17813 --- /dev/null +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1OutOfServiceBedSummaryTransformerTest.kt @@ -0,0 +1,87 @@ +package uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.transformer.cas1 + +import io.mockk.every +import io.mockk.junit5.MockKExtension +import io.mockk.mockk +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1OutOfServiceBedReason +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.BedEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.Cas1OutOfServiceBedEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.Cas1OutOfServiceBedReasonEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.Cas1OutOfServiceBedRevisionEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.RoomEntityFactory +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicEntity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1OutOfServiceBedReasonTransformer +import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1OutOfServiceBedSummaryTransformer +import java.time.LocalDate +import java.util.UUID + +@ExtendWith(MockKExtension::class) +class Cas1OutOfServiceBedSummaryTransformerTest { + + private val cas1OutOfServiceBedReasonTransformer = mockk() + + private val transformer = Cas1OutOfServiceBedSummaryTransformer( + cas1OutOfServiceBedReasonTransformer, + ) + + @Test + fun toCas1OutOfServiceBedSummary() { + val roomCharacteristic = CharacteristicEntity( + id = UUID.randomUUID(), + propertyName = "hasLift", + name = "hasLift", + serviceScope = "approved-premises", + modelScope = "room", + isActive = true, + ) + val room = RoomEntityFactory() + .withName("BedRoom 1") + .withCharacteristics(mutableListOf(roomCharacteristic)) + .withDefaults() + .produce() + val startDate = LocalDate.now().plusDays(1) + val endDate = startDate.plusDays(10) + val bed = BedEntityFactory() + .withDefaults() + .withRoom(room) + .produce() + val outOfServiceReason = Cas1OutOfServiceBedReasonEntityFactory() + .produce() + + val expectedReason = Cas1OutOfServiceBedReason( + id = outOfServiceReason.id, + name = outOfServiceReason.name, + isActive = true, + ) + + val outOfServiceBed = Cas1OutOfServiceBedEntityFactory() + .withBed(bed) + .produce() + .apply { + this.revisionHistory += Cas1OutOfServiceBedRevisionEntityFactory() + .withOutOfServiceBed(this) + .withStartDate(startDate) + .withEndDate(endDate) + .withReason(outOfServiceReason) + .produce() + } + + every { cas1OutOfServiceBedReasonTransformer.transformJpaToApi(any()) } returns expectedReason + + val result = transformer.toCas1OutOfServiceBedSummary(outOfServiceBed) + + assertThat(result.id).isEqualTo(outOfServiceBed.id) + assertThat(result.roomName).isEqualTo(bed.room.name) + assertThat(result.startDate).isEqualTo(outOfServiceBed.startDate) + assertThat(result.endDate).isEqualTo(outOfServiceBed.endDate) + assertThat(result.characteristics.size).isEqualTo(1) + assertThat(result.characteristics[0].name).isEqualTo(roomCharacteristic.name) + val reason = result.reason + assertThat(reason.id).isEqualTo(expectedReason.id) + assertThat(reason.name).isEqualTo(expectedReason.name) + assertThat(reason.isActive).isEqualTo(expectedReason.isActive) + } +} diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesDayTransformerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesDayTransformerTest.kt index b77f22373c..a3a299a0fd 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesDayTransformerTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesDayTransformerTest.kt @@ -5,16 +5,15 @@ import io.mockk.junit5.MockKExtension import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ApArea -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1OutOfServiceBedReason +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1OutOfServiceBedSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacityForDay import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCharacteristicAvailability -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingCharacteristic import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingDaySummary +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceCharacteristic import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PersonSummaryDiscriminator import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.RestrictedPersonSummary -import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesEntityFactory import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1PremisesDayTransformer import java.time.LocalDate import java.util.UUID @@ -27,24 +26,9 @@ class Cas1PremisesDayTransformerTest { @Test fun toCas1PremisesDaySummary() { - val premise = ApprovedPremisesEntityFactory().withDefaults().produce() - val cas1PremisesSummary = Cas1PremisesSummary( - id = premise.id, - name = premise.name, - apCode = premise.apCode, - postcode = premise.postcode, - apArea = ApArea(UUID.randomUUID(), "identifier", "name"), - bedCount = 5, - availableBeds = 4, - outOfServiceBeds = 1, - supportsSpaceBookings = true, - overbookingSummary = emptyList(), - managerDetails = "manager details", - ) - val currentSearchDay = LocalDate.now() - val capacity = listOf( + val capacity = Cas1PremiseCapacityForDay( date = currentSearchDay, totalBedCount = 5, @@ -54,8 +38,7 @@ class Cas1PremisesDayTransformerTest { Cas1PremiseCharacteristicAvailability(Cas1SpaceBookingCharacteristic.IS_SINGLE, 10, 4), Cas1PremiseCharacteristicAvailability(Cas1SpaceBookingCharacteristic.IS_WHEELCHAIR_DESIGNATED, 20, 8), ), - ), - ) + ) val spaceBookings = listOf( Cas1SpaceBookingDaySummary( @@ -72,24 +55,28 @@ class Cas1PremisesDayTransformerTest { ), ) - val premiseCapacity = Cas1PremiseCapacity( - premise = cas1PremisesSummary, - startDate = currentSearchDay, - endDate = currentSearchDay, - capacity = capacity, + val outOfServiceBeds = listOf( + Cas1OutOfServiceBedSummary( + id = UUID.randomUUID(), + startDate = LocalDate.now().minusDays(5), + endDate = LocalDate.now().plusDays(5), + reason = Cas1OutOfServiceBedReason(UUID.randomUUID(), "reason", true), + characteristics = listOf(Cas1SpaceCharacteristic.isSingle), + ), ) val result = transformer.toCas1PremisesDaySummary( currentSearchDay, - premiseCapacity, + capacity, spaceBookings, + outOfServiceBeds, ) assertThat(result.forDate).isEqualTo(currentSearchDay) assertThat(result.previousDate).isEqualTo(currentSearchDay.minusDays(1)) assertThat(result.nextDate).isEqualTo(currentSearchDay.plusDays(1)) - assertThat(result.capacity).isEqualTo(capacity[0]) + assertThat(result.capacity).isEqualTo(capacity) assertThat(result.spaceBookings).isEqualTo(spaceBookings) - assertThat(result.outOfServiceBeds).isEmpty() + assertThat(result.outOfServiceBeds).isEqualTo(outOfServiceBeds) } }