Skip to content

Commit

Permalink
Implement logic to obtain out of service bed information
Browse files Browse the repository at this point in the history
Amended existing findOutOfServiceBedIds query, renamed to findOutOfServiceBedIdForDates to fetch by optionally provided date
Incorporate oosbeds into the premise day summary
  • Loading branch information
RobBoothMOJ committed Dec 28, 2024
1 parent 3887251 commit b19672c
Show file tree
Hide file tree
Showing 13 changed files with 534 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Cas1PremisesSummary> {
Expand Down Expand Up @@ -96,30 +100,42 @@ class Cas1PremisesController(
bookingsSortDirection: SortDirection?,
bookingsSortBy: Cas1SpaceBookingDaySummarySortField?,
): ResponseEntity<Cas1PremisesDaySummary> {
userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_SPACE_BOOKING_VIEW)
userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW)

val premiseSummaryInfo = cas1PremisesService.getPremisesSummary(premisesId)
val premiseCapacity = cas1PremisesService.getPremiseCapacity(premisesId, date, date, null)
val premisesCapacitySummary = cas1PremiseCapacityTransformer.toCas1PremiseCapacitySummary(
premiseSummaryInfo = extractEntityFromCasResult(premiseSummaryInfo),
premiseCapacity = extractEntityFromCasResult(premiseCapacity),
)

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,
),
),
),
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),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ interface Cas1OutOfServiceBedRepository : JpaRepository<Cas1OutOfServiceBedEntit
WHERE
(CAST(:premisesId AS UUID) IS NULL OR oosb.premises_id = :premisesId) AND
(CAST(:apAreaId AS UUID) IS NULL OR apa.id = :apAreaId) AND
(FALSE = :excludePast OR dd.end_date >= 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)
"""
}
Expand All @@ -70,12 +70,13 @@ interface Cas1OutOfServiceBedRepository : JpaRepository<Cas1OutOfServiceBedEntit
""",
nativeQuery = true,
)
fun findOutOfServiceBedIds(
fun findOutOfServiceBedIdsForDate(
premisesId: UUID?,
apAreaId: UUID?,
excludePast: Boolean,
excludeCurrent: Boolean,
excludeFuture: Boolean,
date: LocalDate? = LocalDate.now(),
pageable: Pageable?,
): Page<String>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,11 @@ class Cas1OutOfServiceBedService(
return success(cancellationEntity)
}

fun getOutOfServiceBeds(
fun getOutOfServiceBedsForDate(
temporality: Set<Temporality>,
premisesId: UUID?,
apAreaId: UUID?,
date: LocalDate,
pageCriteria: PageCriteria<Cas1OutOfServiceBedSortField>,
): Pair<List<Cas1OutOfServiceBedEntity>, PaginationMetadata?> {
val sortFieldString = when (pageCriteria.sortBy) {
Expand All @@ -277,29 +278,45 @@ 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))

return Pair(outOfServiceBeds, getMetadata(page, pageCriteria))
}

fun getOutOfServiceBeds(
temporality: Set<Temporality>,
premisesId: UUID?,
apAreaId: UUID?,
pageCriteria: PageCriteria<Cas1OutOfServiceBedSortField>,
): Pair<List<Cas1OutOfServiceBedEntity>, 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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Cas1OutOfServiceBedEntity>> {
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,
)
}
}
Original file line number Diff line number Diff line change
@@ -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 }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.Cas1PremiseCapacity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesDaySummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingDaySummary
Expand All @@ -13,13 +14,14 @@ class Cas1PremisesDayTransformer {
date: LocalDate,
premisesCapacity: Cas1PremiseCapacity,
spaceBookings: List<Cas1SpaceBookingDaySummary>,
outOfServiceBeds: List<Cas1OutOfServiceBedSummary>,
) =
Cas1PremisesDaySummary(
forDate = date,
previousDate = date.minusDays(1),
nextDate = date.plusDays(1),
capacity = premisesCapacity.capacity.first(),
capacity = premisesCapacity.capacity[0],
spaceBookings = spaceBookings,
outOfServiceBeds = emptyList(),
outOfServiceBeds = outOfServiceBeds,
)
}
2 changes: 2 additions & 0 deletions src/main/resources/static/cas1-schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,8 @@ components:
id:
type: string
format: uuid
roomName:
type: string
startDate:
type: string
format: date
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/static/codegen/built-cas1-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6884,6 +6884,8 @@ components:
id:
type: string
format: uuid
roomName:
type: string
startDate:
type: string
format: date
Expand Down
Loading

0 comments on commit b19672c

Please sign in to comment.