Skip to content

Commit

Permalink
PI-1701 add referral group to referral (#2852)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj authored Dec 1, 2023
1 parent 7b3b66f commit 0f276b3
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.probation
import uk.gov.justice.digital.hmpps.integrations.delius.person.registration.entity.RegisterType
import uk.gov.justice.digital.hmpps.integrations.delius.person.registration.entity.RegistrationRepository
import uk.gov.justice.digital.hmpps.integrations.delius.probationarea.ProbationArea
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ApGroupLink
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ApGroupLinkId
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.Dataset
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceDataRepository
import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffRepository
Expand All @@ -68,6 +70,7 @@ class DataLoader(
private val registerTypeRepository: RegisterTypeRepository,
private val addressRepository: AddressRepository,
private val approvedPremisesRepository: ApprovedPremisesRepository,
private val apGroupLinkRepository: ApGroupLinkRepository,
private val probationAreaRepository: ProbationAreaRepository,
private val officeLocationRepository: OfficeLocationRepository,
private val staffRepository: StaffRepository,
Expand Down Expand Up @@ -116,6 +119,7 @@ class DataLoader(
)
approvedPremisesRepository.save(ApprovedPremisesGenerator.NO_STAFF)
officeLocationRepository.save(OfficeLocationGenerator.DEFAULT)
apGroupLinkRepository.saveAll(ApprovedPremisesGenerator.AP_GROUP_LINKS)

lduRepository.save(TeamGenerator.AP_TEAM_LDU)
teamRepository.save(TeamGenerator.APPROVED_PREMISES_TEAM)
Expand Down Expand Up @@ -207,7 +211,7 @@ class DataLoader(

interface DatasetRepository : JpaRepository<Dataset, Long>
interface ProbationAreaRepository : JpaRepository<ProbationArea, Long>
interface ApGroupLinkRepository : JpaRepository<ApGroupLink, ApGroupLinkId>
interface AddressRepository : JpaRepository<Address, Long>
interface RegisterTypeRepository : JpaRepository<RegisterType, Long>

interface StaffUserRepository : JpaRepository<StaffUser, Long>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.ApprovedPremises
import uk.gov.justice.digital.hmpps.integrations.delius.location.OfficeLocation
import uk.gov.justice.digital.hmpps.integrations.delius.probationarea.ProbationArea
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ApGroupLink
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ApGroupLinkId
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceData

object ApprovedPremisesGenerator {
val DEFAULT = generate(ReferenceDataGenerator.NHC_Q001, AddressGenerator.Q001)
val NO_STAFF = generate(ReferenceDataGenerator.NHC_Q002, AddressGenerator.Q002)
val AP_GROUP_LINKS = listOf(generateGroupLink(DEFAULT), generateGroupLink(NO_STAFF))

fun generate(
code: ReferenceData,
Expand All @@ -17,6 +20,11 @@ object ApprovedPremisesGenerator {
selectable: Boolean = true,
id: Long = IdGenerator.getAndIncrement()
) = ApprovedPremises(id, code, address, pa, selectable)

fun generateGroupLink(
approvedPremises: ApprovedPremises,
apGroup: ReferenceData = ReferenceDataGenerator.REFERRAL_GROUP
): ApGroupLink = ApGroupLink(ApGroupLinkId(approvedPremises.id, apGroup.id))
}

object OfficeLocationGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object ReferenceDataGenerator {
ALL_DATASETS[DatasetCode.AP_REFERRAL_CATEGORY]!!.id
)
}.associateBy { it.code }
val REFERRAL_GROUP = generate("NE", ALL_DATASETS[DatasetCode.AP_REFERRAL_GROUPING]!!.id, "North East")
val ACCEPTED_DEFERRED_ADMISSION = generate("AD", ALL_DATASETS[DatasetCode.REFERRAL_DECISION]!!.id)
val AP_REFERRAL_SOURCE = generate("AP", ALL_DATASETS[DatasetCode.SOURCE_TYPE]!!.id)
val YN_UNKNOWN = generate("D", ALL_DATASETS[DatasetCode.YES_NO]!!.id)
Expand Down Expand Up @@ -71,8 +72,9 @@ object ReferenceDataGenerator {
code: String,
datasetId: Long,
description: String = "Description of $code",
selectable: Boolean = true,
id: Long = IdGenerator.getAndIncrement()
) = ReferenceData(id, code, description, datasetId)
) = ReferenceData(id, code, description, datasetId, selectable)

fun all(): List<ReferenceData> = listOf(
OWNER_ADDRESS_TYPE,
Expand All @@ -83,6 +85,7 @@ object ReferenceDataGenerator {
NHC_Q002,
STAFF_GRADE,
REFERRAL_DATE_TYPE,
REFERRAL_GROUP,
ACCEPTED_DEFERRED_ADMISSION,
AP_REFERRAL_SOURCE,
YN_UNKNOWN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ object ReferralGenerator {
expectedDepartureDate = expectedDepartureDate,
decisionDate = decisionDate,
categoryId = ReferenceDataGenerator.REFERRAL_CATEGORIES[ApprovedPremisesCategoryCode.OTHER.value]!!.id,
referralGroupId = ReferenceDataGenerator.REFERRAL_GROUP.id,
decisionId = ReferenceDataGenerator.ACCEPTED_DEFERRED_ADMISSION.id,
referralNotes = null,
decisionNotes = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ internal class MessagingIntegrationTest {
referral.categoryId,
equalTo(ReferenceDataGenerator.REFERRAL_CATEGORIES[ApprovedPremisesCategoryCode.VOLUNTARY_MAPPA.value]?.id)
)
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))
assertThat(referral.disabilityIssuesId, equalTo(ReferenceDataGenerator.YN_UNKNOWN.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Referral(
val referralDateTypeId: Long?,
@Column(name = "referral_category_id")
val categoryId: Long,
val referralGroupId: Long?,

val referringTeamId: Long,
val referringStaffId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import jakarta.persistence.AttributeConverter
import jakarta.persistence.Column
import jakarta.persistence.Convert
import jakarta.persistence.Converter
import jakarta.persistence.Embeddable
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import org.hibernate.annotations.Immutable
import org.hibernate.type.YesNoConverter
import java.io.Serializable

@Immutable
@Entity
Expand All @@ -24,7 +27,10 @@ class ReferenceData(
val description: String,

@Column(name = "reference_data_master_id", nullable = false)
val datasetId: Long
val datasetId: Long,

@Convert(converter = YesNoConverter::class)
val selectable: Boolean
)

@Immutable
Expand All @@ -47,6 +53,7 @@ enum class DatasetCode(val value: String) {
AP_NON_ARRIVAL_REASON("AP NON ARRIVAL REASON"),
AP_REFERRAL_CATEGORY("AP REFERRAL CATEGORY"),
AP_REFERRAL_DATE_TYPE("AP REFERRAL DATE TYPE"),
AP_REFERRAL_GROUPING("AP REFERRAL GROUPING"),
ETHNICITY("ETHNICITY"),
GENDER("GENDER"),
GENDER_IDENTITY("GENDER IDENTITY"),
Expand Down Expand Up @@ -75,3 +82,14 @@ class DatasetCodeConverter : AttributeConverter<DatasetCode, String> {

override fun convertToEntityAttribute(dbData: String): DatasetCode = DatasetCode.fromString(dbData)
}

@Embeddable
class ApGroupLinkId(val approvedPremisesId: Long, @Column(name = "standard_reference_list_id") val apGroupId: Long) : Serializable

@Immutable
@Entity
@Table(name = "r_approved_premises_group")
class ApGroupLink(
@Id
val id: ApGroupLinkId
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ interface ReferenceDataRepository : JpaRepository<ReferenceData, Long> {
"""
)
fun findByCodeAndDatasetCode(code: String, datasetCode: DatasetCode): ReferenceData?

@Query(
"""
select rd from ReferenceData rd
join ApGroupLink apg on rd.id = apg.id.apGroupId
where apg.id.approvedPremisesId = :apId
and rd.selectable = true
"""
)
fun findApprovedPremisesGroup(apId: Long): ReferenceData?
}

fun ReferenceDataRepository.findAddressStatusByCode(code: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class ReferralService(
releaseType
).value
).id,
referralGroupId = referenceDataRepository.findApprovedPremisesGroup(ap.id)?.id,
decisionId = referenceDataRepository.acceptedDeferredAdmission().id,
referralNotes = Nsi.EXT_REF_BOOKING_PREFIX + bookingId + System.lineSeparator() + notes,
decisionNotes = notes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ internal class ApprovedPremisesServiceTest {
Nsi.EXT_REF_BOOKING_PREFIX + bookingId,
ReferenceDataGenerator.REFERRAL_DATE_TYPE.id,
ReferenceDataGenerator.REFERRAL_CATEGORIES[ApprovedPremisesCategoryCode.OTHER.value]!!.id,
ReferenceDataGenerator.REFERRAL_GROUP.id,
1, 1,
"Reason",
ReferenceDataGenerator.OTHER_REFERRAL_SOURCE.id,
Expand Down

0 comments on commit 0f276b3

Please sign in to comment.