Skip to content

Commit

Permalink
PI-2654 Use external reference for approved premises referrals
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Nov 27, 2024
1 parent 9a7ba10 commit 4d166e3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.ApprovedPremises
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Referral
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Residence
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.Nsi
import uk.gov.justice.digital.hmpps.integrations.delius.person.Person
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ApprovedPremisesCategoryCode
import uk.gov.justice.digital.hmpps.service.EXT_REF_BOOKING_PREFIX
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit
import java.util.*
Expand All @@ -20,22 +19,25 @@ object ReferralGenerator {
val BOOKING_ID = UUID.randomUUID().toString()
var BOOKING_WITHOUT_ARRIVAL = generateReferral(
person = PersonGenerator.PERSON_WITH_BOOKING,
referralNotes = "${Nsi.EXT_REF_BOOKING_PREFIX}$BOOKING_ID${System.lineSeparator()}Some other notes"
referralNotes = "Some other notes",
externalReference = "${EXT_REF_BOOKING_PREFIX}$BOOKING_ID",
)

val ARRIVED_ID = UUID.randomUUID().toString()
var BOOKING_ARRIVED = generateReferral(
person = PersonGenerator.PERSON_WITH_BOOKING,
referralNotes = "${Nsi.EXT_REF_BOOKING_PREFIX}$ARRIVED_ID${System.lineSeparator()}Some other notes",
referralNotes = "Some other notes",
externalReference = "${EXT_REF_BOOKING_PREFIX}$ARRIVED_ID",
expectedArrivalDate = LocalDate.now(),
expectedDepartureDate = LocalDate.now().plusDays(7)
expectedDepartureDate = LocalDate.now().plusDays(7),
)
var ARRIVAL = generateResidence(PersonGenerator.PERSON_WITH_BOOKING, BOOKING_ARRIVED)

val DEPARTED_ID = UUID.randomUUID().toString()
var BOOKING_DEPARTED = generateReferral(
person = PersonGenerator.PERSON_WITH_BOOKING,
referralNotes = "${Nsi.EXT_REF_BOOKING_PREFIX}$DEPARTED_ID${System.lineSeparator()}Some other notes",
referralNotes = "Some other notes",
externalReference = "${EXT_REF_BOOKING_PREFIX}$DEPARTED_ID",
expectedArrivalDate = LocalDate.now().minusDays(8),
expectedDepartureDate = LocalDate.now().minusDays(1)
)
Expand All @@ -56,7 +58,8 @@ object ReferralGenerator {
expectedArrivalDate: LocalDate? = LocalDate.now().plusDays(1),
expectedDepartureDate: LocalDate? = LocalDate.now().plusDays(8),
decisionDate: ZonedDateTime? = ZonedDateTime.now(EuropeLondon),
referralNotes: String? = null
referralNotes: String? = null,
externalReference: String? = null
) = Referral(
personId = person.id,
eventId = eventId,
Expand Down Expand Up @@ -93,7 +96,8 @@ object ReferralGenerator {
decisionTeamId = TeamGenerator.APPROVED_PREMISES_TEAM.id,
decisionStaffId = 26553,
referringTeamId = TeamGenerator.UNALLOCATED.id,
referringStaffId = 563828
referringStaffId = 563828,
externalReference = externalReference
)

fun generateResidence(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.EventDetails
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.PersonArrived
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.PersonDeparted
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.PreferredResidence
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.PreferredResidenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ReferralRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ResidenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.outcome.ContactOutcome
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.Nsi.Companion.EXT_REF_BOOKING_PREFIX
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.person.address.PersonAddressRepository
Expand All @@ -40,6 +37,7 @@ import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.messaging.crn
import uk.gov.justice.digital.hmpps.messaging.telemetryProperties
import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader
import uk.gov.justice.digital.hmpps.service.EXT_REF_BOOKING_PREFIX
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import uk.gov.justice.digital.hmpps.test.CustomMatchers.isCloseTo
import java.time.LocalDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referra
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.outcome.ContactOutcome
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.Nsi.Companion.EXT_REF_BOOKING_PREFIX
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.person.address.PersonAddressRepository
Expand All @@ -42,6 +41,7 @@ import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.messaging.crn
import uk.gov.justice.digital.hmpps.messaging.telemetryProperties
import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader
import uk.gov.justice.digital.hmpps.service.EXT_REF_BOOKING_PREFIX
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import uk.gov.justice.digital.hmpps.test.CustomMatchers.isCloseTo
import java.time.LocalDate
Expand Down Expand Up @@ -185,6 +185,7 @@ internal class MessagingIntegrationTest {
referral.categoryId,
equalTo(ReferenceDataGenerator.REFERRAL_CATEGORIES[ApprovedPremisesCategoryCode.VOLUNTARY_MAPPA.value]?.id)
)
assertThat(referral.externalReference, equalTo("${EXT_REF_BOOKING_PREFIX}14c80733-4b6d-4f35-b724-66955aac320c"))
assertThat(referral.referralGroupId, equalTo(ReferenceDataGenerator.REFERRAL_GROUP.id))
assertThat(referral.referralDate, equalTo(LocalDate.parse("2022-11-28")))
assertThat(referral.activeArsonRiskId, equalTo(ReferenceDataGenerator.YN_UNKNOWN.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class Referral(
val rohSelfId: Long,
val rohOthersId: Long,
@Lob
val riskInformation: String?
val riskInformation: String?,
val externalReference: String?,
) {
@Column(name = "original_ap_admit_date")
var admissionDate: LocalDate? = null
Expand Down Expand Up @@ -157,7 +158,7 @@ class Event(
)

interface ReferralRepository : JpaRepository<Referral, Long> {
fun findByPersonIdAndCreatedByUserIdAndReferralNotesContains(
fun findByPersonIdAndCreatedByUserIdAndExternalReference(
personId: Long,
createdByUserId: Long,
externalRef: String
Expand All @@ -182,7 +183,7 @@ interface ReferralRepository : JpaRepository<Referral, Long> {
join ApprovedPremises ap on ap.id = r.approvedPremisesId
join Person p on p.id = r.personId
left join Residence res on res.referralId = r.id
where p.crn = :crn and r.referralNotes like '%' || :externalRef || '%'
where p.crn = :crn and r.externalReference = :externalRef
"""
)
fun findReferralDetail(crn: String, externalRef: String): ReferralAndResidence?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EntityListeners
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.Lob
import jakarta.persistence.ManyToOne
import jakarta.persistence.SequenceGenerator
import jakarta.persistence.Table
import jakarta.persistence.Version
import jakarta.persistence.*
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedBy
Expand Down Expand Up @@ -107,10 +96,6 @@ class Nsi(
field = value
active = value == null
}

companion object {
const val EXT_REF_BOOKING_PREFIX = "urn:uk:gov:hmpps:approved-premises-service:booking:"
}
}

interface NsiRepository : JpaRepository<Nsi, Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class NsiService(
details: PersonArrived,
ap: ApprovedPremises
): Pair<PersonAddress?, PersonAddress>? {
val externalReference = Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId
val externalReference = EXT_REF_BOOKING_PREFIX + details.bookingId
nsiRepository.findByPersonIdAndExternalReference(person.id, externalReference) ?: run {
val staff = staffRepository.getByCode(details.keyWorker.staffCode)
val nsi = nsiRepository.save(
Expand Down Expand Up @@ -95,7 +95,7 @@ class NsiService(

fun personDeparted(person: Person, details: PersonDeparted, ap: ApprovedPremises): PersonAddress? {
val nsi =
nsiRepository.findByPersonIdAndExternalReference(person.id, Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId)
nsiRepository.findByPersonIdAndExternalReference(person.id, EXT_REF_BOOKING_PREFIX + details.bookingId)
nsi?.actualEndDate = details.departedAt
nsi?.outcome = referenceDataRepository.referralCompleted()
contactService.createContact(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.contact.outcome.ContactOutcome
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.Nsi
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.person.getByCrn
Expand Down Expand Up @@ -56,10 +55,10 @@ class ReferralService(
val rTeam = teamRepository.getUnallocatedTeam(ap.probationArea.code)
val rStaff = staffRepository.getByCode(details.bookedBy.staffMember.staffCode)
val findReferral = {
referralRepository.findByPersonIdAndCreatedByUserIdAndReferralNotesContains(
referralRepository.findByPersonIdAndCreatedByUserIdAndExternalReference(
person.id,
ServiceContext.servicePrincipal()!!.userId,
Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId
EXT_REF_BOOKING_PREFIX + details.bookingId
)
}
findReferral() ?: run {
Expand Down Expand Up @@ -91,7 +90,7 @@ class ReferralService(

fun bookingChanged(crn: String, details: BookingChanged, ap: ApprovedPremises) {
val person = personRepository.getByCrn(crn)
val referral = getReferral(person, Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId)
val referral = getReferral(person, EXT_REF_BOOKING_PREFIX + details.bookingId)
contactService.createContact(
ContactDetails(
date = details.changedAt,
Expand Down Expand Up @@ -123,7 +122,7 @@ class ReferralService(

fun bookingCancelled(crn: String, details: BookingCancelled, ap: ApprovedPremises) {
val person = personRepository.getByCrn(crn)
val externalReference = Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId
val externalReference = EXT_REF_BOOKING_PREFIX + details.bookingId
val referral = findReferral(person, externalReference)?.also {
if (preferredResidenceRepository.existsByApprovedPremisesReferralId(it.id)) {
preferredResidenceRepository.deleteByApprovedPremisesReferralId(it.id)
Expand Down Expand Up @@ -160,7 +159,7 @@ class ReferralService(
}

fun personNotArrived(person: Person, ap: ApprovedPremises, dateTime: ZonedDateTime, details: PersonNotArrived) {
val referral = getReferral(person, Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId)
val referral = getReferral(person, EXT_REF_BOOKING_PREFIX + details.bookingId)
referral.nonArrivalDate = dateTime.toLocalDate()
referral.nonArrivalNotes = details.notes
referral.nonArrivalReasonId =
Expand All @@ -185,15 +184,15 @@ class ReferralService(
}

fun personArrived(person: Person, ap: ApprovedPremises, details: PersonArrived) {
val referral = getReferral(person, Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId)
val referral = getReferral(person, EXT_REF_BOOKING_PREFIX + details.bookingId)
referral.admissionDate = details.arrivedAt.toLocalDate()
val kw = staffRepository.getByCode(details.keyWorker.staffCode)
residenceRepository.save(details.residence(person, ap, referral, kw))
}

fun personDeparted(person: Person, details: PersonDeparted) {
val serviceUserId = ServiceContext.servicePrincipal()!!.userId
val externalReference = Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId
val externalReference = EXT_REF_BOOKING_PREFIX + details.bookingId
val referral = getReferral(person, externalReference)
val residence = residenceRepository.findByReferralIdAndCreatedByUserId(referral.id, serviceUserId)
?: throw IgnorableMessageException(
Expand Down Expand Up @@ -230,14 +229,14 @@ class ReferralService(
}

fun findReferral(person: Person, externalReference: String): Referral? =
referralRepository.findByPersonIdAndCreatedByUserIdAndReferralNotesContains(
referralRepository.findByPersonIdAndCreatedByUserIdAndExternalReference(
person.id,
ServiceContext.servicePrincipal()!!.userId,
externalReference
)

fun getReferralDetails(crn: String, bookingId: String): ReferralDetail =
referralRepository.findReferralDetail(crn, Nsi.EXT_REF_BOOKING_PREFIX + bookingId)?.let {
referralRepository.findReferralDetail(crn, EXT_REF_BOOKING_PREFIX + bookingId)?.let {
ReferralDetail(
ApReferral(
it.referral.referralDate,
Expand Down Expand Up @@ -288,7 +287,7 @@ class ReferralService(
).id,
referralGroupId = referenceDataRepository.findApprovedPremisesGroup(ap.id)?.id,
decisionId = referenceDataRepository.acceptedDeferredAdmission().id,
referralNotes = Nsi.EXT_REF_BOOKING_PREFIX + bookingId + System.lineSeparator() + notes,
referralNotes = EXT_REF_BOOKING_PREFIX + bookingId + System.lineSeparator() + notes,
decisionNotes = notes,
referralSourceId = referralSourceRepository.getByCode("OTH").id,
sourceTypeId = referenceDataRepository.apReferralSource().id,
Expand Down Expand Up @@ -318,7 +317,8 @@ class ReferralService(
decisionTeamId = apTeam.id,
decisionStaffId = apStaff.id,
referringTeamId = referringTeam.id,
referringStaffId = referringStaff.id
referringStaffId = referringStaff.id,
externalReference = EXT_REF_BOOKING_PREFIX + bookingId
)
}

Expand All @@ -340,3 +340,5 @@ class ReferralService(
uk.gov.justice.digital.hmpps.model.ApprovedPremises(approvedPremises)
)
}

const val EXT_REF_BOOKING_PREFIX = "urn:uk:gov:hmpps:approved-premises-service:booking:"
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ internal class ApprovedPremisesServiceTest {
LocalDate.now(),
LocalDate.now(),
ZonedDateTime.now(),
Nsi.EXT_REF_BOOKING_PREFIX + bookingId,
"Some notes...",
ReferenceDataGenerator.REFERRAL_DATE_TYPE.id,
ReferenceDataGenerator.REFERRAL_CATEGORIES[ApprovedPremisesCategoryCode.OTHER.value]!!.id,
ReferenceDataGenerator.REFERRAL_GROUP.id,
Expand All @@ -586,13 +586,14 @@ internal class ApprovedPremisesServiceTest {
ReferenceDataGenerator.RISK_UNKNOWN.id,
ReferenceDataGenerator.RISK_UNKNOWN.id,
ReferenceDataGenerator.RISK_UNKNOWN.id,
null
null,
EXT_REF_BOOKING_PREFIX + bookingId,
)
whenever(
referralRepository.findByPersonIdAndCreatedByUserIdAndReferralNotesContains(
referralRepository.findByPersonIdAndCreatedByUserIdAndExternalReference(
person.id,
UserGenerator.AUDIT_USER.id,
Nsi.EXT_REF_BOOKING_PREFIX + bookingId
EXT_REF_BOOKING_PREFIX + bookingId
)
).thenReturn(ref)
return ref
Expand Down

0 comments on commit 4d166e3

Please sign in to comment.