Skip to content

Commit

Permalink
PI-2099: Added court appearances
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 committed Aug 6, 2024
1 parent 80e4e3a commit ee19a28
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"courtAppearances": [
{
"courtAppearanceId": 90,
"appearanceDate": "2024-08-06T12:00:00",
"courtCode": "BRMNCC",
"courtName": "Birmingham Crown Court",
"appearanceType": {
"code": "CRN",
"description": "Crown Court"
},
"crn": "C123456"
}
]
}
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}/courtAppearances"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName": "get_court_appearances_C123456.json"
}
},
{
"request": {
"method": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator.MAIN_OFFENC
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.integrations.delius.service.toAttendance
import uk.gov.justice.digital.hmpps.integrations.delius.service.toCourtAppearance
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
import java.time.LocalDate
Expand Down Expand Up @@ -271,4 +272,17 @@ internal class ConvictionByCrnAndEventIdIntegrationTest {

assertThat(response.attendances[0], equalTo(ATTENDANCE_CONTACT_1.toAttendance()))
}

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

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

assertThat(response.courtAppearances[0], equalTo(COURT_APPEARANCE.toCourtAppearance()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ internal class ProxyIntegrationTest {
},
"CONVICTION_BY_ID_PSS": {
"convictionId": "?"
},
"CONVICTION_BY_ID_COURT_APPEARANCES": {
"convictionId": "?",
"activeOnly": true
}
}
}
Expand All @@ -253,7 +257,7 @@ internal class ProxyIntegrationTest {
.withToken()
).andExpect(status().is2xxSuccessful).andReturn().response.contentAsJson<CompareAllReport>()

assertThat(res.totalNumberOfRequests, equalTo(10))
assertThat(res.totalNumberOfRequests, equalTo(11))
assertThat(res.totalNumberOfCrns, equalTo(2))
assertThat(res.currentPageNumber, equalTo(1))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ data class Court(
val courtType: KeyValue
)

data class CourtAppearanceBasicWrapper(
val courtAppearances: List<CourtAppearanceBasic> = emptyList()
)

data class CourtAppearanceBasic(
val courtAppearanceId: Long,
val appearanceDate: LocalDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class CommunityApiController(
mapOf(
"crn" to crn,
"convictionId" to convictionId
), Uri.CONVICTION_BY_ID_NSIS, request
), Uri.CONVICTION_BY_ID_PSS, request
)

if (featureFlags.enabled("ccd-conviction-pss-enabled")) {
Expand All @@ -212,6 +212,26 @@ class CommunityApiController(
return proxy(request)
}

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

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

if (featureFlags.enabled("ccd-conviction-by-id-court-appearances")) {
return convictionResource.getConvictionCourtAppearances(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 @@ -61,6 +61,11 @@ enum class Uri(
"getPssRequirementsByConvictionId",
listOf("crn", "convictionId"),
),

CONVICTION_BY_ID_COURT_APPEARANCES(
"/secure/offenders/crn/{crn}/convictions/{convictionId}/courtAppearances",
"convictionResource",
"getConvictionCourtAppearances",
listOf("crn", "convictionId"),
),
DUMMY("/dummy", "dummyResource", "getDummy", listOf("crn")),
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import io.swagger.v3.oas.annotations.Parameter
import jakarta.validation.constraints.NotEmpty
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import uk.gov.justice.digital.hmpps.integrations.delius.service.AttendanceService
import uk.gov.justice.digital.hmpps.integrations.delius.service.ConvictionService
import uk.gov.justice.digital.hmpps.integrations.delius.service.InterventionService
import uk.gov.justice.digital.hmpps.integrations.delius.service.RequirementService
import uk.gov.justice.digital.hmpps.integrations.delius.service.*

@RestController
@RequestMapping("probation-case/{crn}/convictions")
Expand All @@ -16,7 +13,8 @@ class ConvictionResource(
private val convictionService: ConvictionService,
private val requirementService: RequirementService,
private val interventionService: InterventionService,
private val attendanceService: AttendanceService
private val attendanceService: AttendanceService,
private val courtAppearanceService: CourtAppearanceService
) {

@GetMapping
Expand Down Expand Up @@ -90,4 +88,10 @@ class ConvictionResource(
@PathVariable crn: String,
@PathVariable convictionId: Long
) = attendanceService.getAttendancesFor(crn, convictionId)

@GetMapping("/{convictionId}/courtAppearances")
fun getConvictionCourtAppearances(
@PathVariable crn: String,
@PathVariable convictionId: Long
) = courtAppearanceService.getCourtAppearancesFor(crn, convictionId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class CourtAppearance(
}
}

interface CourtAppearanceRepository : JpaRepository<CourtAppearance, Long> {

fun findByPersonIdAndEventId(personId: Long, eventId: Long): List<CourtAppearance>
}

interface CourtReportRepository : JpaRepository<CourtReport, Long> {

@Query(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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.conviction.CourtAppearanceBasic
import uk.gov.justice.digital.hmpps.api.model.conviction.CourtAppearanceBasicWrapper
import uk.gov.justice.digital.hmpps.integrations.delius.event.courtappearance.entity.CourtAppearance
import uk.gov.justice.digital.hmpps.integrations.delius.event.courtappearance.entity.CourtAppearanceRepository
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

@Service
class CourtAppearanceService(
private val personRepository: PersonRepository,
private val eventRepository: EventRepository,
private val courtAppearanceRepository: CourtAppearanceRepository
) {

fun getCourtAppearancesFor(crn: String, eventId: Long): CourtAppearanceBasicWrapper {
val person = personRepository.getPerson(crn)
val event = eventRepository.getByPersonAndEventNumber(person, eventId)
val courtAppearances = courtAppearanceRepository.findByPersonIdAndEventId(person.id, event.id)
.sortedByDescending { it.appearanceDate }
.map { it.toCourtAppearance() }
return CourtAppearanceBasicWrapper(courtAppearances)
}
}

fun CourtAppearance.toCourtAppearance() = CourtAppearanceBasic(
courtAppearanceId = id,
appearanceDate = appearanceDate.toLocalDateTime(),
courtCode = court.code,
courtName = court.courtName,
appearanceType = KeyValue(court.courtType.code, court.courtType.description),
crn = person.crn
)

0 comments on commit ee19a28

Please sign in to comment.