Skip to content

Commit 9391c98

Browse files
Merge branch 'main' into offences-courtname
2 parents 27f1726 + 6bc0cb1 commit 9391c98

File tree

12 files changed

+153
-142
lines changed

12 files changed

+153
-142
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ __pycache__/
8686
*.key
8787
*.csr
8888
*.pem
89+
90+
# Localstack
91+
localstack/cache

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ProbationOffenderSearchGateway.kt

+21-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person
1111
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
1212
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
1313
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
14+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.probationoffendersearch.ContactDetailsWithAddress
1415
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.probationoffendersearch.Offender
1516

1617
@Component
@@ -116,8 +117,26 @@ class ProbationOffenderSearchGateway(
116117
}
117118

118119
fun getAddressesForPerson(hmppsId: String): Response<List<Address>> {
119-
val offender = getOffender(hmppsId)
120-
return Response(data = offender.data?.contactDetails?.addresses.orEmpty().map { it.toAddress() }, errors = offender.errors)
120+
val result =
121+
webClient.requestList<ContactDetailsWithAddress>(
122+
HttpMethod.POST,
123+
"/search",
124+
authenticationHeader(),
125+
UpstreamApi.PROBATION_OFFENDER_SEARCH,
126+
mapOf("crn" to hmppsId),
127+
)
128+
129+
return when (result) {
130+
is WebClientWrapperResponse.Success -> {
131+
return Response(result.data.firstOrNull()?.contactDetails?.addresses.orEmpty().map { it.toAddress() })
132+
}
133+
is WebClientWrapperResponse.Error -> {
134+
Response(
135+
data = emptyList(),
136+
errors = result.errors,
137+
)
138+
}
139+
}
121140
}
122141

