diff --git a/projects/prison-custody-status-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/NotificationGenerator.kt b/projects/prison-custody-status-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/NotificationGenerator.kt index fde0fc64ba..875007bb89 100644 --- a/projects/prison-custody-status-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/NotificationGenerator.kt +++ b/projects/prison-custody-status-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/NotificationGenerator.kt @@ -25,5 +25,6 @@ object NotificationGenerator { val PRISONER_ETR_IN_CUSTODY = ResourceLoader.notification("prisoner-received-etr-custody") val PRISONER_ECSLIRC_IN_CUSTODY = ResourceLoader.notification("prisoner-received-ecslirc-custody") val PRISONER_ADMIN_MERGE = ResourceLoader.notification("prisoner-released-admin-merge") - val PRISONER_RELEASED_HISTORIC = ResourceLoader.notification("prisoner-released-historic") + val PRISONER_RELEASED_HISTORIC = + ResourceLoader.notification("prisoner-released-hospital-historic") } diff --git a/projects/prison-custody-status-to-delius/src/dev/resources/messages/prisoner-released-historic.json b/projects/prison-custody-status-to-delius/src/dev/resources/messages/prisoner-released-hospital-historic.json similarity index 50% rename from projects/prison-custody-status-to-delius/src/dev/resources/messages/prisoner-released-historic.json rename to projects/prison-custody-status-to-delius/src/dev/resources/messages/prisoner-released-hospital-historic.json index b9c3d8c394..6d581dfd40 100644 --- a/projects/prison-custody-status-to-delius/src/dev/resources/messages/prisoner-released-historic.json +++ b/projects/prison-custody-status-to-delius/src/dev/resources/messages/prisoner-released-hospital-historic.json @@ -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": "", diff --git a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/PrisonerMovement.kt b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/PrisonerMovement.kt index 98639cd9a3..1dfa3eebba 100644 --- a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/PrisonerMovement.kt +++ b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/PrisonerMovement.kt @@ -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 { @@ -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()) diff --git a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateLocationAction.kt b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateLocationAction.kt index 3b57af6506..64cfe943f8 100644 --- a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateLocationAction.kt +++ b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateLocationAction.kt @@ -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 } diff --git a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateStatusAction.kt b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateStatusAction.kt index 88ce12b46f..33aa549171 100644 --- a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateStatusAction.kt +++ b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/actions/UpdateStatusAction.kt @@ -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)