Skip to content

Commit

Permalink
Remove unused fields and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Oct 29, 2024
1 parent 1a4f2b8 commit 75913a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,16 @@ internal class IntegrationTest {

@Test
fun `returns csv report`() {
whenever(upwAppointmentRepository.getUnpaidWorkAppointments(any(), eq("N56"), any())).thenReturn(
whenever(upwAppointmentRepository.getUnpaidWorkAppointments(any(), eq("N56"))).thenReturn(
listOf(
object : UnpaidWorkAppointment {
override val crn = "A123456"
override val firstName = "Test"
override val mobileNumber = "07000000000"
override val appointmentDate = "01/01/2000"
override val appointmentTimes = "08:00, 11:00"
override val nextWorkSessionProjectType = "Group session"
override val today = "01/01/2000"
override val sendSmsForDay = "01/01/2000"
override val fullName = "Test Test"
override val numberOfEvents = "1"
override val activeUpwRequirements = "1"
override val custodialStatus = null
override val currentRemandStatus = null
override val allowSms = "Y"
override val originalMobileNumber = "070 0000 0000"
override val upwMinutesRemaining = "123"
override val crn = "A123456"
override val eventNumber = "1"
override val upwAppointmentId = "123"
}
)
)
Expand All @@ -58,8 +49,8 @@ internal class IntegrationTest {
.andExpect(
content().string(
"""
crn,firstName,mobileNumber,appointmentDate,appointmentTimes,"nextWorkSessionProjectType",today,sendSmsForDay,fullName,numberOfEvents,activeUpwRequirements,custodialStatus,currentRemandStatus,allowSms,originalMobileNumber,upwMinutesRemaining
A123456,Test,07000000000,01/01/2000,"08:00, 11:00","Group session",01/01/2000,01/01/2000,"Test Test",1,1,,,Y,"070 0000 0000",123
firstName,mobileNumber,appointmentDate,appointmentTimes,crn,eventNumber,upwAppointmentId
Test,07000000000,01/01/2000,"08:00, 11:00",A123456,1,123
""".trimIndent()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ class ApiController(
@PreAuthorize("hasRole('PROBATION_API__REMINDERS__UPW_APPOINTMENTS')")
fun handle(
@RequestParam providerCode: String,
@RequestParam projectTypeCodes: List<String> =
listOf("A", "ES", "G", "I", "IP", "NP1", "NP2", "P", "PL", "UP09"),
@RequestParam date: LocalDate = LocalDate.now().plusDays(2)
): String {
val results = upwAppointmentRepository.getUnpaidWorkAppointments(date, providerCode, projectTypeCodes)
return csvMapper
.writer(csvMapper.schemaFor(UnpaidWorkAppointment::class.java).withHeader())
.writeValueAsString(results)
}
): String = csvMapper
.writer(csvMapper.schemaFor(UnpaidWorkAppointment::class.java).withHeader())
.writeValueAsString(upwAppointmentRepository.getUnpaidWorkAppointments(date, providerCode))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,20 @@ package uk.gov.justice.digital.hmpps.model
import com.fasterxml.jackson.annotation.JsonPropertyOrder

@JsonPropertyOrder(
"crn",
"firstName",
"mobileNumber",
"appointmentDate",
"appointmentTimes",
"nextWorkSessionProjectType",
"today",
"sendSmsForDay",
"fullName",
"numberOfEvents",
"activeUpwRequirements",
"custodialStatus",
"currentRemandStatus",
"allowSms",
"originalMobileNumber",
"upwMinutesRemaining"
"crn",
"eventNumber",
"upwAppointmentId",
)
interface UnpaidWorkAppointment {
val crn: String
val firstName: String
val mobileNumber: String
val appointmentDate: String
val appointmentTimes: String
val nextWorkSessionProjectType: String
val today: String
val sendSmsForDay: String
val fullName: String
val numberOfEvents: String
val activeUpwRequirements: String
val custodialStatus: String?
val currentRemandStatus: String?
val allowSms: String
val originalMobileNumber: String
val upwMinutesRemaining: String
val crn: String
val eventNumber: String
val upwAppointmentId: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,121 +10,93 @@ interface UpwAppointmentRepository : JpaRepository<UpwAppointment, Long> {
@Query(
"""
select
crn as "crn",
any_value(first_name) as "firstName",
any_value(mobile_number) as "mobileNumber",
any_value(to_char(appointment_date_time, 'DD/MM/YYYY')) as "appointmentDate",
listagg(distinct to_char(appointment_date_time, 'HH24:MI'), ', ') as "appointmentTimes",
listagg(distinct next_work_session_project_type, ', ') as "nextWorkSessionProjectType",
to_char(current_date, 'DD/MM/YYYY') as "today",
to_char(:date, 'DD/MM/YYYY') as "sendSmsForDay",
any_value(full_name) as "fullName",
count(distinct event_number) as "numberOfEvents",
listagg(distinct active_upw_requirements, ', ') as "activeUpwRequirements",
listagg(distinct custodial_status, ', ') as "custodialStatus",
any_value(current_remand_status) as "currentRemandStatus",
any_value(restrictions) as "restrictions",
any_value(exclusions) as "exclusions",
any_value(com_area) as "comArea",
any_value(allow_sms) as "allowSms",
any_value(original_mobile_number) as "originalMobileNumber",
listagg(distinct upw_minutes_remaining, ', ') as "upwMinutesRemaining"
any_value(to_char(appointment_date, 'DD/MM/YYYY')) as "appointmentDate",
listagg(distinct to_char(appointment_time, 'HH24:MI'), ', ') as "appointmentTimes",
-- for testing --
crn as "crn",
listagg(distinct event_number, ', ') as "eventNumber",
listagg(distinct upw_appointment_id, ', ') as "upwAppointmentId"
from (
with duplicate_mobile_numbers as (
select offender.offender_id from offender
join offender duplicate on replace(duplicate.mobile_number, ' ', '') = replace(offender.mobile_number, ' ', '') and duplicate.offender_id <> offender.offender_id and duplicate.soft_deleted = 0
where offender.soft_deleted = 0
)
select
crn,
(select count(*) from restriction where restriction.offender_id = offender.offender_id) as restrictions,
(select count(*) from exclusion where exclusion.offender_id = offender.offender_id) as exclusions,
com_area.code as com_area,
upw_appointment.upw_appointment_id,
event_number,
first_name,
replace(mobile_number, ' ', '') as mobile_number,
upw_appointment.appointment_date + (upw_appointment.start_time - trunc(upw_appointment.start_time)) as appointment_date_time,
upw_project_type.code_description as next_work_session_project_type,
r_contact_outcome_type.description as outcome_description,
to_char(current_date, 'DD/MM/YYYY') as today,
to_char(:date, 'DD/MM/YYYY') as send_sms_for_day,
first_name || ' ' || surname as full_name,
1 as number_of_events,
(select count(*) from rqmnt
join r_rqmnt_type_main_category on r_rqmnt_type_main_category.rqmnt_type_main_category_id = rqmnt.rqmnt_type_main_category_id and r_rqmnt_type_main_category.code in ('W', 'W0', 'W1', 'W2')
where rqmnt.disposal_id = disposal.disposal_id and rqmnt.active_flag = 1 and rqmnt.soft_deleted = 0) as active_upw_requirements,
custodial_status.code_value as custodial_status,
case when exists (select 1 from registration
join r_register_type on r_register_type.register_type_id = registration.register_type_id and r_register_type.code in ('IWWO', 'IWWB', 'WRSM')
where registration.offender_id = offender.offender_id and registration.deregistered = 0 and registration.soft_deleted = 0) then 'Y' else 'N' end as warrant,
case when exists (select 1 from registration
join r_register_type on r_register_type.register_type_id = registration.register_type_id and r_register_type.code in ('HUAL')
where registration.offender_id = offender.offender_id and registration.deregistered = 0 and registration.soft_deleted = 0) then 'Y' else 'N' end as unlawfully_at_large,
current_remand_status,
case when exists (select 1 from personal_circumstance
join r_circumstance_type on r_circumstance_type.circumstance_type_id = personal_circumstance.circumstance_type_id and r_circumstance_type.code_value = 'RIC'
where personal_circumstance.offender_id = offender.offender_id and personal_circumstance.soft_deleted = 0 and (personal_circumstance.end_date is null or personal_circumstance.end_date > current_date)
) then 'Y' else 'N' end as remanded_in_custody_circumstance,
allow_sms,
mobile_number as original_mobile_number,
(select total_minutes_ordered + positive_adjustments - negative_adjustments - minutes_credited from (
select
case
when r_disposal_type.pre_cja2003 = 'Y' then disposal.length * 60
else (select sum(rqmnt.length) * 60 from rqmnt
join r_rqmnt_type_main_category on r_rqmnt_type_main_category.rqmnt_type_main_category_id = rqmnt.rqmnt_type_main_category_id and r_rqmnt_type_main_category.code = 'W'
where rqmnt.disposal_id = disposal.disposal_id and rqmnt.soft_deleted = 0)
end as total_minutes_ordered,
(select coalesce(sum(adjustment_amount), 0) from upw_adjustment where upw_adjustment.upw_details_id = upw_details.upw_details_id and adjustment_type = 'POSITIVE' and upw_adjustment.soft_deleted = 0)
as positive_adjustments,
(select coalesce(sum(adjustment_amount), 0) from upw_adjustment where upw_adjustment.upw_details_id = upw_details.upw_details_id and adjustment_type = 'NEGATIVE' and upw_adjustment.soft_deleted = 0)
as negative_adjustments,
(select coalesce(sum(appts.minutes_credited), 0) from upw_appointment appts where appts.upw_details_id = upw_details.upw_details_id and appts.soft_deleted = 0)
as minutes_credited
from dual
)) as upw_minutes_remaining
upw_appointment.appointment_date as appointment_date,
(upw_appointment.start_time - trunc(upw_appointment.start_time)) as appointment_time,
crn,
event_number,
upw_appointment.upw_appointment_id
from offender
join offender_manager on offender_manager.offender_id = offender.offender_id and offender_manager.active_flag = 1
join probation_area com_area on com_area.probation_area_id = offender_manager.probation_area_id
join event on event.offender_id = offender.offender_id and event.active_flag = 1 and event.soft_deleted = 0
join disposal on disposal.event_id = event.event_id and disposal.active_flag = 1 and disposal.soft_deleted = 0
join r_disposal_type on r_disposal_type.disposal_type_id = disposal.disposal_type_id
join upw_details on upw_details.disposal_id = disposal.disposal_id and upw_details.soft_deleted = 0
join upw_appointment on upw_appointment.upw_details_id = upw_details.upw_details_id and upw_appointment.soft_deleted = 0 and trunc(upw_appointment.appointment_date) = :date
left join r_contact_outcome_type on r_contact_outcome_type.contact_outcome_type_id = upw_appointment.contact_outcome_type_id
join upw_project on upw_project.upw_project_id = upw_appointment.upw_project_id
join r_standard_reference_list upw_project_type on upw_project_type.standard_reference_list_id = upw_project.project_type_id and upw_project_type.code_value in :projectTypeCodes
join r_standard_reference_list upw_project_type on upw_project_type.standard_reference_list_id = upw_project.project_type_id and upw_project_type.code_value in ('ES','ICP','NP1','NP2','PI','PIP','PIP2','PL','PS','PSP','WH1')
join probation_area on probation_area.probation_area_id = upw_project.probation_area_id and probation_area.code = :providerCode
left join custody on custody.disposal_id = disposal.disposal_id and custody.soft_deleted = 0
left join r_standard_reference_list custodial_status on custodial_status.standard_reference_list_id = custody.custodial_status_id
left join exclusion on exclusion.offender_id = offender.offender_id and exclusion_date < current_date and (exclusion_end_date is null or current_date < exclusion_end_date)
left join restriction on restriction.offender_id = offender.offender_id and restriction_date < current_date and (restriction_end_date is null or current_date < restriction_end_date)
where offender.soft_deleted = 0
-- allow_sms <> 'N'
and (allow_sms is null or allow_sms = 'Y')
-- mobile_number_is_valid = 'Y'
-- valid mobile number and sms allowed
and replace(mobile_number, ' ', '') like '07%'
and length(replace(mobile_number, ' ', '')) = 11
and validate_conversion(replace(mobile_number, ' ', '') as number) = 1
-- duplicate_mobile_number_exists = 'N'
and not exists (select 1 from duplicate_mobile_numbers where offender.offender_id = duplicate_mobile_numbers.offender_id)
-- outcome_description = null
and (allow_sms is null or allow_sms = 'Y')
-- no other cases with the same mobile number
and not exists (
select 1 from offender duplicate where duplicate.offender_id <> offender.offender_id and duplicate.soft_deleted = 0 and replace(duplicate.mobile_number, ' ', '') = replace(offender.mobile_number, ' ', '')
)
-- has an active unpaid work requirement
and exists (select 1 from rqmnt
join r_rqmnt_type_main_category on r_rqmnt_type_main_category.rqmnt_type_main_category_id = rqmnt.rqmnt_type_main_category_id and r_rqmnt_type_main_category.code in ('W', 'W0', 'W1', 'W2')
where rqmnt.disposal_id = disposal.disposal_id and rqmnt.active_flag = 1 and rqmnt.soft_deleted = 0)
-- has unpaid work time remaining
and (select total_minutes_ordered + positive_adjustments - negative_adjustments - minutes_credited from (
select
case
when r_disposal_type.pre_cja2003 = 'Y' then disposal.length * 60
else (select sum(rqmnt.length) * 60 from rqmnt
join r_rqmnt_type_main_category on r_rqmnt_type_main_category.rqmnt_type_main_category_id = rqmnt.rqmnt_type_main_category_id and r_rqmnt_type_main_category.code = 'W'
where rqmnt.disposal_id = disposal.disposal_id and rqmnt.soft_deleted = 0)
end as total_minutes_ordered,
(select coalesce(sum(adjustment_amount), 0) from upw_adjustment where upw_adjustment.upw_details_id = upw_details.upw_details_id and adjustment_type = 'POSITIVE' and upw_adjustment.soft_deleted = 0)
as positive_adjustments,
(select coalesce(sum(adjustment_amount), 0) from upw_adjustment where upw_adjustment.upw_details_id = upw_details.upw_details_id and adjustment_type = 'NEGATIVE' and upw_adjustment.soft_deleted = 0)
as negative_adjustments,
(select coalesce(sum(appts.minutes_credited), 0) from upw_appointment appts where appts.upw_details_id = upw_details.upw_details_id and appts.soft_deleted = 0)
as minutes_credited
from dual)) > 0
-- appointment does not have an outcome
and r_contact_outcome_type.description is null
-- not in custody
and (custodial_status.code_value is null or custodial_status.code_value not in ('A', 'C', 'D'))
-- not on remand
and (current_remand_status is null or (current_remand_status not in ('Warrant With Bail', 'Warrant Without Bail', 'Remanded In Custody', 'Unlawfully at Large') and current_remand_status not like 'UAL%'))
and not exists (select 1 from personal_circumstance
join r_circumstance_type on r_circumstance_type.circumstance_type_id = personal_circumstance.circumstance_type_id and r_circumstance_type.code_value = 'RIC'
where personal_circumstance.offender_id = offender.offender_id and personal_circumstance.soft_deleted = 0 and (personal_circumstance.end_date is null or personal_circumstance.end_date > current_date))
-- not unlawfully at large
and not exists (select 1 from registration
join r_register_type on r_register_type.register_type_id = registration.register_type_id and r_register_type.code in ('HUAL')
where registration.offender_id = offender.offender_id and registration.deregistered = 0 and registration.soft_deleted = 0)
-- not on warrant
and not exists (select 1 from registration
join r_register_type on r_register_type.register_type_id = registration.register_type_id and r_register_type.code in ('IWWO', 'IWWB', 'WRSM')
where registration.offender_id = offender.offender_id and registration.deregistered = 0 and registration.soft_deleted = 0)
-- no access limitations
and restriction_id is null and exclusion_id is null
)
where active_upw_requirements > 0
and upw_minutes_remaining > 0
and warrant = 'N'
and unlawfully_at_large = 'N'
and (current_remand_status is null or (current_remand_status not in ('Warrant With Bail', 'Warrant Without Bail', 'Remanded In Custody', 'Unlawfully at Large') and current_remand_status not like 'UAL%'))
and remanded_in_custody_circumstance = 'N'
and (custodial_status is null or custodial_status not in ('A', 'C', 'D'))
and restrictions = 0 and exclusions = 0
group by crn
order by crn
""", nativeQuery = true
)
fun getUnpaidWorkAppointments(
date: LocalDate,
providerCode: String,
projectTypeCodes: List<String>,
providerCode: String
): List<UnpaidWorkAppointment>
}

0 comments on commit 75913a4

Please sign in to comment.