Skip to content

Commit

Permalink
PI-2217 Move Transactional annotation to handler method (#3888)
Browse files Browse the repository at this point in the history
* PI-20217 Move Transactional annotation to handler method

* Move annotation to handler class

* Move annotation to handler class
  • Loading branch information
marcus-bcl authored Jun 11, 2024
1 parent 5fa8b12 commit bab923d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,8 +23,6 @@ class ContactService(
private val providerService: ProviderService,
private val telemetryService: TelemetryService
) : AuditableService(auditedInteractionService) {

@Transactional
fun <T : Cas3Event> createOrUpdateContact(
crn: String,
person: Person? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -37,11 +33,9 @@ interface PersonRepository : JpaRepository<Person, Long> {
fun findByCrn(crn: String): Person?

@Lock(LockModeType.PESSIMISTIC_READ)
override fun findById(personId: Long): Optional<Person>
@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) }
Original file line number Diff line number Diff line change
Expand Up @@ -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<Team>,

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,6 +16,7 @@ import uk.gov.justice.digital.hmpps.telemetry.notificationReceived
import java.net.URI

@Component
@Transactional
class Handler(
override val converter: NotificationConverter<HmppsDomainEvent>,
private val telemetryService: TelemetryService,
Expand Down Expand Up @@ -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" })
Original file line number Diff line number Diff line change
Expand Up @@ -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<HmppsDomainEvent>,
private val telemetryService: TelemetryService,
Expand All @@ -33,8 +34,6 @@ class Handler(
private val referenceDataRepository: ReferenceDataRepository,
private val featureFlags: FeatureFlags
) : NotificationHandler<HmppsDomainEvent> {

@Transactional
override fun handle(notification: Notification<HmppsDomainEvent>) {
telemetryService.notificationReceived(notification)

Expand Down

0 comments on commit bab923d

Please sign in to comment.