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-1618 remove unnecessary locks #2488

Merged
merged 1 commit 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
@@ -1,17 +1,10 @@
package uk.gov.justice.digital.hmpps.integrations.delius.person

import jakarta.persistence.LockModeType
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

interface PersonRepository : JpaRepository<Person, Long> {
fun findByCrnAndSoftDeletedIsFalse(crn: String): Person?

@Lock(LockModeType.PESSIMISTIC_READ)
@Query("select p.id from Person p where p.id = :id")
fun findForUpdate(id: Long): Long
}

fun PersonRepository.getByCrn(crn: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.getByCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.getNsiTransferReason
import uk.gov.justice.digital.hmpps.integrations.delius.person.Person
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceDataRepository
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.referralCompleted
import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffRepository
Expand All @@ -36,7 +35,6 @@ class NsiService(
private val nsiTypeRepository: NsiTypeRepository,
private val nsiStatusRepository: NsiStatusRepository,
private val nsiManagerRepository: NsiManagerRepository,
private val personRepository: PersonRepository,
private val teamRepository: TeamRepository,
private val staffRepository: StaffRepository,
private val transferReasonRepository: TransferReasonRepository,
Expand All @@ -53,56 +51,53 @@ class NsiService(
) {
val externalReference = Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId
nsiRepository.findByPersonIdAndExternalReference(person.id, externalReference) ?: run {
personRepository.findForUpdate(person.id)
nsiRepository.findByPersonIdAndExternalReference(person.id, externalReference) ?: run {
val staff = staffRepository.getByCode(details.keyWorker.staffCode)
val nsi = nsiRepository.save(
Nsi(
person = person,
type = nsiTypeRepository.getByCode(NsiTypeCode.APPROVED_PREMISES_RESIDENCE.code),
status = nsiStatusRepository.getByCode(NsiStatusCode.IN_RESIDENCE.code),
referralDate = details.applicationSubmittedOn,
expectedStartDate = details.arrivedAt.toLocalDate(),
actualStartDate = details.arrivedAt,
expectedEndDate = details.expectedDepartureOn,
notes = listOfNotNull(
details.notes,
"For more details, click here: ${details.applicationUrl}"
).joinToString(System.lineSeparator() + System.lineSeparator()),
externalReference = externalReference
)
)
val team = teamRepository.getApprovedPremisesTeam(details.premises.legacyApCode)
nsiManagerRepository.save(
NsiManager(
nsi = nsi,
staff = staff,
team = team,
probationArea = team.probationArea,
startDate = details.arrivedAt,
transferReason = transferReasonRepository.getNsiTransferReason()
)
)
addressService.updateMainAddress(person, details, ap)
contactService.createContact(
ContactDetails(
date = details.arrivedAt,
type = ContactTypeCode.ARRIVED,
locationCode = ap.locationCode(),
description = "Arrived at ${details.premises.name}",
notes = listOfNotNull(
details.notes,
"For more details, click here: ${details.applicationUrl}"
).joinToString(System.lineSeparator() + System.lineSeparator())
),
val staff = staffRepository.getByCode(details.keyWorker.staffCode)
val nsi = nsiRepository.save(
Nsi(
person = person,
eventId = eventRepository.getByEventNumber(person.id, details.eventNumber).id,
type = nsiTypeRepository.getByCode(NsiTypeCode.APPROVED_PREMISES_RESIDENCE.code),
status = nsiStatusRepository.getByCode(NsiStatusCode.IN_RESIDENCE.code),
referralDate = details.applicationSubmittedOn,
expectedStartDate = details.arrivedAt.toLocalDate(),
actualStartDate = details.arrivedAt,
expectedEndDate = details.expectedDepartureOn,
notes = listOfNotNull(
details.notes,
"For more details, click here: ${details.applicationUrl}"
).joinToString(System.lineSeparator() + System.lineSeparator()),
externalReference = externalReference
)
)
val team = teamRepository.getApprovedPremisesTeam(details.premises.legacyApCode)
nsiManagerRepository.save(
NsiManager(
nsi = nsi,
staff = staff,
team = team,
probationAreaCode = ap.probationArea.code
probationArea = team.probationArea,
startDate = details.arrivedAt,
transferReason = transferReasonRepository.getNsiTransferReason()
)
referralService.personArrived(person, ap, details)
}
)
addressService.updateMainAddress(person, details, ap)
contactService.createContact(
ContactDetails(
date = details.arrivedAt,
type = ContactTypeCode.ARRIVED,
locationCode = ap.locationCode(),
description = "Arrived at ${details.premises.name}",
notes = listOfNotNull(
details.notes,
"For more details, click here: ${details.applicationUrl}"
).joinToString(System.lineSeparator() + System.lineSeparator())
),
person = person,
eventId = eventRepository.getByEventNumber(person.id, details.eventNumber).id,
staff = staff,
team = team,
probationAreaCode = ap.probationArea.code
)
referralService.personArrived(person, ap, details)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ internal class ApprovedPremisesServiceTest {
nsiTypeRepository,
nsiStatusRepository,
nsiManagerRepository,
personRepository,
teamRepository,
staffRepository,
transferReasonRepository,
Expand Down