From 1adf116b0c96da1555a2f9ff7ef0cfb648d3ad6f Mon Sep 17 00:00:00 2001 From: stevomcallister <78762879+stevomcallister@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:40:34 +0000 Subject: [PATCH] PI-1596 Added address processing (#2463) --- .../__files/cas3-person-departed-update.json | 8 +++++++- .../simulations/__files/cas3-person-departed.json | 8 +++++++- .../justice/digital/hmpps/CASIntegrationTest.kt | 3 +++ .../integrations/approvedpremesis/EventDetails.kt | 12 ++++++++++-- .../hmpps/integrations/delius/AddressService.kt | 14 ++++++++++++++ .../gov/justice/digital/hmpps/messaging/Handler.kt | 7 +++++-- 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed-update.json b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed-update.json index e103f3d5c0..eb76a59f49 100644 --- a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed-update.json +++ b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed-update.json @@ -12,6 +12,12 @@ "bookingId": "14c80733-4b6d-4f35-b724-66955aac320e", "bookingUrl": "https://approved-premises-dev.hmpps.service.justice.gov.uk/someURLtoTheBooking", "departedAt": "2022-11-30T12:00:00", - "notes": "Person departed." + "notes": "Person departed.", + "reason": "Left of own accord | Other", + "reasonDetail": "", + "moveOnCategory": { + "description": "Friends and family", + "label": "" + } } } \ No newline at end of file diff --git a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed.json b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed.json index 181b6df54e..8bbee6db88 100644 --- a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed.json +++ b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-departed.json @@ -12,6 +12,12 @@ "bookingId": "14c80733-4b6d-4f35-b724-66955aac320e", "bookingUrl": "https://approved-premises-dev.hmpps.service.justice.gov.uk/someURLtoTheBooking", "departedAt": "2022-11-30T12:00:00", - "notes": "Person departed." + "notes": "Person departed.", + "reason": "Left of own accord | Other", + "reasonDetail": "", + "moveOnCategory": { + "description": "Friends and family", + "label": "" + } } } \ 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 8f4568097a..9e60e97d80 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 @@ -170,6 +170,9 @@ internal class CASIntegrationTest { val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn) MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EADP")) + val person = personRepository.findByCrnAndSoftDeletedIsFalse(event.message.crn()) + val address = addressRepository.findAll().filter { it.personId == person?.id }[0] + MatcherAssert.assertThat(address!!.status.code, Matchers.equalTo("P")) } @Test 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 7758b01692..dae1760509 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 @@ -109,9 +109,17 @@ data class PersonDeparted( val bookingId: String, val bookingUrl: String, val departedAt: ZonedDateTime, - val notes: String + val notes: String, + val reason: String, + val reasonDetail: String, + val moveOnCategory: Category + ) : Cas3Event { override val urn = "urn:hmpps:cas3:person-departed:$bookingId" - override val noteText = "${DeliusDateFormatter.format(departedAt)} $notes $bookingUrl" + override val noteText = "${DeliusDateFormatter.format(departedAt)} $notes $reason $reasonDetail ${moveOnCategory.description}" override val contactTypeCode = ContactType.PERSON_DEPARTED } + +data class Category( + val description: String +) diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt index ca93d58e4a..674e2fc6f9 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt @@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.integrations.delius import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.PersonArrived +import uk.gov.justice.digital.hmpps.integrations.delius.entity.AddressTypeCode import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddress import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddressRepository @@ -10,6 +11,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.entity.cas3AddressType import uk.gov.justice.digital.hmpps.integrations.delius.entity.mainAddressStatus import uk.gov.justice.digital.hmpps.integrations.delius.entity.previousAddressStatus import java.time.LocalDate +import java.time.ZonedDateTime @Service class AddressService( @@ -30,6 +32,18 @@ class AddressService( } } + fun endMainCAS3Address(person: Person, endDate: ZonedDateTime) { + val currentMain = personAddressRepository.findMainAddress(person.id) + currentMain?.apply { + if (currentMain.type.code == AddressTypeCode.CAS3.code) { + val previousStatus = referenceDataRepository.previousAddressStatus() + currentMain.status = previousStatus + currentMain.endDate = endDate.toLocalDate() + personAddressRepository.save(currentMain) + } + } + } + private fun toPersonAddress(person: Person, details: PersonArrived): PersonAddress { val addressLines = details.premises.addressLines return PersonAddress( 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 5e1ec34c17..a17228d167 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 @@ -65,9 +65,12 @@ class Handler( } "accommodation.cas3.person.departed" -> { - contactService.createContact(event.crn()) { - cas3ApiClient.getPersonDeparted(event.url()) + val person = personRepository.getByCrn(event.crn()) + val detail = cas3ApiClient.getPersonDeparted(event.url()) + contactService.createContact(event.crn(), person) { + detail } + addressService.endMainCAS3Address(person, detail.eventDetails.departedAt) telemetryService.trackEvent("PersonDeparted", event.telemetryProperties()) }