Skip to content

Commit d9fa141

Browse files
Added service function
1 parent 646bdfe commit d9fa141

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/person/AlertsController.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AlertsController(
4444
@RequestParam(required = false, defaultValue = "10", name = "perPage") perPage: Int,
4545
): PaginatedResponse<Alert> {
4646
val hmppsId = encodedHmppsId.decodeUrlCharacters()
47-
val response = getAlertsForPersonService.execute(hmppsId)
47+
val response = getAlertsForPersonService.getAlertsForPnd(hmppsId)
4848

4949
if (response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) {
5050
throw EntityNotFoundException("Could not find person with id: $hmppsId")

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetAlertsForPersonService.kt

+25
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,29 @@ class GetAlertsForPersonService(
2525
errors = nomisAlerts.errors + personResponse.errors,
2626
)
2727
}
28+
29+
fun getAlertsForPnd(hmppsId: String): Response<List<Alert>> {
30+
val personResponse = getPersonService.execute(hmppsId = hmppsId)
31+
val nomisNumber = personResponse.data?.identifiers?.nomisNumber
32+
var nomisAlerts: Response<List<Alert>> = Response(data = emptyList())
33+
34+
if (nomisNumber != null) {
35+
val allNomisAlerts = nomisGateway.getAlertsForPerson(nomisNumber)
36+
val filteredAlerts =
37+
allNomisAlerts.data?.filter {
38+
it.code in
39+
listOf(
40+
"BECTER", "HA", "XA", "XCA", "XEL", "XELH", "XER", "XHT", "XILLENT",
41+
"XIS", "XR", "XRF", "XSA", "HA2", "RCS", "RDV", "RKC", "RPB", "RPC",
42+
"RSS", "RST", "RDP", "REG", "RLG", "ROP", "RRV", "RTP", "RYP", "HS", "SC",
43+
)
44+
}.orEmpty()
45+
nomisAlerts = Response(data = filteredAlerts, errors = allNomisAlerts.errors)
46+
}
47+
48+
return Response(
49+
data = nomisAlerts.data,
50+
errors = nomisAlerts.errors + personResponse.errors,
51+
)
52+
}
2853
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/person/AlertsControllerTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ internal class AlertsControllerTest(
193193
beforeTest {
194194
Mockito.reset(getAlertsForPersonService)
195195
Mockito.reset(auditService)
196-
whenever(getAlertsForPersonService.execute(hmppsId)).thenReturn(
196+
whenever(getAlertsForPersonService.getAlertsForPnd(hmppsId)).thenReturn(
197197
Response(
198198
data =
199199
listOf(
@@ -220,7 +220,7 @@ internal class AlertsControllerTest(
220220
}
221221

222222
it("returns paginated results for PND") {
223-
whenever(getAlertsForPersonService.execute(hmppsId)).thenReturn(
223+
whenever(getAlertsForPersonService.getAlertsForPnd(hmppsId)).thenReturn(
224224
Response(
225225
data =
226226
List(20) {
@@ -252,7 +252,7 @@ internal class AlertsControllerTest(
252252
URLEncoder.encode(hmppsIdForPersonWithNoAlerts, StandardCharsets.UTF_8)
253253
val alertPath = "/v1/persons/$encodedHmppsIdForPersonWithNoAlerts/alerts/pnd"
254254

255-
whenever(getAlertsForPersonService.execute(hmppsIdForPersonWithNoAlerts)).thenReturn(
255+
whenever(getAlertsForPersonService.getAlertsForPnd(hmppsIdForPersonWithNoAlerts)).thenReturn(
256256
Response(
257257
data = emptyList(),
258258
),
@@ -275,7 +275,7 @@ internal class AlertsControllerTest(
275275
it("gets the alerts for PND for a person with the matching ID") {
276276
mockMvc.performAuthorised(pndPath)
277277

278-
verify(getAlertsForPersonService, VerificationModeFactory.times(1)).execute(hmppsId)
278+
verify(getAlertsForPersonService, VerificationModeFactory.times(1)).getAlertsForPnd(hmppsId)
279279
}
280280
}
281281
},

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetAlertsForPersonServiceTest.kt

+30-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
22

33
import io.kotest.core.spec.style.DescribeSpec
44
import io.kotest.matchers.collections.shouldHaveSize
5+
import io.kotest.matchers.shouldBe
56
import org.mockito.Mockito
67
import org.mockito.internal.verification.VerificationModeFactory
78
import org.mockito.kotlin.verify
@@ -30,7 +31,8 @@ internal class GetAlertsForPersonServiceTest(
3031
val hmppsId = "1234/56789B"
3132
val prisonerNumber = "Z99999ZZ"
3233
val deliusCrn = "X777776"
33-
val alert = Alert()
34+
val alert = Alert(code = "XA", codeDescription = "Test Alert XA")
35+
val nonMatchingAlert = Alert(code = "INVALID", codeDescription = "Invalid Alert")
3436

3537
val person =
3638
Person(firstName = "Qui-gon", lastName = "Jin", identifiers = Identifiers(nomisNumber = prisonerNumber, deliusCrn = deliusCrn))
@@ -47,6 +49,7 @@ internal class GetAlertsForPersonServiceTest(
4749
data =
4850
listOf(
4951
alert,
52+
nonMatchingAlert,
5053
),
5154
),
5255
)
@@ -108,5 +111,31 @@ internal class GetAlertsForPersonServiceTest(
108111
val response = getAlertsForPersonService.execute(hmppsId)
109112
response.errors.shouldHaveSize(1)
110113
}
114+
115+
it("records errors when it cannot find PND alerts for a person") {
116+
whenever(nomisGateway.getAlertsForPerson(id = prisonerNumber)).thenReturn(
117+
Response(
118+
data = emptyList(),
119+
errors =
120+
listOf(
121+
UpstreamApiError(
122+
causedBy = UpstreamApi.NOMIS,
123+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
124+
),
125+
),
126+
),
127+
)
128+
129+
val response = getAlertsForPersonService.getAlertsForPnd(hmppsId)
130+
response.errors.shouldHaveSize(1)
131+
}
132+
133+
it("returns PND filtered data") {
134+
val response = getAlertsForPersonService.getAlertsForPnd(hmppsId)
135+
136+
response.data.shouldHaveSize(1)
137+
response.data[0].code shouldBe "XA"
138+
response.data[0].codeDescription shouldBe "Test Alert XA"
139+
}
111140
},
112141
)

0 commit comments

Comments
 (0)