Skip to content

Commit

Permalink
PI-1466 update to accept noms or crn (#2285)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj authored Sep 14, 2023
1 parent ea64da5 commit 50d8898
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{
"request": {
"method": "GET",
"url": "/api/pom-allocation/A00123Y"
"url": "/api/pom-allocation/A0123BY"
},
"response": {
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class AllocationMessagingIntegrationTest {
"POM Allocated",
mapOf(
"prisonId" to "SWI",
"nomsId" to "A00123Y",
"nomsId" to "A0123BY",
"allocationDate" to "2023-05-09"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ interface PersonRepository : JpaRepository<Person, Long> {
@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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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
).also { person.set("managers", listOf(it)) }

whenever(personRepository.findByNomsId(person.nomsId!!)).thenReturn(person)

service.findByNomsId(person.nomsId!!)
service.findByIdentifier(Identifier(person.nomsId!!))

verify(ldapTemplate, never()).search(any(), any<AttributesMapper<String?>>())
}
Expand Down

0 comments on commit 50d8898

Please sign in to comment.