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-2217 Move Transactional annotation to handler method #3888

Merged
merged 3 commits into from
Jun 11, 2024
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
@@ -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