Skip to content

Commit

Permalink
PI-2586 - pre-sentence event creation
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-bcl committed Nov 26, 2024
1 parent c61e5c2 commit 118ae1c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ object EventGenerator {
person = person,
number = "1",
referralDate = LocalDate.now(),
active = true
active = true,
ftcCount = 0
)
}

Expand Down Expand Up @@ -56,7 +57,11 @@ object ContactGenerator {
person = person,
startTime = ZonedDateTime.now(),
endTime = ZonedDateTime.now().plusDays(1),
type = type
type = type,
staff = StaffGenerator.UNALLOCATED,
staffEmployeeId = StaffGenerator.UNALLOCATED.id,
team = TeamGenerator.UNALLOCATED,
providerEmployeeId = TeamGenerator.UNALLOCATED.id
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Contact(
@Column(name = "provider_employee_id")
val providerEmployeeId: Long? = null,

@Column(name = "hours_credited")
@Column(name = "hours_credited", columnDefinition = "NUMBER(10,2)")
var hoursCredited: Double? = null,

@Column(name = "notes", columnDefinition = "clob")
Expand Down Expand Up @@ -126,7 +126,7 @@ class Contact(
@Column
val contactOutcomeTypeId: Long? = null,

@Column(nullable = false)
@Column
@CreatedBy
var createdByUserId: Long = 0,

Expand All @@ -141,7 +141,7 @@ class Contact(
val trustProviderFlag: Boolean = false,

@Column
val staffEmployeeId: Long? = null,
val staffEmployeeId: Long,

@Column
val probationAreaId: Long? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class CourtAppearance(
@Column(name = "soft_deleted", columnDefinition = "number")
val softDeleted: Boolean,

@Column(name = "partition_area_id")
val partitionAreaId: Long? = null,
@Column
val partitionAreaId: Long = 0L,

@ManyToOne
@JoinColumn(name = "court_id")
Expand All @@ -72,18 +72,18 @@ class CourtAppearance(
@JoinColumn(name = "remand_status_id")
val remandStatus: ReferenceData? = null,

@Column(nullable = false)
@CreatedBy
@Column(name = "created_by_user_id", updatable = false)
var createdByUserId: Long = 0,

@Column(name = "last_updated_user_id")
@LastModifiedBy
var lastUpdatedUserId: Long = 0,

@CreatedDate
@Column(nullable = false)
var createdDatetime: ZonedDateTime = ZonedDateTime.now(),

@LastModifiedBy
@Column(name = "last_updated_user_id")
var lastModifiedUserId: Long = 0,

@Column(nullable = false)
@LastModifiedDate
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class Event(
val id: Long? = null,

@Column(name = "consecutive_to_event_id")
val consecutiveToId: Long? = null,
val consecutiveId: Long? = null,

@Column(name = "consecutive_with_event_id")
val consecutiveWithId: Long? = null,
@Column(name = "concurrent_with_event_id")
val concurrentId: Long? = null,

@ManyToOne
@JoinColumn(name = "offender_id", nullable = false)
Expand Down Expand Up @@ -77,7 +77,7 @@ class Event(
val breachEnd: LocalDate? = null,

@Column
val ftcCount: Long? = null,
val ftcCount: Long,

@Column(columnDefinition = "NUMBER", nullable = false)
val pendingTransfer: Boolean = false,
Expand Down Expand Up @@ -137,19 +137,21 @@ class OrderManager(
val providerEmployeeId: Long? = null,

@CreatedDate
val createdDateTime: ZonedDateTime = ZonedDateTime.now(),

@CreatedBy
var createdByUserId: Long = 0,
val createdDatetime: ZonedDateTime = ZonedDateTime.now(),

@LastModifiedDate
var lastModifiedDate: ZonedDateTime = ZonedDateTime.now(),
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now(),

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

@Column
@CreatedBy
var createdByUserId: Long = 0,

@Column(name = "last_updated_user_id")
@LastModifiedBy
var lastModifiedUserId: Long = 0,
var lastUpdatedUserId: Long = 0,

@ManyToOne
@JoinColumn(name = "transfer_reason_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ class MainOffence(
@JoinColumn(name = "offender_id", nullable = false)
val person: Person,

@Column
@CreatedBy
@Column(name = "created_by_user_id")
var createdByUserId: Long = 0,

@Column(name = "last_updated_user_id")
@LastModifiedBy
var lastUpdatedUserId: Long = 0,

@CreatedDate
@Column(name = "created_datetime")
var createdDateTime: ZonedDateTime = ZonedDateTime.now(),

@LastModifiedBy
@Column
var lastUpdatedUserId: Long = 0,

@Column
@LastModifiedDate
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ class Handler(
}

// Insert each defendant as a person record, send relevant SNS messages
val savedEntities = personService.insertPerson(defendant, courtCode)
notifier.caseCreated(savedEntities.person)
savedEntities.address?.let { notifier.addressCreated(it) }
val savedPersonEntities = personService.insertPerson(defendant, courtCode)
notifier.caseCreated(savedPersonEntities.person)
savedPersonEntities.address?.let { notifier.addressCreated(it) }

telemetryService.trackEvent(
"PersonCreated", mapOf(
"hearingId" to hearing.id,
"CRN" to savedEntities.person.crn,
"personId" to savedEntities.person.id.toString(),
"personManagerId" to savedEntities.personManager.id.toString(),
"equalityId" to savedEntities.equality.id.toString(),
"addressId" to savedEntities.address?.id.toString()
"CRN" to savedPersonEntities.person.crn,
"personId" to savedPersonEntities.person.id.toString(),
"personManagerId" to savedPersonEntities.personManager.id.toString(),
"equalityId" to savedPersonEntities.equality.id.toString(),
"addressId" to savedPersonEntities.address?.id.toString()
)
)

Expand All @@ -84,20 +84,41 @@ class Handler(
val existingCourtAppearance = courtAppearanceRepository.findLatestByCaseUrn(caseUrn)

if (existingCourtAppearance != null) {
eventService.insertCourtAppearance(
val savedCourtAppearance = eventService.insertCourtAppearance(
existingCourtAppearance.event,
courtCode,
hearing.hearingDays.first().sittingDay,
caseUrn
)
telemetryService.trackEvent(
"CourtAppearanceCreated", mapOf(
"hearingId" to hearing.id,
"courtAppearanceId" to savedCourtAppearance.id.toString(),
"personId" to savedCourtAppearance.person.id.toString(),
"eventId" to savedCourtAppearance.event.id.toString(),
)
)
} else {
eventService.insertEvent(
val savedEventEntities = eventService.insertEvent(
mainOffence,
savedEntities.person,
savedPersonEntities.person,
courtCode,
hearing.hearingDays.first().sittingDay,
caseUrn
)
telemetryService.trackEvent(
"EventCreated", mapOf(
"hearingId" to hearing.id,
"eventId" to savedEventEntities.event.id.toString(),
"eventNumber" to savedEventEntities.event.number,
"CRN" to savedEventEntities.event.person.crn,
"personId" to savedEventEntities.event.person.id.toString(),
"orderManagerId" to savedEventEntities.orderManager.id.toString(),
"mainOffenceId" to savedEventEntities.mainOffence.id.toString(),
"courtAppearanceIds" to savedEventEntities.courtAppearances.joinToString(",") { it.id.toString() },
"contactIds" to savedEventEntities.contacts.joinToString(",") { it.id.toString() }
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class EventService(
person = person,
number = eventRepository.getNextEventNumber(person.id!!),
referralDate = sittingDay.toLocalDate(), // TODO: Identify event's referral date
active = true
active = true,
ftcCount = 0
)
)

Expand Down Expand Up @@ -139,14 +140,14 @@ class EventService(
person = person,
startTime = sittingDay.with(LocalDate.of(1970, 1, 1)),
endTime = null,
staff = unallocatedStaff,
team = unallocatedTeam,
alert = true,
eventId = savedEvent.id!!,
type = courtAppearanceContactType,
staffEmployeeId = unallocatedStaff.id,
probationAreaId = court.provider.id,
trustProviderTeamId = unallocatedTeam.id
team = unallocatedTeam,
trustProviderTeamId = unallocatedTeam.id,
staff = unallocatedStaff,
staffEmployeeId = unallocatedStaff.id
)
)

Expand All @@ -160,9 +161,11 @@ class EventService(
alert = true,
eventId = savedEvent.id,
type = courtAppearanceContactType,
staffEmployeeId = unallocatedStaff.id,
probationAreaId = court.provider.id,
trustProviderTeamId = unallocatedTeam.id
team = unallocatedTeam,
trustProviderTeamId = unallocatedTeam.id,
staff = unallocatedStaff,
staffEmployeeId = unallocatedStaff.id
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.entity.CourtAppearanceRe
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Equality
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.service.EventService
import uk.gov.justice.digital.hmpps.service.InsertEventResult
import uk.gov.justice.digital.hmpps.service.InsertPersonResult
import uk.gov.justice.digital.hmpps.service.PersonService
import uk.gov.justice.digital.hmpps.telemetry.TelemetryMessagingExtensions.notificationReceived
Expand Down Expand Up @@ -65,6 +66,16 @@ internal class HandlerTest {
)
)

whenever(eventService.insertEvent(any(), any(), any(), any(), any())).thenReturn(
InsertEventResult(
EventGenerator.DEFAULT,
MainOffenceGenerator.DEFAULT,
listOf(CourtAppearanceGenerator.TRIAL_ADJOURNMENT, CourtAppearanceGenerator.TRIAL_ADJOURNMENT),
listOf(ContactGenerator.EAPP, ContactGenerator.EAPP),
OrderManagerGenerator.DEFAULT
)
)

val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT)
handler.handle(notification)
verify(telemetryService).notificationReceived(notification)
Expand Down Expand Up @@ -129,7 +140,7 @@ internal class HandlerTest {

@Test
fun `Inserts event when case urn does not exist`() {
whenever(courtAppearanceRepository.findLatestByCaseUrn(any())).thenReturn(CourtAppearanceGenerator.TRIAL_ADJOURNMENT)
whenever(courtAppearanceRepository.findLatestByCaseUrn(any())).thenReturn(null)

whenever(personService.insertPerson(any(), any())).thenReturn(
InsertPersonResult(
Expand All @@ -140,6 +151,16 @@ internal class HandlerTest {
)
)

whenever(eventService.insertEvent(any(), any(), any(), any(), any())).thenReturn(
InsertEventResult(
EventGenerator.DEFAULT,
MainOffenceGenerator.DEFAULT,
listOf(CourtAppearanceGenerator.TRIAL_ADJOURNMENT, CourtAppearanceGenerator.TRIAL_ADJOURNMENT),
listOf(ContactGenerator.EAPP, ContactGenerator.EAPP),
OrderManagerGenerator.DEFAULT
)
)

whenever(probationSearchClient.match(any())).thenReturn(
ProbationMatchResponse(
matches = emptyList(),
Expand All @@ -153,15 +174,15 @@ internal class HandlerTest {

verify(telemetryService).notificationReceived(notification)
verify(personService).insertPerson(any(), any())
verify(eventService, never()).insertEvent(any(), any(), any(), any(), any())
verify(eventService).insertCourtAppearance(any(), any(), any(), any())
verify(eventService).insertEvent(any(), any(), any(), any(), any())
verify(eventService, never()).insertCourtAppearance(any(), any(), any(), any())
verify(notifier).caseCreated(any())
verify(notifier).addressCreated(any())
}

@Test
fun `Inserts court appearance record when case urn exists`() {
whenever(courtAppearanceRepository.findLatestByCaseUrn(any())).thenReturn(null)
whenever(courtAppearanceRepository.findLatestByCaseUrn(any())).thenReturn(CourtAppearanceGenerator.TRIAL_ADJOURNMENT)

whenever(personService.insertPerson(any(), any())).thenReturn(
InsertPersonResult(
Expand All @@ -172,6 +193,10 @@ internal class HandlerTest {
)
)

whenever(eventService.insertCourtAppearance(any(), any(), any(), any())).thenReturn(
CourtAppearanceGenerator.TRIAL_ADJOURNMENT
)

whenever(probationSearchClient.match(any())).thenReturn(
ProbationMatchResponse(
matches = emptyList(),
Expand All @@ -185,8 +210,8 @@ internal class HandlerTest {

verify(telemetryService).notificationReceived(notification)
verify(personService).insertPerson(any(), any())
verify(eventService).insertEvent(any(), any(), any(), any(), any())
verify(eventService, never()).insertCourtAppearance(any(), any(), any(), any())
verify(eventService, never()).insertEvent(any(), any(), any(), any(), any())
verify(eventService).insertCourtAppearance(any(), any(), any(), any())
verify(notifier).caseCreated(any())
verify(notifier).addressCreated(any())
}
Expand Down

0 comments on commit 118ae1c

Please sign in to comment.