diff --git a/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index 6db9497cc..3d7637d02 100644 --- a/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -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.* @@ -76,6 +77,9 @@ internal class IntegrationTest { @Autowired lateinit var objectMapper: ObjectMapper + @Autowired + lateinit var entityManager: EntityManager + @MockBean lateinit var telemetryService: TelemetryService @@ -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)) @@ -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)) diff --git a/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt b/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt index 3f9450bca..0d52de815 100644 --- a/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt +++ b/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt @@ -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 = listOf() + var reviews: MutableList = 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 { + 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() } }