Skip to content

Commit

Permalink
PI-2469: Added external api refdata endpoint (#4220)
Browse files Browse the repository at this point in the history
* PI-2469: Added external api refdata endpoint

* Formatting changes

* PI-2469: Added external api refdata endpoint

---------

Co-authored-by: probation-integration-bot[bot] <177347787+probation-integration-bot[bot]@users.noreply.github.com>
  • Loading branch information
1 parent dbcc4ea commit 31677b2
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class DataLoader(
persist(RegistrationGenerator.SERIOUS_FURTHER_OFFENCE_TYPE)
persist(RegistrationGenerator.generate(RegistrationGenerator.SERIOUS_FURTHER_OFFENCE_TYPE))
persist(RegistrationGenerator.MAPPA_TYPE)
persist(RegistrationGenerator.DATASET_TYPE_OTHER)
persist(RegistrationGenerator.DATASET_TYPE_GENDER)
persist(RegistrationGenerator.REFDATA_MALE)
persist(RegistrationGenerator.REFDATA_FEMALE)
RegistrationGenerator.CATEGORIES.values.forEach(::persist)
RegistrationGenerator.LEVELS.values.forEach(::persist)
persist(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.DATASET_TYPE_OTHER
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integration.delius.entity.*
import uk.gov.justice.digital.hmpps.set
Expand Down Expand Up @@ -38,12 +39,27 @@ object DataGenerator {

val COURT = Court(id = IdGenerator.getAndIncrement(), name = "Manchester Crown Court")
val COURT_APPEARANCE_TYPE =
ReferenceData(id = IdGenerator.getAndIncrement(), code = "SEN", description = "Sentence")
ReferenceData(
id = IdGenerator.getAndIncrement(),
code = "SEN",
description = "Sentence",
dataset = DATASET_TYPE_OTHER
)
val COURT_APPEARANCE_PLEA =
ReferenceData(id = IdGenerator.getAndIncrement(), code = "GLT", description = "Not guilty")
ReferenceData(
id = IdGenerator.getAndIncrement(),
code = "GLT",
description = "Not guilty",
dataset = DATASET_TYPE_OTHER
)
val DISPOSAL_TYPE =
DisposalType(id = IdGenerator.getAndIncrement(), description = "ORA Suspended Sentence Order", "SC")
val MONTHS = ReferenceData(id = IdGenerator.getAndIncrement(), code = "MNTH", description = "Months")
val MONTHS = ReferenceData(
id = IdGenerator.getAndIncrement(),
code = "MNTH",
description = "Months",
dataset = DATASET_TYPE_OTHER
)

val EVENT: Event

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integration.delius.entity.Person
import uk.gov.justice.digital.hmpps.integration.delius.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integration.delius.entity.RegisterType
import uk.gov.justice.digital.hmpps.integration.delius.entity.RegistrationEntity
import uk.gov.justice.digital.hmpps.integration.delius.entity.*
import uk.gov.justice.digital.hmpps.model.Category
import uk.gov.justice.digital.hmpps.model.Level
import java.time.LocalDate
Expand All @@ -13,6 +10,10 @@ object RegistrationGenerator {
val CHILD_PROTECTION_TYPE = generateType(RegisterType.CHILD_PROTECTION_CODE)
val SERIOUS_FURTHER_OFFENCE_TYPE = generateType(RegisterType.SERIOUS_FURTHER_OFFENCE_CODE)
val MAPPA_TYPE = generateType(RegisterType.MAPPA_CODE)
val DATASET_TYPE_GENDER = Dataset(IdGenerator.getAndIncrement(), "GENDER")
val DATASET_TYPE_OTHER = Dataset(IdGenerator.getAndIncrement(), "OTHER")
val REFDATA_MALE = generateReferenceData("M", description = "MALE", dataset = DATASET_TYPE_GENDER)
val REFDATA_FEMALE = generateReferenceData("F", description = "FEMALE", dataset = DATASET_TYPE_GENDER)
val CATEGORIES = Category.entries.map { generateReferenceData(it.name) }.associateBy { it.code }
val LEVELS = Level.entries.map { generateReferenceData(it.name) }.associateBy { it.code }

Expand All @@ -35,6 +36,8 @@ object RegistrationGenerator {
fun generateReferenceData(
code: String,
description: String = "Description of $code",
id: Long = IdGenerator.getAndIncrement()
) = ReferenceData(code, description, id)
dataset: Dataset = DATASET_TYPE_OTHER,
id: Long = IdGenerator.getAndIncrement(),

) = ReferenceData(code, description, dataset, id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.data.generator.DataGenerator.PERSON
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.REFDATA_FEMALE
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.REFDATA_MALE
import uk.gov.justice.digital.hmpps.model.PersonIdentifier
import uk.gov.justice.digital.hmpps.model.ProbationReferenceData
import uk.gov.justice.digital.hmpps.model.RefData
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
import java.time.LocalDate
Expand Down Expand Up @@ -141,4 +145,18 @@ internal class IntegrationTest {
mockMvc.perform(get("/identifier-converter/noms-to-crn/A0001DZ").withToken())
.andExpect(status().isNotFound)
}

@Test
fun `returns reference data for ethnicity, gender and register_types`() {
val response = mockMvc.perform(get("/reference-data").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<ProbationReferenceData>()
Assertions.assertEquals(
response.probationReferenceData.get("GENDER"),
listOf(
RefData(REFDATA_FEMALE.code, REFDATA_FEMALE.description),
RefData(REFDATA_MALE.code, REFDATA_MALE.description)
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.digital.hmpps.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.ReferenceDataService

@RestController
@RequestMapping("/reference-data")
@PreAuthorize("hasRole('PROBATION_API__HMPPS_API__CASE_DETAIL')")
@Tag(name = "Identifier Converter", description = "Requires PROBATION_API__HMPPS_API__CASE_DETAIL")
class ReferenceDataController(
private val referenceDataService: ReferenceDataService
) {
@GetMapping
@Operation(summary = "Gets delius reference data ")
fun refData() = referenceDataService.getReferenceData()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package uk.gov.justice.digital.hmpps.integration.delius.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.Immutable

@Entity
Expand All @@ -17,7 +14,23 @@ data class ReferenceData(
@Column(name = "code_description")
val description: String,

@ManyToOne
@JoinColumn(name = "reference_data_master_id")
val dataset: Dataset,

@Id
@Column(name = "standard_reference_list_id")
val id: Long,
)

@Immutable
@Entity
@Table(name = "r_reference_data_master")
class Dataset(
@Id
@Column(name = "reference_data_master_id")
val id: Long,

@Column(name = "code_set_name")
val code: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate

@Immutable
Expand Down Expand Up @@ -75,12 +76,32 @@ class RegisterType(
}
}

interface RefData {
val codeSet: String
val code: String
val description: String
}

interface RegistrationRepository : JpaRepository<RegistrationEntity, Long> {
@EntityGraph(attributePaths = ["type", "category", "level"])
fun findFirstByPersonIdAndTypeCodeOrderByDateDesc(personId: Long, typeCode: String): RegistrationEntity?

@EntityGraph(attributePaths = ["type", "category", "level"])
fun findByPersonIdAndTypeCodeInOrderByDateDesc(personId: Long, typeCode: List<String>): List<RegistrationEntity>

@Query(
"""
select rdm.code_set_name as codeSet, rdl.code_value as code, rdl.code_description as description
from r_standard_reference_list rdl join r_reference_data_master rdm
on rdm.reference_data_master_id = rdl.reference_data_master_id
where rdm.code_set_name in ('GENDER','ETHNICITY')
union
select 'REGISTER_TYPES' as set_name, rt.code, rt.description
from r_register_type rt
order by codeSet, code
""", nativeQuery = true
)
fun getReferenceData(): List<RefData>
}

fun RegistrationRepository.findMappa(personId: Long) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.justice.digital.hmpps.model

import io.swagger.v3.oas.annotations.media.Schema

data class RefData(
@Schema(description = "reference data code", example = "M")
val code: String,
@Schema(description = "reference data description", example = "MALE")
val description: String
)

data class ProbationReferenceData(
val probationReferenceData: Map<String, List<RefData>>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.gov.justice.digital.hmpps.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.integration.delius.entity.RegistrationRepository
import uk.gov.justice.digital.hmpps.model.ProbationReferenceData
import uk.gov.justice.digital.hmpps.model.RefData

@Service
class ReferenceDataService(
private val registrationRepository: RegistrationRepository,
) {
fun getReferenceData(): ProbationReferenceData =
ProbationReferenceData(
registrationRepository.getReferenceData()
.groupByTo(LinkedHashMap(), { it.codeSet.trim() }, { RefData(it.code.trim(), it.description) })
)
}

0 comments on commit 31677b2

Please sign in to comment.