Skip to content

Commit

Permalink
PI-2781 - add new api
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Jan 30, 2025
1 parent ee382cd commit ac603a6
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ 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.Name
import uk.gov.justice.digital.hmpps.api.model.sentence.PreviousOrder
import uk.gov.justice.digital.hmpps.api.model.sentence.PreviousOrderHistory
import uk.gov.justice.digital.hmpps.api.model.overview.Order
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.INACTIVE_EVENT_1
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.INACTIVE_ORDER_1
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.MAIN_OFFENCE_3
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OFFENCE_1
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
Expand All @@ -28,8 +32,14 @@ class OrderIntegrationTest {
mockMvc
.perform(MockMvcRequestBuilders.get("/sentence/X123456/previous-orders"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized)

mockMvc
.perform(MockMvcRequestBuilders.get("/sentence/X123456/previous-orders/1"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized)
}



@Test
fun `no previous orders`() {
val response = mockMvc
Expand All @@ -45,6 +55,21 @@ class OrderIntegrationTest {
assertEquals(expected, response)
}

@Test
fun `no previous orders by crn and event number`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonDetailsGenerator.PERSONAL_DETAILS.crn}/previous-orders/1")
.withToken()
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<PreviousOrderHistory>()

val expected = PreviousOrderHistory(Name("Caroline", "Louise", "Bloggs"), listOf())

assertEquals(expected, response)
}

@Test
fun `return previous orders`() {
val response = mockMvc
Expand All @@ -68,4 +93,36 @@ class OrderIntegrationTest {

assertEquals(expected, response)
}

@Test
fun `return previous orders by crn and event id`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/previous-orders/${INACTIVE_EVENT_1.eventNumber}").withToken()
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<PreviousOrderInformation>()

val expected = PreviousOrderInformation(
Name("Forename", "Middle1", "Surname"),
"Default Sentence Type (25 Years)",
Sentence(
OffenceDetails(
INACTIVE_EVENT_1.eventNumber,
Offence(OFFENCE_1.description, 1),
MAIN_OFFENCE_3.date,
INACTIVE_EVENT_1.notes,
listOf()
),
Conviction(null, null, null, listOf()),
Order(INACTIVE_ORDER_1.type.description, INACTIVE_ORDER_1.length, startDate = INACTIVE_ORDER_1.date, breaches = 0, endDate = null),
courtDocuments = listOf(),
requirements = null,
licenceConditions = null
)

)

assertEquals(expected, response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class SentenceController(
@Operation(summary = "Display inactive events")
fun getPreviousEvents(@PathVariable crn: String) = ordersService.getPreviousEvents(crn)

@GetMapping("/previous-orders/{eventNumber}")
@Operation(summary = "Display inactive events")
fun getPreviousEvent(@PathVariable crn: String, @PathVariable eventNumber: String) = ordersService.getPreviousEvent(crn, eventNumber)

@GetMapping("/offences/{eventNumber}")
@Operation(summary = "Display additional offence details")
fun getAdditionalOffenceDetails(@PathVariable crn: String, @PathVariable eventNumber: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ import uk.gov.justice.digital.hmpps.api.model.Name
data class PreviousOrderHistory(
val name: Name,
val previousOrders: List<PreviousOrder> = emptyList()
)

data class PreviousOrderInformation(
val name: Name,
val title: String? = null,
val sentence: Sentence? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ data class Sentence(
val offenceDetails: OffenceDetails,
val conviction: Conviction? = null,
val order: Order? = null,
val requirements: List<Requirement> = listOf(),
val requirements: List<Requirement>?,
val courtDocuments: List<CourtDocument> = listOf(),
val unpaidWorkProgress: String? = null,
val licenceConditions: List<LicenceCondition> = listOf()
val licenceConditions: List<LicenceCondition>?
)

data class MinimalSentence(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ interface EventSentenceRepository : JpaRepository<Event, Long> {
)
fun findSentencesByPersonId(id: Long): List<Event>

fun findEventByPersonIdAndEventNumberAndActiveIsFalse(id: Long, eventNumber: String): Event?

fun findEventByPersonIdAndEventNumberAndActiveIsTrue(id: Long, eventNumber: String): Event?

}

fun EventSentenceRepository.getEvent(id: Long, eventNumber: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package uk.gov.justice.digital.hmpps.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.sentence.PreviousOrder
import uk.gov.justice.digital.hmpps.api.model.sentence.PreviousOrderHistory
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.getPerson
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.*

@Service
class OrdersService(
private val personRepository: PersonRepository,
private val eventRepository: EventSentenceRepository
private val eventRepository: EventSentenceRepository,
private val sentenceService: SentenceService
) {

fun getPreviousEvents(crn: String): PreviousOrderHistory {
Expand All @@ -23,6 +23,13 @@ class OrdersService(
return PreviousOrderHistory(name = person.toName(), events.map { it.toPreviousOrder() })
}

fun getPreviousEvent(crn: String, eventNumber: String): PreviousOrderInformation {
val person = personRepository.getPerson(crn)
val event = eventRepository.findEventByPersonIdAndEventNumberAndActiveIsFalse(person.id, eventNumber)

return PreviousOrderInformation(person.toName(), event?.setTitle() , sentenceService.getInactiveEvent(event))
}

fun Person.toName() =
Name(forename, secondName, surname)

Expand All @@ -31,4 +38,9 @@ class OrdersService(
mainOffence?.offence?.description,
disposal?.terminationDate
)

fun Event.setTitle(): String? {
return "${disposal?.type?.description} (${disposal?.length} ${disposal?.lengthUnit?.description})"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SentenceService(
null -> true
else -> eventNumber == it.eventNumber
}
}?.toSentence(crn)
}?.toSentence()
)
}

Expand Down Expand Up @@ -76,6 +76,8 @@ class SentenceService(
)
}

fun getInactiveEvent(event: Event?) = event?.toInactiveSentence()

fun Event.toSentenceSummary() = SentenceSummary(
eventNumber,
disposal?.type?.description ?: "Pre-Sentence"
Expand All @@ -94,29 +96,16 @@ class SentenceService(
.map { it.toMinimalRequirement() },
)

fun Event.toSentence(crn: String): Sentence {
fun Event.toSentence(): Sentence {
val courtAppearance = courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(id)
val additionalSentences = additionalSentenceRepository.getAllByEventId(id)

return Sentence(
OffenceDetails(
eventNumber = eventNumber,
offence = mainOffence?.let { Offence(it.offence.description, it.offenceCount) },
dateOfOffence = mainOffence?.date,
notes = notes,
additionalOffences = additionalOffences.map {
Offence(description = it.offence.description, count = it.offenceCount ?: 0)
}
),
Conviction(
sentencingCourt = courtAppearance?.court?.name,
responsibleCourt = court?.name,
convictionDate = convictionDate,
additionalSentences.map { it.toAdditionalSentence() }
),
toOffenceDetails(),
toConviction(courtAppearance, additionalSentences),
order = disposal?.toOrder(),
requirements = requirementRepository.getRequirements(id, eventNumber)
.map { it.toRequirement() },
.map { it.toRequirement() } ,
courtDocuments = documentRepository.getCourtDocuments(id, eventNumber).map { it.toCourtDocument() },
unpaidWorkProgress = disposal?.id?.let { getUnpaidWorkTime(it) },
licenceConditions = disposal?.let {
Expand All @@ -127,6 +116,36 @@ class SentenceService(
)
}

fun Event.toInactiveSentence(): Sentence {
val courtAppearance = courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(id)
val additionalSentences = additionalSentenceRepository.getAllByEventId(id)

return Sentence(
toOffenceDetails(),
toConviction(courtAppearance, additionalSentences),
order = disposal?.toOrder(),
courtDocuments = documentRepository.getCourtDocuments(id, eventNumber).map { it.toCourtDocument() },
requirements = null,
licenceConditions = null
)
}

fun Event.toOffenceDetails() = OffenceDetails(
eventNumber = eventNumber,
offence = mainOffence?.let { Offence(it.offence.description, it.offenceCount) },
dateOfOffence = mainOffence?.date,
notes = notes,
additionalOffences = additionalOffences.map {
Offence(description = it.offence.description, count = it.offenceCount ?: 0)
}
)

fun Event.toConviction(courtAppearance: CourtAppearance?, additionalSentences: List<ExtraSentence>) = Conviction(
sentencingCourt = courtAppearance?.court?.name,
responsibleCourt = court?.name,
convictionDate = convictionDate,
additionalSentences.map { it.toAdditionalSentence() }
)
fun ExtraSentence.toAdditionalSentence(): AdditionalSentence =
AdditionalSentence(length, amount, notes, type.description)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class OrderServiceTest {
@Mock
lateinit var eventRepository: EventSentenceRepository

@Mock
lateinit var sentenceService: SentenceService

@InjectMocks
lateinit var service: OrdersService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SentenceServiceTest {
person = PersonGenerator.OVERVIEW,
active = true,
inBreach = true,
disposal = PersonGenerator.ACTIVE_ORDER,
disposal = ACTIVE_ORDER,
eventNumber = "123457",
mainOffence = PersonGenerator.MAIN_OFFENCE_1,
notes = "overview",
Expand Down Expand Up @@ -306,7 +306,7 @@ class SentenceServiceTest {
null
)

assertEquals(expected, response.sentence!!.requirements[0])
assertEquals(expected, response.sentence!!.requirements!![0])
}

@Test
Expand Down Expand Up @@ -379,7 +379,7 @@ class SentenceServiceTest {
null
)

assertEquals(expected, response.sentence!!.requirements[0])
assertEquals(expected, response.sentence!!.requirements!![0])
}

@Test
Expand Down

0 comments on commit ac603a6

Please sign in to comment.