From 50d88984189a4c070c2a421fe5c830b0fdfe4751 Mon Sep 17 00:00:00 2001 From: Anthony Britton <105213050+anthony-britton-moj@users.noreply.github.com> Date: Thu, 14 Sep 2023 15:19:14 +0100 Subject: [PATCH] PI-1466 update to accept noms or crn (#2285) --- .../hmpps/data/generator/PersonGenerator.kt | 2 +- .../resources/messages/new-allocation.json | 2 +- .../mappings/manage-pom-cases-api.json | 2 +- .../AllocationMessagingIntegrationTest.kt | 2 +- .../digital/hmpps/ApiIntegrationTest.kt | 24 +++++++++++++++---- .../api/resource/ProbationRecordResource.kt | 5 ++-- .../delius/person/entity/Person.kt | 6 +++++ .../hmpps/services/ProbationRecordService.kt | 21 ++++++++++++++-- .../services/ProbationRecordServiceTest.kt | 4 ++-- 9 files changed, 53 insertions(+), 15 deletions(-) diff --git a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index 35c483c661..68cbdd8be5 100644 --- a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -7,7 +7,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team import uk.gov.justice.digital.hmpps.integrations.delius.reference.entity.ReferenceData object PersonGenerator { - val DEFAULT = generate("T123456", "A00123Y", ReferenceDataGenerator.TIER_2) + val DEFAULT = generate("T123456", "A0123BY", ReferenceDataGenerator.TIER_2) val HANDOVER = generate("H123456", "A1024BY") val UPDATE_HANDOVER_AND_START = generate("H123457", "A2048BY") val CREATE_HANDOVER_AND_START = generate("H123458", "A4096BY") diff --git a/projects/manage-pom-cases-and-delius/src/dev/resources/messages/new-allocation.json b/projects/manage-pom-cases-and-delius/src/dev/resources/messages/new-allocation.json index 9130491211..01ec97a496 100644 --- a/projects/manage-pom-cases-and-delius/src/dev/resources/messages/new-allocation.json +++ b/projects/manage-pom-cases-and-delius/src/dev/resources/messages/new-allocation.json @@ -2,7 +2,7 @@ "Type": "Notification", "MessageId": "63ba0b4b-340e-4853-b343-6429965a3961", "TopicArn": "arn:aws:sns:eu-west-2:000000000000:domain-event", - "Message": "{\"eventType\":\"offender-management.pom.allocated\",\"detailUrl\":\"http://localhost:{wiremock.port}/api/pom-allocation/A00123Y\",\"occurredAt\":\"2023-05-09T13:25:19.102Z\",\"personReference\":{\"identifiers\":[{\"type\":\"NOMS\",\"value\":\"A00123Y\"}]}}", + "Message": "{\"eventType\":\"offender-management.pom.allocated\",\"detailUrl\":\"http://localhost:{wiremock.port}/api/pom-allocation/A0123BY\",\"occurredAt\":\"2023-05-09T13:25:19.102Z\",\"personReference\":{\"identifiers\":[{\"type\":\"NOMS\",\"value\":\"A0123BY\"}]}}", "Timestamp": "2023-05-09T13:25:20.000Z", "MessageAttributes": { "eventType": { diff --git a/projects/manage-pom-cases-and-delius/src/dev/resources/simulations/mappings/manage-pom-cases-api.json b/projects/manage-pom-cases-and-delius/src/dev/resources/simulations/mappings/manage-pom-cases-api.json index a45797c874..92ce69a8e0 100644 --- a/projects/manage-pom-cases-and-delius/src/dev/resources/simulations/mappings/manage-pom-cases-api.json +++ b/projects/manage-pom-cases-and-delius/src/dev/resources/simulations/mappings/manage-pom-cases-api.json @@ -42,7 +42,7 @@ { "request": { "method": "GET", - "url": "/api/pom-allocation/A00123Y" + "url": "/api/pom-allocation/A0123BY" }, "response": { "headers": { diff --git a/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocationMessagingIntegrationTest.kt b/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocationMessagingIntegrationTest.kt index 17dff73575..5c3042a9e3 100644 --- a/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocationMessagingIntegrationTest.kt +++ b/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocationMessagingIntegrationTest.kt @@ -64,7 +64,7 @@ internal class AllocationMessagingIntegrationTest { "POM Allocated", mapOf( "prisonId" to "SWI", - "nomsId" to "A00123Y", + "nomsId" to "A0123BY", "allocationDate" to "2023-05-09" ) ) diff --git a/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ApiIntegrationTest.kt b/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ApiIntegrationTest.kt index 5fef9a1094..7e61ab949b 100644 --- a/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ApiIntegrationTest.kt +++ b/projects/manage-pom-cases-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ApiIntegrationTest.kt @@ -5,7 +5,9 @@ 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 @@ -22,6 +24,7 @@ import uk.gov.justice.digital.hmpps.api.model.Team 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.ReferenceDataGenerator +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person import uk.gov.justice.digital.hmpps.security.withOAuth2Token @AutoConfigureMockMvc @@ -37,11 +40,14 @@ internal class ApiIntegrationTest { @Autowired lateinit var objectMapper: ObjectMapper - @Test - fun `successful retrieval of a case record by noms id`() { - val person = PersonGenerator.DEFAULT + @ParameterizedTest + @MethodSource("caseIdentifiers") + fun `successful retrieval of a case record by crn or noms id`(identifier: String, person: Person) { val res = mockMvc - .perform(get("/case-records/${person.nomsId}").withOAuth2Token(wireMockServer)) + .perform( + get("/case-records/$identifier") + .withOAuth2Token(wireMockServer) + ) .andExpect(status().is2xxSuccessful) .andReturn().response.contentAsString @@ -73,4 +79,12 @@ internal class ApiIntegrationTest { ) ) } + + companion object { + @JvmStatic + fun caseIdentifiers() = listOf( + Arguments.of(PersonGenerator.DEFAULT.crn, PersonGenerator.DEFAULT), + Arguments.of(PersonGenerator.DEFAULT.nomsId, PersonGenerator.DEFAULT) + ) + } } diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationRecordResource.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationRecordResource.kt index ebe41ef449..1559132e6e 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationRecordResource.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationRecordResource.kt @@ -4,11 +4,12 @@ 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 +import uk.gov.justice.digital.hmpps.services.Identifier import uk.gov.justice.digital.hmpps.services.ProbationRecordService @RestController class ProbationRecordResource(val prService: ProbationRecordService) { @PreAuthorize("hasRole('ROLE_MANAGE_POM_CASES')") - @GetMapping(value = ["/case-records/{nomsId}"]) - fun handle(@PathVariable("nomsId") nomsId: String) = prService.findByNomsId(nomsId) + @GetMapping(value = ["/case-records/{identifier}"]) + fun handle(@PathVariable("identifier") identifier: String) = prService.findByIdentifier(Identifier(identifier)) } diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt index 388b2b593b..a63e515131 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt @@ -81,9 +81,15 @@ interface PersonRepository : JpaRepository { @EntityGraph(attributePaths = ["currentTier", "managers.team.district", "managers.staff.user"]) fun findByNomsId(nomsId: String): Person? + @EntityGraph(attributePaths = ["currentTier", "managers.team.district", "managers.staff.user"]) + fun findByCrn(crn: String): Person? + @Query("select p.id from Person p where p.nomsId = :nomsId and p.softDeleted = false") fun findIdFromNomsId(nomsId: String): Long? } fun PersonRepository.getByNomsId(nomsId: String) = findByNomsId(nomsId) ?: throw NotFoundException("Person", "nomsId", nomsId) + +fun PersonRepository.getByCrn(crn: String) = + findByCrn(crn) ?: throw NotFoundException("Person", "crn", crn) diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordService.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordService.kt index 7e38c6e46a..03977a6c5a 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordService.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordService.kt @@ -5,6 +5,7 @@ import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.api.model.ProbationRecord import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.CaseAllocationRepository import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getByCrn import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getByNomsId import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.registration.entity.RegistrationRepository import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.registration.entity.findMappaRegistration @@ -18,8 +19,11 @@ class ProbationRecordService( private val registrationRepository: RegistrationRepository, private val ldapTemplate: LdapTemplate ) { - fun findByNomsId(nomsId: String): ProbationRecord { - val person = personRepository.getByNomsId(nomsId) + fun findByIdentifier(identifier: Identifier): ProbationRecord { + val person = when (identifier.type) { + Identifier.Type.CRN -> personRepository.getByCrn(identifier.value) + Identifier.Type.NOMS -> personRepository.getByNomsId(identifier.value) + } val user = person.manager.staff.user user?.username?.also { user.email = ldapTemplate.findEmailByUsername(it) @@ -30,3 +34,16 @@ class ProbationRecordService( return person.record(decision, registration, vloAssigned) } } + +data class Identifier(val value: String) { + val type = Type.of(value) + + enum class Type(val regex: Regex) { + CRN("[A-Z]\\d{6}".toRegex()), NOMS("[A-Z]\\d{4}[A-Z]{2}".toRegex()); + + companion object { + fun of(value: String): Type = entries.firstOrNull { it.regex.matches(value) } + ?: throw IllegalArgumentException("UnrecognisedIdentifierType") + } + } +} diff --git a/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordServiceTest.kt b/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordServiceTest.kt index 51866a30bf..54c8194aeb 100644 --- a/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordServiceTest.kt +++ b/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/ProbationRecordServiceTest.kt @@ -39,7 +39,7 @@ internal class ProbationRecordServiceTest { @Test fun `no user results in null email`() { - val person = PersonGenerator.generate("N123456", "NU1234SR") + val person = PersonGenerator.generate("N123456", "N1234SR") PersonManagerGenerator.generate( staff = ProviderGenerator.generateStaff("NoUser", "No", "User"), person = person @@ -47,7 +47,7 @@ internal class ProbationRecordServiceTest { whenever(personRepository.findByNomsId(person.nomsId!!)).thenReturn(person) - service.findByNomsId(person.nomsId!!) + service.findByIdentifier(Identifier(person.nomsId!!)) verify(ldapTemplate, never()).search(any(), any>()) }