Skip to content

Commit

Permalink
PI-2358 - update logic to deal with soft deleted key dates
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Jul 16, 2024
1 parent 9530e79 commit 65766d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ internal class IntegrationTest {
@Autowired
lateinit var custodyRepository: CustodyRepository

private val sedDate = "2025-09-10"

@Test
fun `Custody Key Dates updated as expected`() {
val notification = Notification(message = MessageGenerator.SENTENCE_DATE_CHANGED)
Expand All @@ -70,7 +72,7 @@ internal class IntegrationTest {
verify(telemetryService).trackEvent(
eq("KeyDatesUpdated"),
check {
assertThat(it[CustodyDateType.SENTENCE_EXPIRY_DATE.code], equalTo("2025-09-10"))
assertThat(it[CustodyDateType.SENTENCE_EXPIRY_DATE.code], equalTo(sedDate))
},
anyMap()
)
Expand All @@ -89,6 +91,15 @@ internal class IntegrationTest {
attributes = MessageAttributes(eventType = "SENTENCE_CHANGED")
)

val custodyId =
custodyRepository.findCustodyId(PersonGenerator.PERSON_WITH_KEYDATES_BY_CRN.id, "48340A").first()

var custody = custodyRepository.findCustodyById(custodyId)

//check key date is soft deleted b4 message is processed
val led = custody.keyDate(CustodyDateType.LICENCE_EXPIRY_DATE.code)
assertThat(led?.getSoftDeleteFlag(), equalTo(true))

val first = CompletableFuture.runAsync {
channelManager.getChannel(queueName).publishAndWait(notification)
}
Expand All @@ -100,15 +111,14 @@ internal class IntegrationTest {

verify(telemetryService, times(2)).notificationReceived(notification)

val custodyId =
custodyRepository.findCustodyId(PersonGenerator.PERSON_WITH_KEYDATES_BY_CRN.id, "48340A").first()
val custody = custodyRepository.findCustodyById(custodyId)

custody = custodyRepository.findCustodyById(custodyId)
verifyUpdatedKeyDates(custody)

verify(telemetryService).trackEvent(
eq("KeyDatesUpdated"),
check {
assertThat(it[CustodyDateType.SENTENCE_EXPIRY_DATE.code], equalTo("2025-09-10"))
assertThat(it[CustodyDateType.SENTENCE_EXPIRY_DATE.code], equalTo(sedDate))
},
anyMap()
)
Expand All @@ -127,11 +137,13 @@ internal class IntegrationTest {
val erd = custody.keyDate(CustodyDateType.EXPECTED_RELEASE_DATE.code)
val hde = custody.keyDate(CustodyDateType.HDC_EXPECTED_DATE.code)

assertThat(sed?.date, equalTo(LocalDate.parse("2025-09-10")))
assertThat(sed?.date, equalTo(LocalDate.parse(sedDate)))
assertThat(crd?.date, equalTo(LocalDate.parse("2022-11-26")))
assertThat(led?.date, equalTo(LocalDate.parse("2025-09-11")))
assertThat(erd?.date, equalTo(LocalDate.parse("2022-11-27")))
assertThat(hde?.date, equalTo(LocalDate.parse("2022-10-28")))

assertThat(led?.getSoftDeleteFlag(), equalTo(false))
}

private fun verifyContactCreated() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package uk.gov.justice.digital.hmpps.integrations.delius.custody.date

import jakarta.persistence.*
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import uk.gov.justice.digital.hmpps.integrations.delius.custody.BaseEntity
import uk.gov.justice.digital.hmpps.integrations.delius.custody.date.reference.ReferenceData
import java.time.LocalDate

@Entity
@EntityListeners(AuditingEntityListener::class)
@SQLRestriction("soft_deleted = 0")
@SequenceGenerator(name = "key_date_id_seq", sequenceName = "key_date_id_seq", allocationSize = 1)
@Table(name = "key_date", uniqueConstraints = [UniqueConstraint(columnNames = ["custody_id", "key_date_type_id", "key_date"])])
class KeyDate(
Expand All @@ -31,7 +29,7 @@ class KeyDate(
val date: LocalDate

) : BaseEntity() {
fun changeDate(date: LocalDate): KeyDate? = if (this.date == date) {
fun changeDate(date: LocalDate): KeyDate? = if (this.date == date && !this.softDeleted) {
null
} else {
// create new entity to allow dry run to not make changes
Expand All @@ -41,4 +39,6 @@ class KeyDate(
it.version = version
}
}

fun getSoftDeleteFlag() = softDeleted
}

0 comments on commit 65766d0

Please sign in to comment.