generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/pi 1939 mas overview tab delius integration api offence tab (#…
…3454) * PI-1939 - add new api to return most recent active event * PI-1939 - small refactor with clear ide warning messages * PI-1939 - refactor offence count to Long * PI-1939 - refactor entity names * PI-1939 - update service test * PI-1939 - add controller and integration test * PI-1939 - refactor integration layer and update integration test * PI-1939 - update service test * Formatting changes * PI-1939 - refactor code and apply review comments * PI-1939 - update tests * PI-1939 - get all active sentences, instead of latest sentence * Formatting changes * PI-1939 - update sql to only query required tables --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f7dca6a
commit ba72fef
Showing
11 changed files
with
327 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentenceIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package uk.gov.justice.digital.hmpps | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.MainOffence | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.Offence | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceOverview | ||
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator | ||
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson | ||
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken | ||
import java.time.LocalDate | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class SentenceIntegrationTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Test | ||
fun `get active sentences`() { | ||
val response = mockMvc | ||
.perform(MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}").withToken()) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<SentenceOverview>() | ||
|
||
val expected = SentenceOverview( | ||
listOf( | ||
MainOffence( | ||
Offence("Murder", 1), | ||
LocalDate.of(2024, 3, 12), | ||
"overview", | ||
listOf( | ||
Offence("Burglary", 1), | ||
Offence("Assault", 1) | ||
) | ||
), | ||
MainOffence( | ||
Offence("Another Murder", 1), | ||
LocalDate.of(2024, 3, 12), | ||
"overview", | ||
emptyList() | ||
) | ||
) | ||
) | ||
|
||
assertEquals(expected, response) | ||
} | ||
|
||
@Test | ||
fun `unauthorized status returned`() { | ||
mockMvc | ||
.perform(MockMvcRequestBuilders.get("/sentence/X123456")) | ||
.andExpect(MockMvcResultMatchers.status().isUnauthorized) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package uk.gov.justice.digital.hmpps.api.controller | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springframework.security.access.prepost.PreAuthorize | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import uk.gov.justice.digital.hmpps.service.SentenceService | ||
|
||
@RestController | ||
@Tag(name = "Sentence") | ||
@RequestMapping("/sentence/{crn}") | ||
@PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')") | ||
class SentenceController(private val sentenceService: SentenceService) { | ||
|
||
@GetMapping | ||
@Operation(summary = "Display the most recent ‘Active Event’ ") | ||
fun getOverview(@PathVariable crn: String) = sentenceService.getMostRecentActiveEvent(crn) | ||
} |
10 changes: 10 additions & 0 deletions
10
...and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/MainOffence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package uk.gov.justice.digital.hmpps.api.model.sentence | ||
|
||
import java.time.LocalDate | ||
|
||
data class MainOffence( | ||
val offence: Offence, | ||
val dateOfOffence: LocalDate, | ||
val notes: String, | ||
val additionalOffences: List<Offence> | ||
) |
6 changes: 6 additions & 0 deletions
6
...ion-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Offence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package uk.gov.justice.digital.hmpps.api.model.sentence | ||
|
||
data class Offence( | ||
val description: String, | ||
val count: Long | ||
) |
6 changes: 6 additions & 0 deletions
6
...elius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package uk.gov.justice.digital.hmpps.api.model.sentence | ||
|
||
data class SentenceOverview( | ||
val offence: List<MainOffence?> | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Sentence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity | ||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.Table | ||
import org.hibernate.annotations.Immutable | ||
import org.hibernate.annotations.SQLRestriction | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event | ||
|
||
interface EventSentenceRepository : JpaRepository<Event, Long> { | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "offender") | ||
@SQLRestriction("soft_deleted = 0") | ||
class Person( | ||
@Id | ||
@Column(name = "offender_id") | ||
val id: Long, | ||
|
||
@Column(columnDefinition = "char(7)") | ||
val crn: String | ||
) | ||
|
||
@Query( | ||
"SELECT e FROM Event e " + | ||
"JOIN Person p ON p.id = e.personId " + | ||
"LEFT JOIN FETCH e.mainOffence m " + | ||
"LEFT JOIN FETCH e.additionalOffences ao " + | ||
"LEFT JOIN FETCH m.offence mo " + | ||
"LEFT JOIN FETCH ao.offence aoo " + | ||
"WHERE p.crn = :crn " + | ||
"AND e.active = true " | ||
) | ||
fun findActiveSentencesByCrn(crn: String): List<Event> | ||
} | ||
|
33 changes: 33 additions & 0 deletions
33
...vision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package uk.gov.justice.digital.hmpps.service | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.MainOffence | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.Offence | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceOverview | ||
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event | ||
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository | ||
|
||
@Service | ||
class SentenceService( | ||
private val eventRepository: EventSentenceRepository | ||
) { | ||
fun getMostRecentActiveEvent(crn: String): SentenceOverview { | ||
val events = eventRepository.findActiveSentencesByCrn(crn) | ||
|
||
return SentenceOverview(events.map { it.toOffence() }) | ||
} | ||
|
||
fun Event.toOffence() = mainOffence?.let { mainOffence -> | ||
MainOffence(offence = Offence( | ||
mainOffence.offence.description, mainOffence.offenceCount | ||
), | ||
dateOfOffence = mainOffence.date, | ||
notes = notes, | ||
additionalOffences = additionalOffences.map { | ||
Offence( | ||
description = it.offence.description, count = it.offenceCount | ||
) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.