From 34bd07e83f03cdb9617254488e7a2cd2c1e96e47 Mon Sep 17 00:00:00 2001 From: stevomcallister <78762879+stevomcallister@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:43:49 +0000 Subject: [PATCH] PI-1585 Added address processing (#2457) --- .../deploy/database/access.yml | 1 + .../justice/digital/hmpps/data/DataLoader.kt | 7 ++ .../data/generator/AddressRDGenerator.kt | 16 ++++ .../hmpps/data/generator/DatasetGenerator.kt | 9 ++ .../__files/cas3-person-arrived.json | 7 ++ .../digital/hmpps/CASIntegrationTest.kt | 19 +++- .../approvedpremesis/EventDetails.kt | 28 +++++- .../integrations/delius/AddressService.kt | 49 ++++++++++ .../integrations/delius/ContactService.kt | 6 +- .../delius/entity/PersonAddress.kt | 93 +++++++++++++++++++ .../delius/entity/ReferenceData.kt | 63 +++++++++++++ .../delius/entity/ReferenceDataRepository.kt | 30 ++++++ .../digital/hmpps/messaging/Handler.kt | 14 ++- .../approvedpremesis/AddressTest.kt | 32 +++++++ 14 files changed, 367 insertions(+), 7 deletions(-) create mode 100644 projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt create mode 100644 projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt create mode 100644 projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt create mode 100644 projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt create mode 100644 projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt create mode 100644 projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceDataRepository.kt 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/deploy/database/access.yml b/projects/cas3-and-delius/deploy/database/access.yml index c9290b149f..1ec7740748 100644 --- a/projects/cas3-and-delius/deploy/database/access.yml +++ b/projects/cas3-and-delius/deploy/database/access.yml @@ -5,6 +5,7 @@ database: tables: - audited_interaction - contact + - offender_address audit: username: Cas3AndDelius 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 3d6d9dad45..cb2a4e3422 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 @@ -7,8 +7,10 @@ import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.context.ApplicationListener import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional +import uk.gov.justice.digital.hmpps.data.generator.AddressRDGenerator import uk.gov.justice.digital.hmpps.data.generator.BusinessInteractionGenerator import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator +import uk.gov.justice.digital.hmpps.data.generator.DatasetGenerator import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator import uk.gov.justice.digital.hmpps.user.AuditUserRepository @@ -28,6 +30,11 @@ class DataLoader( @Transactional override fun onApplicationEvent(are: ApplicationReadyEvent) { em.saveAll( + DatasetGenerator.ADDRESS_STATUS, + DatasetGenerator.ADDRESS_TYPE, + AddressRDGenerator.CAS3_ADDRESS_TYPE, + AddressRDGenerator.MAIN_ADDRESS_STATUS, + AddressRDGenerator.PREV_ADDRESS_STATUS, BusinessInteractionGenerator.UPDATE_CONTACT, ContactTypeGenerator.EARS_CONTACT_TYPE, ContactTypeGenerator.EACA_CONTACT_TYPE, 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 new file mode 100644 index 0000000000..3b3740513d --- /dev/null +++ b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt @@ -0,0 +1,16 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData + +object AddressRDGenerator { + val CAS3_ADDRESS_TYPE = generate("A17", DatasetGenerator.ADDRESS_TYPE.id, "Approved Premises") + val MAIN_ADDRESS_STATUS = generate("M", DatasetGenerator.ADDRESS_STATUS.id, "Main Address") + val PREV_ADDRESS_STATUS = generate("P", DatasetGenerator.ADDRESS_STATUS.id, "Previous Address") + + fun generate( + code: String, + datasetId: Long, + description: String = "Description of $code", + id: Long = IdGenerator.getAndIncrement() + ) = ReferenceData(id, code, description, datasetId) +} 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 new file mode 100644 index 0000000000..51634cf6ec --- /dev/null +++ b/projects/cas3-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt @@ -0,0 +1,9 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.entity.Dataset +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) +} diff --git a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-arrived.json b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-arrived.json index 43aa19ebcc..e985fe7417 100644 --- a/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-arrived.json +++ b/projects/cas3-and-delius/src/dev/resources/simulations/__files/cas3-person-arrived.json @@ -11,6 +11,13 @@ }, "bookingId": "14c80733-4b6d-4f35-b724-66955aac320e", "bookingUrl": "https://approved-premises-dev.hmpps.service.justice.gov.uk/someURLtoTheBooking", + "premises": { + "addressLine1": "12 Church Street", + "addressLine2": "", + "postcode": "BB1 1BB", + "town": "Bimbly Town", + "region": "Bibbinghammcshireshire" + }, "arrivedAt": "2022-11-30T12:00:00", "notes": "person arrived" } 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 bf0a6673cd..8f4568097a 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 @@ -23,7 +23,10 @@ import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.EventDetails import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.PersonArrived import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.PersonDeparted import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactRepository +import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddressRepository +import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonRepository import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager +import uk.gov.justice.digital.hmpps.messaging.crn import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader import uk.gov.justice.digital.hmpps.telemetry.TelemetryService import uk.gov.justice.digital.hmpps.telemetry.notificationReceived @@ -44,6 +47,12 @@ internal class CASIntegrationTest { @Autowired lateinit var contactRepository: ContactRepository + @Autowired + lateinit var addressRepository: PersonAddressRepository + + @Autowired + lateinit var personRepository: PersonRepository + @MockBean lateinit var telemetryService: TelemetryService @@ -135,6 +144,15 @@ internal class CASIntegrationTest { val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn) MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EAAR")) + + val person = personRepository.findByCrnAndSoftDeletedIsFalse(event.message.crn()) + val address = addressRepository.findMainAddress(person!!.id) + + MatcherAssert.assertThat(address!!.type.code, Matchers.equalTo("A17")) + MatcherAssert.assertThat(address.town, Matchers.equalTo(eventDetails.eventDetails.premises.town)) + 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 @@ -164,7 +182,6 @@ internal class CASIntegrationTest { // Then it is logged to telemetry Mockito.verify(telemetryService).notificationReceived(event) - val oldEventDetails = ResourceLoader.file>("cas3-person-departed") val eventDetails = ResourceLoader.file>("cas3-$eventName") val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn) 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 1f40403fbd..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, @@ -70,13 +71,38 @@ data class PersonArrived( val bookingId: String, val bookingUrl: String, val arrivedAt: ZonedDateTime, - val notes: String + val notes: String, + val premises: Address ) : Cas3Event { override val urn = "urn:hmpps:cas3:person-arrived:$bookingId" override val noteText = "${DeliusDateFormatter.format(arrivedAt)} $notes $bookingUrl" override val contactTypeCode = ContactType.PERSON_ARRIVED } +data class Address( + val addressLine1: String, + val addressLine2: String?, + 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( val applicationId: String?, val applicationUrl: 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 new file mode 100644 index 0000000000..ca93d58e4a --- /dev/null +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt @@ -0,0 +1,49 @@ +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.Person +import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddress +import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddressRepository +import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceDataRepository +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 + +@Service +class AddressService( + private val personAddressRepository: PersonAddressRepository, + private val referenceDataRepository: ReferenceDataRepository +) { + fun updateMainAddress(person: Person, details: PersonArrived) { + endMainAddress(person, details.arrivedAt.toLocalDate()) + toPersonAddress(person, details).apply(personAddressRepository::save) + } + + fun endMainAddress(person: Person, endDate: LocalDate) { + val currentMain = personAddressRepository.findMainAddress(person.id) + currentMain?.apply { + val previousStatus = referenceDataRepository.previousAddressStatus() + currentMain.status = previousStatus + currentMain.endDate = endDate + } + } + + 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/ContactService.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt index fe7a17afd6..6b36f6f480 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 @@ -10,6 +10,7 @@ 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.ContactTypeRepository +import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person 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 @@ -28,10 +29,11 @@ class ContactService( fun createContact( crn: String, + person: Person? = null, getEvent: () -> EventDetails ) = audit(BusinessInteractionCode.UPDATE_CONTACT) { val event = getEvent() - val person = personRepository.getByCrn(crn) + val personId = person?.id ?: personRepository.getByCrn(crn).id val existing = contactRepository.getByExternalReference(event.eventDetails.urn) if (existing != null) { if (existing.startTime < event.timestamp) { @@ -49,7 +51,7 @@ class ContactService( contactRepository.save( newContact( event.timestamp, - person.id, + personId, event.eventDetails.contactTypeCode, event.eventDetails.urn, event.eventDetails.noteText 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 new file mode 100644 index 0000000000..babec303a3 --- /dev/null +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt @@ -0,0 +1,93 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.entity + +import jakarta.persistence.Column +import jakarta.persistence.Convert +import jakarta.persistence.Entity +import jakarta.persistence.EntityListeners +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne +import jakarta.persistence.SequenceGenerator +import jakarta.persistence.Table +import org.hibernate.type.YesNoConverter +import org.springframework.data.annotation.CreatedBy +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedBy +import org.springframework.data.annotation.LastModifiedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import java.time.LocalDate +import java.time.ZonedDateTime + +@Entity +@Table(name = "offender_address") +@EntityListeners(AuditingEntityListener::class) +@SequenceGenerator(name = "offender_address_id_generator", sequenceName = "offender_address_id_seq", allocationSize = 1) +class PersonAddress( + @Id + @Column(name = "offender_address_id") + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "offender_address_id_generator") + val id: Long, + @Column(name = "offender_id") + val personId: Long, + @ManyToOne + @JoinColumn(name = "address_type_id") + val type: ReferenceData, + @ManyToOne + @JoinColumn(name = "address_status_id") + var status: ReferenceData, + val streetName: String?, + @Column(name = "town_city") + val town: String?, + val county: String?, + val postcode: String?, + val telephoneNumber: String? = null, + val buildingName: String? = null, + val district: String? = null, + val addressNumber: String? = null, + @Convert(converter = YesNoConverter::class) + val noFixedAbode: Boolean? = false, + @Convert(converter = YesNoConverter::class) + val typeVerified: Boolean? = false, + val startDate: LocalDate = LocalDate.now(), + var endDate: LocalDate? = null, + @Column(updatable = false, columnDefinition = "NUMBER") + val softDeleted: Boolean = false, + + @CreatedDate + @Column(nullable = false) + var createdDatetime: ZonedDateTime = ZonedDateTime.now(), + + @Column(nullable = false) + @CreatedBy + var createdByUserId: Long = 0, + + @Column(nullable = false) + @LastModifiedDate + var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now(), + + @Column(nullable = false) + @LastModifiedBy + var lastUpdatedUserId: Long = 0, + + @Column(nullable = false) + val partitionAreaId: Long = 0 +) + +interface PersonAddressRepository : JpaRepository { + @Query( + """ + select pa from PersonAddress pa + join fetch pa.status + join fetch pa.type + where pa.personId = :personId + and pa.softDeleted = false + and pa.endDate is null + and pa.status.code = 'M' + """ + ) + fun findMainAddress(personId: Long): PersonAddress? +} 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 new file mode 100644 index 0000000000..47e1c7db1a --- /dev/null +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt @@ -0,0 +1,63 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.entity + +import jakarta.persistence.AttributeConverter +import jakarta.persistence.Column +import jakarta.persistence.Convert +import jakarta.persistence.Converter +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.Table +import org.hibernate.annotations.Immutable + +@Immutable +@Entity +@Table(name = "r_standard_reference_list") +class ReferenceData( + @Id + @Column(name = "standard_reference_list_id", nullable = false) + val id: Long, + + @Column(name = "code_value", length = 100, nullable = false) + val code: String, + + @Column(name = "code_description", length = 500, nullable = false) + val description: String, + + @Column(name = "reference_data_master_id", nullable = false) + val datasetId: Long +) + +@Immutable +@Entity +@Table(name = "r_reference_data_master") +class Dataset( + @Id + @Column(name = "reference_data_master_id") + val id: Long, + + @Convert(converter = DatasetCodeConverter::class) + @Column(name = "code_set_name", nullable = false) + val code: DatasetCode +) + +enum class DatasetCode(val value: String) { + ADDRESS_STATUS("ADDRESS STATUS"), + ADDRESS_TYPE("ADDRESS TYPE"); + + companion object { + private val index = DatasetCode.entries.associateBy { it.value } + fun fromString(value: String): DatasetCode = + index[value] ?: throw IllegalArgumentException("Invalid DatasetCode") + } +} + +enum class AddressTypeCode(val code: String) { + CAS3("A17") +} + +@Converter +class DatasetCodeConverter : AttributeConverter { + override fun convertToDatabaseColumn(attribute: DatasetCode): String = attribute.value + + override fun convertToEntityAttribute(dbData: String): DatasetCode = DatasetCode.fromString(dbData) +} diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceDataRepository.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceDataRepository.kt new file mode 100644 index 0000000000..750cf732c7 --- /dev/null +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceDataRepository.kt @@ -0,0 +1,30 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.entity + +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import uk.gov.justice.digital.hmpps.exception.NotFoundException + +interface ReferenceDataRepository : JpaRepository { + + @Query( + """ + select rd from ReferenceData rd + join Dataset ds on rd.datasetId = ds.id + where ds.code = :datasetCode and rd.code = :code + """ + ) + fun findByCodeAndDatasetCode(code: String, datasetCode: DatasetCode): ReferenceData? +} + +fun ReferenceDataRepository.findAddressStatusByCode(code: String) = + findByCodeAndDatasetCode(code, DatasetCode.ADDRESS_STATUS) + +fun ReferenceDataRepository.getAddressStatus(code: String): ReferenceData = + findAddressStatusByCode(code) ?: throw NotFoundException("Address Status", "code", code) + +fun ReferenceDataRepository.mainAddressStatus() = getAddressStatus("M") +fun ReferenceDataRepository.previousAddressStatus() = getAddressStatus("P") + +fun ReferenceDataRepository.cas3AddressType() = + findByCodeAndDatasetCode(AddressTypeCode.CAS3.code, DatasetCode.ADDRESS_TYPE) + ?: throw NotFoundException("Address Type", "code", AddressTypeCode.CAS3.code) 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 9ff73b06f1..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 @@ -3,7 +3,10 @@ package uk.gov.justice.digital.hmpps.messaging import org.springframework.stereotype.Component import uk.gov.justice.digital.hmpps.converter.NotificationConverter import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.Cas3ApiClient +import uk.gov.justice.digital.hmpps.integrations.delius.AddressService import uk.gov.justice.digital.hmpps.integrations.delius.ContactService +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.message.Notification import uk.gov.justice.digital.hmpps.telemetry.TelemetryService @@ -15,7 +18,9 @@ class Handler( override val converter: NotificationConverter, private val telemetryService: TelemetryService, private val contactService: ContactService, - private val cas3ApiClient: Cas3ApiClient + private val addressService: AddressService, + private val cas3ApiClient: Cas3ApiClient, + private val personRepository: PersonRepository ) : NotificationHandler { override fun handle(notification: Notification) { telemetryService.notificationReceived(notification) @@ -50,9 +55,12 @@ class Handler( } "accommodation.cas3.person.arrived" -> { - contactService.createContact(event.crn()) { - cas3ApiClient.getPersonArrived(event.url()) + val person = personRepository.getByCrn(event.crn()) + 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")) + ) + } +}