-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CRS-2297 Version two endpoint. (#2368)
- Loading branch information
Showing
8 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...a/uk/gov/justice/hmpps/prison/api/model/calculation/CalculableSentenceEnvelopeVersion2.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,11 @@ | ||
package uk.gov.justice.hmpps.prison.api.model.calculation | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema | ||
|
||
@Schema(description = "The active sentence envelope is a prisoner to be calculated as part of a bulk calculation") | ||
data class CalculableSentenceEnvelopeVersion2( | ||
@Schema(description = "Prisoner Identifier", example = "A1234AA", requiredMode = Schema.RequiredMode.REQUIRED) | ||
var prisonerNumber: String, | ||
@Schema(description = "The booking ID") | ||
val bookingId: Long, | ||
) |
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
64 changes: 64 additions & 0 deletions
64
src/main/java/uk/gov/justice/hmpps/prison/service/SentenceEnvelopeServiceVersion2.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,64 @@ | ||
package uk.gov.justice.hmpps.prison.service | ||
|
||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.PageImpl | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.data.domain.Sort | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import uk.gov.justice.hmpps.prison.api.model.calculation.CalculableSentenceEnvelopeVersion2 | ||
import uk.gov.justice.hmpps.prison.repository.jpa.model.OffenderBooking | ||
import uk.gov.justice.hmpps.prison.repository.jpa.repository.AgencyLocationRepository | ||
import uk.gov.justice.hmpps.prison.repository.jpa.repository.OffenderBookingId | ||
import uk.gov.justice.hmpps.prison.repository.jpa.repository.OffenderBookingRepository | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
class SentenceEnvelopeServiceVersion2( | ||
private val agencyLocationRepository: AgencyLocationRepository, | ||
private val offenderBookingRepository: OffenderBookingRepository, | ||
private val bookingService: BookingService, | ||
) { | ||
fun getCalculableSentenceEnvelopeByEstablishment( | ||
caseLoad: String, | ||
pageNumber: Int, | ||
pageSize: Int, | ||
): Page<CalculableSentenceEnvelopeVersion2> { | ||
val agencyLocation = agencyLocationRepository.getReferenceById(caseLoad) | ||
val pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by("bookingId")) | ||
val bookingIds = | ||
offenderBookingRepository.findDistinctByActiveTrueAndLocationAndSentences_statusAndSentences_CalculationType_CalculationTypeNotLikeAndSentences_CalculationType_CategoryNot( | ||
agencyLocation, | ||
"A", | ||
"%AGG%", | ||
"LICENCE", | ||
pageRequest, | ||
) | ||
val activeBookings = | ||
offenderBookingRepository.findAllByBookingIdIn(bookingIds.map(OffenderBookingId::bookingId).toList()) | ||
val calculableSentenceEnvelopes = | ||
activeBookings.map { determineCalculableSentenceEnvelope(it) }.sortedBy { it.bookingId } | ||
return PageImpl(calculableSentenceEnvelopes, pageRequest, bookingIds.totalElements) | ||
} | ||
|
||
fun getCalculableSentenceEnvelopeByOffenderNumbers(offenderNumbers: Set<String?>): List<CalculableSentenceEnvelopeVersion2> { | ||
val bookingIds = | ||
offenderBookingRepository.findDistinctByActiveTrueAndOffenderNomsIdInAndSentences_statusAndSentences_CalculationType_CalculationTypeNotLikeAndSentences_CalculationType_CategoryNot( | ||
offenderNumbers, | ||
"A", | ||
"%AGG%", | ||
"LICENCE", | ||
) | ||
|
||
// ensure that the user has access to each of the bookings | ||
bookingIds.forEach { bookingService.verifyBookingAccess(it.bookingId, "VIEW_PRISONER_DATA") } | ||
|
||
val activeBookings = offenderBookingRepository.findAllByBookingIdIn(bookingIds.map(OffenderBookingId::bookingId)) | ||
return activeBookings.map { determineCalculableSentenceEnvelope(it) } | ||
} | ||
|
||
private fun determineCalculableSentenceEnvelope(offenderBooking: OffenderBooking): CalculableSentenceEnvelopeVersion2 = CalculableSentenceEnvelopeVersion2( | ||
offenderBooking.offender.nomsId, | ||
offenderBooking.bookingId, | ||
) | ||
} |
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
4 changes: 4 additions & 0 deletions
4
...rison/api/resource/impl/prison_resource_fixed_recall_calculable_sentence_envelope_v2.json
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,4 @@ | ||
{ | ||
"prisonerNumber": "A1234AE", | ||
"bookingId": -5 | ||
} |
4 changes: 4 additions & 0 deletions
4
...mpps/prison/api/resource/impl/prison_resource_single_calculable_sentence_envelope_v2.json
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,4 @@ | ||
{ | ||
"prisonerNumber": "A1234AB", | ||
"bookingId": -2 | ||
} |