diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 36edfa35ec..14d127e215 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -10,7 +10,6 @@ import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.audit.BusinessInteraction import uk.gov.justice.digital.hmpps.data.generator.* import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator.COURT_APPEARANCE -import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.GENDER_MALE import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode import uk.gov.justice.digital.hmpps.user.AuditUserRepository @@ -53,7 +52,12 @@ class DataLoader( entityManager.persist(ContactGenerator.LIMITED_ACCESS_USER) - entityManager.persist(GENDER_MALE) + entityManager.persistAll( + PersonGenerator.GENDER_MALE, + PersonGenerator.MAPPA_TYPE, + PersonGenerator.MAPPA_CATEGORY, + PersonGenerator.MAPPA_LEVEL + ) PersonGenerator.DISABILITIES.forEach { entityManager.persist(it.type) } PersonGenerator.PROVISIONS.forEach { entityManager.persist(it.type) } @@ -158,9 +162,11 @@ class DataLoader( PersonGenerator.REQUIREMENT_CONTACT_2, PersonGenerator.REGISTER_TYPE_1, PersonGenerator.REGISTER_TYPE_2, + PersonGenerator.MAPPA_TYPE, PersonGenerator.REGISTRATION_1, PersonGenerator.REGISTRATION_2, PersonGenerator.REGISTRATION_3, + PersonGenerator.MAPPA_REGISTRATION, PersonGenerator.REGISTRATION_REVIEW_1, PersonGenerator.REGISTRATION_REVIEW_2, PersonGenerator.REGISTRATION_REVIEW_3, @@ -168,10 +174,12 @@ class DataLoader( PersonGenerator.MAIN_CAT_F_TYPE, PersonGenerator.MAIN_CAT_W_TYPE, PersonGenerator.NSI_BREACH_TYPE, + PersonGenerator.NSI_OPD_TYPE, PersonGenerator.NSI_STATUS, PersonGenerator.BREACH_PREVIOUS_ORDER_1, PersonGenerator.BREACH_PREVIOUS_ORDER_2, PersonGenerator.BREACH_ON_ACTIVE_ORDER, + PersonGenerator.OPD_NSI, UnpaidWorkApptGenerator.UNPAID_WORK_DETAILS_1, UnpaidWorkApptGenerator.APPT1, UnpaidWorkApptGenerator.APPT2, diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index c0d760977d..e0bed4b008 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -21,10 +21,13 @@ import java.time.LocalDate import java.time.LocalDateTime import java.time.ZoneId import java.time.ZonedDateTime +import java.time.temporal.ChronoUnit object PersonGenerator { val GENDER_MALE = ReferenceData(IdGenerator.getAndIncrement(), "M", "Male") + val MAPPA_CATEGORY = ReferenceData(IdGenerator.getAndIncrement(), "X9", "X9 Desc") + val MAPPA_LEVEL = ReferenceData(IdGenerator.getAndIncrement(), "M2", "M2 Desc") val OVERVIEW = generateOverview("X000004") val EVENT_1 = generateEvent( OVERVIEW, @@ -156,9 +159,12 @@ object PersonGenerator { val REGISTER_TYPE_1 = generateRegisterType("CODE1", "Restraining Order") val REGISTER_TYPE_2 = generateRegisterType("CODE2", "Domestic Abuse Perpetrator") - val REGISTRATION_1 = generateRegistration(REGISTER_TYPE_1, OVERVIEW.id, "Notes") - val REGISTRATION_2 = generateRegistration(REGISTER_TYPE_2, OVERVIEW.id, "Notes") - val REGISTRATION_3 = generateRegistration(REGISTER_TYPE_2, OVERVIEW.id, "Notes", deRegistered = true) + val MAPPA_TYPE = generateRegisterType("MAPP", "Mappa") + val REGISTRATION_1 = generateRegistration(REGISTER_TYPE_1, null, OVERVIEW.id, "Notes") + val REGISTRATION_2 = generateRegistration(REGISTER_TYPE_2, null, OVERVIEW.id, "Notes") + val REGISTRATION_3 = generateRegistration(REGISTER_TYPE_2, null, OVERVIEW.id, "Notes", deRegistered = true) + + val MAPPA_REGISTRATION = generateRegistration(MAPPA_TYPE, MAPPA_CATEGORY, OVERVIEW.id, "Notes", level = MAPPA_LEVEL) val REGISTRATION_REVIEW_1 = generateRiskReview( REGISTRATION_2, LocalDate.now().minusDays(4), @@ -460,13 +466,17 @@ object PersonGenerator { fun generateRegistration( type: RegisterType, + category: ReferenceData? = null, personId: Long, notes: String?, id: Long = IdGenerator.getAndIncrement(), - deRegistered: Boolean = false + deRegistered: Boolean = false, + date: LocalDate = LocalDate.now(), + level: ReferenceData? = null, ) = RiskFlag( personId, type, + category, deRegistered, notes, LocalDate.now(), @@ -474,6 +484,8 @@ object PersonGenerator { USER, LocalDate.now().plusDays(1), emptyList(), + date, + level, false, id ) @@ -495,11 +507,14 @@ object PersonGenerator { ) = RegistrationReview(riskFlag, date, reviewDue, notes, true, false, createdDate, IdGenerator.getAndIncrement()) val NSI_BREACH_TYPE = generateNsiType("BRE") + val NSI_OPD_TYPE = generateNsiType("OPD1") val NSI_STATUS = generateNsiStatus("STATUS1", "An NSI Status") val BREACH_PREVIOUS_ORDER_1 = generateNsi(OVERVIEW.id, INACTIVE_ORDER_1.event.id, NSI_BREACH_TYPE, NSI_STATUS) val BREACH_PREVIOUS_ORDER_2 = generateNsi(OVERVIEW.id, INACTIVE_ORDER_2.event.id, NSI_BREACH_TYPE, NSI_STATUS) val BREACH_ON_ACTIVE_ORDER = generateNsi(OVERVIEW.id, ACTIVE_ORDER.event.id, NSI_BREACH_TYPE, NSI_STATUS) + val OPD_NSI = generateNsi(OVERVIEW.id, ACTIVE_ORDER.event.id, NSI_OPD_TYPE, NSI_STATUS) + fun generateNsiType(code: String) = NsiType(id = IdGenerator.getAndIncrement(), code = code) fun generateNsiStatus(code: String, description: String) = NsiStatus(id = IdGenerator.getAndIncrement(), code = code, description = description) @@ -508,7 +523,8 @@ object PersonGenerator { personId: Long, eventId: Long, type: NsiType, - status: NsiStatus + status: NsiStatus, + active: Boolean = true ) = Nsi( id = IdGenerator.getAndIncrement(), @@ -517,7 +533,9 @@ object PersonGenerator { expectedStartDate = LocalDate.now().minusDays(5), eventId = eventId, type = type, - nsiStatus = status + nsiStatus = status, + lastUpdated = ZonedDateTime.now().truncatedTo(ChronoUnit.SECONDS), + active = active ) fun generateCaseload(caseLoadPerson: CaseloadPerson, staff: Staff, team: Team) = diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/OverviewIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/OverviewIntegrationTest.kt index 76068d88f0..02e5efaa0e 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/OverviewIntegrationTest.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/OverviewIntegrationTest.kt @@ -83,7 +83,7 @@ internal class OverviewIntegrationTest { assertThat(res.sentences[1].eventNumber, equalTo(EVENT_1.eventNumber)) assertThat(res.personalDetails.dateOfBirth, equalTo(OVERVIEW.dateOfBirth)) assertThat(res.personalDetails.dateOfBirth, equalTo(OVERVIEW.dateOfBirth)) - assertThat(res.registrations, equalTo(listOf("Restraining Order", "Domestic Abuse Perpetrator"))) + assertThat(res.registrations, equalTo(listOf("Restraining Order", "Domestic Abuse Perpetrator", "Mappa"))) } @Test diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RiskFlagIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RiskFlagIntegrationTest.kt index c425176d3a..1bd3a0e696 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RiskFlagIntegrationTest.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RiskFlagIntegrationTest.kt @@ -14,6 +14,7 @@ import uk.gov.justice.digital.hmpps.api.model.risk.PersonRiskFlag import uk.gov.justice.digital.hmpps.api.model.risk.PersonRiskFlags import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.DEREGISTRATION_1 import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.PERSON_2 import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.REGISTRATION_2 import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.REGISTRATION_3 import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.REGISTRATION_REVIEW_2 @@ -37,7 +38,10 @@ internal class RiskFlagIntegrationTest { .andExpect(status().isOk) .andReturn().response.contentAsJson() assertThat(res.personSummary.crn, equalTo(person.crn)) - assertThat(res.riskFlags.size, equalTo(2)) + assertThat(res.mappa?.level, equalTo(2)) + assertThat(res.mappa?.category, equalTo(0)) + assertThat(res.opd?.eligible, equalTo(true)) + assertThat(res.riskFlags.size, equalTo(3)) assertThat(res.riskFlags[1].description, equalTo(REGISTRATION_2.type.description)) assertThat(res.riskFlags[1].mostRecentReviewDate, equalTo(REGISTRATION_REVIEW_2.date)) assertThat(res.removedRiskFlags.size, equalTo(1)) @@ -52,6 +56,18 @@ internal class RiskFlagIntegrationTest { ) } + @Test + fun `opd and mappa is not returned`() { + val person = PERSON_2 + val res = mockMvc + .perform(get("/risk-flags/${person.crn}").withToken()) + .andExpect(status().isOk) + .andReturn().response.contentAsJson() + assertThat(res.personSummary.crn, equalTo(person.crn)) + assertThat(res.mappa, equalTo(null)) + assertThat(res.opd, equalTo(null)) + } + @Test fun `individual risk flag is returned`() { diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/MappaDetail.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/MappaDetail.kt new file mode 100644 index 0000000000..c25201a90d --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/MappaDetail.kt @@ -0,0 +1,12 @@ +package uk.gov.justice.digital.hmpps.api.model.risk + +import java.time.LocalDate + +data class MappaDetail( + val level: Int? = null, + val levelDescription: String? = null, + val category: Int? = null, + val categoryDescription: String? = null, + val startDate: LocalDate, + val reviewDate: LocalDate? = null, +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/Opd.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/Opd.kt new file mode 100644 index 0000000000..90549e68dc --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/Opd.kt @@ -0,0 +1,8 @@ +package uk.gov.justice.digital.hmpps.api.model.risk + +import java.time.ZonedDateTime + +data class Opd( + val eligible: Boolean, + val date: ZonedDateTime, +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/PersonRiskFlags.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/PersonRiskFlags.kt index 1b4fb15b42..a088c6dbce 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/PersonRiskFlags.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/risk/PersonRiskFlags.kt @@ -4,6 +4,8 @@ import uk.gov.justice.digital.hmpps.api.model.PersonSummary data class PersonRiskFlags( val personSummary: PersonSummary, + val opd: Opd? = null, + val mappa: MappaDetail? = null, val riskFlags: List, val removedRiskFlags: List = emptyList(), ) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/compliance/Nsi.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/compliance/Nsi.kt index b8512e04d6..3ea7a4f367 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/compliance/Nsi.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/compliance/Nsi.kt @@ -5,6 +5,7 @@ import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction import org.springframework.data.jpa.repository.JpaRepository import java.time.LocalDate +import java.time.ZonedDateTime @Immutable @Entity @@ -36,6 +37,9 @@ class Nsi( @Column(name = "nsi_id") val id: Long = 0, + @Column(name = "last_updated_datetime") + val lastUpdated: ZonedDateTime, + @Column(name = "active_flag", columnDefinition = "number") val active: Boolean = true, @@ -63,7 +67,17 @@ class NsiStatus( @Immutable @Entity @Table(name = "r_nsi_type") -class NsiType(val code: String, @Id @Column(name = "nsi_type_id") val id: Long) +class NsiType( + + val code: String, + + @Id + @Column(name = "nsi_type_id") val id: Long +) { + enum class Code(val value: String) { + OPD_COMMUNITY_PATHWAY("OPD1") + } +} interface NsiRepository : JpaRepository { diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/risk/Registration.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/risk/Registration.kt index 5159405673..5bcf7cb254 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/risk/Registration.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/risk/Registration.kt @@ -6,9 +6,13 @@ import org.hibernate.annotations.FetchMode import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction import org.hibernate.type.YesNoConverter +import org.springframework.data.domain.Page +import org.springframework.data.domain.Pageable import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query import uk.gov.justice.digital.hmpps.exception.NotFoundException import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.RegisterType +import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.User import java.time.LocalDate @@ -28,6 +32,10 @@ class RiskFlag( @JoinColumn(name = "register_type_id") val type: RegisterType, + @ManyToOne + @JoinColumn(name = "register_category_id") + val category: ReferenceData?, + @Column(name = "deregistered", columnDefinition = "number") val deRegistered: Boolean, @@ -51,6 +59,13 @@ class RiskFlag( @OneToMany(mappedBy = "registration") val reviews: List = emptyList(), + @Column(name = "registration_date") + val date: LocalDate, + + @ManyToOne + @JoinColumn(name = "register_level_id") + val level: ReferenceData?, + @Column(name = "soft_deleted", columnDefinition = "number") val softDeleted: Boolean, @@ -62,6 +77,18 @@ class RiskFlag( interface RiskFlagRepository : JpaRepository { fun findByPersonId(personId: Long): List fun findByPersonIdAndId(personId: Long, id: Long): RiskFlag? + + @Query( + """ + select r from RiskFlag r + where r.type.code = 'MAPP' + and r.personId = :offenderId + and r.softDeleted = false + and r.deRegistered = false + order by r.date desc + """ + ) + fun findActiveMappaRegistrationByOffenderId(offenderId: Long, pageable: Pageable): Page } fun RiskFlagRepository.getRiskFlag(personId: Long, id: Long): RiskFlag = diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt index db9bb24696..cdca1096e3 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt @@ -1,12 +1,13 @@ package uk.gov.justice.digital.hmpps.service +import org.springframework.data.domain.PageRequest import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.api.model.Name -import uk.gov.justice.digital.hmpps.api.model.risk.PersonRiskFlag -import uk.gov.justice.digital.hmpps.api.model.risk.PersonRiskFlags -import uk.gov.justice.digital.hmpps.api.model.risk.RiskFlag -import uk.gov.justice.digital.hmpps.api.model.risk.RiskFlagRemoval +import uk.gov.justice.digital.hmpps.api.model.risk.* +import uk.gov.justice.digital.hmpps.integrations.delius.compliance.Nsi +import uk.gov.justice.digital.hmpps.integrations.delius.compliance.NsiRepository +import uk.gov.justice.digital.hmpps.integrations.delius.compliance.NsiType import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.getSummary import uk.gov.justice.digital.hmpps.integrations.delius.risk.DeRegistration @@ -16,7 +17,8 @@ import uk.gov.justice.digital.hmpps.integrations.delius.risk.getRiskFlag @Service class RiskService( private val personRepository: PersonRepository, - private val riskFlagRepository: RiskFlagRepository + private val riskFlagRepository: RiskFlagRepository, + private val nsiRepository: NsiRepository ) { @Transactional @@ -33,14 +35,44 @@ class RiskService( fun getPersonRiskFlags(crn: String): PersonRiskFlags { val summary = personRepository.getSummary(crn) val riskFlags = riskFlagRepository.findByPersonId(summary.id) + val opd = nsiRepository.findByPersonIdAndTypeCode(summary.id, NsiType.Code.OPD_COMMUNITY_PATHWAY.value) + .firstOrNull(Nsi::active) + val mappa = riskFlagRepository.findActiveMappaRegistrationByOffenderId(summary.id, PageRequest.of(0, 1)) + .firstOrNull() + return PersonRiskFlags( personSummary = summary.toPersonSummary(), + mappa = mappa?.toMappa(), + opd = opd?.let { Opd(eligible = true, date = opd.lastUpdated) }, riskFlags = riskFlags.filter { !it.deRegistered }.map { it.toRiskFlag() }, removedRiskFlags = riskFlags.filter { it.deRegistered }.map { it.toRiskFlag() } ) } } +enum class MappaLevel(val communityValue: Int, val deliusValue: String) { + NOMINAL(0, "M0"), + ONE(1, "M1"), + TWO(2, "M2"), + THREE(3, "M3"); + + companion object { + fun toCommunityLevel(deliusLevel: String): Int { + return entries.firstOrNull { level: MappaLevel -> level.deliusValue == deliusLevel }?.communityValue + ?: NOMINAL.communityValue + } + } +} + +fun uk.gov.justice.digital.hmpps.integrations.delius.risk.RiskFlag.toMappa() = MappaDetail( + level = level?.code?.let { code -> MappaLevel.toCommunityLevel(code) }, + levelDescription = level?.description, + category = category?.code?.let { code -> MappaCategory.toCommunityCategory(code) }, + categoryDescription = category?.description, + startDate = date, + reviewDate = nextReviewDate +) + fun uk.gov.justice.digital.hmpps.integrations.delius.risk.RiskFlag.toRiskFlag() = RiskFlag( id = id, description = type.description, @@ -57,4 +89,19 @@ fun DeRegistration.toRiskFlagRemoval() = RiskFlagRemoval( notes = notes, removedBy = Name(forename = staff.forename, surname = staff.surname), removalDate = deRegistrationDate -) \ No newline at end of file +) + +internal enum class MappaCategory(val communityValue: Int, val deliusValue: String) { + NOMINAL(0, "X9"), + ONE(1, "M1"), + TWO(2, "M2"), + THREE(3, "M3"), + FOUR(4, "M4"); + + companion object { + fun toCommunityCategory(deliusCategory: String): Int { + return entries.firstOrNull { category: MappaCategory -> category.deliusValue == deliusCategory }?.communityValue + ?: NOMINAL.communityValue + } + } +} diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/RiskServiceTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/RiskServiceTest.kt index 75303db1e5..c3c5a34690 100644 --- a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/RiskServiceTest.kt +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/RiskServiceTest.kt @@ -10,9 +10,16 @@ import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension import org.mockito.kotlin.any import org.mockito.kotlin.whenever +import org.springframework.data.domain.Page +import org.springframework.data.domain.PageImpl +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.PersonGenerator.MAPPA_TYPE +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.generateRegistration import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.PERSONAL_DETAILS +import uk.gov.justice.digital.hmpps.integrations.delius.compliance.NsiRepository import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository +import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData import uk.gov.justice.digital.hmpps.integrations.delius.risk.RiskFlagRepository import uk.gov.justice.digital.hmpps.utils.Summary @@ -25,6 +32,9 @@ internal class RiskServiceTest { @Mock lateinit var riskFlagRepository: RiskFlagRepository + @Mock + lateinit var nsiRepository: NsiRepository + @InjectMocks lateinit var service: RiskService @@ -50,6 +60,9 @@ internal class RiskServiceTest { ) whenever(personRepository.findSummary(crn)).thenReturn(personSummary) whenever(riskFlagRepository.findByPersonId(any())).thenReturn(expectedRiskFlags) + whenever(nsiRepository.findByPersonIdAndTypeCode(any(), any())).thenReturn(emptyList()) + whenever(riskFlagRepository.findActiveMappaRegistrationByOffenderId(any(), any())).thenReturn(Page.empty()) + val res = service.getPersonRiskFlags(crn) assertThat( res.personSummary, equalTo(PERSONAL_DETAILS.toSummary()) @@ -57,6 +70,35 @@ internal class RiskServiceTest { assertThat(res.riskFlags, equalTo(expectedRiskFlags.map { it.toRiskFlag() })) } + @Test + fun `returns nominal community level and category for mappa`() { + val crn = "X000005" + val expectedRiskFlags = listOf( + PersonGenerator.REGISTRATION_1, + PersonGenerator.REGISTRATION_2 + ) + + val otherMappaLevel = ReferenceData(IdGenerator.getAndIncrement(), "OT", "Other Desc") + val otherMappaCategory = ReferenceData(IdGenerator.getAndIncrement(), "OT", "Other Desc") + val mappaRegistration = + generateRegistration(MAPPA_TYPE, otherMappaCategory, 1L, "Notes", level = otherMappaLevel) + + whenever(personRepository.findSummary(crn)).thenReturn(personSummary) + whenever(riskFlagRepository.findByPersonId(any())).thenReturn(expectedRiskFlags) + whenever(nsiRepository.findByPersonIdAndTypeCode(any(), any())).thenReturn(emptyList()) + whenever(riskFlagRepository.findActiveMappaRegistrationByOffenderId(any(), any())).thenReturn( + PageImpl( + listOf( + mappaRegistration + ) + ) + ) + + val res = service.getPersonRiskFlags(crn) + assertThat(res.mappa?.level, equalTo(MappaLevel.NOMINAL.communityValue)) + assertThat(res.mappa?.category, equalTo(MappaLevel.NOMINAL.communityValue)) + } + @Test fun `calls get risk function`() { val crn = "X000005"