Skip to content

Commit

Permalink
PI-1585 Added address processing
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Oct 30, 2023
1 parent 66bf6e7 commit b68700e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ object AddressRDGenerator {
description: String = "Description of $code",
id: Long = IdGenerator.getAndIncrement()
) = ReferenceData(id, code, description, datasetId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<out T : Cas3Event>(
val id: String,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ enum class AddressTypeCode(val code: String) {
CAS3("CAS3")
}


@Converter
class DatasetCodeConverter : AttributeConverter<DatasetCode, String> {
override fun convertToDatabaseColumn(attribute: DatasetCode): String = attribute.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HmppsDomainEvent> {
override fun handle(notification: Notification<HmppsDomainEvent>) {
telemetryService.notificationReceived(notification)
Expand Down Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"))
)
}
}

0 comments on commit b68700e

Please sign in to comment.