Skip to content

Commit 6e5ce1d

Browse files
PRC-507: Fixes a bug where all addresses included the phone numbers for the other addresses in this organisation
1 parent 5741e21 commit 6e5ce1d

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/organisationsapi/mapping/OrganisationAddressDetailsMappers.kt

+19-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import uk.gov.justice.digital.hmpps.organisationsapi.model.response.Organisation
77
import uk.gov.justice.digital.hmpps.organisationsapi.model.response.OrganisationAddressPhoneDetails
88

99
fun OrganisationAddressDetailsEntity.toModel(
10-
phoneNumbers: List<Pair<OrganisationAddressPhoneEntity, OrganisationPhoneDetailsEntity>>,
10+
phoneNumbers: List<Pair<OrganisationAddressPhoneEntity, OrganisationPhoneDetailsEntity?>>,
1111
): OrganisationAddressDetails = OrganisationAddressDetails(
1212
organisationAddressId = organisationAddressId,
1313
organisationId = organisationId,
@@ -35,21 +35,24 @@ fun OrganisationAddressDetailsEntity.toModel(
3535
specialNeedsCodeDescription = specialNeedsCodeDescription,
3636
contactPersonName = contactPersonName,
3737
businessHours = businessHours,
38-
phoneNumbers = phoneNumbers.map { (addressPhoneEntity, phoneEntity) ->
39-
OrganisationAddressPhoneDetails(
40-
organisationAddressPhoneId = addressPhoneEntity.organisationAddressPhoneId,
41-
organisationPhoneId = phoneEntity.organisationPhoneId,
42-
organisationAddressId = organisationAddressId,
43-
organisationId = phoneEntity.organisationId,
44-
phoneType = phoneEntity.phoneType,
45-
phoneTypeDescription = phoneEntity.phoneTypeDescription,
46-
phoneNumber = phoneEntity.phoneNumber,
47-
extNumber = phoneEntity.extNumber,
48-
createdBy = phoneEntity.createdBy,
49-
createdTime = phoneEntity.createdTime,
50-
updatedBy = phoneEntity.updatedBy,
51-
updatedTime = phoneEntity.updatedTime,
52-
)
38+
phoneNumbers = phoneNumbers.mapNotNull { (addressPhoneEntity, phoneEntity) ->
39+
when {
40+
phoneEntity != null -> OrganisationAddressPhoneDetails(
41+
organisationAddressPhoneId = addressPhoneEntity.organisationAddressPhoneId,
42+
organisationPhoneId = phoneEntity.organisationPhoneId,
43+
organisationAddressId = organisationAddressId,
44+
organisationId = phoneEntity.organisationId,
45+
phoneType = phoneEntity.phoneType,
46+
phoneTypeDescription = phoneEntity.phoneTypeDescription,
47+
phoneNumber = phoneEntity.phoneNumber,
48+
extNumber = phoneEntity.extNumber,
49+
createdBy = phoneEntity.createdBy,
50+
createdTime = phoneEntity.createdTime,
51+
updatedBy = phoneEntity.updatedBy,
52+
updatedTime = phoneEntity.updatedTime,
53+
)
54+
else -> null
55+
}
5356
},
5457
createdBy = createdBy,
5558
createdTime = createdTime,

src/main/kotlin/uk/gov/justice/digital/hmpps/organisationsapi/service/OrganisationService.kt

+12-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ class OrganisationService(
4040
fun getOrganisationById(id: Long): OrganisationDetails {
4141
val organisationEntity = organisationRepository.findById(id)
4242
.orElseThrow { EntityNotFoundException("Organisation with id $id not found") }
43+
44+
// Split the address phones from the global phones
4345
val phoneNumbers = organisationPhoneDetailsRepository.findByOrganisationId(id)
4446
val organisationAddressPhones = organisationAddressPhoneRepository.findByOrganisationId(id)
4547
val organisationAddressPhoneIds = organisationAddressPhones.map { it.organisationPhoneId }
4648
val (organisationAddressPhoneNumbers, organisationPhoneNumbers) = phoneNumbers.partition { it.organisationPhoneId in organisationAddressPhoneIds }
49+
4750
return OrganisationDetails(
4851
organisationId = organisationEntity.organisationId!!,
4952
organisationName = organisationEntity.organisationName,
@@ -76,7 +79,15 @@ class OrganisationService(
7679
organisationAddressPhoneNumbers: List<OrganisationPhoneDetailsEntity>,
7780
organisationAddressPhones: List<OrganisationAddressPhoneEntity>,
7881
): List<OrganisationAddressDetails> = organisationAddressRepository.findByOrganisationId(id)
79-
.map { address -> address.toModel(organisationAddressPhones.map { addressPhoneEntity -> addressPhoneEntity to organisationAddressPhoneNumbers.find { it.organisationPhoneId == addressPhoneEntity.organisationPhoneId }!! }) }
82+
.map { address ->
83+
address.toModel(
84+
organisationAddressPhones.map { addressPhoneEntity ->
85+
addressPhoneEntity to organisationAddressPhoneNumbers.find {
86+
(it.organisationPhoneId == addressPhoneEntity.organisationPhoneId && addressPhoneEntity.organisationAddressId == address.organisationAddressId)
87+
}
88+
},
89+
)
90+
}
8091

8192
fun search(request: OrganisationSearchRequest, pageable: Pageable): Page<OrganisationSummary> = organisationSearchRepository.search(request, pageable).toModel()
8293

src/test/kotlin/uk/gov/justice/digital/hmpps/organisationsapi/integration/resource/GetOrganisationByOrganisationIdIntegrationTest.kt

+21-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ class GetOrganisationByOrganisationIdIntegrationTest : SecureApiIntegrationTestB
101101
).setCreatedAndModified(),
102102
),
103103
).setCreatedAndModified(),
104+
MigrateOrganisationAddress(
105+
nomisAddressId = RandomUtils.secure().randomLong(),
106+
type = "BUS",
107+
primaryAddress = false,
108+
mailAddress = false,
109+
serviceAddress = false,
110+
noFixedAddress = false,
111+
locality = "locality",
112+
postCode = "D2 2DN",
113+
startDate = LocalDate.of(2020, 3, 3),
114+
endDate = LocalDate.of(2021, 4, 4),
115+
phoneNumbers = emptyList(),
116+
).setCreatedAndModified(),
104117
),
105118
).setCreatedAndModified()
106119

@@ -158,7 +171,7 @@ class GetOrganisationByOrganisationIdIntegrationTest : SecureApiIntegrationTestB
158171
assertThat(updatedBy).isEqualTo("MODIFIED")
159172
assertThat(updatedTime).isEqualTo(LocalDateTime.of(2020, 3, 4, 11, 45))
160173
}
161-
assertThat(addresses).hasSize(1)
174+
assertThat(addresses).hasSize(2)
162175
with(addresses[0]) {
163176
assertThat(addressType).isEqualTo("BUS")
164177
assertThat(addressTypeDescription).isEqualTo("Business address")
@@ -200,6 +213,13 @@ class GetOrganisationByOrganisationIdIntegrationTest : SecureApiIntegrationTestB
200213
assertThat(updatedTime).isEqualTo(LocalDateTime.of(2020, 3, 4, 11, 45))
201214
}
202215
}
216+
with(addresses[1]) {
217+
// Making sure phone numbers are propagated across ALL addresses for this organisation
218+
assertThat(phoneNumbers).isEmpty()
219+
assertThat(postcode).isEqualTo("D2 2DN")
220+
assertThat(startDate).isEqualTo(LocalDate.of(2020, 3, 3))
221+
assertThat(endDate).isEqualTo(LocalDate.of(2021, 4, 4))
222+
}
203223
}
204224
}
205225

0 commit comments

Comments
 (0)