Skip to content

Commit

Permalink
PI-2541 - apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Sep 20, 2024
1 parent c6c9578 commit 85d952c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.probation.entity.PersonManager
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceData

@Immutable
Expand All @@ -20,30 +19,12 @@ class Person(
@Column(columnDefinition = "char(7)")
val nomsNumber: String,

@OneToMany(mappedBy = "person")
val additionalIdentifier: List<AdditionalIdentifier> = emptyList(),

@OneToOne(mappedBy = "person")
val manager: PersonManager? = null,

@Column(updatable = false, columnDefinition = "number")
val softDeleted: Boolean = false
)

interface PersonRepository : JpaRepository<Person, Long> {
fun findByNomsNumberAndSoftDeletedIsFalse(nomsNumber: String): List<Person>

@Query(
"""
SELECT count(p) FROM Person p
LEFT JOIN p.manager
JOIN p.additionalIdentifier ai
WHERE ai.person.id = :id
AND p.softDeleted = false
AND ai.mergeDetail.code = 'MFCRN'
"""
)
fun findByMergedFromCrn(id: Long): Int
}

@Immutable
Expand All @@ -55,18 +36,27 @@ class AdditionalIdentifier(
@Column(name = "additional_identifier_id")
val id: Long,

@Column(name = "identifier")
val mergedFromCrn: String,

@Column(updatable = false, columnDefinition = "NUMBER")
val softDeleted: Boolean = false,

@ManyToOne
@JoinColumn(name = "offender_id")
val person: Person,
@Column(name = "offender_id")
val personId: Long,

@ManyToOne
@JoinColumn(name = "identifier_name_id")
val mergeDetail: ReferenceData
val name: ReferenceData,

@Column(updatable = false, columnDefinition = "number")
val softDeleted: Boolean = false,
)

interface AdditionalIdentifierRepository: JpaRepository<AdditionalIdentifier, Long> {

@Query(
"""
select count(ai) > 0
from AdditionalIdentifier ai
where ai.personId = :personId
and ai.name.code = 'MFCRN'
"""
)
fun personHasBeenMerged(personId: Long): Boolean
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison

import org.springframework.dao.IncorrectResultSizeDataAccessException
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.Disposal
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.AdditionalIdentifierRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManager
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.probationarea.entity.ProbationArea
Expand All @@ -29,7 +28,7 @@ class PrisonManagerService(
private val prisonManagerRepository: PrisonManagerRepository,
private val contactRepository: ContactRepository,
private val contactTypeRepository: ContactTypeRepository,
private val personRepository: PersonRepository,
private val additionalIdentifierRepository: AdditionalIdentifierRepository,
) {
fun allocateToProbationArea(
disposal: Disposal,
Expand Down Expand Up @@ -62,13 +61,13 @@ class PrisonManagerService(
try {
activePrisonManager = prisonManagerRepository.findActiveManagerAtDate(person.id, allocationDate)
} catch (e: IncorrectResultSizeDataAccessException) {
if (personRepository.findByMergedFromCrn(person.id) == 0) {
if (!additionalIdentifierRepository.personHasBeenMerged(person.id)) {
throw e
}
//where crn has a merged from record
//we can ignore the IncorrectResultSizeDataAccessException and
//process the record no further
throw IgnorableMessageException("MergedPrisonManagerHistory")
return
}

// end-date the previous prison manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import org.mockito.kotlin.*
import org.mockito.quality.Strictness
import org.springframework.dao.IncorrectResultSizeDataAccessException
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactTypeRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.AdditionalIdentifierRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManager
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceDataRepository
Expand Down Expand Up @@ -51,7 +50,7 @@ internal class PrisonManagerServiceTest {
lateinit var contactTypeRepository: ContactTypeRepository

@Mock
lateinit var personRepository: PersonRepository
lateinit var additionalIdentifierRepository: AdditionalIdentifierRepository

@InjectMocks
lateinit var prisonManagerService: PrisonManagerService
Expand Down Expand Up @@ -250,17 +249,9 @@ internal class PrisonManagerServiceTest {
whenever(prisonManagerRepository.findActiveManagerAtDate(PersonGenerator.MATCHABLE_WITH_POM.id, allocationDate))
.thenThrow(IncorrectResultSizeDataAccessException::class.java)

whenever(personRepository.findByMergedFromCrn(PersonGenerator.MATCHABLE_WITH_POM.id)).thenReturn(1)
whenever(additionalIdentifierRepository.personHasBeenMerged(PersonGenerator.MATCHABLE_WITH_POM.id)).thenReturn(true)

val exception = assertThrows<IgnorableMessageException> {
prisonManagerService.allocateToProbationArea(
event.disposal!!,
ProbationAreaGenerator.DEFAULT,
allocationDate
)
}

assertEquals("MergedPrisonManagerHistory", exception.message)
prisonManagerService.allocateToProbationArea(event.disposal!!, ProbationAreaGenerator.DEFAULT, allocationDate)

verify(prisonManagerRepository, never()).saveAndFlush(any())
verify(prisonManagerRepository, never()).save(any())
Expand All @@ -276,7 +267,7 @@ internal class PrisonManagerServiceTest {
whenever(prisonManagerRepository.findActiveManagerAtDate(PersonGenerator.MATCHABLE_WITH_POM.id, allocationDate))
.thenThrow(IncorrectResultSizeDataAccessException::class.java)

whenever(personRepository.findByMergedFromCrn(PersonGenerator.MATCHABLE_WITH_POM.id)).thenReturn(0)
whenever(additionalIdentifierRepository.personHasBeenMerged(PersonGenerator.MATCHABLE_WITH_POM.id)).thenReturn(false)

assertThrows<IncorrectResultSizeDataAccessException> {
prisonManagerService.allocateToProbationArea(
Expand Down

0 comments on commit 85d952c

Please sign in to comment.