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-1615 Move caseload rebuild into afterCommit hook #2486

Merged
merged 2 commits into from
Nov 2, 2023
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
@@ -0,0 +1,27 @@
package uk.gov.justice.digital.hmpps.audit.service

import org.springframework.context.annotation.Conditional
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Service
import org.springframework.transaction.support.TransactionSynchronization
import org.springframework.transaction.support.TransactionSynchronizationManager
import uk.gov.justice.digital.hmpps.audit.config.OracleCondition

@Service
class OptimisationTables(private val optimisationTablesRebuild: OptimisationTablesRebuild?) {
fun rebuild(personId: Long) {
optimisationTablesRebuild?.rebuild(personId)
}
}

@Service
@Conditional(OracleCondition::class)
class OptimisationTablesRebuild(private val jdbcTemplate: JdbcTemplate) {
fun rebuild(personId: Long) {
TransactionSynchronizationManager.registerSynchronization(object : TransactionSynchronization {
override fun afterCommit() {
jdbcTemplate.execute("call PKG_TRIGGERSUPPORT.PROCREBUILDOPTTABLES($personId)")
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@ open class DeliusConnectionProvider : DatasourceConnectionProviderImpl() {
}

override fun closeConnection(connection: Connection) {
if (OptimisationContext.offenderId.get() != null) {
connection.prepareStatement("call PKG_TRIGGERSUPPORT.PROCREBUILDOPTTABLES(${OptimisationContext.offenderId.get()})")
.use {
it.execute()
}
OptimisationContext.offenderId.set(null)
}
connection.prepareStatement("call PKG_VPD_CTX.CLEAR_CLIENT_IDENTIFIER()").use {
it.execute()
}
connection.prepareStatement("call PKG_VPD_CTX.CLEAR_CLIENT_IDENTIFIER()").use { it.execute() }
super.closeConnection(connection)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uk.gov.justice.digital.hmpps.audit.service

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.hasSize
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.transaction.support.TransactionSynchronizationManager

class OptimisationTablesTest {
@Test
fun `handles non-Oracle database`() {
assertDoesNotThrow { OptimisationTables(null).rebuild(123) }
}

@Test
fun `creates afterCommit hook`() {
val jdbcTemplate = mock(JdbcTemplate::class.java)
TransactionSynchronizationManager.initSynchronization()

OptimisationTables(OptimisationTablesRebuild(jdbcTemplate)).rebuild(123)

assertThat(TransactionSynchronizationManager.getSynchronizations(), hasSize(1))
TransactionSynchronizationManager.getSynchronizations()[0].afterCommit()
verify(jdbcTemplate).execute("call PKG_TRIGGERSUPPORT.PROCREBUILDOPTTABLES(123)")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,4 @@ class DeliusConnectionProviderTest {
verify(preparedStatement).execute()
verify(preparedStatement).close()
}

@Test
fun `when offender id in optimisation context sp called`() {
OptimisationContext.offenderId.set(765)
whenever(connection.prepareStatement(anyString())).thenReturn(preparedStatement)
deliusConnectionProvider.closeConnection(connection)
verify(connection, times(2)).prepareStatement(anyString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package uk.gov.justice.digital.hmpps.integrations.tier

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.datasource.OptimisationContext
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.datetime.DeliusDateTimeFormatter
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.contact.Contact
Expand Down Expand Up @@ -35,14 +35,15 @@ class TierService(
private val staffRepository: StaffRepository,
private val teamRepository: TeamRepository,
private val contactTypeRepository: ContactTypeRepository,
private val telemetryService: TelemetryService
private val telemetryService: TelemetryService,
private val optimisationTables: OptimisationTables
) {
@Transactional
fun updateTier(crn: String, tierCalculation: TierCalculation) {
val person = personRepository.findByCrnAndSoftDeletedIsFalse(crn) ?: return let {
telemetryService.trackEvent("PersonNotFound", tierCalculation.telemetryProperties(crn))
}
OptimisationContext.offenderId.set(person.id)
optimisationTables.rebuild(person.id)
val tier = referenceDataRepository.getByCodeAndSetName("U${tierCalculation.tierScore}", "TIER")
val changeReason = referenceDataRepository.getByCodeAndSetName("ATS", "TIER CHANGE REASON")
val latestTierChangeDate = managementTierRepository.findFirstByIdPersonIdOrderByIdDateChangedDesc(person.id)?.id?.dateChanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.mockito.kotlin.any
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator
Expand Down Expand Up @@ -57,6 +58,8 @@ internal class TierServiceTest {

@Mock lateinit var telemetryService: TelemetryService

@Mock lateinit var optimisationTables: OptimisationTables

@InjectMocks lateinit var tierService: TierService

private val tierScore = ReferenceDataGenerator.generate("someTierCode", ReferenceDataSetGenerator.TIER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package uk.gov.justice.digital.hmpps.service
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.datasource.OptimisationContext
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.exception.ConflictException
import uk.gov.justice.digital.hmpps.exception.NotActiveException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand Down Expand Up @@ -33,7 +33,8 @@ class AllocateEventService(
private val allocationValidator: AllocationValidator,
private val contactTypeRepository: ContactTypeRepository,
private val contactRepository: ContactRepository,
private val transferReasonRepository: TransferReasonRepository
private val transferReasonRepository: TransferReasonRepository,
private val optimisationTables: OptimisationTables
) : ManagerService<OrderManager>(auditedInteractionService, orderManagerRepository) {

@Transactional
Expand All @@ -43,7 +44,7 @@ class AllocateEventService(

it["offenderId"] = event.person.id
it["eventId"] = event.id
OptimisationContext.offenderId.set(event.person.id)
optimisationTables.rebuild(event.person.id)

if (!event.active) throw NotActiveException("Event", "number", allocationDetail.eventNumber)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package uk.gov.justice.digital.hmpps.service
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.datasource.OptimisationContext
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.datetime.DeliusDateTimeFormatter
import uk.gov.justice.digital.hmpps.exception.ConflictException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand Down Expand Up @@ -31,7 +31,8 @@ class AllocatePersonService(
private val allocationValidator: AllocationValidator,
private val contactTypeRepository: ContactTypeRepository,
private val contactRepository: ContactRepository,
private val responsibleOfficerRepository: ResponsibleOfficerRepository
private val responsibleOfficerRepository: ResponsibleOfficerRepository,
private val optimisationTables: OptimisationTables
) : ManagerService<PersonManager>(auditedInteractionService, personManagerRepository) {

@Transactional
Expand All @@ -41,7 +42,7 @@ class AllocatePersonService(
?: throw NotFoundException("Person", "crn", allocationDetail.crn)

it["offenderId"] = personId
OptimisationContext.offenderId.set(personId)
optimisationTables.rebuild(personId)

val activeOffenderManager = personManagerRepository.findActiveManager(
personId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.datasource.OptimisationContext
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.exception.ConflictException
import uk.gov.justice.digital.hmpps.exception.NotActiveException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand Down Expand Up @@ -32,7 +32,8 @@ class AllocateRequirementService(
private val allocationValidator: AllocationValidator,
private val contactTypeRepository: ContactTypeRepository,
private val contactRepository: ContactRepository,
private val transferReasonRepository: TransferReasonRepository
private val transferReasonRepository: TransferReasonRepository,
private val optimisationTables: OptimisationTables
) : ManagerService<RequirementManager>(auditedInteractionService, requirementManagerRepository) {

@Transactional
Expand All @@ -44,7 +45,7 @@ class AllocateRequirementService(
it["offenderId"] = requirement.person.id
it["eventId"] = requirement.disposal.event.id
it["requirementId"] = requirement.id
OptimisationContext.offenderId.set(requirement.person.id)
optimisationTables.rebuild(requirement.person.id)

if (requirement.person.crn != crn) {
throw ConflictException("Requirement ${allocationDetail.requirementId} not for $crn")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.data.generator.EventGenerator
import uk.gov.justice.digital.hmpps.data.generator.OrderManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
Expand Down Expand Up @@ -54,6 +55,9 @@ internal class AllocateEventServiceTest {
@Mock
private lateinit var transferReasonRepository: TransferReasonRepository

@Mock
private lateinit var optimisationTables: OptimisationTables

@InjectMocks
private lateinit var allocateEventService: AllocateEventService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator
import uk.gov.justice.digital.hmpps.data.generator.OrderManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
Expand Down Expand Up @@ -65,6 +66,9 @@ internal class AllocatePersonServiceTest {
@Mock
private lateinit var responsibleOfficerRepository: ResponsibleOfficerRepository

@Mock
private lateinit var optimisationTables: OptimisationTables

@InjectMocks
private lateinit var allocatePersonService: AllocatePersonService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator
import uk.gov.justice.digital.hmpps.data.generator.DisposalGenerator
import uk.gov.justice.digital.hmpps.data.generator.EventGenerator
Expand Down Expand Up @@ -69,6 +70,9 @@ internal class AllocateRequirementServiceTest {
@Mock
private lateinit var transferReasonRepository: TransferReasonRepository

@Mock
private lateinit var optimisationTables: OptimisationTables

@InjectMocks
private lateinit var allocateRequirementService: AllocateRequirementService

Expand Down