Skip to content

Commit

Permalink
PI-1602 additional fix for duplicate addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Oct 31, 2023
1 parent 7501d15 commit ce80418
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ internal class CASIntegrationTest {
MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EAAR"))

val person = personRepository.findByCrnAndSoftDeletedIsFalse(event.message.crn())
val address = addressRepository.findMainAddressForUpdate(person!!.id)
val address = addressRepository.findMainAddress(person!!.id)

MatcherAssert.assertThat(address!!.type.code, Matchers.equalTo("A17"))
MatcherAssert.assertThat(address.town, Matchers.equalTo(eventDetails.eventDetails.premises.town))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AddressService(
}

fun endMainAddress(person: Person, endDate: LocalDate) {
val currentMain = personAddressRepository.findMainAddressForUpdate(person.id)
val currentMain = personAddressRepository.findMainAddress(person.id)
currentMain?.apply {
val previousStatus = referenceDataRepository.previousAddressStatus()
currentMain.status = previousStatus
Expand All @@ -35,7 +35,7 @@ class AddressService(
}

fun endMainCAS3Address(person: Person, endDate: ZonedDateTime) {
val currentMain = personAddressRepository.findMainAddressForUpdate(person.id)
val currentMain = personAddressRepository.findMainAddress(person.id)
currentMain?.apply {
if (currentMain.type.code == AddressTypeCode.CAS3.code) {
val previousStatus = referenceDataRepository.previousAddressStatus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactTypeReposi
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.getByCrn
import uk.gov.justice.digital.hmpps.integrations.delius.entity.getByCrnForUpdate
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import java.time.ZonedDateTime

Expand All @@ -33,7 +33,7 @@ class ContactService(
getEvent: () -> EventDetails<T>
) = audit(BusinessInteractionCode.UPDATE_CONTACT) {
val event = getEvent()
val personId = person?.id ?: personRepository.getByCrn(crn).id
val personId = person?.id ?: personRepository.getByCrnForUpdate(crn).id
val existing = contactRepository.getByExternalReference(event.eventDetails.urn)
if (existing != null) {
if (existing.startTime < event.timestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package uk.gov.justice.digital.hmpps.integrations.delius.entity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.LockModeType
import jakarta.persistence.Table
import org.hibernate.annotations.Immutable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Lock
import uk.gov.justice.digital.hmpps.exception.NotFoundException

@Immutable
Expand All @@ -29,12 +31,9 @@ class Person(

interface PersonRepository : JpaRepository<Person, Long> {

@Lock(LockModeType.PESSIMISTIC_READ)
fun findByCrnAndSoftDeletedIsFalse(crn: String): Person?
fun findByNomsNumberAndSoftDeletedIsFalse(nomsNumber: String): Person?
}

fun PersonRepository.getByCrn(crn: String) =
fun PersonRepository.getByCrnForUpdate(crn: String) =
findByCrnAndSoftDeletedIsFalse(crn) ?: throw NotFoundException("Person", "crn", crn)

fun PersonRepository.getByNoms(noms: String) =
findByNomsNumberAndSoftDeletedIsFalse(noms) ?: throw NotFoundException("Person", "noms", noms)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.LockModeType
import jakarta.persistence.ManyToOne
import jakarta.persistence.SequenceGenerator
import jakarta.persistence.Table
Expand All @@ -19,7 +18,6 @@ import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Lock
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate
import java.time.ZonedDateTime
Expand Down Expand Up @@ -91,6 +89,5 @@ interface PersonAddressRepository : JpaRepository<PersonAddress, Long> {
and pa.status.code = 'M'
"""
)
@Lock(LockModeType.PESSIMISTIC_READ)
fun findMainAddressForUpdate(personId: Long): PersonAddress?
fun findMainAddress(personId: Long): PersonAddress?
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.Cas3ApiClient
import uk.gov.justice.digital.hmpps.integrations.delius.AddressService
import uk.gov.justice.digital.hmpps.integrations.delius.ContactService
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.getByCrn
import uk.gov.justice.digital.hmpps.integrations.delius.entity.getByCrnForUpdate
import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
Expand Down Expand Up @@ -55,7 +55,7 @@ class Handler(
}

"accommodation.cas3.person.arrived" -> {
val person = personRepository.getByCrn(event.crn())
val person = personRepository.getByCrnForUpdate(event.crn())
val detail = cas3ApiClient.getPersonArrived(event.url())
contactService.createContact(event.crn(), person) {
detail
Expand All @@ -65,7 +65,7 @@ class Handler(
}

"accommodation.cas3.person.departed" -> {
val person = personRepository.getByCrn(event.crn())
val person = personRepository.getByCrnForUpdate(event.crn())
val detail = cas3ApiClient.getPersonDeparted(event.url())
contactService.createContact(event.crn(), person) {
detail
Expand Down

0 comments on commit ce80418

Please sign in to comment.