Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Mar 22, 2024
1 parent b0e8e5a commit c71c2b4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,45 @@ import uk.gov.justice.digital.hmpps.entity.Person
import uk.gov.justice.digital.hmpps.entity.PersonManager
import uk.gov.justice.digital.hmpps.entity.Staff
import uk.gov.justice.digital.hmpps.entity.Team
import java.time.LocalDate

object PersonGenerator {
val DEFAULT = generate("X123123", "A1234YZ")
val DEFAULT = generate(
"X123123",
"A1234YZ",
"Fred",
"Williams",
LocalDate.of(1982, 8, 19),
"020 346 7982",
"07452819463",
"[email protected]"
)
val DEFAULT_MANAGER = generateManager(DEFAULT)
val CREATE_APPOINTMENT = generate("C123456")
val CREATE_APPOINTMENT = generate("C123456", null, "James", "Brown", LocalDate.of(1990, 5, 12))

fun generate(
crn: String,
noms: String? = null,
firstName: String,
surname: String,
dateOfBirth: LocalDate,
telephoneNumber: String? = null,
mobileNumber: String? = null,
emailAddress: String? = null,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) =
Person(
id,
crn,
noms,
softDeleted
)
) = Person(
id,
crn,
noms,
firstName,
surname,
dateOfBirth,
telephoneNumber,
mobileNumber,
emailAddress,
softDeleted
)

fun generateManager(
person: Person,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest
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.CaseIdentifiers
import uk.gov.justice.digital.hmpps.api.model.Manager
import uk.gov.justice.digital.hmpps.api.model.MappaDetail
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
Expand Down Expand Up @@ -66,4 +63,24 @@ class CaseDetailIntegrationTests {
)
assertFalse(manager.unallocated)
}

@Test
fun `person detail is returned correctly`() {
val detail = mockMvc.perform(get("/probation-cases/${PersonGenerator.DEFAULT.crn}").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<PersonDetail>()

assertThat(detail.crn, equalTo(PersonGenerator.DEFAULT.crn))
assertThat(detail.name, equalTo(Name("Fred", "Williams")))
assertThat(detail.dateOfBirth, equalTo(LocalDate.of(1982, 8, 19)))
assertThat(
detail.contactDetails, equalTo(
ContactDetails(
"020 346 7982",
"07452819463",
"[email protected]"
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ class CaseDetailController(private val caseDetailService: CaseDetailService) {
@PreAuthorize("hasRole('PROBATION_API__RESETTLEMENT_PASSPORT__CASE_DETAIL')")
@GetMapping("/{crn}/community-manager")
fun getCommunityManager(@PathVariable crn: String) = caseDetailService.findCommunityManager(crn)

@PreAuthorize("hasRole('PROBATION_API__RESETTLEMENT_PASSPORT__CASE_DETAIL')")
@GetMapping("/{crn}")
fun getPersonDetail(@PathVariable crn: String) = caseDetailService.getPersonDetail(crn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.digital.hmpps.api.model

import java.time.LocalDate

data class PersonDetail(
val crn: String,
val name: Name,
val dateOfBirth: LocalDate,
val contactDetails: ContactDetails?
)

data class ContactDetails(val telephone: String?, val mobile: String?, val email: String?) {
companion object {
fun of(telephone: String?, mobile: String?, email: String?): ContactDetails? =
if (telephone == null && mobile == null && email == null) {
null
} else {
ContactDetails(telephone, mobile, email)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import jakarta.persistence.Id
import jakarta.persistence.Table
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import java.time.LocalDate

@Entity
@Immutable
Expand All @@ -22,5 +23,15 @@ class Person(
@Column(name = "noms_number", columnDefinition = "char(7)")
val noms: String?,

val firstName: String,
val surname: String,
@Column(name = "date_of_birth_date")
val dateOfBirth: LocalDate,

val telephoneNumber: String?,
val mobileNumber: String?,
@Column(name = "e_mail_address")
val emailAddress: String?,

val softDeleted: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package uk.gov.justice.digital.hmpps.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.CaseIdentifiers
import uk.gov.justice.digital.hmpps.api.model.Manager
import uk.gov.justice.digital.hmpps.api.model.MappaDetail
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.entity.Category
import uk.gov.justice.digital.hmpps.entity.Level
import uk.gov.justice.digital.hmpps.entity.PersonManagerRepository
import uk.gov.justice.digital.hmpps.entity.PersonRepository
import uk.gov.justice.digital.hmpps.entity.RegistrationRepository
import uk.gov.justice.digital.hmpps.api.model.*
import uk.gov.justice.digital.hmpps.entity.*
import uk.gov.justice.digital.hmpps.entity.Staff
import uk.gov.justice.digital.hmpps.entity.findMappa
import uk.gov.justice.digital.hmpps.exception.NotFoundException

@Service
Expand All @@ -37,6 +29,8 @@ class CaseDetailService(

fun findCommunityManager(crn: String) = personManagerRepository.findByPersonCrn(crn)?.staff?.asManager()
?: throw NotFoundException("Person", "crn", crn)

fun getPersonDetail(crn: String) = personRepository.getByCrn(crn).detail()
}

private fun String.toMappaLevel() = Level.entries.find { it.name == this }?.number
Expand All @@ -50,3 +44,7 @@ private fun Staff.asManager() = if (code.endsWith("U")) {
} else {
Manager(Name(forename, surname), false)
}

private fun Person.detail() = PersonDetail(
crn, Name(firstName, surname), dateOfBirth, ContactDetails.of(telephoneNumber, mobileNumber, emailAddress)
)

0 comments on commit c71c2b4

Please sign in to comment.