Skip to content

Commit

Permalink
PI-1583 added booking cancelled processing (#2440)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister authored Oct 24, 2023
1 parent c2e814c commit 343591f
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
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",
"cancellationReason": "Not appropriate",
"cancellationContext": "Suitability"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ import java.net.URI
)
interface Cas3ApiClient {
@GetMapping fun getApplicationSubmittedDetails(uri: URI): EventDetails<ApplicationSubmitted>

@GetMapping fun getBookingCancelledDetails(uri: URI): EventDetails<BookingCancelled>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ data class EventDetails<T>(
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?
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -57,7 +81,7 @@ class ContactService(
return Contact(
offenderId = personId,
type = contactType,
notes = "",
notes = notes,
date = occurredAt.toLocalDate(),
startTime = occurredAt,
isSensitive = contactType.isSensitive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class ContactType(
) {
companion object {
const val REFERRAL_SUBMITTED = "EARS"
const val BOOKING_CANCELLED = "EACA"
}
}

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

0 comments on commit 343591f

Please sign in to comment.