Skip to content

Commit

Permalink
Feature/pi 1939 mas overview offence tab convictions (#3493)
Browse files Browse the repository at this point in the history
* PI-1939 - remove duplicate entity class

* PI-1939 - add new entities and generators

* PI-1939 - refactor and update sql to get conviction information

* PI-1939 - refactor model

* PI-1939 - further refactor

* PI-1939 - add conviction section and tests

* PI-1939 - update integration tests

* PI-1939 - add additional sentences

* PI-1939 - update integration tests

* Formatting changes

* PI-1939 - add additional sentences to Conviction and update tests

* Formatting changes

* PI-1939 - rename repository method

* PI-1939 - remove debug logging frm integration test

* PI-1939 - update service test

* PI-1939 - refactor entity class to make fk fields not null

* Formatting changes

* PI-1939 - apply review comments

* PI-1939 - apply review comments

* PI-1939 - apply review comments

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
achimber-moj and github-actions[bot] authored Mar 19, 2024
1 parent c40b9d6 commit 3de265a
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator
import uk.gov.justice.digital.hmpps.user.AuditUserRepository

Expand Down Expand Up @@ -42,11 +40,26 @@ class DataLoader(
entityManager.persistCollection(PersonGenerator.PERSONAL_CIRCUMSTANCES)
entityManager.persist(PersonGenerator.OVERVIEW)

entityManager.persist(CourtGenerator.BHAM)
entityManager.persist(PersonGenerator.EVENT_1)
entityManager.persist(PersonGenerator.EVENT_2)
entityManager.persist(PersonGenerator.INACTIVE_EVENT_1)
entityManager.persist(PersonGenerator.INACTIVE_EVENT_2)

entityManager.persist(AdditionalSentenceGenerator.REF_DISQ)
entityManager.persist(AdditionalSentenceGenerator.REF_FINE)
entityManager.persist(
AdditionalSentenceGenerator.generateSentence(
3,
null,
null,
PersonGenerator.EVENT_1,
AdditionalSentenceGenerator.REF_DISQ
)
)
entityManager.persist(CourtGenerator.DEFAULT)
entityManager.persist(CourtAppearanceGenerator.generate())

entityManager.persistAll(
PersonGenerator.DEFAULT_DISPOSAL_TYPE,
PersonGenerator.ACTIVE_ORDER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentence

object AdditionalSentenceGenerator {

val REF_DISQ = generateReferenceData("DISQ", "Disqualified from Driving")
val REF_FINE = generateReferenceData("FINE", "Fine")

val SENTENCE_DISQ = generateSentence(length = 3, referenceData = REF_DISQ, event = PersonGenerator.EVENT_1)
val SENTENCE_FINE =
generateSentence(amount = 500, referenceData = REF_FINE, notes = "fine notes", event = PersonGenerator.EVENT_2)

fun generateSentence(
length: Long? = null,
amount: Long? = null,
notes: String? = null,
event: Event,
referenceData: ReferenceData
) = AdditionalSentence(
IdGenerator.getAndIncrement(),
length,
amount,
notes,
false,
event,
referenceData
)

private fun generateReferenceData(code: String, description: String) =
ReferenceData(IdGenerator.getAndIncrement(), code, description)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance
import java.time.LocalDate

object CourtAppearanceGenerator {

fun generate(
court: Court = CourtGenerator.DEFAULT,
date: LocalDate = LocalDate.now().minusMonths(5),
id: Long = IdGenerator.getAndIncrement(),
event: Event = PersonGenerator.EVENT_1
): CourtAppearance {
return CourtAppearance(
id,
date,
court,
event
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court

object CourtGenerator {
val DEFAULT = Court(
IdGenerator.getAndIncrement(),
"Hull Court"
)

val BHAM = Court(
IdGenerator.getAndIncrement(),
"Birmingham Court"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uk.gov.justice.digital.hmpps.data.generator
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator.USER
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
Expand All @@ -11,7 +13,15 @@ import java.time.ZonedDateTime
object PersonGenerator {

val OVERVIEW = generateOverview("X000004")
val EVENT_1 = generateEvent(OVERVIEW, eventNumber = "7654321", notes = "overview", additionalOffences = emptyList())
val OFFENDER_WITHOUT_EVENTS = generateOverview("X000005")
val EVENT_1 = generateEvent(
OVERVIEW,
eventNumber = "7654321",
notes = "overview",
additionalOffences = emptyList(),
court = CourtGenerator.BHAM,
convictionDate = LocalDate.now()
)
val EVENT_2 = generateEvent(
OVERVIEW,
eventNumber = "1234567",
Expand Down Expand Up @@ -103,6 +113,8 @@ object PersonGenerator {
fun generateEvent(
person: Person,
id: Long = IdGenerator.getAndIncrement(),
court: Court? = null,
convictionDate: LocalDate? = null,
eventNumber: String,
active: Boolean = true,
inBreach: Boolean = false,
Expand All @@ -114,6 +126,8 @@ object PersonGenerator {
Event(
id,
person.id,
court,
convictionDate,
eventNumber,
disposal = disposal,
inBreach = inBreach,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import uk.gov.justice.digital.hmpps.api.model.sentence.MainOffence
import uk.gov.justice.digital.hmpps.api.model.sentence.Offence
import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceOverview
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
Expand All @@ -22,6 +20,18 @@ class SentenceIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Test
fun `no active sentences`() {
val response = mockMvc
.perform(MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OFFENDER_WITHOUT_EVENTS.crn}").withToken())
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<SentenceOverview>()

val expected = SentenceOverview(listOf())

assertEquals(expected, response)
}

@Test
fun `get active sentences`() {
val response = mockMvc
Expand All @@ -31,20 +41,31 @@ class SentenceIntegrationTest {

val expected = SentenceOverview(
listOf(
MainOffence(
Offence("Murder", 1),
LocalDate.now(),
"overview",
listOf(
Offence("Burglary", 1),
Offence("Assault", 1)
Sentence(
OffenceDetails(
Offence("Murder", 1),
LocalDate.now(),
"overview",
listOf(
Offence("Burglary", 1),
Offence("Assault", 1)
)
),
Conviction(
"Hull Court",
"Birmingham Court",
LocalDate.now(),
listOf(AdditionalSentence(3, null, null, "Disqualified from Driving"))
)
),
MainOffence(
Offence("Another Murder", 1),
LocalDate.now(),
"overview",
emptyList()
Sentence(
OffenceDetails(
Offence("Another Murder", 1),
LocalDate.now(),
"overview",
emptyList()
),
Conviction(null, null, null, listOf())
)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

data class AdditionalSentence(
val length: Long?,
val amount: Long?,
val notes: String?,
val description: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

import java.time.LocalDate

data class Conviction(
val sentencingCourt: String?,
val responsibleCourt: String?,
val convictionDate: LocalDate?,
val additionalSentences: List<AdditionalSentence>
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package uk.gov.justice.digital.hmpps.api.model.sentence

import java.time.LocalDate

data class MainOffence(
val offence: Offence,
val dateOfOffence: LocalDate,
data class OffenceDetails(
val offence: Offence?,
val dateOfOffence: LocalDate?,
val notes: String?,
val additionalOffences: List<Offence>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

data class Sentence(
val offence: OffenceDetails,
val conviction: Conviction? = null,

)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

data class SentenceOverview(
val offence: List<MainOffence?>

val sentences: List<Sentence>,
)
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.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court
import java.time.LocalDate

@Immutable
Expand All @@ -19,6 +20,13 @@ class Event(
@Column(name = "offender_id")
val personId: Long,

@ManyToOne
@JoinColumn(name = "court_id")
val court: Court? = null,

@Column(name = "conviction_date")
val convictionDate: LocalDate?,

@Column(name = "event_number")
val eventNumber: String,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity

import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData

@Immutable
@Entity
@Table(name = "additional_sentence")
@SQLRestriction("soft_deleted = 0")
class AdditionalSentence(

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

@Column(name = "length")
val length: Long? = null,

@Column(name = "amount")
val amount: Long? = null,

@Column(name = "notes", columnDefinition = "clob")
val notes: String? = null,

@Column(columnDefinition = "number")
val softDeleted: Boolean = false,

@ManyToOne
@JoinColumn(name = "event_id", nullable = false)
val event: Event,

@ManyToOne
@JoinColumn(name = "additional_sentence_type_id", nullable = false)
val type: ReferenceData,

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity

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

@Entity
@Immutable
class Court(

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

@Column(name = "court_name", nullable = true)
val name: String?

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity

import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import java.time.LocalDate

@Entity
@Immutable
@SQLRestriction("soft_deleted = 0")
class CourtAppearance(
@Id
@Column(name = "court_appearance_id")
val id: Long,

@Column(name = "appearance_date")
val date: LocalDate,

@JoinColumn(name = "court_id")
@ManyToOne
val court: Court,

@ManyToOne
@JoinColumn(name = "event_id")
val event: Event,

@Column(name = "soft_deleted", columnDefinition = "NUMBER", nullable = false)
var softDeleted: Boolean = false
)
Loading

0 comments on commit 3de265a

Please sign in to comment.