@@ -4,9 +4,12 @@ import org.springframework.beans.factory.annotation.Autowired
4
4
import org.springframework.stereotype.Service
5
5
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.PrisonerOffenderSearchGateway
6
6
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.ProbationOffenderSearchGateway
7
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.NomisNumber
7
8
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.OffenderSearchResponse
8
9
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person
9
10
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
11
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
12
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
10
13
11
14
@Service
12
15
class GetPersonService (
@@ -19,6 +22,68 @@ class GetPersonService(
19
22
return Response (data = personFromProbationOffenderSearch.data, errors = personFromProbationOffenderSearch.errors)
20
23
}
21
24
25
+ enum class IdentifierType {
26
+ NOMS ,
27
+ CRN ,
28
+ UNKNOWN ,
29
+ }
30
+
31
+ fun identifyHmppsId (input : String ): IdentifierType {
32
+ val nomsPattern = Regex (" ^[A-Z]\\ d{4}[A-Z]{2}$" )
33
+ val crnPattern = Regex (" ^[A-Z]{2}\\ d{6}$" )
34
+
35
+ return when {
36
+ nomsPattern.matches(input) -> IdentifierType .NOMS
37
+ crnPattern.matches(input) -> IdentifierType .CRN
38
+ else -> IdentifierType .UNKNOWN
39
+ }
40
+ }
41
+
42
+ /* *
43
+ * Identify whether the hmppsId is a noms number or a crn
44
+ * When it is a noms number then return it.
45
+ * When it is a CRN look up the prisoner in probation offender search and then return it
46
+ */
47
+ fun getNomisNumber (hmppsId : String ): Response <NomisNumber ?> {
48
+ return when (identifyHmppsId(hmppsId)) {
49
+ IdentifierType .NOMS -> Response (data = NomisNumber (hmppsId))
50
+
51
+ IdentifierType .CRN -> {
52
+ val personFromProbationOffenderSearch = probationOffenderSearchGateway.getPerson(id = hmppsId)
53
+ val nomisNumber = personFromProbationOffenderSearch.data?.identifiers?.nomisNumber
54
+ val errors = personFromProbationOffenderSearch.errors.toMutableList()
55
+
56
+ if (nomisNumber == null ) {
57
+ errors.add(
58
+ UpstreamApiError (
59
+ description = " NOMIS number not found" ,
60
+ type = UpstreamApiError .Type .ENTITY_NOT_FOUND ,
61
+ causedBy = UpstreamApi .PROBATION_OFFENDER_SEARCH ,
62
+ ),
63
+ )
64
+ }
65
+
66
+ Response (
67
+ data = nomisNumber?.let { NomisNumber (it) },
68
+ errors = errors,
69
+ )
70
+ }
71
+
72
+ IdentifierType .UNKNOWN ->
73
+ Response (
74
+ data = null ,
75
+ errors =
76
+ listOf (
77
+ UpstreamApiError (
78
+ description = " Invalid HMPPS ID: $hmppsId " ,
79
+ type = UpstreamApiError .Type .BAD_REQUEST ,
80
+ causedBy = UpstreamApi .NOMIS ,
81
+ ),
82
+ ),
83
+ )
84
+ }
85
+ }
86
+
22
87
fun getCombinedDataForPerson (hmppsId : String ): Response <OffenderSearchResponse > {
23
88
val probationResponse = probationOffenderSearchGateway.getPerson(id = hmppsId)
24
89
0 commit comments