diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt index 68346e4bb5..dce8c1cf28 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt @@ -1,14 +1,12 @@ package uk.gov.justice.digital.hmpps.integrations.delius import org.springframework.stereotype.Service -import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.PersonArrived import uk.gov.justice.digital.hmpps.integrations.delius.entity.* import java.time.LocalDate import java.time.ZonedDateTime @Service -@Transactional class AddressService( private val personAddressRepository: PersonAddressRepository, private val referenceDataRepository: ReferenceDataRepository, @@ -20,8 +18,8 @@ class AddressService( } fun updateCas3Address(person: Person, details: PersonArrived) { - val personForUpdate = personRepository.getByIdForUpdate(person.id) - val currentMain = personAddressRepository.findMainAddress(personForUpdate.id) + personRepository.findForUpdate(person.id) + val currentMain = personAddressRepository.findMainAddress(person.id) if (currentMain?.type?.code == AddressTypeCode.CAS3.code) { val addressLines = details.premises.addressLines currentMain.apply { @@ -37,8 +35,8 @@ class AddressService( } fun endMainAddress(person: Person, endDate: LocalDate) { - val personForUpdate = personRepository.getByIdForUpdate(person.id) - val currentMain = personAddressRepository.findMainAddress(personForUpdate.id) + personRepository.findForUpdate(person.id) + val currentMain = personAddressRepository.findMainAddress(person.id) currentMain?.apply { val previousStatus = referenceDataRepository.previousAddressStatus() currentMain.status = previousStatus @@ -47,8 +45,8 @@ class AddressService( } fun endMainCAS3Address(person: Person, endDate: ZonedDateTime) { - val personForUpdate = personRepository.getByIdForUpdate(person.id) - val currentMain = personAddressRepository.findMainAddress(personForUpdate.id) + personRepository.findForUpdate(person.id) + val currentMain = personAddressRepository.findMainAddress(person.id) currentMain?.apply { if (currentMain.type.code == AddressTypeCode.CAS3.code) { val previousStatus = referenceDataRepository.previousAddressStatus() diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt index 87f36b1e3f..a24fd9f8ed 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/ContactService.kt @@ -1,7 +1,6 @@ package uk.gov.justice.digital.hmpps.integrations.delius import org.springframework.stereotype.Service -import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.audit.service.AuditableService import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService import uk.gov.justice.digital.hmpps.exception.NotFoundException @@ -24,8 +23,6 @@ class ContactService( private val providerService: ProviderService, private val telemetryService: TelemetryService ) : AuditableService(auditedInteractionService) { - - @Transactional fun createOrUpdateContact( crn: String, person: Person? = null, diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Person.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Person.kt index 32c449cf8b..4c4b30fa92 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Person.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Person.kt @@ -1,16 +1,12 @@ package uk.gov.justice.digital.hmpps.integrations.delius.entity -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.Id -import jakarta.persistence.LockModeType -import jakarta.persistence.Table +import jakarta.persistence.* import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Lock +import org.springframework.data.jpa.repository.Query import uk.gov.justice.digital.hmpps.exception.NotFoundException -import java.util.Optional @Immutable @Entity @@ -37,11 +33,9 @@ interface PersonRepository : JpaRepository { fun findByCrn(crn: String): Person? @Lock(LockModeType.PESSIMISTIC_READ) - override fun findById(personId: Long): Optional + @Query("select p.id from Person p where p.id = :personId") + fun findForUpdate(personId: Long): Long } fun PersonRepository.getByCrn(crn: String) = findByCrn(crn) ?: throw NotFoundException("Person", "crn", crn) - -fun PersonRepository.getByIdForUpdate(personId: Long) = - findById(personId).orElseThrow { NotFoundException("Person", "id", personId) } diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Provider.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Provider.kt index 20ff747058..cac6018749 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Provider.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/Provider.kt @@ -44,12 +44,12 @@ class Staff( @Column(name = "officer_code", columnDefinition = "char(7)") val code: String, - @ManyToMany @JoinTable( name = "staff_team", joinColumns = [JoinColumn(name = "staff_id")], inverseJoinColumns = [JoinColumn(name = "team_id")] ) + @ManyToMany(fetch = FetchType.EAGER) val teams: List, @Id diff --git a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt index 804d2c9fdd..cd975c4352 100644 --- a/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt +++ b/projects/cas3-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt @@ -1,6 +1,7 @@ package uk.gov.justice.digital.hmpps.messaging import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.converter.NotificationConverter import uk.gov.justice.digital.hmpps.datetime.DeliusDateTimeFormatter import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.Cas3ApiClient @@ -15,6 +16,7 @@ import uk.gov.justice.digital.hmpps.telemetry.notificationReceived import java.net.URI @Component +@Transactional class Handler( override val converter: NotificationConverter, private val telemetryService: TelemetryService, @@ -111,6 +113,6 @@ class Handler( ) } -fun HmppsDomainEvent.crn(): String = personReference.findCrn() ?: throw IllegalArgumentException("Missing CRN") +fun HmppsDomainEvent.crn(): String = requireNotNull(personReference.findCrn()) { "Missing CRN" } -fun HmppsDomainEvent.url(): URI = URI.create(detailUrl ?: throw IllegalArgumentException("Missing detail url")) +fun HmppsDomainEvent.url(): URI = URI.create(requireNotNull(detailUrl) { "Missing detail url" }) diff --git a/projects/manage-offences-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt b/projects/manage-offences-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt index c93e912eaa..fcdbd986e6 100644 --- a/projects/manage-offences-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt +++ b/projects/manage-offences-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt @@ -24,6 +24,7 @@ const val FF_CREATE_OFFENCE = "manage-offences-create-offence" const val FF_UPDATE_OFFENCE = "manage-offences-update-offence" @Component +@Transactional class Handler( override val converter: NotificationConverter, private val telemetryService: TelemetryService, @@ -33,8 +34,6 @@ class Handler( private val referenceDataRepository: ReferenceDataRepository, private val featureFlags: FeatureFlags ) : NotificationHandler { - - @Transactional override fun handle(notification: Notification) { telemetryService.notificationReceived(notification)