Skip to content

Commit

Permalink
PI-1602 additional fix for duplicate main addresses (#2475)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister authored Oct 31, 2023
1 parent 518599d commit 2bcb768
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ internal class CASIntegrationTest {

MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EAAR"))

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

MatcherAssert.assertThat(address!!.type.code, Matchers.equalTo("A17"))
Expand All @@ -170,7 +170,7 @@ internal class CASIntegrationTest {
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EADP"))
val person = personRepository.findByCrnAndSoftDeletedIsFalse(event.message.crn())
val person = personRepository.findByCrn(event.message.crn())
val address = addressRepository.findAll().filter { it.personId == person?.id }[0]
MatcherAssert.assertThat(address!!.status.code, Matchers.equalTo("P"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import uk.gov.justice.digital.hmpps.integrations.delius.entity.AddressTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddress
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddressRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceDataRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.cas3AddressType
import uk.gov.justice.digital.hmpps.integrations.delius.entity.getByIdForUpdate
import uk.gov.justice.digital.hmpps.integrations.delius.entity.mainAddressStatus
import uk.gov.justice.digital.hmpps.integrations.delius.entity.previousAddressStatus
import java.time.LocalDate
Expand All @@ -18,15 +20,17 @@ import java.time.ZonedDateTime
@Transactional
class AddressService(
private val personAddressRepository: PersonAddressRepository,
private val referenceDataRepository: ReferenceDataRepository
private val referenceDataRepository: ReferenceDataRepository,
private val personRepository: PersonRepository
) {
fun updateMainAddress(person: Person, details: PersonArrived) {
endMainAddress(person, details.arrivedAt.toLocalDate())
toPersonAddress(person, details).apply(personAddressRepository::save)
}

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

fun endMainCAS3Address(person: Person, endDate: ZonedDateTime) {
val currentMain = personAddressRepository.findMainAddress(person.id)
val personForUpdate = personRepository.getByIdForUpdate(person.id)
val currentMain = personAddressRepository.findMainAddress(personForUpdate.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 @@ -3,14 +3,19 @@ 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.hibernate.annotations.Where
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Lock
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import java.util.Optional

@Immutable
@Entity
@Table(name = "offender")
@Where(clause = "soft_deleted = 0")
class Person(

@Column(columnDefinition = "char(7)")
Expand All @@ -29,12 +34,14 @@ class Person(

interface PersonRepository : JpaRepository<Person, Long> {

fun findByCrnAndSoftDeletedIsFalse(crn: String): Person?
fun findByNomsNumberAndSoftDeletedIsFalse(nomsNumber: String): Person?
fun findByCrn(crn: String): Person?

@Lock(LockModeType.PESSIMISTIC_READ)
override fun findById(personId: Long): Optional<Person>
}

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

fun PersonRepository.getByNoms(noms: String) =
findByNomsNumberAndSoftDeletedIsFalse(noms) ?: throw NotFoundException("Person", "noms", noms)
fun PersonRepository.getByIdForUpdate(personId: Long) =
findById(personId).orElseThrow { NotFoundException("Person", "id", personId) }

0 comments on commit 2bcb768

Please sign in to comment.