From c318511a4b1f49deec29f4829f30fcf747c03fa9 Mon Sep 17 00:00:00 2001 From: Anthony Britton Date: Thu, 9 Nov 2023 14:19:05 +0000 Subject: [PATCH] PI-1637 --- .../deploy/values-dev.yml | 2 - .../deploy/values-preprod.yml | 2 - .../deploy/values-prod.yml | 2 - .../justice/digital/hmpps/data/DataLoader.kt | 30 +++++- .../generator/CommunityManagerGenerator.kt | 39 ++++++++ .../src/dev/resources/local-public-key.pub | 8 +- .../__files/hmpps-auth-token-body.json | 2 +- .../digital/hmpps/CommunityManagerIntTest.kt | 76 ++++++++++++++ .../justice/digital/hmpps/IntegrationTest.kt | 31 ------ .../gov/justice/digital/hmpps/api/Manager.kt | 13 +++ .../digital/hmpps/api/ManagerResource.kt | 16 +++ .../digital/hmpps/controller/ApiController.kt | 17 ---- .../delius/manager/ManagerService.kt | 18 ++++ .../delius/manager/entity/CommunityManager.kt | 99 +++++++++++++++++++ 14 files changed, 294 insertions(+), 61 deletions(-) create mode 100644 projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CommunityManagerGenerator.kt create mode 100644 projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CommunityManagerIntTest.kt delete mode 100644 projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt create mode 100644 projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/Manager.kt create mode 100644 projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/ManagerResource.kt delete mode 100644 projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt create mode 100644 projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/ManagerService.kt create mode 100644 projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/CommunityManager.kt diff --git a/projects/prisoner-profile-and-delius/deploy/values-dev.yml b/projects/prisoner-profile-and-delius/deploy/values-dev.yml index 23add44ac2..263c5877ac 100644 --- a/projects/prisoner-profile-and-delius/deploy/values-dev.yml +++ b/projects/prisoner-profile-and-delius/deploy/values-dev.yml @@ -1,5 +1,3 @@ -enabled: false # TODO set this to true when you're ready to deploy your service - generic-service: ingress: host: prisoner-profile-and-delius-dev.hmpps.service.justice.gov.uk diff --git a/projects/prisoner-profile-and-delius/deploy/values-preprod.yml b/projects/prisoner-profile-and-delius/deploy/values-preprod.yml index 421d49d09b..87e740eb5a 100644 --- a/projects/prisoner-profile-and-delius/deploy/values-preprod.yml +++ b/projects/prisoner-profile-and-delius/deploy/values-preprod.yml @@ -1,5 +1,3 @@ -enabled: false # TODO set this to true when you're ready to deploy your service - generic-service: ingress: host: prisoner-profile-and-delius-preprod.hmpps.service.justice.gov.uk diff --git a/projects/prisoner-profile-and-delius/deploy/values-prod.yml b/projects/prisoner-profile-and-delius/deploy/values-prod.yml index 216029f618..18ab2f448e 100644 --- a/projects/prisoner-profile-and-delius/deploy/values-prod.yml +++ b/projects/prisoner-profile-and-delius/deploy/values-prod.yml @@ -1,5 +1,3 @@ -enabled: false # TODO set this to true when you're ready to deploy your service - generic-service: ingress: host: prisoner-profile-and-delius.hmpps.service.justice.gov.uk diff --git a/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index f6ea94c9a7..d74c40ee69 100644 --- a/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -1,17 +1,26 @@ 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.CommunityManagerGenerator.ALLOCATED_PERSON +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.STAFF +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.TEAM +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_PERSON +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_STAFF +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.generateCommunityManager 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 +28,24 @@ 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... + listOf( + TEAM, + UNALLOCATED_STAFF, + STAFF, + UNALLOCATED_PERSON, + ALLOCATED_PERSON, + generateCommunityManager( + ALLOCATED_PERSON, + STAFF + ), + generateCommunityManager( + UNALLOCATED_PERSON, + UNALLOCATED_STAFF + ) + ).saveAll() } + + fun List.saveAll() = forEach { entityManager.persist(it) } } diff --git a/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CommunityManagerGenerator.kt b/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CommunityManagerGenerator.kt new file mode 100644 index 0000000000..e34a9ecddb --- /dev/null +++ b/projects/prisoner-profile-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CommunityManagerGenerator.kt @@ -0,0 +1,39 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.CommunityManager +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Person +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Staff +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Team + +object CommunityManagerGenerator { + val TEAM = generateTeam("N01PPT") + val UNALLOCATED_STAFF = generateStaff("N01PPTU", "Unallocated", "Staff") + val STAFF = generateStaff("N01PPTA", "James", "Brown") + val ALLOCATED_PERSON = generatePerson("A123456", "A1234TD") + val UNALLOCATED_PERSON = generatePerson("U123456", "U1234TD") + + fun generateTeam( + code: String, + description: String = "Description of $code", + id: Long = IdGenerator.getAndIncrement() + ) = Team(code, description, id) + + fun generateStaff(code: String, forename: String, surname: String, id: Long = IdGenerator.getAndIncrement()) = + Staff(code, forename, surname, id) + + fun generatePerson( + crn: String, + nomsId: String, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = Person(crn, nomsId, softDeleted, id) + + fun generateCommunityManager( + person: Person, + staff: Staff, + team: Team = TEAM, + active: Boolean = true, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = CommunityManager(person, team, staff, active, softDeleted, id) +} diff --git a/projects/prisoner-profile-and-delius/src/dev/resources/local-public-key.pub b/projects/prisoner-profile-and-delius/src/dev/resources/local-public-key.pub index c0b70f3172..b19c1e9863 100644 --- a/projects/prisoner-profile-and-delius/src/dev/resources/local-public-key.pub +++ b/projects/prisoner-profile-and-delius/src/dev/resources/local-public-key.pub @@ -1,6 +1,6 @@ -----BEGIN PUBLIC KEY----- -MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDo3hw1/oChbttEOxEH4NUDrH+Y -n2x0DavAmDjMbhcSiQ6+/t8Nz/N03BauWzFOGBtftnQrHfnF+O7RAKj8zMjcbIq4 -QrYeXEpnaFCGEwTtOBpxvSEWPrLEpr1gCarBQZDp67ag+SYqrDgkn2Vme/dMvMUQ -xUO3DT6jg9921J6TlwIDAQAB +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDju2wS/xXRfjdRI9thM2yJ7xO4 +cy5o8eJ1tljIrqJvKcdsmuflNv7IXGZP4OIQBhALkS3CQxIhBl9gNAtyu2AdYUdn +bSlBS5qLtLg2EoPn2GyGSwRiZCmZcmDsHdn8DX0zNCgZN9i/B/yCL0gXgvHRGEqP +QCtNM3Urqkvn97LVjQIDAQAB -----END PUBLIC KEY----- \ No newline at end of file diff --git a/projects/prisoner-profile-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json b/projects/prisoner-profile-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json index 33e1aa358c..76da8faf95 100644 --- a/projects/prisoner-profile-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json +++ b/projects/prisoner-profile-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json @@ -1,5 +1,5 @@ { - "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsInVzZXJfbmFtZSI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiYXV0aF9zb3VyY2UiOiJub25lIiwiaXNzIjoiaHR0cHM6Ly9zaWduLWluLWRldi5obXBwcy5zZXJ2aWNlLmp1c3RpY2UuZ292LnVrL2F1dGgvaXNzdWVyIiwiZXhwIjo5OTk5OTk5OTk5LCJhdXRob3JpdGllcyI6WyJST0xFX0VYQU1QTEUiLCJST0xFX1dPUktMT0FEX1JFQUQiLCJST0xFX0FMTE9DQVRJT05fQ09OVEVYVCJdLCJqdGkiOiIyNUR1Um4xLWh5SFpld0xjZEpKeHdWTDAzS1UiLCJjbGllbnRfaWQiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiaWF0IjoxNjYzNzU3MzExfQ.5FTCUjA7QZMPxO_EMzkGNSM-IkPk2hfPXyzuNiAa7uuqYva_yCducrC5FdetAiC1W6XpUB7wfoMNDmbW2xepj5oRhcxDx18r92aLPYnKkxaA68hLQF90euMtTzfBzOPg-rKDTNIJKrUC-YoQlFKuCauw0Z5cw1XT6R9GIfi5Yx4", + "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsInVzZXJfbmFtZSI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiYXV0aF9zb3VyY2UiOiJub25lIiwiaXNzIjoiaHR0cHM6Ly9zaWduLWluLWRldi5obXBwcy5zZXJ2aWNlLmp1c3RpY2UuZ292LnVrL2F1dGgvaXNzdWVyIiwiZXhwIjo5OTk5OTk5OTk5LCJhdXRob3JpdGllcyI6WyJST0xFX1BST0JBVElPTl9BUElfX1BSSVNPTkVSX1BST0ZJTEVfX0NBU0VfREVUQUlMIl0sImp0aSI6IjI1RHVSbjEtaHlIWmV3TGNkSkp4d1ZMMDNLVSIsImNsaWVudF9pZCI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJpYXQiOjE2NjM3NTczMTF9.cDtZYUfjtSS1VM1A1733-4BLfbKN-XS7WHu0mt3ZP0XafVQRR8F7qyisQAwsvSSdvzIqqINsHP7IP2x_NBQupQav4ABBEigGnBD-qPeekCCGr8O7Nxea_hcrAo_cj0UANQB0PlBkZyOYcE77L4zj1-JmphO1F58KI0cGYUaWOVs", "token_type": "bearer", "expires_in": 9999999999, "scope": "read write", diff --git a/projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CommunityManagerIntTest.kt b/projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CommunityManagerIntTest.kt new file mode 100644 index 0000000000..47e3d82496 --- /dev/null +++ b/projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CommunityManagerIntTest.kt @@ -0,0 +1,76 @@ +package uk.gov.justice.digital.hmpps + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.github.tomakehurst.wiremock.WireMockServer +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource +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.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.Manager +import uk.gov.justice.digital.hmpps.api.Team +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.ALLOCATED_PERSON +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.STAFF +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.TEAM +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_PERSON +import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_STAFF +import uk.gov.justice.digital.hmpps.integrations.delius.manager.name +import uk.gov.justice.digital.hmpps.security.withOAuth2Token + +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = RANDOM_PORT) +internal class CommunityManagerIntTest { + @Autowired + lateinit var mockMvc: MockMvc + + @Autowired + lateinit var wireMockServer: WireMockServer + + @Autowired + lateinit var objectMapper: ObjectMapper + + @ParameterizedTest + @MethodSource("communityManagers") + fun `returns the community manager correctly`(nomsId: String, expected: Manager) { + val res = mockMvc + .perform( + get("/probation-cases/$nomsId/community-manager") + .withOAuth2Token(wireMockServer) + ) + .andExpect(status().is2xxSuccessful) + .andReturn().response.contentAsString + + val actual = objectMapper.readValue(res) + assertThat(actual, equalTo(expected)) + } + + @Test + fun `noms id not found throws 404`() { + mockMvc + .perform( + get("/probation-cases/invalid/community-manager") + .withOAuth2Token(wireMockServer) + ) + .andExpect(status().isNotFound) + } + + companion object { + @JvmStatic + fun communityManagers() = listOf( + Arguments.of(ALLOCATED_PERSON.nomsId, Manager(STAFF.code, STAFF.name(), Team(TEAM.code, TEAM.description))), + Arguments.of( + UNALLOCATED_PERSON.nomsId, + Manager(UNALLOCATED_STAFF.code, UNALLOCATED_STAFF.name(), Team(TEAM.code, TEAM.description)) + ) + ) + } +} diff --git a/projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt deleted file mode 100644 index d2382a469a..0000000000 --- a/projects/prisoner-profile-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ /dev/null @@ -1,31 +0,0 @@ -package uk.gov.justice.digital.hmpps - -import com.github.tomakehurst.wiremock.WireMockServer -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.security.withOAuth2Token -import uk.gov.justice.digital.hmpps.telemetry.TelemetryService - -@AutoConfigureMockMvc -@SpringBootTest(webEnvironment = RANDOM_PORT) -internal class IntegrationTest { - @Autowired lateinit var mockMvc: MockMvc - - @Autowired lateinit var wireMockServer: WireMockServer - - @MockBean lateinit var telemetryService: TelemetryService - - @Test - fun `API call retuns a success response`() { - mockMvc - .perform(get("/example/123").withOAuth2Token(wireMockServer)) - .andExpect(status().is2xxSuccessful) - } -} diff --git a/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/Manager.kt b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/Manager.kt new file mode 100644 index 0000000000..13a549f6e0 --- /dev/null +++ b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/Manager.kt @@ -0,0 +1,13 @@ +package uk.gov.justice.digital.hmpps.api + +data class Manager( + val code: String, + val name: Name, + val team: Team +) { + val unallocated = code.endsWith("U") +} + +data class Name(val forename: String, val surname: String) + +data class Team(val code: String, val description: String) diff --git a/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/ManagerResource.kt b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/ManagerResource.kt new file mode 100644 index 0000000000..53fc507e81 --- /dev/null +++ b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/ManagerResource.kt @@ -0,0 +1,16 @@ +package uk.gov.justice.digital.hmpps.api + +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestMethod +import org.springframework.web.bind.annotation.RestController +import uk.gov.justice.digital.hmpps.integrations.delius.manager.ManagerService + +@RestController +@RequestMapping("probation-cases/{nomsId}") +class ManagerResource(private val managerService: ManagerService) { + @PreAuthorize("hasRole('PROBATION_API__PRISONER_PROFILE__CASE_DETAIL')") + @RequestMapping("community-manager", method = [RequestMethod.GET]) + fun findCommunityManager(@PathVariable nomsId: String): Manager = managerService.findCommunityManager(nomsId) +} diff --git a/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt deleted file mode 100644 index e5f139965c..0000000000 --- a/projects/prisoner-profile-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/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/ManagerService.kt b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/ManagerService.kt new file mode 100644 index 0000000000..ad436aba64 --- /dev/null +++ b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/ManagerService.kt @@ -0,0 +1,18 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.manager + +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.api.Manager +import uk.gov.justice.digital.hmpps.api.Name +import uk.gov.justice.digital.hmpps.api.Team +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.CommunityManager +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.CommunityManagerRepository +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Staff +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.getByNomsId + +@Service +class ManagerService(private val communityManagerRepository: CommunityManagerRepository) { + fun findCommunityManager(nomsId: String) = communityManagerRepository.getByNomsId(nomsId).asManager() +} + +fun CommunityManager.asManager() = Manager(staff.code, staff.name(), Team(team.code, team.description)) +fun Staff.name() = Name(forename, surname) diff --git a/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/CommunityManager.kt b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/CommunityManager.kt new file mode 100644 index 0000000000..58010f639f --- /dev/null +++ b/projects/prisoner-profile-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/CommunityManager.kt @@ -0,0 +1,99 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.manager.entity + +import jakarta.persistence.Column +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.springframework.data.jpa.repository.JpaRepository +import uk.gov.justice.digital.hmpps.exception.NotFoundException + +@Immutable +@Entity +@Table(name = "offender_manager") +@Where(clause = "soft_deleted = 0 and active_flag = 1") +class CommunityManager( + + @ManyToOne + @JoinColumn(name = "offender_id") + val person: Person, + + @ManyToOne + @JoinColumn(name = "team_id") + val team: Team, + + @ManyToOne + @JoinColumn(name = "allocation_staff_id") + val staff: Staff, + + @Column(name = "active_flag", columnDefinition = "number") + val active: Boolean, + + @Column(columnDefinition = "number") + val softDeleted: Boolean, + + @Id + @Column(name = "offender_manager_id") + val id: Long +) + +@Immutable +@Entity +@Table(name = "offender") +@Where(clause = "soft_deleted = 0") +class Person( + + @Column(columnDefinition = "char(7)") + val crn: String, + + @Column(name = "noms_number", columnDefinition = "char(7)") + val nomsId: String? = null, + + @Column(columnDefinition = "number") + val softDeleted: Boolean, + + @Id + @Column(name = "offender_id") + val id: Long +) + +@Immutable +@Entity +@Table(name = "team") +class Team( + + @Column(name = "code", columnDefinition = "char(6)") + val code: String, + + val description: String, + + @Id + @Column(name = "team_id") + val id: Long +) + +@Immutable +@Entity +@Table(name = "staff") +class Staff( + + @Column(name = "officer_code", columnDefinition = "char(7)") + val code: String, + + val forename: String, + val surname: String, + + @Id + @Column(name = "staff_id") + val id: Long +) + +interface CommunityManagerRepository : JpaRepository { + fun findByPersonNomsId(nomsId: String): CommunityManager? +} + +fun CommunityManagerRepository.getByNomsId(nomsId: String): CommunityManager = + findByPersonNomsId(nomsId) ?: throw NotFoundException("Person", "nomsId", nomsId)