Skip to content

Commit

Permalink
PI-2100: Added court report endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 committed Aug 6, 2024
1 parent e80e2d2 commit a7cdb3b
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ class DataLoader(
val activePssRequirement = SentenceGenerator.generatePssRequirement(custody.id, active = true)
val inactivePssRequirement = SentenceGenerator.generatePssRequirement(custody.id, active = false)
val currentCourtAppearance = SentenceGenerator.COURT_APPEARANCE
val currentCourtReport = SentenceGenerator.generateCourtReport(currentCourtAppearance)
val currentCourtReport =
SentenceGenerator.generateCourtReport(currentCourtAppearance, PersonGenerator.CURRENTLY_MANAGED.id)
val reportManager = SentenceGenerator.generateCourtReportManager(currentCourtReport)

em.saveAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,19 @@ object SentenceGenerator {
id = id
)

fun generateCourtReport(courtAppearance: CourtAppearance, id: Long = IdGenerator.getAndIncrement()) =
fun generateCourtReport(
courtAppearance: CourtAppearance,
personId: Long,
id: Long = IdGenerator.getAndIncrement()
) =
CourtReport(
LocalDate.now(),
LocalDate.now().plusDays(5),
personId,
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS),
LocalDateTime.now().plusDays(5).truncatedTo(ChronoUnit.MICROS),
null,
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS),
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS),
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS),
ReferenceDataGenerator.COURT_REPORT_TYPE,
null,
courtAppearance,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"courtReportId": 125,
"offenderId": 65,
"requestedDate": "2024-08-06T18:59:19.420309",
"requiredDate": "2024-08-11T18:59:19.420318",
"allocationDate": "2024-08-06T18:59:19.420326",
"sentToCourtDate": "2024-08-06T18:59:19.420329",
"receivedByCourtDate": "2024-08-06T18:59:19.420332",
"courtReportType": {
"code": "CR1",
"description": "court report"
},
"reportManagers": [
{
"staff": {
"code": "N01ABBA",
"forenames": "Bob Micheal",
"surname": "Smith"
},
"active": true
}
]
}
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
"mappings": [
{
"request": {
"method": "GET",
"urlPathTemplate": "/secure/offenders/crn/{crn}/convictions/{convictionId}/courtReports"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName": "get_court_reports_C123456.json"
}
},
{
"request": {
"method": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,18 @@ internal class ConvictionByCrnAndEventIdIntegrationTest {

assertThat(response.courtAppearances[0], equalTo(COURT_APPEARANCE.toCourtAppearance()))
}

@Test
fun `call convictions by id and courtReports`() {
val crn = PersonGenerator.CURRENTLY_MANAGED.crn
val event = SentenceGenerator.CURRENTLY_MANAGED

val response = mockMvc
.perform(get("/probation-case/$crn/convictions/${event.id}/courtReports").withToken())
.andExpect(status().isOk)
.andReturn().response.contentAsJson<List<CourtReportMinimal>>()

assertThat(response.size, equalTo(1))
assertThat(response[0].reportManagers?.size, equalTo(1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ internal class ProxyIntegrationTest {
"CONVICTION_BY_ID_COURT_APPEARANCES": {
"convictionId": "?",
"activeOnly": true
},
"CONVICTION_BY_ID_COURT_REPORTS": {
"convictionId": "?",
"activeOnly": true
}
}
}
Expand All @@ -257,7 +261,7 @@ internal class ProxyIntegrationTest {
.withToken()
).andExpect(status().is2xxSuccessful).andReturn().response.contentAsJson<CompareAllReport>()

assertThat(res.totalNumberOfRequests, equalTo(11))
assertThat(res.totalNumberOfRequests, equalTo(12))
assertThat(res.totalNumberOfCrns, equalTo(2))
assertThat(res.currentPageNumber, equalTo(1))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ data class StaffHuman(
val code: String,
val forenames: String,
val surname: String,
val unallocated: Boolean
val unallocated: Boolean? = null
)

data class Institution(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uk.gov.justice.digital.hmpps.api.model.conviction

import uk.gov.justice.digital.hmpps.api.model.KeyValue
import uk.gov.justice.digital.hmpps.api.model.StaffHuman
import java.time.LocalDateTime

data class CourtReportMinimal(
val courtReportId: Long,
val offenderId: Long,
val requestedDate: LocalDateTime,
val requiredDate: LocalDateTime,
val allocationDate: LocalDateTime? = null,
val completedDate: LocalDateTime? = null,
val sentToCourtDate: LocalDateTime? = null,
val receivedByCourtDate: LocalDateTime? = null,
val courtReportType: KeyValue? = null,
val reportManagers: List<ReportManager> = emptyList(),
val deliveredCourtReportType: KeyValue? = null,

)

data class ReportManager(
val staff: StaffHuman?,
val active: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ class CommunityApiController(
return proxy(request)
}

@GetMapping("/offenders/crn/{crn}/convictions/{convictionId}/courtReports")
fun convictionByIdCourtReports(
request: HttpServletRequest,
@PathVariable crn: String,
@PathVariable convictionId: Long,
): Any {

sendComparisonReport(
mapOf(
"crn" to crn,
"convictionId" to convictionId
), Uri.CONVICTION_BY_ID_COURT_REPORTS, request
)

if (featureFlags.enabled("ccd-conviction-by-id-court-reports")) {
return convictionResource.getConvictionCourtReports(crn, convictionId)
}
return proxy(request)
}

@GetMapping("/**")
fun proxy(request: HttpServletRequest): ResponseEntity<String> {
val headers = request.headerNames.asSequence().associateWith { request.getHeader(it) }.toMutableMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,11 @@ enum class Uri(
"getConvictionCourtAppearances",
listOf("crn", "convictionId"),
),
CONVICTION_BY_ID_COURT_REPORTS(
"/secure/offenders/crn/{crn}/convictions/{convictionId}/courtReports",
"convictionResource",
"getConvictionCourtReports",
listOf("crn", "convictionId"),
),
DUMMY("/dummy", "dummyResource", "getDummy", listOf("crn")),
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ConvictionResource(
private val requirementService: RequirementService,
private val interventionService: InterventionService,
private val attendanceService: AttendanceService,
private val courtAppearanceService: CourtAppearanceService
private val courtAppearanceService: CourtAppearanceService,
private val courtReportService: CourtReportService
) {

@GetMapping
Expand Down Expand Up @@ -94,4 +95,10 @@ class ConvictionResource(
@PathVariable crn: String,
@PathVariable convictionId: Long
) = courtAppearanceService.getCourtAppearancesFor(crn, convictionId)

@GetMapping("/{convictionId}/courtReports")
fun getConvictionCourtReports(
@PathVariable crn: String,
@PathVariable convictionId: Long
) = courtReportService.getCourtReportsFor(crn, convictionId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Court
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Immutable
@Table(name = "court_appearance")
@SQLRestriction("soft_deleted = 0")
class CourtAppearance(

@JoinColumn(name = "event_id")
@ManyToOne
val event: Event,
Expand Down Expand Up @@ -66,6 +66,17 @@ interface CourtReportRepository : JpaRepository<CourtReport, Long> {
"""
)
fun getAllByEvent(event: Event): List<CourtReport>

@Query(
"""
select courtReport
from CourtReport courtReport
where courtReport.personId = :personId
and courtReport.courtAppearance.event.id = :eventId
and courtReport.softDeleted = false
"""
)
fun findByPersonIdAndEventId(personId: Long, eventId: Long): List<CourtReport>
}

@Immutable
Expand All @@ -92,12 +103,24 @@ class Outcome(
@Table(name = "court_report")
@SQLRestriction("soft_deleted = 0")
class CourtReport(

@Column(name = "offender_id")
val personId: Long,
@Column(name = "date_requested")
val dateRequested: LocalDate,
val dateRequested: LocalDateTime,
@Column(name = "date_required")
val dateRequired: LocalDate,
val dateRequired: LocalDateTime,
@Column(name = "completed_Date")
val dateCompleted: LocalDate?,
val dateCompleted: LocalDateTime?,

@Column(name = "allocation_date")
val allocationDate: LocalDateTime? = null,

@Column(name = "sent_to_court_date")
val sentToCourtDate: LocalDateTime? = null,

@Column(name = "received_by_court_date")
val receivedByCourtDate: LocalDateTime? = null,

@ManyToOne
@JoinColumn(name = "court_report_type_id")
Expand Down Expand Up @@ -133,7 +156,7 @@ class ReportManager(

@JoinColumn(name = "staff_id")
@OneToOne
val staff: Staff,
val staff: Staff? = null,

@Column(name = "active_flag", columnDefinition = "number")
var active: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package uk.gov.justice.digital.hmpps.integrations.delius.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.KeyValue
import uk.gov.justice.digital.hmpps.api.model.StaffHuman
import uk.gov.justice.digital.hmpps.api.model.conviction.CourtReportMinimal
import uk.gov.justice.digital.hmpps.integrations.delius.event.courtappearance.entity.CourtReport
import uk.gov.justice.digital.hmpps.integrations.delius.event.courtappearance.entity.CourtReportRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.courtappearance.entity.ReportManager
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.EventRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.getByPersonAndEventNumber
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.provider.entity.Staff

@Service
class CourtReportService(
private val personRepository: PersonRepository,
private val eventRepository: EventRepository,
private val courtReportRepository: CourtReportRepository
) {

fun getCourtReportsFor(crn: String, eventId: Long): List<CourtReportMinimal> {
val person = personRepository.getPerson(crn)
val event = eventRepository.getByPersonAndEventNumber(person, eventId)
return courtReportRepository.findByPersonIdAndEventId(person.id, event.id)
.map { it.toCourtReport() }
}
}

fun CourtReport.toCourtReport() = CourtReportMinimal(
courtReportId = id,
offenderId = personId,
requestedDate = dateRequested,
requiredDate = dateRequired,
allocationDate = allocationDate,
completedDate = dateCompleted,
sentToCourtDate = sentToCourtDate,
receivedByCourtDate = receivedByCourtDate,
courtReportType = courtReportType?.let { KeyValue(it.code, it.description) },
reportManagers = reportManagers.filter { it.staff != null }.map { it.toReportManager() },
deliveredCourtReportType = deliveredCourtReportType?.let { KeyValue(it.code, it.description) }
)

fun Staff.toStaffHuman() = StaffHuman(
forenames = listOfNotNull(forename, forename2).joinToString(" "),
surname = surname,
code = code
)

fun ReportManager.toReportManager() = uk.gov.justice.digital.hmpps.api.model.conviction.ReportManager(
active = active,
staff = staff?.toStaffHuman()
)

0 comments on commit a7cdb3b

Please sign in to comment.