From cf09ead278b01578e6da326b4f3e2a6b14af60c8 Mon Sep 17 00:00:00 2001 From: Marcus Aspin Date: Tue, 8 Oct 2024 16:25:11 +0100 Subject: [PATCH] PI-2574 Handle missing booking from Prison API (#4287) --- .../digital/hmpps/PcstdIntegrationTest.kt | 21 +++++++++++++++++++ .../digital/hmpps/messaging/Handler.kt | 10 ++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/projects/prison-custody-status-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/PcstdIntegrationTest.kt b/projects/prison-custody-status-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/PcstdIntegrationTest.kt index 04d44020eb..1ccf2041f5 100644 --- a/projects/prison-custody-status-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/PcstdIntegrationTest.kt +++ b/projects/prison-custody-status-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/PcstdIntegrationTest.kt @@ -1,5 +1,6 @@ package uk.gov.justice.digital.hmpps +import com.github.tomakehurst.wiremock.client.WireMock.* import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.* @@ -670,4 +671,24 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() { ) } } + + @Test + fun `missing booking is ignored`() { + val notification = NotificationGenerator.PRISONER_RELEASED + wireMockServer.stubFor( + get(urlPathEqualTo("/api/bookings/offenderNo/${BookingGenerator.RELEASED.personReference}")) + .willReturn(aResponse().withStatus(404)) + ) + + channelManager.getChannel(queueName).publishAndWait(notification) + + verifyTelemetry("BookingNotFound") { + mapOf( + "occurredAt" to notification.message.occurredAt.toString(), + "nomsNumber" to "A0001AA", + "institution" to "WSI", + "details" to "Movement reason code NCS" + ) + } + } } diff --git a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt index 509015a9a3..7b10965cce 100644 --- a/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt +++ b/projects/prison-custody-status-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt @@ -6,6 +6,7 @@ import org.openfolder.kotlinasyncapi.annotation.channel.Message import org.openfolder.kotlinasyncapi.annotation.channel.Publish import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional +import org.springframework.web.client.HttpClientErrorException import uk.gov.justice.digital.hmpps.converter.NotificationConverter import uk.gov.justice.digital.hmpps.datetime.EuropeLondon import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException @@ -115,9 +116,12 @@ class Handler( } } - private fun PrisonApiClient.bookingFromNomsId(nomsId: String) = - getBookingByNomsId(nomsId).takeIf { it.active || it.movementType == "REL" } - ?: throw IgnorableMessageException("BookingInactive", mapOf("nomsNumber" to nomsId)) + private fun PrisonApiClient.bookingFromNomsId(nomsId: String) = try { + getBookingByNomsId(nomsId) + } catch (e: HttpClientErrorException.NotFound) { + throw IgnorableMessageException("BookingNotFound", mapOf("nomsNumber" to nomsId)) + }.takeIf { it.active || it.movementType == "REL" } + ?: throw IgnorableMessageException("BookingInactive", mapOf("nomsNumber" to nomsId)) } fun HmppsDomainEvent.prisonId() = additionalInformation["prisonId"] as String?