Skip to content

Commit

Permalink
PI-2097 update model and test
Browse files Browse the repository at this point in the history
Signed-off-by: Amardeep Chimber <[email protected]>
  • Loading branch information
achimber-moj committed Jun 5, 2024
1 parent f63f754 commit 54cd1fe
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import uk.gov.justice.digital.hmpps.api.model.DocumentType
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.user.AuditUserRepository
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
import java.time.ZonedDateTime

@Component
Expand Down Expand Up @@ -113,21 +115,35 @@ class DataLoader(

val noSentenceEvent =
SentenceGenerator.generateEvent(PersonGenerator.NO_SENTENCE, referralDate = LocalDate.now())
val noSentenceManager = SentenceGenerator.generateOrderManager(noSentenceEvent, StaffGenerator.UNALLOCATED)
val noSentenceManager =
SentenceGenerator.generateOrderManager(
noSentenceEvent,
StaffGenerator.UNALLOCATED,
CourtGenerator.PROBATIONARE_AREA,
ZonedDateTime.of(LocalDate.now(), LocalTime.NOON, ZoneId.of("Europe/London")),
ZonedDateTime.of(LocalDate.now().minusDays(1), LocalTime.NOON, ZoneId.of("Europe/London"))
)
val outcome = SentenceGenerator.OUTCOME
val courtAppearance = SentenceGenerator.generateCourtAppearance(noSentenceEvent, outcome, ZonedDateTime.now())
em.saveAll(noSentenceEvent, noSentenceManager, outcome, courtAppearance)

val newEvent = SentenceGenerator.generateEvent(PersonGenerator.NEW_TO_PROBATION, referralDate = LocalDate.now())
val newSentence =
SentenceGenerator.generateSentence(newEvent, LocalDate.now(), DisposalTypeGenerator.CURFEW_ORDER)
val newManager = SentenceGenerator.generateOrderManager(newEvent, StaffGenerator.UNALLOCATED)
val newManager =
SentenceGenerator.generateOrderManager(
newEvent,
StaffGenerator.UNALLOCATED,
CourtGenerator.PROBATIONARE_AREA,
ZonedDateTime.of(LocalDate.now().minusDays(1), LocalTime.NOON, ZoneId.of("Europe/London")),
ZonedDateTime.of(LocalDate.now().minusDays(3), LocalTime.NOON, ZoneId.of("Europe/London"))
)
em.saveAll(newEvent, newSentence, newManager)

val currentEvent = SentenceGenerator.CURRENTLY_MANAGED
val currentSentence = SentenceGenerator.CURRENT_SENTENCE
val custody = SentenceGenerator.CURRENT_CUSTODY
val currentManager = SentenceGenerator.generateOrderManager(currentEvent, StaffGenerator.ALLOCATED)
val currentManager = SentenceGenerator.CURRENT_ORDER_MANAGER
val mainOffence = SentenceGenerator.MAIN_OFFENCE_DEFAULT
val additionalOffence = SentenceGenerator.ADDITIONAL_OFFENCE_DEFAULT
val requirement = SentenceGenerator.generateRequirement(disposal = currentSentence)
Expand Down Expand Up @@ -187,7 +203,14 @@ class DataLoader(
terminationDate = LocalDate.now().minusDays(7),
active = false
)
val preManager = SentenceGenerator.generateOrderManager(preEvent, StaffGenerator.ALLOCATED)
val preManager =
SentenceGenerator.generateOrderManager(
preEvent,
StaffGenerator.ALLOCATED,
CourtGenerator.PROBATIONARE_AREA,
ZonedDateTime.of(LocalDate.now().minusDays(7), LocalTime.NOON, ZoneId.of("Europe/London")),
ZonedDateTime.of(LocalDate.now().minusDays(10), LocalTime.NOON, ZoneId.of("Europe/London"))
)
em.saveAll(preEvent, preSentence, preManager)

em.merge(CourtCaseNoteGenerator.CASE_NOTE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.event.nsi.Nsi
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.ProbationAreaEntity
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
Expand Down Expand Up @@ -55,6 +57,14 @@ object SentenceGenerator {
ZonedDateTime.of(LocalDate.now(), LocalTime.NOON, TIME_ZONE)
)

val CURRENT_ORDER_MANAGER = SentenceGenerator.generateOrderManager(
CURRENTLY_MANAGED,
StaffGenerator.ALLOCATED,
CourtGenerator.PROBATIONARE_AREA,
ZonedDateTime.of(LocalDate.now(), LocalTime.NOON, ZoneId.of("Europe/London")),
ZonedDateTime.of(LocalDate.now().minusDays(3), LocalTime.NOON, ZoneId.of("Europe/London"))
)

val CONDITIONAL_RELEASE_KEY_DATE = generateKeyDates(LocalDate.now(), CURRENT_CUSTODY, ReferenceDataGenerator.ACR)
val LED_KEY_DATE = generateKeyDates(LocalDate.now().plusDays(1), CURRENT_CUSTODY, ReferenceDataGenerator.LED)
val HDC_KEY_DATE = generateKeyDates(LocalDate.now().plusDays(2), CURRENT_CUSTODY, ReferenceDataGenerator.HDE)
Expand Down Expand Up @@ -140,11 +150,15 @@ object SentenceGenerator {

fun generateOrderManager(
event: Event,
staff: Staff,
staff: Staff? = null,
probationArea: ProbationAreaEntity,
allocatedDate: ZonedDateTime,
endDate: ZonedDateTime,
team: Team? = null,
active: Boolean = true,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = OrderManager(event, staff, active, softDeleted, id)
) = OrderManager(event, staff, active, probationArea, team, allocatedDate, endDate, softDeleted, id)

fun generateCourtAppearance(
event: Event,
Expand Down Expand Up @@ -203,15 +217,15 @@ object SentenceGenerator {
offenceCount = 1,
PersonGenerator.CURRENTLY_MANAGED.id,
ZonedDateTime.of(LocalDate.now().minusDays(3), LocalTime.NOON, TIME_ZONE),
LocalDate.now().plusDays(1)
ZonedDateTime.of(LocalDate.now().plusDays(1), LocalTime.NOON, TIME_ZONE),
)

val ADDITIONAL_OFFENCE_DEFAULT = generateAdditionalOffence(
CURRENTLY_MANAGED,
ADDITIONAL_OFFENCE,
LocalDate.now(),
ZonedDateTime.of(LocalDate.now().minusMonths(1), LocalTime.NOON, TIME_ZONE),
LocalDate.now().plusMonths(1),
ZonedDateTime.of(LocalDate.now().plusMonths(1), LocalTime.NOON, TIME_ZONE),
)

fun generateOffence(
Expand Down Expand Up @@ -252,7 +266,7 @@ object SentenceGenerator {
offenceCount: Long,
offenderId: Long,
created: ZonedDateTime,
updated: LocalDate,
updated: ZonedDateTime,
tics: Long? = null,
verdict: String? = null,
id: Long = IdGenerator.getAndIncrement(),
Expand All @@ -264,7 +278,7 @@ object SentenceGenerator {
offence: Offence,
date: LocalDate,
created: ZonedDateTime,
updated: LocalDate,
updated: ZonedDateTime,
id: Long = IdGenerator.getAndIncrement(),
softDeleted: Boolean = false,
offenceCount: Long? = null,
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.integrations.delius.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Officer
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.OfficerPk
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PartitionArea
Expand All @@ -10,7 +11,8 @@ object StaffGenerator {
val UNALLOCATED = generate("N01UATU")
val ALLOCATED = generate("N01ABBA")
val OFFICER = generateOfficer()
fun generate(code: String, id: Long = IdGenerator.getAndIncrement()) = Staff(code, "Bob", "Micheal", "Smith", id)
fun generate(code: String, id: Long = IdGenerator.getAndIncrement(), grade: ReferenceData? = null) =
Staff(code, "Bob", "Micheal", "Smith", grade, id)

fun generateOfficer() =
Officer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ import uk.gov.justice.digital.hmpps.api.model.KeyValue
import uk.gov.justice.digital.hmpps.api.model.conviction.*
import uk.gov.justice.digital.hmpps.data.generator.AdditionalSentenceGenerator.SENTENCE_DISQ
import uk.gov.justice.digital.hmpps.data.generator.CourtGenerator.BHAM
import uk.gov.justice.digital.hmpps.data.generator.CourtGenerator.PROBATIONARE_AREA
import uk.gov.justice.digital.hmpps.data.generator.DisposalTypeGenerator.CURFEW_ORDER
import uk.gov.justice.digital.hmpps.data.generator.InstitutionGenerator.WSIHMP
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator.ADDITIONAL_OFFENCE
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator.COURT_APPEARANCE
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator.CURRENT_ORDER_MANAGER
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator.CURRENT_SENTENCE
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator.MAIN_OFFENCE
import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator.ALLOCATED
import uk.gov.justice.digital.hmpps.data.generator.UnpaidWorkGenerator.UNPAID_WORK_DETAILS_1
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
Expand Down Expand Up @@ -217,6 +220,20 @@ internal class ConvictionIntegrationTest {
COURT_APPEARANCE.court.courtName,
KeyValue(COURT_APPEARANCE.appearanceType.code, COURT_APPEARANCE.appearanceType.description),
COURT_APPEARANCE.person.crn
),
listOf(
OrderManager(
PROBATIONARE_AREA.id,
null,
CURRENT_ORDER_MANAGER.id,
ALLOCATED.getName(),
ALLOCATED.code,
CURRENT_ORDER_MANAGER.allocationDate,
CURRENT_ORDER_MANAGER.endDate,
null,
null,
PROBATIONARE_AREA.code
)
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ data class Conviction(
val latestCourtAppearanceOutcome: KeyValue?,
val custody: Custody? = null,
val responsibleCourt: Court?,
val courtAppearance: CourtAppearanceBasic?
val courtAppearance: CourtAppearanceBasic?,
val orderManagers: List<OrderManager>?
)

data class Offence(
Expand All @@ -33,7 +34,7 @@ data class Offence(
val verdict: String?,
val offenderId: Long,
val createdDatetime: ZonedDateTime,
val lastUpdatedDatetime: LocalDate,
val lastUpdatedDatetime: ZonedDateTime,
)

data class OffenceDetail(
Expand Down Expand Up @@ -159,3 +160,16 @@ data class CourtAppearanceBasic(
val crn: String
)

data class OrderManager(
val probationAreaId: Long,
val teamId: Long?,
val officerId: Long,
val name: String?,
val staffCode: String?,
val dateStartOfAllocation: ZonedDateTime,
val dateEndOfAllocation: ZonedDateTime,
val gradeCode: String?,
val teamCode: String?,
val probationAreaCode: String
)

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import uk.gov.justice.digital.hmpps.integrations.delius.event.courtappearance.en
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Court
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Disposal
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.ProbationAreaEntity
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team
import java.time.LocalDate
import java.time.ZonedDateTime

@Immutable
@Entity
Expand Down Expand Up @@ -63,6 +66,9 @@ class Event(
@OneToMany(mappedBy = "event")
val courtAppearances: List<CourtAppearance> = emptyList(),

@OneToMany(mappedBy = "event")
val orderManagers: List<OrderManager> = emptyList(),

@ManyToOne
@JoinColumn(name = "court_id")
val court: Court?,
Expand Down Expand Up @@ -107,11 +113,23 @@ class OrderManager(

@ManyToOne
@JoinColumn(name = "allocation_staff_id")
val staff: Staff,
val staff: Staff?,

@Column(name = "active_flag", columnDefinition = "number")
val active: Boolean,

@ManyToOne
@JoinColumn(name = "probation_area_id")
val probationArea: ProbationAreaEntity,

@OneToOne
@JoinColumn(name = "allocation_team_id")
val team: Team?,

val allocationDate: ZonedDateTime,

val endDate: ZonedDateTime,

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MainOffence(
val created: ZonedDateTime,

@Column(name = "last_updated_datetime", nullable = false)
val updated: LocalDate,
val updated: ZonedDateTime,

@Column(updatable = false, columnDefinition = "NUMBER")
val softDeleted: Boolean = false
Expand Down Expand Up @@ -78,7 +78,7 @@ class AdditionalOffence(
val created: ZonedDateTime,

@Column(name = "last_updated_datetime", nullable = false)
val updated: LocalDate,
val updated: ZonedDateTime,

@Id
@Column(name = "additional_offence_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package uk.gov.justice.digital.hmpps.integrations.delius.provider.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData

@Immutable
@Entity
Expand All @@ -25,13 +21,24 @@ class Staff(
@Column
val surname: String,

@ManyToOne
@JoinColumn(name = "staff_grade_id")
val grade: ReferenceData?,

@Id
@Column(name = "staff_id")
val id: Long
) {
fun isUnallocated(): Boolean {
return code.endsWith("U")
}

fun getName(): String {
return when {
forename2 == null -> "$forename $surname"
else -> "$forename $forename2 $surname"
}
}
}

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getPerson
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.Offence as OffenceEntity
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.OrderManager as OrderManagerEntity
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.AdditionalSentence as AdditionalSentenceEntity
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Court as CourtEntity
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Custody as CustodyEntity
Expand Down Expand Up @@ -44,7 +45,8 @@ class ConvictionService(
toLatestCourtAppearanceOutcome(),
disposal?.custody?.toCustody(),
court?.toCourt(),
toLatestOrSentencingCourtAppearanceOf()
toLatestOrSentencingCourtAppearanceOf(),
orderManagers.map { it.toOrderManager() }
)

fun Event.toOffences(): List<Offence> {
Expand Down Expand Up @@ -235,6 +237,19 @@ class ConvictionService(
KeyValue(courtType.code, courtType.description),
)

fun OrderManagerEntity.toOrderManager(): OrderManager =
OrderManager(
probationArea.id,
team?.id,
id,
staff?.getName(),
staff?.code,
allocationDate,
endDate,
staff?.grade?.code,
team?.code,
probationArea.code
)
}

enum class KeyDateTypes(val code: String) {
Expand Down

0 comments on commit 54cd1fe

Please sign in to comment.