Skip to content

Commit

Permalink
PI-2733 Only ignore historical updates to status/location for IRC and…
Browse files Browse the repository at this point in the history
… Secure Hospital releases (#4540)

* PI-2733 Only ignore historical updates to status/location for IRC and Secure Hospital releases

* Formatting changes

---------

Co-authored-by: probation-integration-bot[bot] <177347787+probation-integration-bot[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 93fdd97 commit b3fdb81
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ object NotificationGenerator {
val PRISONER_ETR_IN_CUSTODY = ResourceLoader.notification<HmppsDomainEvent>("prisoner-received-etr-custody")
val PRISONER_ECSLIRC_IN_CUSTODY = ResourceLoader.notification<HmppsDomainEvent>("prisoner-received-ecslirc-custody")
val PRISONER_ADMIN_MERGE = ResourceLoader.notification<HmppsDomainEvent>("prisoner-released-admin-merge")
val PRISONER_RELEASED_HISTORIC = ResourceLoader.notification<HmppsDomainEvent>("prisoner-released-historic")
val PRISONER_RELEASED_HISTORIC =
ResourceLoader.notification<HmppsDomainEvent>("prisoner-released-hospital-historic")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Type": "Notification",
"MessageId": "682dd2a4-c193-461c-a8e9-cc976a961aa5",
"TopicArn": "",
"Message": "{\"eventType\":\"prison-offender-events.prisoner.released\",\"additionalInformation\":{\"nomsNumber\":\"A0001AA\",\"reason\":\"RELEASED\",\"details\":\"Movement reason code NCS\",\"nomisMovementReasonCode\":\"NCS\",\"currentLocation\":\"OUTSIDE_PRISON\",\"prisonId\":\"WSI\",\"currentPrisonStatus\":\"NOT_UNDER_PRISON_CARE\"},\"version\":1,\"occurredAt\":\"2010-01-01T07:03:50.912169+01:00\",\"publishedAt\":\"2010-01-01T08:00:33.477735848+01:00\",\"description\":\"A prisoner has been released from prison\",\"personReference\":{\"identifiers\":[{\"type\":\"NOMS\",\"value\":\"A0001AA\"}]}}",
"Message": "{\"eventType\":\"prison-offender-events.prisoner.released\",\"additionalInformation\":{\"nomsNumber\":\"A0001AA\",\"reason\":\"RELEASED\",\"source\":\"PRISON\",\"nomisMovementReasonCode\":\"HO\",\"details\":\"ACTIVE IN:REL-HO\",\"currentLocation\":\"OUTSIDE_PRISON\",\"prisonId\": null,\"currentPrisonStatus\":\"NOT_UNDER_PRISON_CARE\"},\"version\":1,\"occurredAt\":\"2010-01-01T07:03:50.912169+01:00\",\"publishedAt\":\"2010-01-01T08:00:33.477735848+01:00\",\"description\":\"A prisoner has been released to hospital\", \"personReference\":{\"identifiers\":[{\"type\":\"NOMS\",\"value\":\"A0001AA\"}]}}",
"Timestamp": "2022-05-04T07:00:33.487Z",
"SignatureVersion": "1",
"Signature": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.digital.hmpps.messaging

import uk.gov.justice.digital.hmpps.integrations.delius.custody.entity.Custody
import java.time.LocalDate
import java.time.ZonedDateTime

sealed interface PrisonerMovement {
Expand Down Expand Up @@ -77,8 +78,13 @@ fun PrisonerMovement.receivedDateValid(custody: Custody): Boolean {
return occurredAt <= ZonedDateTime.now() && (releaseDate == null || occurredAt >= releaseDate)
}

fun PrisonerMovement.statusDateValid(custody: Custody): Boolean =
occurredAt <= ZonedDateTime.now() && occurredAt.toLocalDate() >= custody.statusChangeDate
fun PrisonerMovement.occurredAfter(date: LocalDate?): Boolean =
occurredAt <= ZonedDateTime.now() && (date == null || occurredAt.toLocalDate() >= date)

fun PrisonerMovement.locationDateValid(custody: Custody): Boolean =
occurredAt <= ZonedDateTime.now() && (custody.locationChangeDate == null || occurredAt.toLocalDate() >= custody.locationChangeDate)
fun PrisonerMovement.isReceived() = this is PrisonerMovement.Received

fun PrisonerMovement.isReleased() = this is PrisonerMovement.Released &&
!(isHospitalRelease() || isIrcRelease() || isAbsconded())

fun PrisonerMovement.isToSecureUnitOutsidePrison() = this is PrisonerMovement.Released &&
(isHospitalRelease() || isIrcRelease())
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,11 @@ class UpdateLocationAction(
}
}

private fun checkPreconditions(prisonerMovement: PrisonerMovement, custody: Custody): ActionResult? {
if (prisonerMovement is PrisonerMovement.Received && custody.institution?.nomisCdeCode == prisonerMovement.toPrisonId) {
return ActionResult.Ignored("PrisonerLocationCorrect", prisonerMovement.telemetryProperties())
}

if (prisonerMovement is PrisonerMovement.Received && !prisonerMovement.receivedDateValid(custody)) {
return ActionResult.Ignored("PrisonerLocationCorrect", prisonerMovement.telemetryProperties())
}
private fun checkPreconditions(movement: PrisonerMovement, custody: Custody) = if (
(movement.isReceived() && !(movement.locationChanged(custody) && movement.receivedDateValid(custody))) ||
(movement.isReleased() && !movement.releaseDateValid(custody)) ||
(movement.isToSecureUnitOutsidePrison() && !movement.occurredAfter(custody.locationChangeDate))
) ActionResult.Ignored("PrisonerLocationCorrect", movement.telemetryProperties()) else null

if (prisonerMovement is PrisonerMovement.Released &&
!(prisonerMovement.isHospitalRelease() || prisonerMovement.isIrcRelease() || prisonerMovement.isAbsconded()) &&
!prisonerMovement.releaseDateValid(custody)
) {
return ActionResult.Ignored("PrisonerLocationCorrect", prisonerMovement.telemetryProperties())
}

if (!prisonerMovement.locationDateValid(custody)) {
return ActionResult.Ignored("PrisonerLocationCorrect", prisonerMovement.telemetryProperties())
}

return null
}
private fun PrisonerMovement.locationChanged(custody: Custody) = custody.institution?.nomisCdeCode != toPrisonId
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,11 @@ class UpdateStatusAction(
ActionResult.Success(ActionResult.Type.StatusUpdated, prisonerMovement.telemetryProperties())
}

private fun checkPreconditions(prisonerMovement: PrisonerMovement, custody: Custody): ActionResult? {
if (prisonerMovement is PrisonerMovement.Received &&
!(custody.status.canChange() && prisonerMovement.receivedDateValid(custody))
) {
return ActionResult.Ignored("PrisonerStatusCorrect", prisonerMovement.telemetryProperties())
}

if (prisonerMovement is PrisonerMovement.Released &&
!(prisonerMovement.isHospitalRelease() || prisonerMovement.isIrcRelease() || prisonerMovement.isAbsconded()) &&
!(custody.canBeReleased() && prisonerMovement.releaseDateValid(custody))
) {
return ActionResult.Ignored("PrisonerStatusCorrect", prisonerMovement.telemetryProperties())
}

if (!prisonerMovement.statusDateValid(custody)) {
return ActionResult.Ignored("PrisonerStatusCorrect", prisonerMovement.telemetryProperties())
}

return null
}
private fun checkPreconditions(movement: PrisonerMovement, custody: Custody) = if (
(movement.isReceived() && !(custody.status.canChange() && movement.receivedDateValid(custody))) ||
(movement.isReleased() && !(custody.canBeReleased() && movement.releaseDateValid(custody))) ||
(movement.isToSecureUnitOutsidePrison() && !movement.occurredAfter(custody.statusChangeDate))
) ActionResult.Ignored("PrisonerStatusCorrect", movement.telemetryProperties()) else null
}

private fun ReferenceData.canChange() = !NO_CHANGE_STATUSES.map { it.code }.contains(code)

0 comments on commit b3fdb81

Please sign in to comment.