Skip to content

Commit

Permalink
PI-1650 updated to be the same as what community api does (#2745)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister authored Nov 27, 2023
1 parent 532cd33 commit 5b9c800
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ class DataLoader(
CourtAppearanceGenerator.DEFAULT_EVENT,
CourtAppearanceGenerator.DEFAULT_CA,
ConvictionEventGenerator.PERSON,
ConvictionEventGenerator.OFFENCE_OTHER,
ConvictionEventGenerator.OFFENCE_MAIN,
ConvictionEventGenerator.ADDITIONAL_OFFENCE_TYPE,
ConvictionEventGenerator.OFFENCE_MAIN_TYPE,
ConvictionEventGenerator.DEFAULT_EVENT,
ConvictionEventGenerator.INACTIVE_EVENT,
ConvictionEventGenerator.MAIN_OFFENCE,
ConvictionEventGenerator.OTHER_OFFENCE,
ConvictionEventGenerator.DISPOSAL_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ object ConvictionEventGenerator {
"X012772",
"1231234"
)
val OFFENCE_MAIN = Offence(
val OFFENCE_MAIN_TYPE = Offence(
IdGenerator.getAndIncrement(),
"C01",
"Murder"
)
val OFFENCE_OTHER = Offence(
val ADDITIONAL_OFFENCE_TYPE = Offence(
IdGenerator.getAndIncrement(),
"C02",
"Stealing a kitten"
Expand All @@ -30,15 +30,21 @@ object ConvictionEventGenerator {
LocalDate.now(),
PERSON
)
val INACTIVE_EVENT = ConvictionEventEntity(
IdGenerator.getAndIncrement(),
LocalDate.now(),
PERSON,
active = false
)
val MAIN_OFFENCE = MainOffence(
IdGenerator.getAndIncrement(),
DEFAULT_EVENT,
OFFENCE_MAIN
OFFENCE_MAIN_TYPE
)
val OTHER_OFFENCE = AdditionalOffence(
IdGenerator.getAndIncrement(),
DEFAULT_EVENT,
OFFENCE_OTHER
ADDITIONAL_OFFENCE_TYPE
)
val DISPOSAL_TYPE = DisposalType(
IdGenerator.getAndIncrement(),
Expand All @@ -64,12 +70,12 @@ object ConvictionEventGenerator {
val MAIN_OFFENCE_2 = MainOffence(
IdGenerator.getAndIncrement(),
EVENT_2,
OFFENCE_MAIN
OFFENCE_MAIN_TYPE
)
val OTHER_OFFENCE_2 = AdditionalOffence(
IdGenerator.getAndIncrement(),
EVENT_2,
OFFENCE_OTHER
ADDITIONAL_OFFENCE_TYPE
)
val DISPOSAL_2 = Disposal(
IdGenerator.getAndIncrement(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class ConvictionsIntegrationTest {
.andExpect(status().is2xxSuccessful).andReturn()

val detailResponse = objectMapper.readValue(result.response.contentAsString, ConvictionsContainer::class.java)
Assertions.assertThat(detailResponse).isEqualTo(getConvictions())
Assertions.assertThat(detailResponse).isEqualTo(getConvictions(true))
}

@Test
Expand All @@ -57,20 +57,52 @@ internal class ConvictionsIntegrationTest {
.perform(get("/convictions/$crn?type=CRN").withOAuth2Token(wireMockServer))
.andExpect(status().is2xxSuccessful).andReturn()

val detailResponse = objectMapper.readValue(result.response.contentAsString, ConvictionsContainer::class.java)
Assertions.assertThat(detailResponse).isEqualTo(getConvictions(true))
}

@Test
fun `API call retuns only active convictions success response using CRN`() {
val crn = ConvictionEventGenerator.PERSON.crn
val result = mockMvc
.perform(get("/convictions/$crn?type=CRN&activeOnly=true").withOAuth2Token(wireMockServer))
.andExpect(status().is2xxSuccessful).andReturn()

val detailResponse = objectMapper.readValue(result.response.contentAsString, ConvictionsContainer::class.java)
Assertions.assertThat(detailResponse).isEqualTo(getConvictions())
}

private fun getConvictions(): ConvictionsContainer = ConvictionsContainer(
listOf(
@Test
fun `API call retuns only active convictions success response using NOMS`() {
val noms = ConvictionEventGenerator.PERSON.nomsNumber
val result = mockMvc
.perform(get("/convictions/$noms?type=NOMS&activeOnly=true").withOAuth2Token(wireMockServer))
.andExpect(status().is2xxSuccessful).andReturn()

val detailResponse = objectMapper.readValue(result.response.contentAsString, ConvictionsContainer::class.java)
Assertions.assertThat(detailResponse).isEqualTo(getConvictions())
}

private fun getConvictions(withInActive: Boolean = false): ConvictionsContainer {
val activeConviction =
Conviction(
ConvictionEventGenerator.DEFAULT_EVENT.id,
ConvictionEventGenerator.DEFAULT_EVENT.convictionDate,
ConvictionEventGenerator.DISPOSAL_TYPE.description,
listOf(
Offence(ConvictionEventGenerator.OFFENCE_MAIN.description, true),
Offence(ConvictionEventGenerator.OFFENCE_OTHER.description, false)
Offence(
ConvictionEventGenerator.MAIN_OFFENCE.id,
ConvictionEventGenerator.OFFENCE_MAIN_TYPE.description,
true
),
Offence(
ConvictionEventGenerator.OTHER_OFFENCE.id,
ConvictionEventGenerator.ADDITIONAL_OFFENCE_TYPE.description,
false
)
),
Sentence(
ConvictionEventGenerator.DISPOSAL.id,
ConvictionEventGenerator.DISPOSAL.startDate,
null,
Custody(
Expand All @@ -88,6 +120,18 @@ internal class ConvictionsIntegrationTest {
)
)
)

val inactiveConviction = Conviction(
ConvictionEventGenerator.INACTIVE_EVENT.id,
ConvictionEventGenerator.INACTIVE_EVENT.convictionDate,
"unknown",
listOf(),
null
)
)
return if (withInActive) {
ConvictionsContainer(listOf(activeConviction, inactiveConviction))
} else {
ConvictionsContainer(listOf(activeConviction))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ internal class CourtAppearancesIntegrationTest {
Type(CourtAppearanceGenerator.DEFAULT_CA_TYPE.code, CourtAppearanceGenerator.DEFAULT_CA_TYPE.description),
CourtAppearanceGenerator.DEFAULT_COURT.code,
CourtAppearanceGenerator.DEFAULT_COURT.name,
CourtAppearanceGenerator.DEFAULT_PERSON.crn
CourtAppearanceGenerator.DEFAULT_PERSON.crn,
CourtAppearanceGenerator.DEFAULT_CA.id,
CourtAppearanceGenerator.DEFAULT_PERSON.id
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal class DetailsIntegrationTest {
DetailsGenerator.DISTRICT.description,
DetailsGenerator.DEFAULT_PA.description,
Name(DetailsGenerator.STAFF.forename, DetailsGenerator.STAFF.middleName, DetailsGenerator.STAFF.surname),
ConvictionEventGenerator.OFFENCE_MAIN.description,
ConvictionEventGenerator.OFFENCE_MAIN_TYPE.description,
DetailsGenerator.PERSON.religion?.description,
listOf(KeyDate(KeyDateGenerator.SED_KEYDATE.code, KeyDateGenerator.SED_KEYDATE.description, KeyDateGenerator.KEYDATE.date)),
DetailsGenerator.RELEASE.date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ConvictionController(private val convictionService: ConvictionService) {
@GetMapping(value = ["/convictions/{value}"])
fun convictions(
@PathVariable value: String,
@RequestParam(required = false, defaultValue = "CRN") type: IdentifierType
) = convictionService.getConvictions(value, type)
@RequestParam(required = false, defaultValue = "CRN") type: IdentifierType,
@RequestParam(required = false, defaultValue = "false") activeOnly: Boolean
) = convictionService.getConvictions(value, type, activeOnly)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.time.LocalDate
@Immutable
@Table(name = "event")
@Entity
@Where(clause = "soft_deleted = 0 and active_flag = 1")
@Where(clause = "soft_deleted = 0")
class ConvictionEventEntity(
@Id
@Column(name = "event_id")
Expand Down Expand Up @@ -172,7 +172,7 @@ interface ConvictionEventRepository : JpaRepository<ConvictionEventEntity, Long>
"disposal.type"
]
)
fun getAllByConvictionEventPersonCrn(crn: String): List<ConvictionEventEntity>
fun getAllByConvictionEventPersonIdOrderByConvictionDateDesc(personId: Long): List<ConvictionEventEntity>

@EntityGraph(
attributePaths = [
Expand All @@ -181,7 +181,7 @@ interface ConvictionEventRepository : JpaRepository<ConvictionEventEntity, Long>
"disposal.type"
]
)
fun getAllByConvictionEventPersonIdOrderByConvictionDateDesc(personId: Long): List<ConvictionEventEntity>
fun getAllByConvictionEventPersonCrn(crn: String): List<ConvictionEventEntity>

@EntityGraph(
attributePaths = [
Expand All @@ -191,6 +191,24 @@ interface ConvictionEventRepository : JpaRepository<ConvictionEventEntity, Long>
]
)
fun getAllByConvictionEventPersonNomsNumber(nomsNumber: String): List<ConvictionEventEntity>

@EntityGraph(
attributePaths = [
"mainOffence.offence",
"additionalOffences.offence",
"disposal.type"
]
)
fun getAllByConvictionEventPersonCrnAndActiveIsTrue(crn: String): List<ConvictionEventEntity>

@EntityGraph(
attributePaths = [
"mainOffence.offence",
"additionalOffences.offence",
"disposal.type"
]
)
fun getAllByConvictionEventPersonNomsNumberAndActiveIsTrue(nomsNumber: String): List<ConvictionEventEntity>
}

fun ConvictionEventRepository.getLatestConviction(personId: Long) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package uk.gov.justice.digital.hmpps.model
import java.time.LocalDate

data class Conviction(
val convictionId: Long,
val convictionDate: LocalDate?,
val outcome: String,
val offences: List<Offence>,
val sentence: Sentence?
)

data class Offence(
val offenceId: Long,
val description: String,
val mainOffence: Boolean = false
)
Expand All @@ -19,6 +21,7 @@ data class ConvictionsContainer(
)

data class Sentence(
val sentenceId: Long,
val startDate: LocalDate,
val expectedEndDate: LocalDate?,
val custody: Custody?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ data class CourtAppearance(
val type: Type,
val courtCode: String,
val courtName: String,
val crn: String
val crn: String,
val courtAppearanceId: Long,
val offenderId: Long
)

data class Type(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,49 @@ class ConvictionService(
private val convictionEventRepository: ConvictionEventRepository,
private val custodyRepository: CustodyRepository
) {
fun getConvictions(value: String, type: IdentifierType): ConvictionsContainer {
fun getConvictions(value: String, type: IdentifierType, activeOnly: Boolean): ConvictionsContainer {
val convictions = when (type) {
IdentifierType.CRN -> convictionEventRepository.getAllByConvictionEventPersonCrn(value)
IdentifierType.NOMS -> convictionEventRepository.getAllByConvictionEventPersonNomsNumber(value)
IdentifierType.CRN -> if (activeOnly) {
convictionEventRepository.getAllByConvictionEventPersonCrnAndActiveIsTrue(
value
)
} else {
convictionEventRepository.getAllByConvictionEventPersonCrn(value)
}

IdentifierType.NOMS -> if (activeOnly) {
convictionEventRepository.getAllByConvictionEventPersonNomsNumberAndActiveIsTrue(
value
)
} else {
convictionEventRepository.getAllByConvictionEventPersonNomsNumber(value)
}
}
val convictionModels = mutableListOf<Conviction>()
convictions.map { convictionEventEntity ->
val custody = convictionEventEntity.disposal?.let { custodyRepository.getCustodyByDisposalId(it.id) }
val offences = mutableListOf<Offence>()
offences.add(Offence(convictionEventEntity.mainOffence!!.offence.description, true))
convictionEventEntity.additionalOffences.forEach { offences.add(Offence(it.offence.description, false)) }
convictionEventEntity.mainOffence?.let {
offences.add(
Offence(
convictionEventEntity.mainOffence.id,
convictionEventEntity.mainOffence.offence.description,
true
)
)
}
offences.addAll(
convictionEventEntity.additionalOffences.map {
Offence(
it.id,
it.offence.description,
false
)
}
)
convictionModels.add(
Conviction(
convictionEventEntity.id,
convictionEventEntity.convictionDate,
convictionEventEntity.disposal?.type?.description ?: "unknown",
offences,
Expand All @@ -45,7 +75,7 @@ class ConvictionService(
}

private fun Disposal.asModel(custody: uk.gov.justice.digital.hmpps.entity.Custody?) =
Sentence(startDate, expectedEndDate, custody?.custodyModel())
Sentence(id, startDate, expectedEndDate, custody?.custodyModel())

private fun ReferenceData.custodialStatus() = CustodyStatus(code, description)
private fun uk.gov.justice.digital.hmpps.entity.Custody.custodyModel() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class CourtAppearanceService(private val courtAppearanceRepository: CourtAppeara
Type(it.appearanceType.code, it.appearanceType.description),
it.court.code,
it.court.name,
it.courtAppearanceEventEntity.courtAppearancePerson.crn
it.courtAppearanceEventEntity.courtAppearancePerson.crn,
it.id,
it.courtAppearanceEventEntity.courtAppearancePerson.id
)
)
}
Expand Down

0 comments on commit 5b9c800

Please sign in to comment.