From b68700e564f55854d165b792493b61aa50eaddee Mon Sep 17 00:00:00 2001 From: stevomcallister Date: Mon, 30 Oct 2023 12:16:19 +0000 Subject: [PATCH] PI-1585 Added address processing --- .../data/generator/AddressRDGenerator.kt | 2 +- .../hmpps/data/generator/DatasetGenerator.kt | 2 +- .../digital/hmpps/CASIntegrationTest.kt | 1 - .../approvedpremesis/EventDetails.kt | 17 ++++++++++ .../integrations/delius/AddressService.kt | 27 +++++++++------- .../delius/entity/PersonAddress.kt | 2 +- .../delius/entity/ReferenceData.kt | 1 - .../digital/hmpps/messaging/Handler.kt | 9 +++--- .../approvedpremesis/AddressTest.kt | 32 +++++++++++++++++++ 9 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 projects/cas3-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/AddressTest.kt diff --git a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt index d617acaba3..020ccda32a 100644 --- a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt +++ b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt @@ -13,4 +13,4 @@ object AddressRDGenerator { description: String = "Description of $code", id: Long = IdGenerator.getAndIncrement() ) = ReferenceData(id, code, description, datasetId) -} \ No newline at end of file +} diff --git a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt index df2430f9d4..51634cf6ec 100644 --- a/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt +++ b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt @@ -6,4 +6,4 @@ import uk.gov.justice.digital.hmpps.integrations.delius.entity.DatasetCode object DatasetGenerator { val ADDRESS_TYPE = Dataset(IdGenerator.getAndIncrement(), DatasetCode.ADDRESS_TYPE) val ADDRESS_STATUS = Dataset(IdGenerator.getAndIncrement(), DatasetCode.ADDRESS_STATUS) -} \ 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 9f431e82dd..219ff0eb87 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 @@ -153,7 +153,6 @@ internal class CASIntegrationTest { MatcherAssert.assertThat(address.streetName, Matchers.equalTo(eventDetails.eventDetails.premises.addressLine1)) MatcherAssert.assertThat(address.county, Matchers.equalTo(eventDetails.eventDetails.premises.region)) MatcherAssert.assertThat(address.postcode, Matchers.equalTo(eventDetails.eventDetails.premises.postcode)) - } @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 4beb99c34a..7758b01692 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 @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.integrations.approvedpremesis import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactType import java.time.ZonedDateTime +import java.util.LinkedList data class EventDetails( val id: String, @@ -84,6 +85,22 @@ data class Address( val postcode: String, val town: String?, val region: String +) { + val addressLines: AddressLines + get() { + val lines = LinkedList(addressLine1.chunked(35) + (addressLine2?.chunked(35) ?: listOf())) + return if (lines.size < 3) { + AddressLines(null, lines.pop(), lines.firstOrNull()) + } else { + AddressLines(lines.pop(), lines.pop(), lines.pop()) + } + } +} + +data class AddressLines( + val buildingName: String?, + val streetName: String, + val district: String? ) data class PersonDeparted( 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 d92348f804..ca93d58e4a 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 @@ -30,15 +30,20 @@ class AddressService( } } - private fun toPersonAddress(person: Person, details: PersonArrived) = PersonAddress( - 0, - person.id, - referenceDataRepository.cas3AddressType(), - referenceDataRepository.mainAddressStatus(), - details.premises.addressLine1, - details.premises.town, - details.premises.region, - details.premises.postcode, - startDate = details.arrivedAt.toLocalDate() - ) + private fun toPersonAddress(person: Person, details: PersonArrived): PersonAddress { + val addressLines = details.premises.addressLines + return PersonAddress( + 0, + person.id, + referenceDataRepository.cas3AddressType(), + referenceDataRepository.mainAddressStatus(), + buildingName = addressLines.buildingName, + streetName = addressLines.streetName, + district = addressLines.district, + town = details.premises.town, + county = details.premises.region, + postcode = details.premises.postcode, + startDate = details.arrivedAt.toLocalDate() + ) + } } diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt index 46c0d0fae9..babec303a3 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt @@ -46,7 +46,7 @@ class PersonAddress( val postcode: String?, val telephoneNumber: String? = null, val buildingName: String? = null, - val district: String? = null, + val district: String? = null, val addressNumber: String? = null, @Convert(converter = YesNoConverter::class) val noFixedAbode: Boolean? = false, diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt index 9158df1339..55d32106cd 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt @@ -55,7 +55,6 @@ enum class AddressTypeCode(val code: String) { CAS3("CAS3") } - @Converter class DatasetCodeConverter : AttributeConverter { override fun convertToDatabaseColumn(attribute: DatasetCode): String = attribute.value 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 76d40d770a..5e1ec34c17 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 @@ -20,7 +20,7 @@ class Handler( private val contactService: ContactService, private val addressService: AddressService, private val cas3ApiClient: Cas3ApiClient, - private val personRepository: PersonRepository, + private val personRepository: PersonRepository ) : NotificationHandler { override fun handle(notification: Notification) { telemetryService.notificationReceived(notification) @@ -56,11 +56,10 @@ class Handler( "accommodation.cas3.person.arrived" -> { val person = personRepository.getByCrn(event.crn()) - contactService.createContact(event.crn(),person) { - cas3ApiClient.getPersonArrived(event.url()) - } - val detail = cas3ApiClient.getPersonArrived(event.url()) + contactService.createContact(event.crn(), person) { + detail + } addressService.updateMainAddress(person, detail.eventDetails) telemetryService.trackEvent("PersonArrived", event.telemetryProperties()) } diff --git a/projects/cas3-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/AddressTest.kt b/projects/cas3-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/AddressTest.kt new file mode 100644 index 0000000000..31c0233a5a --- /dev/null +++ b/projects/cas3-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/integrations/approvedpremesis/AddressTest.kt @@ -0,0 +1,32 @@ +package uk.gov.justice.digital.hmpps.integrations.approvedpremesis + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource + +class AddressTest { + + @ParameterizedTest + @MethodSource("addressLines") + fun testAddressLines(address: Address, addressLines: AddressLines) { + assertThat(address.addressLines, equalTo(addressLines)) + } + + companion object { + + private val address = Address(" ", null, "NN1 1NN", "town", "region") + private const val thirtyFive = "9 this is quite a long address line" + private const val extension = " with an even longer bit on the end" + private const val seventy = thirtyFive + extension + + @JvmStatic + fun addressLines() = listOf( + Arguments.of(address.copy(addressLine1 = thirtyFive), AddressLines(null, thirtyFive, null)), + Arguments.of(address.copy(addressLine1 = thirtyFive, addressLine2 = extension), AddressLines(null, thirtyFive, extension)), + Arguments.of(address.copy(addressLine1 = seventy), AddressLines(null, thirtyFive, extension)), + Arguments.of(address.copy(addressLine1 = seventy, addressLine2 = "with an extra line"), AddressLines(thirtyFive, extension, "with an extra line")) + ) + } +}