Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class ContactIdentityService(
fun update(contactId: Long, contactIdentityId: Long, request: UpdateIdentityRequest, user: User): ContactIdentityDetails {
validateContactExists(contactId)
val existing = validateExistingIdentity(contactIdentityId)

val duplicateExisting = contactIdentityRepository.findByContactId(contactId)
.filter { it.contactIdentityId != contactIdentityId }
.any { it.identityType == request.identityType && it.identityValue == request.identityValue }

if (duplicateExisting) {
throw DuplicateIdentityDocumentException(request.identityType, request.identityValue)
}

val type = referenceCodeService.validateReferenceCode(ReferenceCodeGroup.ID_TYPE, request.identityType, allowInactive = true)
validatePNC(request.identityType, request.identityValue)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,35 @@ class UpdateContactIdentityIntegrationTest : SecureAPIIntegrationTestBase() {
stubEvents.assertHasNoEvents(OutboundEvent.CONTACT_IDENTITY_UPDATED)
}

@Test
fun `should not update the identity if another document exists with same type and value`() {
val documentToUpdate = testAPIClient.createAContactIdentity(
savedContactId,
CreateIdentityRequest(
identityType = "PASS",
identityValue = "9887676",
issuingAuthority = null,
),
)
val request = aMinimalRequest()

val errors = webTestClient.put()
.uri("/contact/$savedContactId/identity/${documentToUpdate.contactIdentityId}")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(setAuthorisationUsingCurrentUser())
.bodyValue(request)
.exchange()
.expectStatus()
.is4xxClientError
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(ErrorResponse::class.java)
.returnResult().responseBody!!

assertThat(errors.userMessage).isEqualTo("Contact already has an identity document matching type \"DL\" and value \"DL123456789\"")
stubEvents.assertHasNoEvents(OutboundEvent.CONTACT_IDENTITY_UPDATED)
}

@Test
fun `should update the identity with minimal fields`() {
val request = UpdateIdentityRequest(
Expand Down
Loading