Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cas2v2/cba 130 search by crn #2848

Draft
wants to merge 2 commits into
base: cas2v2/dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class OAuth2ResourceServerSecurityConfiguration {
authorize(HttpMethod.POST, "/cas2v2/submissions/*/status-updates", hasRole("CAS2_ASSESSOR"))
authorize(HttpMethod.GET, "/cas2v2/reference-data/**", hasAnyRole("CAS2_ASSESSOR", "POM"))
authorize(HttpMethod.GET, "/cas2v2/reports/**", hasRole("CAS2_MI"))
authorize(HttpMethod.GET, "/cas2v2/people/**", permitAll)
authorize("/cas2v2/**", hasAnyAuthority("ROLE_POM", "ROLE_LICENCE_CA"))

authorize(HttpMethod.GET, "/cas3-api.yml", permitAll)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,74 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.OASysRiskOfSer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.OASysRiskToSelf
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Person
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PersonRisks
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserQualification
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonInfoResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.ProbationOffenderSearchResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.community.OffenderDetailSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.BadRequestProblem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.ForbiddenProblem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.NotFoundProblem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.AuthorisableActionResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.NomisUserService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas2.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.OASysSectionsTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.PersonTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.RisksTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService as DeliusOffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService as OASysOffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas2.OffenderService as NomsOffenderService

@Service("Cas2v2PeopleController")
class Cas2v2PeopleController(
private val offenderService: OffenderService,
private val nomsOffenderService: NomsOffenderService,
private val deliusOffenderService: DeliusOffenderService,
private val oaSysOffenderService: OASysOffenderService,
private val oaSysSectionsTransformer: OASysSectionsTransformer,
private val personTransformer: PersonTransformer,
private val risksTransformer: RisksTransformer,
private val nomisUserService: NomisUserService,
private val deliusUserService: UserService,
) : PeopleCas2v2Delegate {

override fun peopleSearchGet(nomsNumber: String): ResponseEntity<Person> {
val currentUser = nomisUserService.getUserForRequest()
override fun peopleSearchGet(nomsNumber: String?, crn: String?): ResponseEntity<Person> {
return when {
nomsNumber != null -> searchByNoms(nomsNumber)
crn != null -> searchByCrn(crn)
else -> throw BadRequestProblem(errorDetail = "Either nomsNumber or crn must be provided")
}
}

val probationOffenderResult = offenderService.getPersonByNomsNumber(nomsNumber, currentUser)
@SuppressWarnings("ThrowsCount")
private fun searchByNoms(nomsNumber: String): ResponseEntity<Person> {
val currentUser = nomisUserService.getUserForRequest()
val probationOffenderResult = nomsOffenderService.getPersonByNomsNumber(nomsNumber, currentUser)

when (probationOffenderResult) {
is ProbationOffenderSearchResult.NotFound -> throw NotFoundProblem(nomsNumber, "Offender")
is ProbationOffenderSearchResult.Forbidden -> throw ForbiddenProblem()
is ProbationOffenderSearchResult.Unknown ->
throw probationOffenderResult.throwable
?: RuntimeException("Could not retrieve person info for Prison Number: $nomsNumber")
?: BadRequestProblem(errorDetail = "Could not retrieve person info for Prison Number: $nomsNumber")

is ProbationOffenderSearchResult.Success.Full -> return ResponseEntity.ok(
personTransformer.transformProbationOffenderToPersonApi(probationOffenderResult, nomsNumber),
)
}
}

private fun searchByCrn(crn: String): ResponseEntity<Person> {
val deliusUser = deliusUserService.getUserForRequest()
val personInfo = deliusOffenderService.getPersonInfoResult(crn, deliusUser.deliusUsername, deliusUser.hasQualification(UserQualification.LAO))

when (personInfo) {
is PersonInfoResult.NotFound -> throw NotFoundProblem(crn, "Offender")
is PersonInfoResult.Unknown -> throw personInfo.throwable ?: BadRequestProblem(errorDetail = "Could not retrieve person info for CRN: $crn")
is PersonInfoResult.Success -> return ResponseEntity.ok(
personTransformer.transformModelToPersonApi(personInfo),
)
}
}

override fun peopleCrnOasysRiskToSelfGet(crn: String): ResponseEntity<OASysRiskToSelf> {
getOffenderDetails(crn)

Expand Down Expand Up @@ -93,7 +121,7 @@ class Cas2v2PeopleController(
}

override fun peopleCrnRisksGet(crn: String): ResponseEntity<PersonRisks> {
val risks = when (val risksResult = offenderService.getRiskByCrn(crn)) {
val risks = when (val risksResult = nomsOffenderService.getRiskByCrn(crn)) {
is AuthorisableActionResult.Unauthorised -> throw ForbiddenProblem()
is AuthorisableActionResult.NotFound -> throw NotFoundProblem(crn, "Person")
is AuthorisableActionResult.Success -> risksResult.entity
Expand All @@ -103,7 +131,7 @@ class Cas2v2PeopleController(
}

private fun getOffenderDetails(crn: String): OffenderDetailSummary {
val offenderDetails = when (val offenderDetailsResult = offenderService.getOffenderByCrn(crn)) {
val offenderDetails = when (val offenderDetailsResult = nomsOffenderService.getOffenderByCrn(crn)) {
is AuthorisableActionResult.NotFound -> throw NotFoundProblem(crn, "Person")
is AuthorisableActionResult.Unauthorised -> throw ForbiddenProblem()
is AuthorisableActionResult.Success -> offenderDetailsResult.entity
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/static/cas2v2-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,16 @@ paths:
get:
tags:
- People operations
summary: Searches for a Person by their Prison Number (NOMIS ID)
summary: Searches for a Person by their Prison Number (NOMIS ID) or CRN
parameters:
- name: nomsNumber
in: query
description: Prison Number to search for
required: true
schema:
type: string
- name: crn
in: query
description: CRN to search for
schema:
type: string
responses:
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/static/codegen/built-cas2v2-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,16 @@ paths:
get:
tags:
- People operations
summary: Searches for a Person by their Prison Number (NOMIS ID)
summary: Searches for a Person by their Prison Number (NOMIS ID) or CRN
parameters:
- name: nomsNumber
in: query
description: Prison Number to search for
required: true
schema:
type: string
- name: crn
in: query
description: CRN to search for
schema:
type: string
responses:
Expand Down
Loading
Loading