Skip to content

Commit

Permalink
PI-1991: Added addresses and person summary (#3475)
Browse files Browse the repository at this point in the history
* PI-1991: Added addresses and person summary
  • Loading branch information
pmcphee77 authored Mar 15, 2024
1 parent c950fad commit ee95234
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object PersonDetailsGenerator {
val CONTACT_ADDRESS = generateContactAddress("31", "Test Steet", "Test town", "Test County", "NE1 56A")
val PERSONAL_CONTACT_1 = PersonalContactEntity(
IdGenerator.getAndIncrement(),
PERSONAL_DETAILS.id,
PERSONAL_DETAILS,
"Sam",
"Steven",
"Smith",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.util.ResourceUtils
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.PersonSummary
import uk.gov.justice.digital.hmpps.api.model.personalDetails.AddressOverview
import uk.gov.justice.digital.hmpps.api.model.personalDetails.PersonalContact
import uk.gov.justice.digital.hmpps.api.model.personalDetails.PersonalDetails
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.ALIAS_1
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.PERSONAL_CONTACT_1
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.PERSONAL_DETAILS
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.PREVIOUS_ADDRESS
import uk.gov.justice.digital.hmpps.service.toContact
import uk.gov.justice.digital.hmpps.service.toSummary
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
import java.time.LocalDate
Expand Down Expand Up @@ -51,12 +54,8 @@ internal class PersonalDetailsIntegrationTest {
assertThat(res.mainAddress?.verified, equalTo(true))
assertThat(res.mainAddress?.type, equalTo("Address type 1"))
assertThat(res.mainAddress?.postcode, equalTo("NE2 56A"))
assertThat(res.otherAddresses.size, equalTo(1))
assertThat(res.otherAddresses[0].type, equalTo("Address type 2"))
assertThat(res.otherAddresses[0].status, equalTo("Another Address"))
assertThat(res.otherAddresses[0].verified, equalTo(true))
assertThat(res.otherAddresses[0].postcode, equalTo("NE4 5AN"))
assertThat(res.previousAddresses[0].postcode, equalTo("NE4 END"))
assertThat(res.otherAddressCount, equalTo(1))
assertThat(res.previousAddressCount, equalTo(1))
assertThat(res.contacts.size, equalTo(1))
assertThat(res.contacts[0].name, equalTo(Name("Sam", "Steven", "Smith")))
assertThat(res.contacts[0].address?.postcode, equalTo("NE1 56A"))
Expand Down Expand Up @@ -161,4 +160,26 @@ internal class PersonalDetailsIntegrationTest {
.perform(get("/personal-details/X999999/personal-contact/999999999").withToken())
.andExpect(status().isNotFound)
}

@Test
fun `addresses are returned`() {
val person = PERSONAL_DETAILS
val contact = PERSONAL_CONTACT_1
val res = mockMvc
.perform(get("/personal-details/${person.crn}/addresses").withToken())
.andExpect(status().isOk)
.andReturn().response.contentAsJson<AddressOverview>()
assertThat(res.personSummary, equalTo(person.toSummary()))
assertThat(res.mainAddress?.postcode, equalTo("NE2 56A"))
assertThat(res.previousAddresses[0].postcode, equalTo("NE4 END"))
assertThat(res.previousAddresses[0].to, equalTo(PREVIOUS_ADDRESS.endDate))
assertThat(res.otherAddresses[0].status, equalTo("Another Address"))
}

@Test
fun `addresses person not found`() {
mockMvc
.perform(get("/personal-details/X999999/addresses").withToken())
.andExpect(status().isNotFound)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ class PersonalDetailsController(private val personalDetailsService: PersonalDeta
@Operation(summary = "Person Contact")
fun getPersonContact(@PathVariable crn: String, @PathVariable id: Long) =
personalDetailsService.getPersonContact(crn, id)

@GetMapping("/addresses")
@Operation(summary = "Person Addresses")
fun getPersonAddresses(@PathVariable crn: String) =
personalDetailsService.getPersonAddresses(crn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.justice.digital.hmpps.api.model.personalDetails

import uk.gov.justice.digital.hmpps.api.model.PersonSummary

data class AddressOverview(
val personSummary: PersonSummary,
val mainAddress: Address?,
val otherAddresses: List<Address>,
val previousAddresses: List<Address>
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package uk.gov.justice.digital.hmpps.api.model.personalDetails

import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.PersonSummary

data class PersonalContact(
val personSummary: PersonSummary,
val name: Name,
val relationship: String?,
val relationshipType: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ data class PersonalDetails(
val name: Name,
val contacts: List<PersonalContact>,
val mainAddress: Address?,
val otherAddresses: List<Address> = emptyList(),
val previousAddresses: List<Address> = emptyList(),
val otherAddressCount: Int,
val previousAddressCount: Int,
val preferredGender: String,
val dateOfBirth: LocalDate,
val preferredName: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Person(
)

interface PersonSummaryEntity {
val id: Long
val forename: String
val secondName: String?
val thirdName: String?
Expand All @@ -91,7 +92,7 @@ interface PersonRepository : JpaRepository<Person, Long> {

@Query(
"""
select p.first_name as forename, p.second_name as secondName, p.third_name as thirdName,
select p.offender_id as id, p.first_name as forename, p.second_name as secondName, p.third_name as thirdName,
p.surname, p.crn, p.pnc_number as pnc, p.date_of_birth_date as dateOfBirth
from offender p where p.crn = :crn and p.soft_deleted = 0
""", nativeQuery = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.hibernate.annotations.Immutable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData

@Entity
Expand All @@ -15,8 +16,9 @@ class PersonalContactEntity(
@Column(name = "personal_contact_id")
val id: Long,

@Column(name = "offender_id")
val personId: Long,
@ManyToOne
@JoinColumn(name = "offender_id")
val person: Person,

@Column(name = "first_name")
val forename: String,
Expand Down Expand Up @@ -47,8 +49,7 @@ interface PersonalContactRepository : JpaRepository<PersonalContactEntity, Long>

@Query(
"""
select pc from PersonalContactEntity pc join Person p on p.id = pc.personId
where p.crn = :crn and pc.id = :contactId
select pc from PersonalContactEntity pc where pc.id = :contactId and pc.person.crn = :crn
"""
)
fun findById(crn: String, contactId: Long): PersonalContactEntity?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class PersonalDetailsService(
crn = person.crn,
name = person.name(),
mainAddress = mainAddress,
otherAddresses = otherAddresses,
previousAddresses = previousAddresses,
otherAddressCount = otherAddresses.size,
previousAddressCount = previousAddresses.size,
contacts = personalContacts.map(PersonalContactEntity::toContact),
preferredGender = person.gender.description,
dateOfBirth = person.dateOfBirth,
Expand Down Expand Up @@ -92,16 +92,34 @@ class PersonalDetailsService(
fun getPersonSummary(crn: String): PersonSummary {
return personRepository.getSummary(crn).toPersonSummary()
}

fun getPersonAddresses(crn: String): AddressOverview {
val person = personRepository.getSummary(crn)
val addresses = addressRepository.findByPersonId(person.id)
val currentAddresses = addresses.filter { it.endDate == null }

return AddressOverview(
personSummary = person.toPersonSummary(),
mainAddress = currentAddresses.firstOrNull { it.status.code == "M" }?.toAddress(),
otherAddresses = currentAddresses.filter { it.status.code != "M" }.map(PersonAddress::toAddress)
.mapNotNull { it },
previousAddresses = addresses.filter { it.endDate != null }.map(PersonAddress::toAddress).mapNotNull { it }
)
}
}

fun PersonalContactEntity.toContact() = PersonalContact(
name = Name(forename = forename, middleName = middleNames, surname = surname),
personSummary = person.toSummary(),
name = Name(forename, middleNames, surname),
relationship = relationship,
address = address.toAddress(),
notes = notes,
relationshipType = relationshipType.description
)

fun Person.toSummary() =
PersonSummary(name = Name(forename, secondName, surname), pnc = pnc, dateOfBirth = dateOfBirth, crn = crn)

fun Person.name() = Name(forename, listOfNotNull(secondName, thirdName).joinToString(" "), surname)
fun PersonAddress.toAddress() = Address.from(
buildingName = buildingName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.PersonSummary
import uk.gov.justice.digital.hmpps.api.model.personalDetails.Circumstances
import uk.gov.justice.digital.hmpps.api.model.personalDetails.Disabilities
import uk.gov.justice.digital.hmpps.api.model.personalDetails.PersonalDetails
import uk.gov.justice.digital.hmpps.api.model.personalDetails.Provisions
import uk.gov.justice.digital.hmpps.api.model.personalDetails.*
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator
import uk.gov.justice.digital.hmpps.service.PersonalDetailsService
import uk.gov.justice.digital.hmpps.service.toAddress
Expand Down Expand Up @@ -50,7 +47,8 @@ internal class PersonalDetailsControllerTest {
mainAddress = PersonDetailsGenerator.PERSON_ADDRESS_1.toAddress(),
mobileNumber = "0897672332",
telephoneNumber = "090876522",
otherAddresses = listOfNotNull(PersonDetailsGenerator.PERSON_ADDRESS_2.toAddress()),
otherAddressCount = 1,
previousAddressCount = 1,
preferredGender = "Male",
preferredName = "Steve",
religionOrBelief = "Christian",
Expand Down Expand Up @@ -99,4 +97,23 @@ internal class PersonalDetailsControllerTest {
val res = controller.getPersonSummary(crn)
assertThat(res, equalTo(expectedResponse))
}

@Test
fun `calls get addresses function `() {
val crn = "X000005"
val expectedSummary = PersonSummary(
Name(forename = "TestName", middleName = null, surname = "TestSurname"), pnc = "Test PNC",
crn = "CRN",
dateOfBirth = LocalDate.now(),
)

val expectedResponse = AddressOverview(
personSummary = expectedSummary, mainAddress = PersonDetailsGenerator.PERSON_ADDRESS_1.toAddress(),
otherAddresses = listOfNotNull(PersonDetailsGenerator.PERSON_ADDRESS_2.toAddress()),
previousAddresses = listOfNotNull(PersonDetailsGenerator.PREVIOUS_ADDRESS.toAddress())
)
whenever(personalDetailsService.getPersonAddresses(crn)).thenReturn(expectedResponse)
val res = controller.getPersonAddresses(crn)
assertThat(res, equalTo(expectedResponse))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
import uk.gov.justice.digital.hmpps.alfresco.AlfrescoClient
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.PersonSummary
import uk.gov.justice.digital.hmpps.api.model.personalDetails.AddressOverview
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.*
Expand Down Expand Up @@ -115,6 +118,7 @@ internal class PersonalDetailsServiceTest {
val crn = "X000005"

val expected = Summary(
id = 1,
forename = "TestName",
surname = "TestSurname", crn = "CRN", pnc = "PNC", dateOfBirth = LocalDate.now().minusYears(50)
)
Expand All @@ -123,15 +127,48 @@ internal class PersonalDetailsServiceTest {
assertThat(res, equalTo(expected.toPersonSummary()))
}

@Test
fun `calls get addresses function`() {
val crn = "X000005"

val summary = Summary(
id = 1,
forename = "TestName",
surname = "TestSurname", crn = "CRN", pnc = "PNC", dateOfBirth = LocalDate.now().minusYears(50)
)

val addresses = listOf(
PersonDetailsGenerator.PERSON_ADDRESS_1,
PersonDetailsGenerator.PERSON_ADDRESS_2,
PersonDetailsGenerator.PREVIOUS_ADDRESS,
)

val expectedResponse = AddressOverview(
personSummary = PersonSummary(
Name(forename = summary.forename, middleName = null, surname = summary.surname),
pnc = summary.pnc,
crn = summary.crn,
dateOfBirth = summary.dateOfBirth
),
previousAddresses = listOfNotNull(PersonDetailsGenerator.PREVIOUS_ADDRESS.toAddress()),
mainAddress = PersonDetailsGenerator.PERSON_ADDRESS_1.toAddress(),
otherAddresses = listOfNotNull(PersonDetailsGenerator.PERSON_ADDRESS_2.toAddress())
)

whenever(personRepository.findSummary(crn)).thenReturn(summary)
whenever(addressRepository.findByPersonId(1)).thenReturn(addresses)
val res = service.getPersonAddresses(crn)
assertThat(res, equalTo(expectedResponse))
}

data class Summary(
override val id: Long,
override val forename: String,
override val secondName: String? = null,
override val thirdName: String? = null,
override val surname: String,
override val crn: String,
override val pnc: String?,
override val dateOfBirth: LocalDate
) : PersonSummaryEntity {

}
) : PersonSummaryEntity
}

0 comments on commit ee95234

Please sign in to comment.