diff --git a/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index f6ea94c9a7..74d75deaed 100644 --- a/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -1,17 +1,21 @@ package uk.gov.justice.digital.hmpps.data import jakarta.annotation.PostConstruct +import jakarta.persistence.EntityManager import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty 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.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator import uk.gov.justice.digital.hmpps.user.AuditUserRepository @Component @ConditionalOnProperty("seed.database") class DataLoader( - private val auditUserRepository: AuditUserRepository + private val auditUserRepository: AuditUserRepository, + private val entityManager: EntityManager ) : ApplicationListener { @PostConstruct @@ -19,7 +23,20 @@ class DataLoader( auditUserRepository.save(UserGenerator.AUDIT_USER) } + @Transactional override fun onApplicationEvent(are: ApplicationReadyEvent) { - // Perform dev/test database setup here, using JPA repositories and generator classes... + saveAll( + PersonGenerator.TITLE, + PersonGenerator.GENDER, + PersonGenerator.ETHNICITY, + PersonGenerator.NATIONALITY, + PersonGenerator.MAIN_ADDRESS, + PersonGenerator.MIN_PERSON, + PersonGenerator.FULL_PERSON, + *PersonGenerator.FULL_PERSON_ALIASES.toTypedArray(), + *PersonGenerator.FULL_PERSON_ADDRESSES.toTypedArray() + ) } + + private fun saveAll(vararg entities: Any) = entities.forEach(entityManager::merge) } diff --git a/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt new file mode 100644 index 0000000000..0ec748753c --- /dev/null +++ b/projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -0,0 +1,128 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integration.delius.entity.Alias +import uk.gov.justice.digital.hmpps.integration.delius.entity.Person +import uk.gov.justice.digital.hmpps.integration.delius.entity.PersonAddress +import uk.gov.justice.digital.hmpps.integration.delius.entity.ReferenceData +import java.time.LocalDate + +object PersonGenerator { + val ETHNICITY = generateReferenceData("ETH") + val GENDER = generateReferenceData("GEN") + val NATIONALITY = generateReferenceData("NAT") + val TITLE = generateReferenceData("TIT") + val MAIN_ADDRESS = generateReferenceData("M", "Main Address") + + val MIN_PERSON = + generatePerson("M123456", firstname = "Isabelle", surname = "Necessary", dob = LocalDate.of(1990, 3, 5)) + val FULL_PERSON = generatePerson( + "F123456", + "A3349EX", + "2011/0593710D", + "89861/11W", + "FJ123456W", + "94600E", + "Frederick", + "Paul", + "Bernard", + "Johnson", + LocalDate.of(1975, 7, 15), + "No Previous", + "Freddy", + "0191 755 4789", + "07895746789", + "fred@gmail.com", + TITLE, + GENDER, + NATIONALITY, + ETHNICITY, + "Description of ethnicity" + ) + + val FULL_PERSON_ALIASES = listOf( + generateAlias( + FULL_PERSON.id, "Freddy", null, null, "Banter", LocalDate.of(1974, 2, 17) + ) + ) + + val FULL_PERSON_ADDRESSES = listOf( + generateAddress(FULL_PERSON.id, MAIN_ADDRESS, "PC1 1TS", LocalDate.now().minusDays(30)) + ) + + fun generateReferenceData( + code: String, + description: String = "Description of $code", + id: Long = IdGenerator.getAndIncrement() + ) = ReferenceData(code, description, id) + + fun generatePerson( + crn: String, + nomsId: String? = null, + pnc: String? = null, + cro: String? = null, + niNumber: String? = null, + prisonerNumber: String? = null, + firstname: String, + secondName: String? = null, + thirdName: String? = null, + surname: String, + dob: LocalDate, + previousSurname: String? = null, + preferredName: String? = null, + telephoneNumber: String? = null, + mobileNumber: String? = null, + emailAddress: String? = null, + title: ReferenceData? = null, + gender: ReferenceData? = null, + nationality: ReferenceData? = null, + ethnicity: ReferenceData? = null, + ethnicityDescription: String? = null, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = Person( + crn, + nomsId, + pnc, + cro, + niNumber, + prisonerNumber, + firstname, + secondName, + thirdName, + surname, + dob, + previousSurname, + preferredName, + telephoneNumber, + mobileNumber, + emailAddress, + title, + gender, + nationality, + ethnicity, + ethnicityDescription, + softDeleted, + id + ) + + fun generateAlias( + personId: Long, + firstName: String, + secondName: String?, + thirdName: String?, + surname: String, + dateOfBirth: LocalDate, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = Alias(personId, firstName, secondName, thirdName, surname, dateOfBirth, softDeleted, id) + + fun generateAddress( + personId: Long, + status: ReferenceData, + postcode: String, + startDate: LocalDate, + endDate: LocalDate? = null, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = PersonAddress(personId, status, postcode, startDate, endDate, softDeleted, id) +} \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CorePersonIntegrationTest.kt b/projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CorePersonIntegrationTest.kt new file mode 100644 index 0000000000..5b6cc8da08 --- /dev/null +++ b/projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CorePersonIntegrationTest.kt @@ -0,0 +1,64 @@ +package uk.gov.justice.digital.hmpps + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT +import org.springframework.boot.test.mock.mockito.MockBean +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.PersonDetail +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator +import uk.gov.justice.digital.hmpps.integration.delius.entity.Alias +import uk.gov.justice.digital.hmpps.integration.delius.entity.PersonAddress +import uk.gov.justice.digital.hmpps.service.asAddress +import uk.gov.justice.digital.hmpps.service.asModel +import uk.gov.justice.digital.hmpps.service.detail +import uk.gov.justice.digital.hmpps.telemetry.TelemetryService +import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson +import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken + +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = RANDOM_PORT) +internal class CorePersonIntegrationTest { + @Autowired + lateinit var mockMvc: MockMvc + + @MockBean + lateinit var telemetryService: TelemetryService + + @Test + fun `correctly returns detail by crn`() { + val detail = mockMvc + .perform(get("/probation-cases/${PersonGenerator.MIN_PERSON.crn}").withToken()) + .andExpect(status().is2xxSuccessful) + .andReturn().response.contentAsJson() + + assertThat( + detail, + equalTo(PersonGenerator.MIN_PERSON.detail(listOf(), listOf())) + ) + } + + @Test + fun `correctly returns detail by id`() { + val detail = mockMvc + .perform(get("/probation-cases/${PersonGenerator.FULL_PERSON.id}").withToken()) + .andExpect(status().is2xxSuccessful) + .andReturn().response.contentAsJson() + + assertThat( + detail, + equalTo( + PersonGenerator.FULL_PERSON.detail( + PersonGenerator.FULL_PERSON_ALIASES.map(Alias::asModel), + PersonGenerator.FULL_PERSON_ADDRESSES.mapNotNull(PersonAddress::asAddress) + ) + ) + ) + } +} diff --git a/projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt deleted file mode 100644 index d9006c74c5..0000000000 --- a/projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ /dev/null @@ -1,30 +0,0 @@ -package uk.gov.justice.digital.hmpps - -import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT -import org.springframework.boot.test.mock.mockito.MockBean -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.test.MockMvcExtensions.withToken -import uk.gov.justice.digital.hmpps.telemetry.TelemetryService - -@AutoConfigureMockMvc -@SpringBootTest(webEnvironment = RANDOM_PORT) -internal class IntegrationTest { - @Autowired - lateinit var mockMvc: MockMvc - - @MockBean - lateinit var telemetryService: TelemetryService - - @Test - fun `API call retuns a success response`() { - mockMvc - .perform(get("/example/123").withToken()) - .andExpect(status().is2xxSuccessful) - } -} diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/PersonDetail.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/PersonDetail.kt new file mode 100644 index 0000000000..badfaf9dfa --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/PersonDetail.kt @@ -0,0 +1,52 @@ +package uk.gov.justice.digital.hmpps.api.model + +import java.time.LocalDate + +data class PersonDetail( + val identifiers: Identifiers, + val name: Name, + val dateOfBirth: LocalDate, + val title: CodeDescription?, + val gender: CodeDescription?, + val nationality: CodeDescription?, + val ethnicity: CodeDescription?, + val ethnicityDescription: String?, + val contactDetails: ContactDetails?, + val aliases: List, + val addresses: List
+) + +data class Identifiers( + val deliusId: Long, + val crn: String, + val nomsId: String?, + val prisonerNumber: String?, + val pnc: String?, + val cro: String?, + val ni: String? +) + +data class Name( + val forename: String, + val middleName: String?, + val surname: String, + val previousSurname: String? = null, + val preferred: String? = null +) + +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) + } + } +} + +data class CodeDescription(val code: String, val description: String) + +data class Alias(val name: Name, val dateOfBirth: LocalDate) + +data class Address(val postcode: String) \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/PersonResource.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/PersonResource.kt new file mode 100644 index 0000000000..875b4ac3a8 --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/PersonResource.kt @@ -0,0 +1,26 @@ +package uk.gov.justice.digital.hmpps.api.resource + +import org.springframework.security.access.prepost.PreAuthorize +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.PersonDetail +import uk.gov.justice.digital.hmpps.service.PersonService + +@RestController +@RequestMapping("probation-cases") +class PersonResource(private val personService: PersonService) { + @PreAuthorize("hasRole('PROBATION_API__CORE_PERSON__CASE_DETAIL')") + @GetMapping(value = ["/{identifier}"]) + fun getPersonDetails( + @PathVariable identifier: String + ): PersonDetail { + val id = identifier.toLongOrNull() + return if (id == null) { + personService.getPersonDetail(identifier) + } else { + personService.getPersonDetail(id) + } + } +} diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt deleted file mode 100644 index e5f139965c..0000000000 --- a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt +++ /dev/null @@ -1,17 +0,0 @@ -package uk.gov.justice.digital.hmpps.controller - -import org.springframework.security.access.prepost.PreAuthorize -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.RestController - -@RestController -class ApiController { - @PreAuthorize("hasRole('ROLE_EXAMPLE')") - @GetMapping(value = ["/example/{inputId}"]) - fun handle( - @PathVariable("inputId") inputId: String - ) { - // TODO Not yet implemented - } -} diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Alias.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Alias.kt new file mode 100644 index 0000000000..daf236a7b6 --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Alias.kt @@ -0,0 +1,33 @@ +package uk.gov.justice.digital.hmpps.integration.delius.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.Table +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import org.springframework.data.jpa.repository.JpaRepository +import java.time.LocalDate + +@Immutable +@Entity +@Table(name = "alias") +@SQLRestriction("soft_deleted = 0") +class Alias( + @Column(name = "offender_id") + val personId: Long, + val firstName: String, + val secondName: String?, + val thirdName: String?, + val surname: String, + @Column(name = "date_of_birth_date") + val dateOfBirth: LocalDate, + val softDeleted: Boolean, + @Id + @Column(name = "alias_id") + val id: Long +) + +interface AliasRepository : JpaRepository { + fun findByPersonId(personId: Long): List +} \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Person.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Person.kt new file mode 100644 index 0000000000..b6740b7de5 --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Person.kt @@ -0,0 +1,96 @@ +package uk.gov.justice.digital.hmpps.integration.delius.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import org.springframework.data.jpa.repository.EntityGraph +import org.springframework.data.jpa.repository.JpaRepository +import uk.gov.justice.digital.hmpps.exception.NotFoundException +import java.time.LocalDate +import java.util.* + +@NamedEntityGraph( + name = "person-with-ref-data", + attributeNodes = [ + NamedAttributeNode("title"), + NamedAttributeNode("gender"), + NamedAttributeNode("nationality"), + NamedAttributeNode("ethnicity") + ] +) +@Entity +@Immutable +@Table(name = "offender") +@SQLRestriction("soft_deleted = 0") +data class Person( + + @Column(columnDefinition = "char(7)") + val crn: String, + + @Column(name = "nomsNumber", columnDefinition = "char(7)") + val nomsId: String?, + + @Column(name = "pnc_number", columnDefinition = "char(13)") + val pnc: String?, + + @Column(name = "cro_number", columnDefinition = "char(12)") + val cro: String?, + + @Column(name = "ni_number", columnDefinition = "char(9)") + val niNumber: String?, + + @Column(name = "most_recent_prisoner_number") + val prisonerNumber: String?, + + val firstname: String, + val secondName: String?, + val thirdName: String?, + val surname: String, + @Column(name = "date_of_birth_date") + val dob: LocalDate, + val previousSurname: String?, + val preferredName: String?, + val telephoneNumber: String?, + val mobileNumber: String?, + @Column(name = "e_mail_address") + val emailAddress: String?, + + @ManyToOne + @JoinColumn(name = "title_id") + val title: ReferenceData?, + + @ManyToOne + @JoinColumn(name = "gender_id") + val gender: ReferenceData?, + + @ManyToOne + @JoinColumn(name = "nationality_id") + val nationality: ReferenceData?, + + @ManyToOne + @JoinColumn(name = "ethnicity_id") + val ethnicity: ReferenceData?, + + val ethnicityDescription: String?, + + @Column(name = "soft_deleted", columnDefinition = "number") + val softDeleted: Boolean, + + @Id + @Column(name = "offender_id") + val id: Long +) + +interface PersonRepository : JpaRepository { + @EntityGraph(value = "person-with-ref-data") + fun findByCrn(crn: String): Person? + + @EntityGraph(value = "person-with-ref-data") + override fun findById(id: Long): Optional +} + +fun PersonRepository.getByCrn(crn: String): Person = + findByCrn(crn) ?: throw NotFoundException("Person", "crn", crn) + +fun PersonRepository.getByPersonId(id: Long): Person = + findById(id).orElseThrow { NotFoundException("Person", "id", id) } \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/PersonAddress.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/PersonAddress.kt new file mode 100644 index 0000000000..dc66745478 --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/PersonAddress.kt @@ -0,0 +1,36 @@ +package uk.gov.justice.digital.hmpps.integration.delius.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import org.springframework.data.jpa.repository.JpaRepository +import java.time.LocalDate + +@Immutable +@Entity +@Table(name = "offender_address") +@SQLRestriction("soft_deleted = 0 and (end_date is null or end_date > current_date)") +class PersonAddress( + @Column(name = "offender_id") + val personId: Long, + @ManyToOne + @JoinColumn(name = "address_status_id") + val status: ReferenceData, + val postcode: String?, + val startDate: LocalDate?, + val endDate: LocalDate?, + val softDeleted: Boolean, + @Id + @Column(name = "offender_address_id") + val id: Long +) + +interface AddressRepository : JpaRepository { + fun findAllByPersonIdAndStatusCodeInOrderByStartDateDesc( + personId: Long, + statusCodes: List + ): List +} + +fun AddressRepository.mainAddresses(personId: Long) = + findAllByPersonIdAndStatusCodeInOrderByStartDateDesc(personId, listOf("M")) \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/ReferenceData.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/ReferenceData.kt new file mode 100644 index 0000000000..543b3bd40a --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/ReferenceData.kt @@ -0,0 +1,20 @@ +package uk.gov.justice.digital.hmpps.integration.delius.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.Table +import org.hibernate.annotations.Immutable + +@Immutable +@Entity +@Table(name = "r_standard_reference_list") +class ReferenceData( + @Column(name = "code_value") + val code: String, + @Column(name = "code_description") + val description: String, + @Id + @Column(name = "standard_reference_list_id") + val id: Long +) \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonMapping.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonMapping.kt new file mode 100644 index 0000000000..c7d2d07efc --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonMapping.kt @@ -0,0 +1,48 @@ +package uk.gov.justice.digital.hmpps.service + +import uk.gov.justice.digital.hmpps.api.model.* +import uk.gov.justice.digital.hmpps.integration.delius.entity.Person +import uk.gov.justice.digital.hmpps.integration.delius.entity.PersonAddress +import uk.gov.justice.digital.hmpps.integration.delius.entity.ReferenceData + +fun Person.detail(aliases: List, addresses: List
) = PersonDetail( + identifiers(), + name(), + dob, + title?.asCodeDescription(), + gender?.asCodeDescription(), + nationality?.asCodeDescription(), + ethnicity?.asCodeDescription(), + ethnicityDescription, + contactDetails(), + aliases, + addresses +) + +fun Person.identifiers() = + Identifiers(id, crn, nomsId?.trim(), prisonerNumber, pnc?.trim(), cro?.trim(), niNumber?.trim()) + +fun Person.name() = + Name( + firstname, + listOfNotNull(secondName, thirdName).ifEmpty { null }?.joinToString(" "), + surname, + previousSurname, + preferredName + ) + +fun Person.contactDetails() = ContactDetails.of(telephoneNumber, mobileNumber, emailAddress) + +fun ReferenceData.asCodeDescription() = CodeDescription(code, description) + +fun uk.gov.justice.digital.hmpps.integration.delius.entity.Alias.asModel() = Alias( + Name( + firstName, + listOfNotNull(secondName, thirdName).ifEmpty { null }?.joinToString(" "), + surname + ), + dateOfBirth +) + +fun PersonAddress.asAddress() = postcode?.let { Address(it) } + diff --git a/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt new file mode 100644 index 0000000000..060904c19f --- /dev/null +++ b/projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt @@ -0,0 +1,22 @@ +package uk.gov.justice.digital.hmpps.service + +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.api.model.PersonDetail +import uk.gov.justice.digital.hmpps.integration.delius.entity.* + +@Service +class PersonService( + private val personRepository: PersonRepository, + private val aliasRepository: AliasRepository, + private val addressRepository: AddressRepository +) { + fun getPersonDetail(crn: String): PersonDetail = personRepository.getByCrn(crn).withDetail() + + fun getPersonDetail(id: Long): PersonDetail = personRepository.getByPersonId(id).withDetail() + + private fun Person.withDetail(): PersonDetail = + detail( + aliasRepository.findByPersonId(id).map(Alias::asModel), + addressRepository.mainAddresses(id).mapNotNull(PersonAddress::asAddress) + ) +} \ No newline at end of file diff --git a/projects/core-person-record-and-delius/src/main/resources/application.yml b/projects/core-person-record-and-delius/src/main/resources/application.yml index c3369f58d9..8c94d39c3c 100644 --- a/projects/core-person-record-and-delius/src/main/resources/application.yml +++ b/projects/core-person-record-and-delius/src/main/resources/application.yml @@ -37,11 +37,9 @@ spring: jpa.hibernate.ddl-auto: create-drop seed.database: true -wiremock.enabled: true -context.initializer.classes: uk.gov.justice.digital.hmpps.wiremock.WireMockInitialiser jwt.authorities: - - ROLE_EXAMPLE + - ROLE_PROBATION_API__CORE_PERSON__CASE_DETAIL logging.level: uk.gov.justice.digital.hmpps: DEBUG