Skip to content

Commit 3dacc93

Browse files
Man 317 discrepancy in rar days displayed (#4554)
* MAN-317 - update rar day count * MAN-317 - use disposal id to get rar instead of requirement id * MAN-317 - use new query for rar calculation in overview page * Formatting changes --------- Co-authored-by: probation-integration-bot[bot] <177347787+probation-integration-bot[bot]@users.noreply.github.com>
1 parent fddeaa5 commit 3dacc93

File tree

13 files changed

+65
-31
lines changed

13 files changed

+65
-31
lines changed

projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RequirementIntegrationTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ class RequirementIntegrationTest {
6767
LocalDate.now().minusDays(2),
6868
LocalDate.now().minusDays(3),
6969
null,
70-
"1 days RAR, 1 completed",
70+
"2 of 12 RAR days completed",
7171
12,
7272
null,
7373
requirementNote = NoteDetail(0, note = "my notes"),
74-
rar = Rar(completed = 1, scheduled = 0, totalDays = 1)
74+
rar = Rar(completed = 1, scheduled = 1, nsiCompleted = 0, totalDays = 2)
7575
)
7676
)
7777

projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentenceIntegrationTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ class SentenceIntegrationTest {
129129
LocalDate.now().minusDays(2),
130130
LocalDate.now().minusDays(3),
131131
null,
132-
"1 days RAR, 1 completed",
132+
"2 of 12 RAR days completed",
133133
12,
134134
null,
135135
listOf(NoteDetail(0, note = "my notes", hasNoteBeenTruncated = false)),
136-
rar = Rar(completed = 1, scheduled = 0, totalDays = 1)
136+
rar = Rar(completed = 1, scheduled = 1, nsiCompleted = 0, totalDays = 2)
137137
),
138138
Requirement(
139139
REQUIREMENT_UNPAID_WORK.id,

projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentencesIntegrationTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class SentencesIntegrationTest {
7575
MinimalLicenceCondition(LC_WITH_1500_CHAR_NOTE.id, LIC_COND_MAIN_CAT.description)
7676
),
7777
requirements = listOf(
78-
MinimalRequirement(REQUIREMENT.id, "1 days RAR, 1 completed"),
78+
MinimalRequirement(REQUIREMENT.id, "2 of 12 RAR days completed"),
7979
MinimalRequirement(REQUIREMENT_UNPAID_WORK.id, "Unpaid Work - Intensive")
8080
)
8181
)

projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/Rar.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.api.model.overview
22

33
data class Rar(
44
val completed: Int,
5+
val nsiCompleted: Int,
56
val scheduled: Int,
6-
val totalDays: Int = completed + scheduled
7+
val totalDays: Int = nsiCompleted + completed + scheduled
78
)

projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/compliance/Nsi.kt

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Nsi(
2424
@Column(name = "event_id")
2525
val eventId: Long?,
2626

27+
@Column(name = "rqmnt_id")
28+
val requirementId: Long? = null,
29+
2730
@Column(name = "actual_start_date")
2831
val actualStartDate: LocalDate?,
2932

projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Contact.kt

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class Contact(
7474
@Lob
7575
val notes: String?,
7676

77+
val nsiId: Long? = null,
78+
7779
@ManyToOne
7880
@JoinColumn(name = "rqmnt_id")
7981
val requirement: Requirement? = null,

projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Requirement.kt

+20-6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ interface RarDays {
6262

6363
interface RequirementDetails {
6464
val id: Long
65+
val disposalId: Long
6566
val expectedStartDate: LocalDate?
6667
val startDate: LocalDate
6768
val commencementDate: LocalDate?
@@ -110,7 +111,7 @@ interface RequirementRepository : JpaRepository<Requirement, Long> {
110111
and (c.attended is null)
111112
and (c.complied is null or c.complied = 'Y')
112113
and mc.code = 'F' and r.active_flag = 1 and r.soft_deleted = 0
113-
and r.disposal_id = :requirementId
114+
and r.disposal_id = :disposalId
114115
union
115116
select count(r.rqmnt_id) as days, 'COMPLETED' as type from contact c
116117
join rqmnt r on r.rqmnt_id = c.rqmnt_id
@@ -119,14 +120,25 @@ interface RequirementRepository : JpaRepository<Requirement, Long> {
119120
and (c.attended = 'Y')
120121
and (c.complied is null or c.complied = 'Y')
121122
and mc.code = 'F' and r.active_flag = 1 and r.soft_deleted = 0
122-
and r.rqmnt_id = :requirementId
123+
and r.disposal_id = :disposalId
124+
union
125+
SELECT count(r.rqmnt_id) as days, 'NSI_COMPLETED' FROM contact c
126+
JOIN nsi n ON n.nsi_id = c.nsi_id
127+
JOIN rqmnt r on r.rqmnt_id = n.rqmnt_id
128+
join r_rqmnt_type_main_category mc on r.rqmnt_type_main_category_id = mc.rqmnt_type_main_category_id
129+
where c.rar_activity = 'Y' and c.soft_deleted = 0
130+
and (c.attended = 'Y')
131+
and (c.complied is null or c.complied = 'Y')
132+
and mc.code = 'F' and r.active_flag = 1 and r.soft_deleted = 0
133+
and r.disposal_id = :disposalId
123134
""", nativeQuery = true
124135
)
125-
fun getRarDaysByRequirementId(requirementId: Long): List<RarDays>
136+
fun getRarDaysByDisposalId(disposalId: Long): List<RarDays>
126137

127138
@Query(
128139
"""
129-
SELECT r.rqmnt_id AS id,
140+
SELECT r.rqmnt_id as id,
141+
r.disposal_id as disposalId,
130142
r.expected_start_date as expectedStartDate,
131143
r.start_date as startDate,
132144
r.commencement_date as commencementDate,
@@ -164,6 +176,7 @@ interface RequirementRepository : JpaRepository<Requirement, Long> {
164176
@Query(
165177
"""
166178
SELECT r.rqmnt_id AS id,
179+
r.disposal_id as disposalId,
167180
r.expected_start_date as expectedStartDate,
168181
r.start_date as startDate,
169182
r.commencement_date as commencementDate,
@@ -211,10 +224,11 @@ interface RequirementRepository : JpaRepository<Requirement, Long> {
211224
}
212225

213226
fun RequirementRepository.getRar(disposalId: Long): Rar {
214-
val rarDays = getRarDays(disposalId)
227+
val rarDays = getRarDaysByDisposalId(disposalId)
215228
val scheduledDays = rarDays.find { it.type == "SCHEDULED" }?.days ?: 0
216229
val completedDays = rarDays.find { it.type == "COMPLETED" }?.days ?: 0
217-
return Rar(completed = completedDays, scheduled = scheduledDays)
230+
val nsiCompletedDays = rarDays.find { it.type == "NSI_COMPLETED" }?.days ?: 0
231+
return Rar(completed = completedDays, nsiCompleted = nsiCompletedDays, scheduled = scheduledDays)
218232
}
219233

220234
@Immutable

projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RequirementService.kt

+13-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RequirementService(
2727
}
2828

2929
fun RequirementDetails.toRequirementSingleNote(noteId: Int): Requirement {
30-
val rar = getRar(id, code)
30+
val rar = getRar(disposalId, code)
3131

3232
val requirement = Requirement(
3333
id,
@@ -37,7 +37,7 @@ class RequirementService(
3737
expectedEndDate,
3838
terminationDate,
3939
terminationReason,
40-
populateRequirementDescription(description, codeDescription, rar),
40+
populateRequirementDescription(description, codeDescription, length, rar),
4141
length,
4242
lengthUnitValue,
4343
requirementNote = toRequirementNote(false).elementAtOrNull(noteId),
@@ -47,20 +47,26 @@ class RequirementService(
4747
return requirement
4848
}
4949

50-
fun getRar(requirementId: Long, requirementType: String): Rar? {
50+
fun getRar(disposalId: Long, requirementType: String): Rar? {
5151
if (requirementType.equals("F", true)) {
52-
val rarDays = requirementRepository.getRarDaysByRequirementId(requirementId)
52+
val rarDays = requirementRepository.getRarDaysByDisposalId(disposalId)
5353
val scheduledDays = rarDays.find { it.type == "SCHEDULED" }?.days ?: 0
5454
val completedDays = rarDays.find { it.type == "COMPLETED" }?.days ?: 0
55-
return Rar(completed = completedDays, scheduled = scheduledDays)
55+
val nsiCompletedDays = rarDays.find { it.type == "NSI_COMPLETED" }?.days ?: 0
56+
return Rar(completed = completedDays, nsiCompleted = nsiCompletedDays, scheduled = scheduledDays)
5657
}
5758

5859
return null
5960
}
6061
}
6162

62-
fun populateRequirementDescription(description: String, codeDescription: String?, rar: Rar?): String {
63-
rar?.let { return "" + it.totalDays + " days RAR, " + it.completed + " completed" }
63+
fun populateRequirementDescription(
64+
description: String,
65+
codeDescription: String?,
66+
requirementLength: Long?,
67+
rar: Rar?
68+
): String {
69+
rar?.let { return "${it.totalDays} of $requirementLength RAR days completed" }
6470

6571
if (codeDescription != null) {
6672
return "$description - $codeDescription"

projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class SentenceService(
143143
fun Disposal.toMinimalOrder() = MinimalOrder(type.description, date, expectedEndDate())
144144

145145
fun RequirementDetails.toRequirement(): Requirement {
146-
val rar = requirementService.getRar(id, code)
146+
val rar = requirementService.getRar(disposalId, code)
147147

148148
val requirement = Requirement(
149149
id,
@@ -153,7 +153,7 @@ class SentenceService(
153153
expectedEndDate,
154154
terminationDate,
155155
terminationReason,
156-
populateRequirementDescription(description, codeDescription, rar),
156+
populateRequirementDescription(description, codeDescription, length, rar),
157157
length,
158158
lengthUnitValue,
159159
toRequirementNote(true),
@@ -164,8 +164,8 @@ class SentenceService(
164164
}
165165

166166
fun RequirementDetails.toMinimalRequirement(): MinimalRequirement {
167-
val rar = requirementService.getRar(id, code)
168-
return MinimalRequirement(id, populateRequirementDescription(description, codeDescription, rar))
167+
val rar = requirementService.getRar(disposalId, code)
168+
return MinimalRequirement(id, populateRequirementDescription(description, codeDescription, length, rar))
169169
}
170170

171171
fun getUnpaidWorkTime(disposalId: Long): String? {

projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/ComplianceControllerTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal class ComplianceControllerTest {
4444
val mainOffence = Offence(code = "CD1", description = "Main Offence")
4545
val order = Order(description = "Order", endDate = LocalDate.now(), length = 1, startDate = LocalDate.now())
4646
val breach = Breach(LocalDate.now(), "A Breach")
47-
val rar = Rar(10, 5)
47+
val rar = Rar(completed = 10, scheduled = 5, nsiCompleted = 0)
4848
val activityCount = ActivityCount(1, 0, 1, 0, 1, 1, 1, 2, 4, 1, 0, 1)
4949
val compliance = Compliance(1, true, 1, 1, 2)
5050
val sentenceCompliance = SentenceCompliance(

projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ComplianceServiceTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ internal class ComplianceServiceTest {
116116
),
117117
)
118118

119-
whenever(requirementRepository.getRarDays(any())).thenReturn(
119+
whenever(requirementRepository.getRarDaysByDisposalId(any())).thenReturn(
120120
listOf(RarDays(1, "COMPLETED"), OverviewServiceTest.RarDays(2, "SCHEDULED"))
121121
)
122122

projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/OverviewServiceTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal class OverviewServiceTest {
7373
whenever(contactRepository.findUpComingAppointments(any(), any(), any())).thenReturn(
7474
listOf(FIRST_APPT_CONTACT)
7575
)
76-
whenever(requirementRepository.getRarDays(any())).thenReturn(
76+
whenever(requirementRepository.getRarDaysByDisposalId(any())).thenReturn(
7777
listOf(RarDays(1, "COMPLETED"), RarDays(2, "SCHEDULED"))
7878
)
7979
whenever(eventRepository.findByPersonId(PersonGenerator.OVERVIEW.id)).thenReturn(

projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceServiceTest.kt

+13-5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class SentenceServiceTest {
7474

7575
private val requirement1 = RequirementDetails(
7676
1,
77+
2,
7778
LocalDate.now().minusDays(21),
7879
LocalDate.now(),
7980
LocalDate.now().minusDays(14),
@@ -122,6 +123,7 @@ class SentenceServiceTest {
122123

123124
val requirement1 = RequirementDetails(
124125
1,
126+
2,
125127
LocalDate.now().minusDays(21),
126128
LocalDate.now(),
127129
LocalDate.now().minusDays(14),
@@ -137,13 +139,14 @@ class SentenceServiceTest {
137139
)
138140
val requirement2 = RequirementDetails(
139141
2,
142+
3,
140143
null,
141144
LocalDate.now(),
142145
null,
143146
null,
144147
null,
145148
null,
146-
null,
149+
30,
147150
null,
148151
"F",
149152
"Main",
@@ -153,6 +156,7 @@ class SentenceServiceTest {
153156

154157
val requirement3 = RequirementDetails(
155158
1,
159+
4,
156160
LocalDate.now(),
157161
LocalDate.now(),
158162
null,
@@ -195,9 +199,9 @@ class SentenceServiceTest {
195199
whenever(requirementRepository.sumTotalUnpaidWorkHoursByDisposal(event.disposal!!.id)).thenReturn(70)
196200
whenever(upwAppointmentRepository.calculateUnpaidTimeWorked(event.disposal!!.id)).thenReturn(3936)
197201

198-
whenever(requirementService.getRar(requirement1._id, requirement1._code)).thenReturn(null)
199-
whenever(requirementService.getRar(requirement2._id, requirement2._code)).thenReturn(Rar(1, 2, 3))
200-
whenever(requirementService.getRar(requirement3._id, requirement3._code)).thenReturn(null)
202+
whenever(requirementService.getRar(requirement1._disposalId, requirement1._code)).thenReturn(null)
203+
whenever(requirementService.getRar(requirement2._disposalId, requirement2._code)).thenReturn(Rar(1, 2, 3))
204+
whenever(requirementService.getRar(requirement3._disposalId, requirement3._code)).thenReturn(null)
201205

202206
val response = service.getEvents(PersonGenerator.OVERVIEW.crn, null)
203207

@@ -247,7 +251,7 @@ class SentenceServiceTest {
247251
requirement2._expectedEndDate,
248252
requirement2._terminationDate,
249253
requirement2._terminationReason,
250-
"3 days RAR, 1 completed",
254+
"6 of 30 RAR days completed",
251255
requirement2._length,
252256
requirement2.lengthUnitValue,
253257
listOf(NoteDetail(0, note = requirement2._notes!!, hasNoteBeenTruncated = false)),
@@ -498,6 +502,7 @@ class SentenceServiceTest {
498502

499503
data class RequirementDetails(
500504
val _id: Long,
505+
val _disposalId: Long,
501506
val _expectedStartDate: LocalDate?,
502507
val _startDate: LocalDate,
503508
val _commencementDate: LocalDate?,
@@ -514,6 +519,9 @@ class SentenceServiceTest {
514519
override val id: Long
515520
get() = _id
516521

522+
override val disposalId: Long
523+
get() = _disposalId
524+
517525
override val expectedStartDate: LocalDate?
518526
get() = _expectedStartDate
519527

0 commit comments

Comments
 (0)