Skip to content

Commit

Permalink
PI-2702 Ensure incomplete reviews are deleted on de-registration
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Dec 17, 2024
1 parent 681558a commit 4241543
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.tomakehurst.wiremock.WireMockServer
import jakarta.persistence.EntityManager
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.hamcrest.Matchers.*
Expand Down Expand Up @@ -76,6 +77,9 @@ internal class IntegrationTest {
@Autowired
lateinit var objectMapper: ObjectMapper

@Autowired
lateinit var entityManager: EntityManager

@MockBean
lateinit var telemetryService: TelemetryService

Expand Down Expand Up @@ -357,6 +361,8 @@ internal class IntegrationTest {
val riskToAdultBefore =
registrationRepository.findByPersonIdAndTypeCode(person.id, RiskType.KNOWN_ADULT.code).single()
assertThat(riskToAdultBefore.level?.code, equalTo(RiskLevel.M.code))
assertThat(riskToAdultBefore.reviews, hasSize(1))
val riskToAdultReviewId = riskToAdultBefore.reviews.single().id
val riskToPublicBefore =
registrationRepository.findByPersonIdAndTypeCode(person.id, RiskType.PUBLIC.code).single()
assertThat(riskToPublicBefore.level?.code, equalTo(RiskLevel.M.code))
Expand Down Expand Up @@ -398,6 +404,7 @@ internal class IntegrationTest {
val riskToAdult = registrationRepository.findByPersonIdAndTypeCode(person.id, RiskType.KNOWN_ADULT.code)
assertThat(riskToAdult, hasSize(0))
assertThat(domainEvents.ofType(RiskType.KNOWN_ADULT), hasSize(1))
assertThat(entityManager.find(RegistrationReview::class.java, riskToAdultReviewId), nullValue())

val riskToPublic = registrationRepository.findByPersonIdAndTypeCode(person.id, RiskType.PUBLIC.code).single()
assertThat(riskToPublic.level?.code, equalTo(RiskLevel.V.code))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,22 @@ class Registration(
var deregistration: DeRegistration? = null
private set

@OneToMany(mappedBy = "registration", cascade = [CascadeType.ALL])
@OneToMany(mappedBy = "registration", cascade = [CascadeType.ALL], orphanRemoval = true)
@OrderBy("date, createdDatetime")
var reviews: List<RegistrationReview> = listOf()
var reviews: MutableList<RegistrationReview> = mutableListOf()
private set

fun withReview(contact: Contact): Registration {
reviews = reviews + RegistrationReview(personId, this, contact, nextReviewDate, null, teamId, staffId)
reviews += RegistrationReview(personId, this, contact, nextReviewDate, null, teamId, staffId)
return this
}

fun deregister(contact: Contact): List<Contact> {
fun deregister(contact: Contact) {
deregistration = DeRegistration(LocalDate.now(), this, personId, contact, contact.teamId, contact.staffId)
deregistered = true
nextReviewDate = null
val splitReviews = reviews.groupBy { it.completed }
reviews = splitReviews[true] ?: listOf()
reviews.removeIf { !it.completed }
reviews.firstOrNull()?.reviewDue = null
return splitReviews[false]?.map { it.contact } ?: listOf()
}
}

Expand Down

0 comments on commit 4241543

Please sign in to comment.