From 43c96b8f386ec86fd988a0efeed45893cc51f824 Mon Sep 17 00:00:00 2001 From: stevomcallister Date: Tue, 24 Oct 2023 16:35:51 +0100 Subject: [PATCH] PI-1583 added booking confirmed processing --- .../justice/digital/hmpps/data/DataLoader.kt | 1 + .../data/generator/ContactTypeGenerator.kt | 5 ++ .../resources/messages/booking-confirmed.json | 18 +++++ .../__files/cas3-booking-confirmed.json | 17 +++++ .../simulations/mappings/cas3-api.json | 13 ++++ .../digital/hmpps/CASIntegrationTest.kt | 16 +++++ .../approvedpremesis/Cas3ApiClient.kt | 2 + .../approvedpremesis/EventDetails.kt | 9 +++ .../integrations/delius/ContactService.kt | 66 ++++++++++++++----- .../integrations/delius/entity/Contact.kt | 1 + .../digital/hmpps/messaging/Handler.kt | 4 ++ 11 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 projects/cas3-and-delius/src/dev/resources/messages/booking-confirmed.json create mode 100644 projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-confirmed.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 39afb5cd97..f037aa3133 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 @@ -31,6 +31,7 @@ class DataLoader( BusinessInteractionGenerator.UPDATE_CONTACT, ContactTypeGenerator.EARS_CONTACT_TYPE, ContactTypeGenerator.EACA_CONTACT_TYPE, + ContactTypeGenerator.EACO_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 5351ada2c7..c17d028a7b 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 @@ -13,4 +13,9 @@ object ContactTypeGenerator { "EACA", false ) + val EACO_CONTACT_TYPE = ContactType( + IdGenerator.getAndIncrement(), + "EACO", + false + ) } diff --git a/projects/cas3-and-delius/src/dev/resources/messages/booking-confirmed.json b/projects/cas3-and-delius/src/dev/resources/messages/booking-confirmed.json new file mode 100644 index 0000000000..ff22563d86 --- /dev/null +++ b/projects/cas3-and-delius/src/dev/resources/messages/booking-confirmed.json @@ -0,0 +1,18 @@ +{ + "eventType": "accommodation.cas3.booking.confirmed", + "version": 1, + "description": "A cas3 booking has been confirmed", + "detailUrl": "http://localhost:{wiremock.port}/cas3-api/events/booking-confirmed/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-confirmed.json b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-confirmed.json new file mode 100644 index 0000000000..2eeb063b4a --- /dev/null +++ b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-booking-confirmed.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", + "expectedArrivedAt": "2022-11-30T12:00:00", + "notes": "The actual time of arrival is unknown so has been defaulted to midday." + } +} \ 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 2a4549f4bd..bf145d28de 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 @@ -25,6 +25,19 @@ "status": 200, "bodyFileName": "cas3-booking-cancelled.json" } + }, + { + "request": { + "method": "GET", + "urlPath": "/cas3-api/events/booking-confirmed/1234" + }, + "response": { + "headers": { + "Content-Type": "application/json" + }, + "status": 200, + "bodyFileName": "cas3-booking-confirmed.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 87b1502b16..0ff5ed98d8 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 @@ -68,4 +68,20 @@ internal class CASIntegrationTest { MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EACA")) } + + @Test + fun `booking confirmed message is processed correctly`() { + val event = prepEvent("booking-confirmed", 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("EACO")) + } } 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 acd6bf91cb..5a024be19e 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 @@ -14,4 +14,6 @@ interface Cas3ApiClient { @GetMapping fun getApplicationSubmittedDetails(uri: URI): EventDetails @GetMapping fun getBookingCancelledDetails(uri: URI): EventDetails + + @GetMapping fun getBookingConfirmedDetails(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 30b3423e90..2ca4853a50 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 @@ -21,3 +21,12 @@ data class BookingCancelled( val cancellationReason: String, val cancellationContext: String? ) + +data class BookingConfirmed( + val applicationId: String, + val applicationUrl: String?, + val bookingId: String, + val bookingUrl: String, + val expectedArrivedAt: ZonedDateTime, + val notes: 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 0469f4206a..d5c34d3cf4 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 @@ -9,12 +9,14 @@ import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractio 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.BOOKING_CONFIRMED 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 import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonRepository import uk.gov.justice.digital.hmpps.integrations.delius.entity.getByCrn import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent +import uk.gov.justice.digital.hmpps.messaging.crn import uk.gov.justice.digital.hmpps.messaging.url import uk.gov.justice.digital.hmpps.telemetry.TelemetryService import java.time.ZonedDateTime @@ -30,36 +32,70 @@ class ContactService( private val cas3ApiClient: Cas3ApiClient ) : AuditableService(auditedInteractionService) { - fun createReferralSubmitted(event: HmppsDomainEvent) = audit(BusinessInteractionCode.UPDATE_CONTACT) { + fun createReferralSubmitted(event: HmppsDomainEvent) { val details = cas3ApiClient.getApplicationSubmittedDetails(event.url()).eventDetails - val crn = event.personReference.findCrn() + val crn = event.crn() val externalReference = details.applicationId - val person = personRepository.getByCrn(crn!!) 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, "")) + createContact( + event.occurredAt, + crn, + "", + REFERRAL_SUBMITTED, + externalReference + ) } } - fun createBookingCancelled(event: HmppsDomainEvent) = audit(BusinessInteractionCode.UPDATE_CONTACT) { + fun createBookingCancelled(event: HmppsDomainEvent) { val details = cas3ApiClient.getBookingCancelledDetails(event.url()).eventDetails - val crn = event.personReference.findCrn() + val crn = event.crn() val externalReference = details.bookingId - val person = personRepository.getByCrn(crn!!) + createContact( + event.occurredAt, + crn, + "${details.cancellationReason} ${details.cancellationContext} ${details.bookingUrl}", + BOOKING_CANCELLED, + externalReference + ) + } - contactRepository.save( - newContact( - event.occurredAt, - person.id, - BOOKING_CANCELLED, - externalReference, - "${details.cancellationReason} ${details.cancellationContext} ${details.bookingUrl}" - ) + fun createBookingConfirmed(event: HmppsDomainEvent) { + val details = cas3ApiClient.getBookingConfirmedDetails(event.url()).eventDetails + val crn = event.crn() + val externalReference = details.bookingId + createContact( + event.occurredAt, + crn, + "${details.expectedArrivedAt} ${details.notes} ${details.bookingUrl}", + BOOKING_CONFIRMED, + externalReference ) } + fun createContact( + contactDate: ZonedDateTime, + crn: String, + notes: String, + contactTypeCode: String, + externalReference: String + ) = + audit(BusinessInteractionCode.UPDATE_CONTACT) { + val person = personRepository.getByCrn(crn) + contactRepository.save( + newContact( + contactDate, + person.id, + contactTypeCode, + externalReference, + notes + ) + ) + } + fun newContact( occurredAt: ZonedDateTime, personId: Long, 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 f6dbddc1da..deea1e5bb1 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 @@ -114,6 +114,7 @@ class ContactType( companion object { const val REFERRAL_SUBMITTED = "EARS" const val BOOKING_CANCELLED = "EACA" + const val BOOKING_CONFIRMED = "EACO" } } 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 9d14a69e3a..eb14785b22 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 @@ -27,6 +27,10 @@ class Handler( contactService.createBookingCancelled(event) telemetryService.trackEvent("ApplicationSubmitted", event.telemetryProperties()) } + "accommodation.cas3.booking.confirmed" -> { + contactService.createBookingConfirmed(event) + telemetryService.trackEvent("ApplicationSubmitted", event.telemetryProperties()) + } else -> throw IllegalArgumentException("Unexpected event type ${event.eventType}") }