123142
private fun authenticationHeader(): Map<String, String> {
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
22

3-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.probationoffendersearch.Address
4-
53
data class ContactDetailsWithEmailAndPhone(
6-
val addresses: List<Address>?,
74
val phoneNumbers: List<PhoneNumber>?,
85
val emails: List<String>?,
96
)

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/probationoffendersearch/Address.kt

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.probationoffendersearch
22

3-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Address
43
import java.time.LocalDate
4+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Address as HmppsAddress
5+
6+
data class ContactDetailsWithAddress(
7+
val contactDetails: Addresses? = null,
8+
)
9+
10+
data class Addresses(
11+
val addresses: List<Address>? = listOf(),
12+
)
513

614
data class Address(
715
val addressNumber: String?,
@@ -18,7 +26,7 @@ data class Address(
1826
val notes: String?,
1927
) {
2028
fun toAddress() =
21-
Address(
29+
HmppsAddress(
2230
country = null,
2331
county = this.county,
2432
endDate = this.to,
@@ -39,7 +47,7 @@ data class Address(
3947
val description: String?,
4048
) {
4149
fun toAddressType() =
42-
Address.Type(
50+
HmppsAddress.Type(
4351
code = this.code,
4452
description = this.description ?: this.code,
4553
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.probationoffendersearch
22

3-
open class ContactDetails(
4-
open val addresses: List<Address>? = listOf(),
5-
)
3+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PhoneNumber
4+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.ContactDetailsWithEmailAndPhone as PersonContactDetails
5+
6+
class ContactDetails(
7+
val phoneNumbers: List<PhoneNumber>?,
8+
val emailAddresses: List<String>?,
9+
) {
10+
fun toContactDetails(): PersonContactDetails =
11+
PersonContactDetails(
12+
phoneNumbers = this.phoneNumbers,
13+
emails = this.emailAddresses,
14+
)
15+
}

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/probationoffendersearch/ContactDetailsWithEmailAndPhone.kt

-17
This file was deleted.

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/probationoffendersearch/Offender.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ data class Offender(
1313
val gender: String? = null,
1414
val offenderProfile: OffenderProfile = OffenderProfile(),
1515
val offenderAliases: List<OffenderAlias> = listOf(),
16-
val contactDetails: ContactDetailsWithEmailAndPhone? = null,
16+
val contactDetails: ContactDetails? = null,
1717
val otherIds: OtherIds = OtherIds(),
1818
val age: Number = 0,
1919
) {
@@ -34,7 +34,7 @@ data class Offender(
3434
),
3535
pncId = otherIds.pncNumber,
3636
hmppsId = otherIds.crn,
37-
contactDetails = this.contactDetails?.toContactdetails(),
37+
contactDetails = this.contactDetails?.toContactDetails(),
3838
)
3939

4040
fun toPersonProtectedCharacteristics(): PersonProtectedCharacteristics =

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

+9-18
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,15 @@ class GetPersonService(
2626
prisonerOffenderSearchGateway.getPrisonOffender(nomsNumber = it)
2727
}
2828

29-
return if (prisonResponse != null) {
30-
Response(
31-
data =
32-
mapOf(
33-
"prisonerOffenderSearch" to prisonResponse.data?.toPerson(),
34-
"probationOffenderSearch" to probationResponse.data,
35-
),
36-
errors = prisonResponse.errors + probationResponse.errors,
29+
val data =
30+
mapOf(
31+
"prisonerOffenderSearch" to prisonResponse?.data?.toPerson(),
32+
"probationOffenderSearch" to probationResponse.data,
3733
)
38-
} else {
39-
Response(
40-
data =
41-
mapOf(
42-
"prisonerOffenderSearch" to null,
43-
"probationOffenderSearch" to probationResponse.data,
44-
),
45-
errors = probationResponse.errors,
46-
)
47-
}
34+
35+
return Response(
36+
data = data,
37+
errors = (prisonResponse?.errors ?: emptyList()) + probationResponse.errors,
38+
)
4839
}
4940
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ internal class PersonControllerTest(
7676
lastName = "Allen",
7777
middleName = "Jonas",
7878
dateOfBirth = LocalDate.parse("2023-03-01"),
79-
contactDetails = ContactDetailsWithEmailAndPhone(emptyList(), phoneNumbers, emails),
79+
contactDetails = ContactDetailsWithEmailAndPhone(phoneNumbers, emails),
8080
),
8181
Person(
8282
firstName = "Barry",
8383
lastName = "Allen",
8484
middleName = "Rock",
8585
dateOfBirth = LocalDate.parse("2022-07-22"),
86-
contactDetails = ContactDetailsWithEmailAndPhone(emptyList(), phoneNumbers, emails),
86+
contactDetails = ContactDetailsWithEmailAndPhone(phoneNumbers, emails),
8787
),
8888
),
8989
),

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/probationoffendersearch/GetAddressesForPersonTest.kt

+8-10
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ class GetAddressesForPersonTest(
2929
) : DescribeSpec(
3030
{
3131
val probationOffenderSearchApiMockServer = ProbationOffenderSearchApiMockServer()
32-
val hmppsId = "2002/1121M"
32+
val hmppsId = "X777776"
3333

3434
beforeEach {
3535
probationOffenderSearchApiMockServer.start()
3636
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
37-
"{\"pncNumber\": \"$hmppsId\"}",
37+
"{\"crn\": \"$hmppsId\"}",
3838
"""
3939
[
4040
{
41-
"firstName": "English",
42-
"surname": "Breakfast",
4341
"contactDetails": {
4442
"addresses": [
4543
{
@@ -102,7 +100,7 @@ class GetAddressesForPersonTest(
102100

103101
it("returns an empty list when no addresses are found") {
104102
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
105-
"{\"pncNumber\": \"$hmppsId\"}",
103+
"{\"crn\": \"$hmppsId\"}",
106104
"""
107105
[
108106
{
@@ -123,7 +121,7 @@ class GetAddressesForPersonTest(
123121

124122
it("returns an error when no results are returned") {
125123
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
126-
"{\"pncNumber\": \"$hmppsId\"}",
124+
"{\"crn\": \"$hmppsId\"}",
127125
"[]",
128126
)
129127

@@ -134,7 +132,7 @@ class GetAddressesForPersonTest(
134132

135133
it("returns an empty list when there is no contactDetails field") {
136134
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
137-
"{\"pncNumber\": \"$hmppsId\"}",
135+
"{\"crn\": \"$hmppsId\"}",
138136
"""
139137
[
140138
{
@@ -152,7 +150,7 @@ class GetAddressesForPersonTest(
152150

153151
it("returns an empty list when contactDetails field is null") {
154152
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
155-
"{\"pncNumber\": \"$hmppsId\"}",
153+
"{\"crn\": \"$hmppsId\"}",
156154
"""
157155
[
158156
{
@@ -171,7 +169,7 @@ class GetAddressesForPersonTest(
171169

172170
it("returns an empty list when contactDetails.addresses field is null") {
173171
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
174-
"{\"pncNumber\": \"$hmppsId\"}",
172+
"{\"crn\": \"$hmppsId\"}",
175173
"""
176174
[
177175
{
@@ -192,7 +190,7 @@ class GetAddressesForPersonTest(
192190

193191
it("returns an empty list when the type is an empty object") {
194192
probationOffenderSearchApiMockServer.stubPostOffenderSearch(
195-
"{\"pncNumber\": \"$hmppsId\"}",
193+
"{\"crn\": \"$hmppsId\"}",
196194
"""
197195
[
198196
{

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

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.ProbationOffend
1414
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers
1515
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person
1616
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
1719
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisoneroffendersearch.POSPrisoner
1820

1921
@ContextConfiguration(
@@ -88,4 +90,21 @@ internal class GetPersonServiceTest(
8890
result.data.shouldBe(expectedResult)
8991
result.errors shouldBe emptyList()
9092
}
93+
94+
it("returns errors when unable to retrieve prison data and data when probation data is available") {
95+
val personFromProbationOffenderSearch = Person("Paula", "First", identifiers = Identifiers(nomisNumber = "A1234AA"))
96+
97+
whenever(probationOffenderSearchGateway.getPerson(hmppsId)).thenReturn(Response(data = personFromProbationOffenderSearch))
98+
whenever(prisonerOffenderSearchGateway.getPrisonOffender("A1234AA")).thenReturn(Response(data = null, errors = listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "MockError"))))
99+
100+
val result = getPersonService.getCombinedDataForPerson(hmppsId)
101+
result.data shouldBe
102+
mapOf(
103+
"prisonerOffenderSearch" to null,
104+
"probationOffenderSearch" to personFromProbationOffenderSearch,
105+
)
106+
result.errors.first().causedBy.shouldBe(UpstreamApi.PRISONER_OFFENDER_SEARCH)
107+
result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
108+
result.errors.first().description.shouldBe("MockError")
109+
}
91110
})

0 commit comments

Comments
 (0)