generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement logic to fetch matching Space Bookings
A new query in the SpaceBookingEntity retrieves the matching space bookings for the given premises and day, filtering and ordering as specified.
- Loading branch information
1 parent
c78b064
commit baa0041
Showing
11 changed files
with
856 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...stice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1SpaceBookingDaySummaryService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1 | ||
|
||
import org.springframework.data.domain.Sort | ||
import org.springframework.stereotype.Service | ||
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.Cas1SpaceBookingDaySummarySortField | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ServiceName | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SortDirection | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingDaySummarySearchResult | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingRepository | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonSummaryInfoResult | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.forCrn | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.CharacteristicService | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserAccessService | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1LimitedAccessStrategy | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.PersonTransformer | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1.Cas1SpaceBookingDaySummaryTransformer | ||
import java.time.LocalDate | ||
import java.util.UUID | ||
|
||
@Service | ||
class Cas1SpaceBookingDaySummaryService( | ||
val userAccessService: UserAccessService, | ||
private val cas1SpaceBookingRepository: Cas1SpaceBookingRepository, | ||
private val characteristicService: CharacteristicService, | ||
private val cas1SpaceBookingDaySummaryTransformer: Cas1SpaceBookingDaySummaryTransformer, | ||
private val offenderService: OffenderService, | ||
private val userService: UserService, | ||
private val cas1PremisesService: Cas1PremisesService, | ||
) { | ||
|
||
fun getBookingDaySummaries( | ||
premisesId: UUID, | ||
date: LocalDate, | ||
bookingsCriteriaFilter: List<Cas1SpaceBookingCharacteristic>?, | ||
bookingsSortBy: Cas1SpaceBookingDaySummarySortField, | ||
bookingsSortDirection: SortDirection, | ||
): CasResult<List<Cas1SpaceBookingDaySummary>> { | ||
if (cas1PremisesService.findPremiseById(premisesId) == null) return CasResult.NotFound("premises", premisesId.toString()) | ||
|
||
val sort = Sort.by( | ||
when (bookingsSortDirection) { | ||
SortDirection.desc -> Sort.Direction.DESC | ||
SortDirection.asc -> Sort.Direction.ASC | ||
}, | ||
bookingsSortBy.value, | ||
) | ||
|
||
val spaceBookingsForDate = cas1SpaceBookingRepository.findAllPremisesBookingsForDate( | ||
premisesId = premisesId, | ||
daySummaryDate = date, | ||
bookingsCriteriaFilter = getBookingCharacteristicIds(bookingsCriteriaFilter), | ||
sort = sort, | ||
) | ||
|
||
val offenderSummaries = getOffenderSummariesForBookings(spaceBookingsForDate) | ||
|
||
val spaceBookingDaySummaries = | ||
spaceBookingsForDate.map { bookingSummary -> | ||
cas1SpaceBookingDaySummaryTransformer.toCas1SpaceBookingDaySummary( | ||
bookingSummary, | ||
PersonTransformer() | ||
.personSummaryInfoToPersonSummary( | ||
offenderSummaries.forCrn(bookingSummary.crn), | ||
), | ||
) | ||
} | ||
return CasResult.Success( | ||
spaceBookingDaySummaries, | ||
) | ||
} | ||
|
||
private fun getBookingCharacteristicIds(bookingsCriteriaFilter: List<Cas1SpaceBookingCharacteristic>?) = | ||
bookingsCriteriaFilter?.let { bookingCriteria -> | ||
val characteristics = bookingCriteria.map { it.value } | ||
characteristicService.getCharacteristicsByPropertyNames(characteristics, ServiceName.approvedPremises) | ||
.map { characteristic -> characteristic.id } | ||
} | ||
|
||
private fun getOffenderSummariesForBookings(spaceBookings: List<Cas1SpaceBookingDaySummarySearchResult>): List<PersonSummaryInfoResult> { | ||
val user = userService.getUserForRequest() | ||
return offenderService.getPersonSummaryInfoResults( | ||
crns = spaceBookings.map { it.crn }.toSet(), | ||
limitedAccessStrategy = user.cas1LimitedAccessStrategy(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...gital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceBookingDaySummaryTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1 | ||
|
||
import org.springframework.stereotype.Component | ||
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.PersonSummary | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingDaySummarySearchResult | ||
|
||
@Component | ||
class Cas1SpaceBookingDaySummaryTransformer { | ||
|
||
fun toCas1SpaceBookingDaySummary( | ||
jpa: Cas1SpaceBookingDaySummarySearchResult, | ||
personSummary: PersonSummary, | ||
) = Cas1SpaceBookingDaySummary( | ||
id = jpa.id, | ||
canonicalArrivalDate = jpa.canonicalArrivalDate, | ||
canonicalDepartureDate = jpa.canonicalDepartureDate, | ||
tier = jpa.tier, | ||
releaseType = jpa.releaseType, | ||
essentialCharacteristics = characteristicEntityToCharacteristic(jpa.characteristicsPropertyNames), | ||
person = personSummary, | ||
) | ||
|
||
private fun characteristicEntityToCharacteristic(characteristics: String?): List<Cas1SpaceBookingCharacteristic> = | ||
characteristics?.let { characteristicList -> | ||
characteristicList.split(",").map { characteristic -> | ||
Cas1SpaceBookingCharacteristic.entries.first { it.value == characteristic } | ||
} | ||
} ?: emptyList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.