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.
Merge pull request #1301 from ministryofjustice/feature/APS-39-displa…
…y-offender-status-at-time-of-submission Feature/aps 39 display offender status at time of submission
- Loading branch information
Showing
27 changed files
with
742 additions
and
60 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
83 changes: 83 additions & 0 deletions
83
...stice/digital/hmpps/approvedpremisesapi/migration/InmateStatusOnSubmissionMigrationJob.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,83 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.migration | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.data.domain.Slice | ||
import org.springframework.transaction.support.TransactionTemplate | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ClientResult | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.PrisonsApiClient | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApplicationRepository | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesApplicationEntity | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.prisonsapi.InOutStatus | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.prisonsapi.PrisonerInPrisonSummary | ||
import java.time.OffsetDateTime | ||
import javax.persistence.EntityManager | ||
|
||
private const val THROTTLE_DELAY_SECONDS = 1 * 1000L | ||
|
||
class InmateStatusOnSubmissionMigrationJob( | ||
private val applicationRepository: ApplicationRepository, | ||
private val entityManager: EntityManager, | ||
private val pageSize: Int, | ||
private val throttle: Boolean, | ||
private val transactionTemplate: TransactionTemplate, | ||
private val prisonsApiClient: PrisonsApiClient, | ||
) : MigrationJob() { | ||
|
||
override val shouldRunInTransaction = false | ||
|
||
private val log = LoggerFactory.getLogger(this::class.java) | ||
|
||
override fun process() { | ||
var page = 1 | ||
var hasNext = true | ||
var slice: Slice<ApprovedPremisesApplicationEntity> | ||
|
||
while (hasNext) { | ||
log.info("Getting page $page") | ||
slice = applicationRepository.getSubmittedApprovedPremisesApplicationsWithoutInOutStatus(PageRequest.of(0, pageSize)) | ||
slice.content.forEach { application -> | ||
transactionTemplate.executeWithoutResult { | ||
updateInOutStatus(application) | ||
} | ||
|
||
if (throttle) { | ||
Thread.sleep(THROTTLE_DELAY_SECONDS) | ||
} | ||
} | ||
entityManager.clear() | ||
hasNext = slice.hasNext() | ||
page += 1 | ||
} | ||
} | ||
|
||
private fun updateInOutStatus(application: ApprovedPremisesApplicationEntity) { | ||
log.info("Determine in out status for application ${application.id} submitted on ${application.submittedAt}") | ||
when (val timelineResult = prisonsApiClient.getPrisonTimeline(application.nomsNumber!!)) { | ||
is ClientResult.Success -> { | ||
val inOutStatus = determineInOutStatus(application.submittedAt!!, timelineResult.body) | ||
log.info("Status is $inOutStatus") | ||
applicationRepository.updateInOutStatus(application.id, inOutStatus.name) | ||
} | ||
is ClientResult.Failure -> { | ||
log.error("Unable to update application ${application.id}", timelineResult.toException()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun determineInOutStatus(submissionDateTime: OffsetDateTime, inPrisonSummary: PrisonerInPrisonSummary): InOutStatus { | ||
val dateOfInterest = submissionDateTime.toLocalDateTime() | ||
|
||
val enclosingPeriod = inPrisonSummary.prisonPeriod?.firstOrNull { period -> | ||
period.entryDate.isBefore(dateOfInterest) && | ||
(period.releaseDate == null || period.releaseDate.isAfter(dateOfInterest)) | ||
} ?: return InOutStatus.OUT | ||
|
||
val inPrison = enclosingPeriod.movementDates.any { movement -> | ||
movement.dateInToPrison!!.isBefore(dateOfInterest) && | ||
(movement.dateOutOfPrison == null || movement.dateOutOfPrison.isAfter(dateOfInterest)) | ||
} | ||
|
||
return if (inPrison) InOutStatus.IN else InOutStatus.OUT | ||
} |
18 changes: 18 additions & 0 deletions
18
...gov/justice/digital/hmpps/approvedpremisesapi/model/prisonsapi/PrisonerInPrisonSummary.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,18 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.model.prisonsapi | ||
|
||
import java.time.LocalDateTime | ||
|
||
data class PrisonerInPrisonSummary( | ||
val prisonPeriod: List<PrisonPeriod>?, | ||
) | ||
|
||
data class PrisonPeriod( | ||
val entryDate: LocalDateTime, | ||
val releaseDate: LocalDateTime? = null, | ||
val movementDates: List<SignificantMovements>, | ||
) | ||
|
||
data class SignificantMovements( | ||
val dateInToPrison: LocalDateTime? = null, | ||
val dateOutOfPrison: LocalDateTime? = null, | ||
) |
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
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
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
1 change: 1 addition & 0 deletions
1
...main/resources/db/migration/all/20240103143423__add_inmate_inout_status_on_submission.sql
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 @@ | ||
ALTER TABLE approved_premises_applications ADD COLUMN "inmate_in_out_status_on_submission" TEXT NULL; |
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.