diff --git a/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferralGenerator.kt b/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferralGenerator.kt index 0b9f8e8a3f..78e9102ad5 100644 --- a/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferralGenerator.kt +++ b/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferralGenerator.kt @@ -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.* @@ -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) ) @@ -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, @@ -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( diff --git a/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationInactiveTest.kt b/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationInactiveTest.kt index 6c2cf40881..a5e1d8b5a7 100644 --- a/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationInactiveTest.kt +++ b/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationInactiveTest.kt @@ -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 @@ -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 diff --git a/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationTest.kt b/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationTest.kt index f0f683716e..2c2b1c004e 100644 --- a/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationTest.kt +++ b/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/MessagingIntegrationTest.kt @@ -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 @@ -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 @@ -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)) diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/approvedpremises/referral/entity/Referral.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/approvedpremises/referral/entity/Referral.kt index 3c195f2d15..66c0df7804 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/approvedpremises/referral/entity/Referral.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/approvedpremises/referral/entity/Referral.kt @@ -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 @@ -157,7 +158,7 @@ class Event( ) interface ReferralRepository : JpaRepository { - fun findByPersonIdAndCreatedByUserIdAndReferralNotesContains( + fun findByPersonIdAndCreatedByUserIdAndExternalReference( personId: Long, createdByUserId: Long, externalRef: String @@ -182,7 +183,7 @@ interface ReferralRepository : JpaRepository { 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? diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/nonstatutoryintervention/entity/Nsi.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/nonstatutoryintervention/entity/Nsi.kt index 1e792fb44f..3d58c7f1e4 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/nonstatutoryintervention/entity/Nsi.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/nonstatutoryintervention/entity/Nsi.kt @@ -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 @@ -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 { diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/NsiService.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/NsiService.kt index 5da22f2981..56f5a89d7f 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/NsiService.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/NsiService.kt @@ -40,7 +40,7 @@ class NsiService( details: PersonArrived, ap: ApprovedPremises ): Pair? { - 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( @@ -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( diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ReferralService.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ReferralService.kt index 4f49f3a5eb..1926dbf3bb 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ReferralService.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ReferralService.kt @@ -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 @@ -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 { @@ -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, @@ -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) @@ -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 = @@ -185,7 +184,7 @@ 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)) @@ -193,7 +192,7 @@ class ReferralService( 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( @@ -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, @@ -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, @@ -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 ) } @@ -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:" \ No newline at end of file diff --git a/projects/approved-premises-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ApprovedPremisesServiceTest.kt b/projects/approved-premises-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ApprovedPremisesServiceTest.kt index 6998474f0a..11cfee464b 100644 --- a/projects/approved-premises-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ApprovedPremisesServiceTest.kt +++ b/projects/approved-premises-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ApprovedPremisesServiceTest.kt @@ -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, @@ -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