Skip to content

Commit

Permalink
PI-2257 make recall decisions and delius not handling merged crn cases (
Browse files Browse the repository at this point in the history
#3906)

* PI-2257 get latest crn when merge from crn is used to identify person.

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2257 update test

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2257 update test

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2257 apply review comments

Signed-off-by: Amardeep Chimber <[email protected]>

---------

Signed-off-by: Amardeep Chimber <[email protected]>
  • Loading branch information
achimber-moj authored Jun 17, 2024
1 parent 47407ee commit db4af8a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class DataLoader(
persist(PersonGenerator.DECISION_NOT_TO_RECALL)
persist(PersonGenerator.RECOMMENDATION_DELETED)
persist(PersonGenerator.RECOMMENDATION_DELETED_INACTIVE_STAFF)
persist(PersonGenerator.RECOMMENDATION_MERGED_FROM)
persist(PersonGenerator.X000077_ADDITIONAL_IDENTIFIER.mergeDetail)
persist(PersonGenerator.X000077_ADDITIONAL_IDENTIFIER)

persist(PersonGenerator.DEFAULT_PROVIDER)
persist(PersonGenerator.DEFAULT_LAU)
Expand All @@ -44,6 +47,7 @@ class DataLoader(
persist(PersonGenerator.DECISION_NOT_TO_RECALL.manager!!)
persist(PersonGenerator.RECOMMENDATION_DELETED.manager!!)
persist(PersonGenerator.RECOMMENDATION_DELETED_INACTIVE_STAFF.manager!!)
persist(PersonGenerator.RECOMMENDATION_MERGED_FROM.manager!!)

persist(PersonGenerator.CASE_SUMMARY.gender)
persist(PersonGenerator.CASE_SUMMARY.ethnicity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.casesummary.District
import uk.gov.justice.digital.hmpps.integrations.delius.casesummary.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.casesummary.Team
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider
import uk.gov.justice.digital.hmpps.integrations.delius.recommendation.person.entity.AdditionalIdentifier
import uk.gov.justice.digital.hmpps.integrations.delius.recommendation.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.recommendation.person.entity.PersonManager
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData
Expand All @@ -28,15 +29,25 @@ object PersonGenerator {
val NO_ACCESS_LIMITATIONS = generateUserAccess("X000007")
val RECOMMENDATION_DELETED = generate("X000008")
val RECOMMENDATION_DELETED_INACTIVE_STAFF = generate("X000009")
val RECOMMENDATION_MERGED_FROM = generate("X000077", softDeleted = true)
val X000077_ADDITIONAL_IDENTIFIER =
AdditionalIdentifier(
IdGenerator.getAndIncrement(),
RECOMMENDATION_MERGED_FROM.crn,
false,
RECOMMENDATION_DELETED,
ReferenceData(IdGenerator.getAndIncrement(), "MFCRN", "Merged From CRN")
)

fun generate(
crn: String,
providerId: Long = DEFAULT_PROVIDER.id,
teamId: Long = DEFAULT_TEAM.id,
staffId: Long = DEFAULT_STAFF.id,
id: Long = IdGenerator.getAndIncrement()
id: Long = IdGenerator.getAndIncrement(),
softDeleted: Boolean = false,
): Person {
val person = Person(id, crn)
val person = Person(id, crn, softDeleted = softDeleted)
val personManager = PersonManager(IdGenerator.getAndIncrement(), person, providerId, teamId, staffId)
person.set("manager", personManager)
return person
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"identifiers": [
{
"type": "CRN",
"value": "X000008"
"value": "X000077"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,30 @@ import uk.gov.justice.digital.hmpps.integrations.delius.recommendation.person.en

interface PersonRepository : JpaRepository<Person, Long> {

@Query("SELECT p FROM Person p LEFT JOIN FETCH p.manager WHERE p.crn = :crn AND p.softDeleted = false AND p.manager.active = true")
@Query(
"""
SELECT p FROM Person p
LEFT JOIN p.manager
WHERE p.crn = :crn
AND p.softDeleted = false
AND p.manager.active = true
"""
)
fun findByCrn(crn: String): Person?

@Query(
"""
SELECT p FROM Person p
LEFT JOIN p.manager
JOIN p.additionalIdentifier ai
WHERE ai.mergedFromCrn = :crn
AND p.softDeleted = false
AND p.manager.active = true
AND ai.mergeDetail.code = 'MFCRN'
"""
)
fun findByMergedFromCrn(crn: String): Person?
}

fun PersonRepository.getPerson(crn: String): Person =
findByCrn(crn) ?: throw NotFoundException("Person", "crn", crn)
findByCrn(crn) ?: findByMergedFromCrn(crn) ?: throw NotFoundException("Person", "crn", crn)
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package uk.gov.justice.digital.hmpps.integrations.delius.recommendation.person.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData

@Immutable
@Entity
Expand All @@ -25,7 +21,11 @@ class Person(
val softDeleted: Boolean = false,

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

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

)

@Immutable
Expand Down Expand Up @@ -53,3 +53,28 @@ class PersonManager(
@Column(name = "active_flag", columnDefinition = "number")
val active: Boolean = true
)

@Immutable
@Entity
@SQLRestriction("soft_deleted = 0")
@Table(name = "additional_identifier")
class AdditionalIdentifier(

@Id
@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,

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

0 comments on commit db4af8a

Please sign in to comment.