Skip to content

Commit

Permalink
Bug/pi 2204 approved premises and delius booking cancellation (#3805)
Browse files Browse the repository at this point in the history
* PI-2204 delete approved_premises_preferred record if one exists

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2204 update integration test

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2204 update integration test

Signed-off-by: Amardeep Chimber <[email protected]>

* Formatting changes

* Empty-Commit

---------

Signed-off-by: Amardeep Chimber <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
achimber-moj and github-actions[bot] authored May 17, 2024
1 parent c04de3b commit 740e603
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ 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
Expand Down Expand Up @@ -73,6 +75,9 @@ internal class MessagingIntegrationTest {
@Autowired
private lateinit var residenceRepository: ResidenceRepository

@Autowired
private lateinit var preferredResidenceRepository: PreferredResidenceRepository

@Autowired
private lateinit var staffRepository: StaffRepository

Expand Down Expand Up @@ -442,6 +447,9 @@ internal class MessagingIntegrationTest {

residenceRepository.findByReferralId(ref.id)?.also(residenceRepository::delete)

preferredResidenceRepository.save(PreferredResidence(0, ref.id))
assertTrue(preferredResidenceRepository.existsByApprovedPremisesResidenceId(ref.id))

channelManager.getChannel(queueName).publishAndWait(event)

verify(telemetryService).trackEvent("BookingCancelled", event.message.telemetryProperties())
Expand All @@ -468,5 +476,7 @@ internal class MessagingIntegrationTest {
it.personId == contact.person.id && it.eventId == contact.eventId
}
assertNull(referral)

assertFalse(preferredResidenceRepository.existsByApprovedPremisesResidenceId(ref.id))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity

import jakarta.persistence.*
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository

@Entity
@Table(name = "approved_premises_preferred")
@EntityListeners(AuditingEntityListener::class)
@SequenceGenerator(name = "ap_preferred_id_seq", sequenceName = "ap_preferred_id_seq", allocationSize = 1)
class PreferredResidence(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ap_preferred_id_seq")
@Column(name = "approved_premises_preferred_id")
val id: Long = 0,

val approvedPremisesResidenceId: Long
)

interface PreferredResidenceRepository : JpaRepository<PreferredResidence, Long> {
fun existsByApprovedPremisesResidenceId(approvedPremisesResidenceId: Long): Boolean

fun deleteByApprovedPremisesResidenceId(approvedPremisesResidenceId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ReferralService(
private val staffRepository: StaffRepository,
private val referralRepository: ReferralRepository,
private val residenceRepository: ResidenceRepository,
private val preferredResidenceRepository: PreferredResidenceRepository,
private val personRepository: PersonRepository,
private val eventRepository: EventRepository,
private val registrationRepository: RegistrationRepository,
Expand Down Expand Up @@ -119,6 +120,10 @@ class ReferralService(
val person = personRepository.getByCrn(crn)
val externalReference = Nsi.EXT_REF_BOOKING_PREFIX + details.bookingId
val referral = findReferral(person, externalReference)?.also {
if (preferredResidenceRepository.existsByApprovedPremisesResidenceId(it.id)) {
preferredResidenceRepository.deleteByApprovedPremisesResidenceId(it.id)
}

val residence = residenceRepository.findByReferralId(it.id)
if (residence == null) referralRepository.delete(it)
else throw IgnorableMessageException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,19 @@ import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.check
import org.mockito.kotlin.whenever
import org.springframework.boot.context.event.ApplicationStartedEvent
import uk.gov.justice.digital.hmpps.data.generator.ApprovedPremisesGenerator
import uk.gov.justice.digital.hmpps.data.generator.AssessedByGenerator
import uk.gov.justice.digital.hmpps.data.generator.BookedByGenerator
import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator
import uk.gov.justice.digital.hmpps.data.generator.EventDetailsGenerator
import uk.gov.justice.digital.hmpps.data.generator.IdGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.ProbationAreaGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator
import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator
import uk.gov.justice.digital.hmpps.data.generator.StaffMemberGenerator
import uk.gov.justice.digital.hmpps.data.generator.SubmittedByGenerator
import uk.gov.justice.digital.hmpps.data.generator.TeamGenerator
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.ApplicationAssessed
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.ApplicationSubmitted
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.ApprovedPremisesApiClient
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.AssessedBy
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.BookedBy
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.BookingMade
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.PersonNotArrived
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.SubmittedBy
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.*
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.ApprovedPremisesRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.ApprovedPremises
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.EventRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.MoveOnCategoryRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Referral
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ReferralRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ReferralSourceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ResidenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.alert.ContactAlertRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.outcome.ContactOutcomeRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeRepository
import uk.gov.justice.digital.hmpps.integrations.delius.location.OfficeLocationRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.Nsi
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiStatus
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiStatusCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiStatusRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiType
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiTypeRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.TransferReason
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.TransferReasonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.*
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.address.PersonAddressRepository
Expand Down Expand Up @@ -155,6 +116,9 @@ internal class ApprovedPremisesServiceTest {
@Mock
lateinit var residenceRepository: ResidenceRepository

@Mock
lateinit var preferredResidenceRepository: PreferredResidenceRepository

@Mock
lateinit var eventRepository: EventRepository

Expand Down Expand Up @@ -199,6 +163,7 @@ internal class ApprovedPremisesServiceTest {
staffRepository,
referralRepository,
residenceRepository,
preferredResidenceRepository,
personRepository,
eventRepository,
registrationRepository,
Expand Down

0 comments on commit 740e603

Please sign in to comment.