From 6ee16f4b6a798822dd980165f910eda3206144d9 Mon Sep 17 00:00:00 2001 From: Anthony Britton Date: Mon, 25 Sep 2023 14:09:02 +0100 Subject: [PATCH] PI-1484 - add endpoint to return addresses --- .../justice/digital/hmpps/data/DataLoader.kt | 10 +++ .../hmpps/data/generator/AddressGenerator.kt | 75 +++++++++++++++++++ .../justice/digital/hmpps/IntegrationTest.kt | 40 ++++++++++ .../digital/hmpps/api/model/Address.kt | 12 ++- .../api/resource/ProbationCaseResource.kt | 13 +++- .../delius/person/entity/PersonAddress.kt | 18 +++-- .../digital/hmpps/service/AddressService.kt | 23 ++++++ 7 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressGenerator.kt create mode 100644 projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AddressService.kt diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index eaf9c991e5..1dadb51a63 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -7,6 +7,7 @@ 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.AddressGenerator import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator @@ -40,5 +41,14 @@ class DataLoader( val person = PersonGenerator.generatePerson("N123456").also(entityManager::persist) PersonGenerator.generateManager(person).also(entityManager::persist) + listOf( + AddressGenerator.ADDRESS_STATUS_MAIN, + AddressGenerator.ADDRESS_STATUS_PREVIOUS, + AddressGenerator.ADDRESS_STATUS_OTHER, + AddressGenerator.ADDRESS_MAIN, + AddressGenerator.ADDRESS_PREVIOUS, + AddressGenerator.ADDRESS_OTHER, + AddressGenerator.ADDRESS_DELETED + ).forEach(entityManager::persist) } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressGenerator.kt new file mode 100644 index 0000000000..c80521bf4a --- /dev/null +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressGenerator.kt @@ -0,0 +1,75 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.AddressStatus +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonAddress +import java.time.LocalDate + +object AddressGenerator { + val ADDRESS_STATUS_MAIN = generateStatus("M") + val ADDRESS_STATUS_PREVIOUS = generateStatus("P") + val ADDRESS_STATUS_OTHER = generateStatus("O") + val ADDRESS_MAIN = generateAddress( + PersonGenerator.DEFAULT_PERSON, + ADDRESS_STATUS_MAIN, + buildingNumber = "21", + streetName = "Mantle Place", + town = "Hearth", + postcode = "H34 7TH" + ) + val ADDRESS_PREVIOUS = generateAddress( + PersonGenerator.DEFAULT_PERSON, + ADDRESS_STATUS_PREVIOUS, + buildingName = "Casa Anterior", + streetName = "Plaza de Espana", + county = "Seville", + postcode = "S3 11E", + startDate = LocalDate.now().minusDays(12), + endDate = LocalDate.now().minusDays(1) + ) + val ADDRESS_OTHER = generateAddress(PersonGenerator.DEFAULT_PERSON, ADDRESS_STATUS_OTHER) + val ADDRESS_DELETED = generateAddress( + PersonGenerator.DEFAULT_PERSON, + ADDRESS_STATUS_OTHER, + buildingNumber = "1", + streetName = "Deleted Close", + district = "Invisible", + softDeleted = true + ) + + fun generateStatus( + code: String, + description: String = "Description of $code", + id: Long = IdGenerator.getAndIncrement() + ) = AddressStatus(code, description, id) + + fun generateAddress( + person: Person, + status: AddressStatus, + buildingName: String? = null, + buildingNumber: String? = null, + streetName: String? = null, + district: String? = null, + town: String? = null, + county: String? = null, + postcode: String? = null, + startDate: LocalDate = LocalDate.now(), + endDate: LocalDate? = null, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = PersonAddress( + person, + status, + buildingName, + buildingNumber, + streetName, + district, + town, + county, + postcode, + startDate, + endDate, + softDeleted, + id + ) +} diff --git a/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index ee45a4dd95..e66b6644b3 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -13,10 +13,12 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDO import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import uk.gov.justice.digital.hmpps.api.model.Address import uk.gov.justice.digital.hmpps.api.model.Manager import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.security.withOAuth2Token import uk.gov.justice.digital.hmpps.service.asManager +import java.time.LocalDate @AutoConfigureMockMvc @SpringBootTest(webEnvironment = RANDOM_PORT) @@ -55,4 +57,42 @@ internal class IntegrationTest { .withOAuth2Token(wireMockServer) ).andExpect(status().isNotFound) } + + @Test + fun `can return all addresses for a crn`() { + val crn = PersonGenerator.DEFAULT_PERSON.crn + + val res = mockMvc + .perform(get("/probation-case/$crn/addresses").withOAuth2Token(wireMockServer)) + .andExpect(status().isOk) + .andReturn().response.contentAsString + + val addresses = objectMapper.readValue>(res) + assertThat(addresses.size, equalTo(2)) + assertThat( + addresses.first(), + equalTo( + Address.from( + buildingNumber = "21", + streetName = "Mantle Place", + town = "Hearth", + postcode = "H34 7TH", + from = LocalDate.now() + ) + ) + ) + assertThat( + addresses.last(), + equalTo( + Address.from( + buildingName = "Casa Anterior", + streetName = "Plaza de Espana", + county = "Seville", + postcode = "S3 11E", + from = LocalDate.now().minusDays(12), + to = LocalDate.now().minusDays(1) + ) + ) + ) + } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Address.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Address.kt index 8ee82cf7f4..8db543d3ba 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Address.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Address.kt @@ -1,5 +1,7 @@ package uk.gov.justice.digital.hmpps.api.model +import java.time.LocalDate + data class Address( val buildingName: String?, val buildingNumber: String?, @@ -7,7 +9,9 @@ data class Address( val district: String?, val town: String?, val county: String?, - val postcode: String? + val postcode: String?, + val from: LocalDate, + val to: LocalDate? ) { companion object { fun from( @@ -17,7 +21,9 @@ data class Address( district: String? = null, town: String? = null, county: String? = null, - postcode: String? = null + postcode: String? = null, + from: LocalDate, + to: LocalDate? = null ): Address? = if ( buildingName == null && buildingNumber == null && streetName == null && @@ -25,7 +31,7 @@ data class Address( ) { null } else { - Address(buildingName, buildingNumber, streetName, district, town, county, postcode) + Address(buildingName, buildingNumber, streetName, district, town, county, postcode, from, to) } } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt index 1d008b7027..f7e0b59b24 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt @@ -5,13 +5,22 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import uk.gov.justice.digital.hmpps.api.model.Address import uk.gov.justice.digital.hmpps.api.model.Manager +import uk.gov.justice.digital.hmpps.service.AddressService import uk.gov.justice.digital.hmpps.service.ManagerService @RestController @RequestMapping("probation-case/{crn}") -class ProbationCaseResource(private val responsibleManagerService: ManagerService) { +class ProbationCaseResource( + private val responsibleManagerService: ManagerService, + private val addressService: AddressService +) { @PreAuthorize("hasRole('CVL_CONTEXT')") @GetMapping("responsible-community-manager") - fun handle(@PathVariable crn: String): Manager = responsibleManagerService.findCommunityManager(crn) + fun findCommunityManager(@PathVariable crn: String): Manager = responsibleManagerService.findCommunityManager(crn) + + @PreAuthorize("hasRole('CVL_CONTEXT')") + @GetMapping("addresses") + fun findAddresses(@PathVariable crn: String): List
= addressService.findAddresses(crn) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/PersonAddress.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/PersonAddress.kt index ae12861492..9d4d46bb0c 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/PersonAddress.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/PersonAddress.kt @@ -1,7 +1,6 @@ package uk.gov.justice.digital.hmpps.integrations.delius.person.entity import jakarta.persistence.Column -import jakarta.persistence.Convert import jakarta.persistence.Entity import jakarta.persistence.Id import jakarta.persistence.JoinColumn @@ -9,17 +8,18 @@ import jakarta.persistence.ManyToOne import jakarta.persistence.Table import org.hibernate.annotations.Immutable import org.hibernate.annotations.Where -import org.hibernate.type.YesNoConverter +import org.springframework.data.jpa.repository.JpaRepository import java.time.LocalDate @Immutable @Entity @Table(name = "offender_address") -@Where(clause = "soft_deleted = 0 and end_date is null") +@Where(clause = "soft_deleted = 0") class PersonAddress( - @Column(name = "offender_id") - val personId: Long, + @ManyToOne + @JoinColumn(name = "offender_id") + val person: Person, @ManyToOne @JoinColumn(name = "address_status_id") @@ -28,7 +28,7 @@ class PersonAddress( @Column(name = "building_name") val buildingName: String?, @Column(name = "address_number") - val addressNumber: String?, + val buildingNumber: String?, @Column(name = "street_name") val streetName: String?, val district: String?, @@ -37,8 +37,6 @@ class PersonAddress( val county: String?, val postcode: String?, - @Convert(converter = YesNoConverter::class) - val noFixedAbode: Boolean, val startDate: LocalDate, val endDate: LocalDate?, @@ -65,3 +63,7 @@ class AddressStatus( @Column(name = "standard_reference_list_id") val id: Long ) + +interface PersonAddressRepository : JpaRepository { + fun findAllByPersonCrnOrderByStartDateDesc(crn: String): List +} diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AddressService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AddressService.kt new file mode 100644 index 0000000000..b6cf20c925 --- /dev/null +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AddressService.kt @@ -0,0 +1,23 @@ +package uk.gov.justice.digital.hmpps.service + +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.api.model.Address +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonAddressRepository + +@Service +class AddressService(private val personAddressRepository: PersonAddressRepository) { + fun findAddresses(crn: String): List
= + personAddressRepository.findAllByPersonCrnOrderByStartDateDesc(crn).mapNotNull { + Address.from( + it.buildingName, + it.buildingNumber, + it.streetName, + it.district, + it.town, + it.county, + it.postcode, + it.startDate, + it.endDate + ) + } +}