Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PI-2358 custody key dates update do not ignore soft deleted records #4038

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ class DataLoader(
KeyDateGenerator.generate(
DEFAULT_CUSTODY,
ReferenceDataGenerator.KEY_DATE_TYPES["PED"]!!,
LocalDate.parse("2022-10-26")
LocalDate.parse("2022-10-26"),
false
),
KeyDateGenerator.generate(
DEFAULT_CUSTODY,
ReferenceDataGenerator.KEY_DATE_TYPES["LED"]!!,
LocalDate.parse("2024-09-10")
LocalDate.parse("2024-09-10"),
false
),
KeyDateGenerator.generate(
DEFAULT_CUSTODY,
ReferenceDataGenerator.KEY_DATE_TYPES["SED"]!!,
LocalDate.parse("2024-08-10")
LocalDate.parse("2024-08-10"),
false
)
)
)
Expand All @@ -111,7 +114,11 @@ class DataLoader(
)
keyDateRepository.saveAll(
ReferenceDataGenerator.KEY_DATE_TYPES.values.map {
KeyDateGenerator.generate(custody, it, LocalDate.parse("2023-12-11"))
if (it.code == "LED") {
KeyDateGenerator.generate(custody, it, LocalDate.parse("2025-09-11"), true)
} else {
KeyDateGenerator.generate(custody, it, LocalDate.parse("2025-12-11"), false)
}
}
)
return custody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import java.time.LocalDate

object KeyDateGenerator {

fun generate(custody: Custody, type: ReferenceData, date: LocalDate) =
KeyDate(IdGenerator.getAndIncrement(), custody, type, date)
fun generate(custody: Custody, type: ReferenceData, date: LocalDate, softDeleted: Boolean) =
KeyDate(IdGenerator.getAndIncrement(), custody, type, date).also { it.softDeleted = softDeleted }
}
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?.softDeleted, 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?.softDeleted, equalTo(false))
}

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

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EntityListeners
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.SequenceGenerator
import org.hibernate.annotations.SQLRestriction
import jakarta.persistence.*
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(

@Id
Expand All @@ -38,14 +32,15 @@ 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
KeyDate(id, custody, type, date).also {
it.createdDateTime = createdDateTime
it.createdUserId = createdUserId
it.version = version
it.softDeleted = false
}
}
}