Skip to content

Commit

Permalink
PI-1484 - add endpoint to return addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Sep 25, 2023
1 parent 26a7774 commit 6ee16f4
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<List<Address>>(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)
)
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package uk.gov.justice.digital.hmpps.api.model

import java.time.LocalDate

data class Address(
val buildingName: String?,
val buildingNumber: String?,
val streetName: String?,
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(
Expand All @@ -17,15 +21,17 @@ 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 &&
district == null && town == null && county == null && postcode == null
) {
null
} else {
Address(buildingName, buildingNumber, streetName, district, town, county, postcode)
Address(buildingName, buildingNumber, streetName, district, town, county, postcode, from, to)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Address> = addressService.findAddresses(crn)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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
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")
Expand All @@ -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?,
Expand All @@ -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?,

Expand All @@ -65,3 +63,7 @@ class AddressStatus(
@Column(name = "standard_reference_list_id")
val id: Long
)

interface PersonAddressRepository : JpaRepository<PersonAddress, Long> {
fun findAllByPersonCrnOrderByStartDateDesc(crn: String): List<PersonAddress>
}
Original file line number Diff line number Diff line change
@@ -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<Address> =
personAddressRepository.findAllByPersonCrnOrderByStartDateDesc(crn).mapNotNull {
Address.from(
it.buildingName,
it.buildingNumber,
it.streetName,
it.district,
it.town,
it.county,
it.postcode,
it.startDate,
it.endDate
)
}
}

0 comments on commit 6ee16f4

Please sign in to comment.