Skip to content

Commit f464552

Browse files
authored
Add update validation same as creation endpoints (#367)
1 parent f5e0b23 commit f464552

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/personalrelationships/service/ContactIdentityService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ class ContactIdentityService(
8989
fun update(contactId: Long, contactIdentityId: Long, request: UpdateIdentityRequest, user: User): ContactIdentityDetails {
9090
validateContactExists(contactId)
9191
val existing = validateExistingIdentity(contactIdentityId)
92+
93+
val duplicateExisting = contactIdentityRepository.findByContactId(contactId)
94+
.filter { it.contactIdentityId != contactIdentityId }
95+
.any { it.identityType == request.identityType && it.identityValue == request.identityValue }
96+
97+
if (duplicateExisting) {
98+
throw DuplicateIdentityDocumentException(request.identityType, request.identityValue)
99+
}
100+
92101
val type = referenceCodeService.validateReferenceCode(ReferenceCodeGroup.ID_TYPE, request.identityType, allowInactive = true)
93102
validatePNC(request.identityType, request.identityValue)
94103

src/test/kotlin/uk/gov/justice/digital/hmpps/personalrelationships/integration/resource/UpdateContactIdentityIntegrationTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,35 @@ class UpdateContactIdentityIntegrationTest : SecureAPIIntegrationTestBase() {
189189
stubEvents.assertHasNoEvents(OutboundEvent.CONTACT_IDENTITY_UPDATED)
190190
}
191191

192+
@Test
193+
fun `should not update the identity if another document exists with same type and value`() {
194+
val documentToUpdate = testAPIClient.createAContactIdentity(
195+
savedContactId,
196+
CreateIdentityRequest(
197+
identityType = "PASS",
198+
identityValue = "9887676",
199+
issuingAuthority = null,
200+
),
201+
)
202+
val request = aMinimalRequest()
203+
204+
val errors = webTestClient.put()
205+
.uri("/contact/$savedContactId/identity/${documentToUpdate.contactIdentityId}")
206+
.accept(MediaType.APPLICATION_JSON)
207+
.contentType(MediaType.APPLICATION_JSON)
208+
.headers(setAuthorisationUsingCurrentUser())
209+
.bodyValue(request)
210+
.exchange()
211+
.expectStatus()
212+
.is4xxClientError
213+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
214+
.expectBody(ErrorResponse::class.java)
215+
.returnResult().responseBody!!
216+
217+
assertThat(errors.userMessage).isEqualTo("Contact already has an identity document matching type \"DL\" and value \"DL123456789\"")
218+
stubEvents.assertHasNoEvents(OutboundEvent.CONTACT_IDENTITY_UPDATED)
219+
}
220+
192221
@Test
193222
fun `should update the identity with minimal fields`() {
194223
val request = UpdateIdentityRequest(

0 commit comments

Comments
 (0)