From 343591f93ad5fca3702522d3fc29d772d350488a Mon Sep 17 00:00:00 2001 From: stevomcallister <78762879+stevomcallister@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:43:50 +0100 Subject: [PATCH] PI-1583 added booking cancelled processing (#2440) --- .../justice/digital/hmpps/data/DataLoader.kt | 3 +- .../data/generator/ContactTypeGenerator.kt | 7 +++- .../resources/messages/booking-cancelled.json | 18 ++++++++++ .../__files/cas3-booking-cancelled.json | 17 ++++++++++ .../simulations/mappings/cas3-api.json | 13 +++++++ .../digital/hmpps/CASIntegrationTest.kt | 19 +++++++++-- .../approvedpremesis/Cas3ApiClient.kt | 2 ++ .../approvedpremesis/EventDetails.kt | 9 +++++ .../integrations/delius/ContactService.kt | 34 ++++++++++++++++--- .../integrations/delius/entity/Contact.kt | 1 + .../digital/hmpps/messaging/Handler.kt | 4 +++ 11 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 projects/cas3-and-delius/src/dev/resources/messages/booking-cancelled.json create mode 100644 projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-cancelled.json diff --git a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index d184ad94c3..39afb5cd97 100644 --- a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -29,7 +29,8 @@ class DataLoader( override fun onApplicationEvent(are: ApplicationReadyEvent) { em.saveAll( BusinessInteractionGenerator.UPDATE_CONTACT, - ContactTypeGenerator.CONTACT_TYPE, + ContactTypeGenerator.EARS_CONTACT_TYPE, + ContactTypeGenerator.EACA_CONTACT_TYPE, PersonGenerator.PERSON_CRN, PersonGenerator.generatePersonManager(PersonGenerator.PERSON_CRN) ) diff --git a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactTypeGenerator.kt b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactTypeGenerator.kt index 66a7f173d6..5351ada2c7 100644 --- a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactTypeGenerator.kt +++ b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactTypeGenerator.kt @@ -3,9 +3,14 @@ package uk.gov.justice.digital.hmpps.data.generator import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactType object ContactTypeGenerator { - val CONTACT_TYPE = ContactType( + val EARS_CONTACT_TYPE = ContactType( IdGenerator.getAndIncrement(), "EARS", false ) + val EACA_CONTACT_TYPE = ContactType( + IdGenerator.getAndIncrement(), + "EACA", + false + ) } diff --git a/projects/cas3-and-delius/src/dev/resources/messages/booking-cancelled.json b/projects/cas3-and-delius/src/dev/resources/messages/booking-cancelled.json new file mode 100644 index 0000000000..1f99089f5d --- /dev/null +++ b/projects/cas3-and-delius/src/dev/resources/messages/booking-cancelled.json @@ -0,0 +1,18 @@ +{ + "eventType": "accommodation.cas3.booking.cancelled", + "version": 1, + "description": "A cas3 booking has been cancelled", + "detailUrl": "http://localhost:{wiremock.port}/cas3-api/events/booking-cancelled/1234", + "occurredAt": "2022-12-04T10:42:43+00:00", + "additionalInformation": { + "applicationId": "68df9f6c-3fcb-4ec6-8fcf-96551cd9b080" + }, + "personReference": { + "identifiers": [ + { + "type": "CRN", + "value": "A000001" + } + ] + } +} \ No newline at end of file diff --git a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-cancelled.json b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-cancelled.json new file mode 100644 index 0000000000..e21baefcab --- /dev/null +++ b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-cancelled.json @@ -0,0 +1,17 @@ +{ + "id": "364145f9-0af8-488e-9901-b4c46cd9ba37", + "timestamp": "2022-11-30T14:53:44", + "eventType": "accommodation.cas3.booking.cancelled", + "eventDetails": { + "applicationId": "68df9f6c-3fcb-4ec6-8fcf-96551cd9b080", + "applicationUrl": "https://approved-premises-dev.hmpps.service.justice.gov.uk/booking/68df9f6c-3fcb-4ec6-8fcf-96551cd9b080", + "personReference": { + "crn": "A000001", + "noms": "A0001AA" + }, + "bookingId": "14c80733-4b6d-4f35-b724-66955aac320c", + "bookingUrl": "https://approved-premises-dev.hmpps.service.justice.gov.uk/someURLtoTheBooking", + "cancellationReason": "Not appropriate", + "cancellationContext": "Suitability" + } +} \ No newline at end of file diff --git a/projects/cas3-and-delius/src/dev/resources/simulations/mappings/cas3-api.json b/projects/cas3-and-delius/src/dev/resources/simulations/mappings/cas3-api.json index 40ef18ea15..2a4549f4bd 100644 --- a/projects/cas3-and-delius/src/dev/resources/simulations/mappings/cas3-api.json +++ b/projects/cas3-and-delius/src/dev/resources/simulations/mappings/cas3-api.json @@ -12,6 +12,19 @@ "status": 200, "bodyFileName": "cas3-referral-submitted.json" } + }, + { + "request": { + "method": "GET", + "urlPath": "/cas3-api/events/booking-cancelled/1234" + }, + "response": { + "headers": { + "Content-Type": "application/json" + }, + "status": 200, + "bodyFileName": "cas3-booking-cancelled.json" + } } ] } \ No newline at end of file diff --git a/projects/cas3-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CASIntegrationTest.kt b/projects/cas3-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CASIntegrationTest.kt index bb6a9ed123..87b1502b16 100644 --- a/projects/cas3-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CASIntegrationTest.kt +++ b/projects/cas3-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CASIntegrationTest.kt @@ -38,8 +38,7 @@ internal class CASIntegrationTest { lateinit var telemetryService: TelemetryService @Test - fun `message is processed correctly`() { - // Given an application-submitted event + fun `referral submitted message is processed correctly`() { val event = prepEvent("referral-submitted", wireMockServer.port()) // When it is received @@ -53,4 +52,20 @@ internal class CASIntegrationTest { MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EARS")) } + + @Test + fun `booking cancelled message is processed correctly`() { + val event = prepEvent("booking-cancelled", wireMockServer.port()) + + // When it is received + channelManager.getChannel(queueName).publishAndWait(event) + + // Then it is logged to telemetry + Mockito.verify(telemetryService).notificationReceived(event) + + val contact = + contactRepository.getByExternalReference("14c80733-4b6d-4f35-b724-66955aac320c") + + MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EACA")) + } } diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/Cas3ApiClient.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/Cas3ApiClient.kt index 840795b2c5..acd6bf91cb 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/Cas3ApiClient.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/Cas3ApiClient.kt @@ -12,4 +12,6 @@ import java.net.URI ) interface Cas3ApiClient { @GetMapping fun getApplicationSubmittedDetails(uri: URI): EventDetails + + @GetMapping fun getBookingCancelledDetails(uri: URI): EventDetails } diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/EventDetails.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/EventDetails.kt index 95191c2d3c..30b3423e90 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/EventDetails.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/EventDetails.kt @@ -12,3 +12,12 @@ data class EventDetails( data class ApplicationSubmitted( val applicationId: String ) + +data class BookingCancelled( + val applicationId: String, + val applicationUrl: String?, + val bookingId: String, + val bookingUrl: String, + val cancellationReason: String, + val cancellationContext: String? +) diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt index 8bd3ce2a29..0469f4206a 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt @@ -8,6 +8,7 @@ import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.Cas3ApiClient import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode import uk.gov.justice.digital.hmpps.integrations.delius.entity.Contact import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactRepository +import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactType.Companion.BOOKING_CANCELLED import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactType.Companion.REFERRAL_SUBMITTED import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactTypeRepository import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonManagerRepository @@ -38,15 +39,38 @@ class ContactService( if (contactRepository.getByExternalReference(externalReference) != null) { telemetryService.trackEvent("Duplicate ApplicationSubmitted event received for crn $crn") } else { - contactRepository.save(newContact(event.occurredAt, person.id, REFERRAL_SUBMITTED, externalReference)) + contactRepository.save(newContact(event.occurredAt, person.id, REFERRAL_SUBMITTED, externalReference, "")) } } - fun newContact(occurredAt: ZonedDateTime, personId: Long, typeCode: String, reference: String): Contact { - val contactType = contactTypeRepository.findByCode(REFERRAL_SUBMITTED) ?: throw NotFoundException( + fun createBookingCancelled(event: HmppsDomainEvent) = audit(BusinessInteractionCode.UPDATE_CONTACT) { + val details = cas3ApiClient.getBookingCancelledDetails(event.url()).eventDetails + val crn = event.personReference.findCrn() + val externalReference = details.bookingId + val person = personRepository.getByCrn(crn!!) + + contactRepository.save( + newContact( + event.occurredAt, + person.id, + BOOKING_CANCELLED, + externalReference, + "${details.cancellationReason} ${details.cancellationContext} ${details.bookingUrl}" + ) + ) + } + + fun newContact( + occurredAt: ZonedDateTime, + personId: Long, + typeCode: String, + reference: String, + notes: String + ): Contact { + val contactType = contactTypeRepository.findByCode(typeCode) ?: throw NotFoundException( "ContactType", "code", - REFERRAL_SUBMITTED + typeCode ) val comDetails = personManagerRepository.findActiveManager(personId) ?: throw NotFoundException( "PersonManager", @@ -57,7 +81,7 @@ class ContactService( return Contact( offenderId = personId, type = contactType, - notes = "", + notes = notes, date = occurredAt.toLocalDate(), startTime = occurredAt, isSensitive = contactType.isSensitive, diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Contact.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Contact.kt index f6b5c16457..f6dbddc1da 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Contact.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Contact.kt @@ -113,6 +113,7 @@ class ContactType( ) { companion object { const val REFERRAL_SUBMITTED = "EARS" + const val BOOKING_CANCELLED = "EACA" } } diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt index 3ec1a3e203..9d14a69e3a 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt @@ -23,6 +23,10 @@ class Handler( contactService.createReferralSubmitted(event) telemetryService.trackEvent("ApplicationSubmitted", event.telemetryProperties()) } + "accommodation.cas3.booking.cancelled" -> { + contactService.createBookingCancelled(event) + telemetryService.trackEvent("ApplicationSubmitted", event.telemetryProperties()) + } else -> throw IllegalArgumentException("Unexpected event type ${event.eventType}") }