Skip to content

Commit

Permalink
PI-1583 added booking confirmed processing
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Oct 24, 2023
1 parent 14b9a54 commit 43c96b8
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ object ContactTypeGenerator {
"EACA",
false
)
val EACO_CONTACT_TYPE = ContactType(
IdGenerator.getAndIncrement(),
"EACO",
false
)
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -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."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface Cas3ApiClient {
@GetMapping fun getApplicationSubmittedDetails(uri: URI): EventDetails<ApplicationSubmitted>

@GetMapping fun getBookingCancelledDetails(uri: URI): EventDetails<BookingCancelled>

@GetMapping fun getBookingConfirmedDetails(uri: URI): EventDetails<BookingConfirmed>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class ContactType(
companion object {
const val REFERRAL_SUBMITTED = "EARS"
const val BOOKING_CANCELLED = "EACA"
const val BOOKING_CONFIRMED = "EACO"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
Expand Down

0 comments on commit 43c96b8

Please sign in to comment.