Skip to content

Commit

Permalink
PI-1939 - add name and event number to response object
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Mar 22, 2024
1 parent fb293d8 commit 24cb891
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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.Name
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
Expand All @@ -27,7 +28,9 @@ class SentenceIntegrationTest {
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<SentenceOverview>()

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

assertEquals(expected, response)
}
Expand All @@ -40,9 +43,11 @@ class SentenceIntegrationTest {
.andReturn().response.contentAsJson<SentenceOverview>()

val expected = SentenceOverview(
Name("Forename", "Middle1", "Surname"),
listOf(
Sentence(
OffenceDetails(
"7654321",
Offence("Murder", 1),
LocalDate.now(),
"overview",
Expand All @@ -60,6 +65,7 @@ class SentenceIntegrationTest {
),
Sentence(
OffenceDetails(
"1234567",
Offence("Another Murder", 1),
LocalDate.now(),
"overview",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.api.model.sentence
import java.time.LocalDate

data class OffenceDetails(
val eventNumber: String,
val offence: Offence?,
val dateOfOffence: LocalDate?,
val notes: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

data class Sentence(
val offence: OffenceDetails,
val offenceDetails: OffenceDetails,
val conviction: Conviction? = null,

)
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

import uk.gov.justice.digital.hmpps.api.model.Name

data class SentenceOverview(
val name: Name,
val sentences: List<Sentence>,
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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.*
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonSummaryEntity
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.getSummary
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance
Expand All @@ -14,22 +16,25 @@ import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Addition
@Service
class SentenceService(
private val eventRepository: EventSentenceRepository,
private val courtApperanceRepository: CourtAppearanceRepository,
private val courtAppearanceRepository: CourtAppearanceRepository,
private val additionalSentenceRepository: AdditionalSentenceRepository,
private val personRepository: PersonRepository
) {
fun getMostRecentActiveEvent(crn: String): SentenceOverview {
val person = personRepository.getSummary(crn)
val events = eventRepository.findActiveSentencesByPersonId(person.id)
return SentenceOverview(events.map {
val courtAppearance = courtApperanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id)
return SentenceOverview(
name = person.toName(),
sentences = events.map {
val courtAppearance = courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id)
val additionalSentences = additionalSentenceRepository.getAllByEventId(it.id)
it.toSentence(courtAppearance, additionalSentences)
})
}

fun Event.toSentence(courtAppearance: CourtAppearance?, additionalSentences: List<ExtraSentence>) = Sentence(
OffenceDetails(
eventNumber = eventNumber,
offence = mainOffence?.let { Offence(it.offence.description, it.offenceCount) },
dateOfOffence = mainOffence?.date,
notes = notes,
Expand All @@ -47,4 +52,7 @@ class SentenceService(

fun ExtraSentence.toAdditionalSentence(): AdditionalSentence =
AdditionalSentence(length, amount, notes, type.description)

fun PersonSummaryEntity.toName() =
Name(forename, secondName , surname)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.*
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.data.generator.AdditionalSentenceGenerator
import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator
import uk.gov.justice.digital.hmpps.data.generator.CourtGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
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.PersonSummaryEntity
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearanceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository
Expand Down Expand Up @@ -43,7 +42,6 @@ class SentenceServiceTest {

@BeforeEach
fun setup() {
val crn = "X000005"
personSummary = PersonalDetailsServiceTest.Summary(
id = 1,
forename = "TestName",
Expand All @@ -59,9 +57,10 @@ class SentenceServiceTest {
listOf()
)

val expected = SentenceOverview(Name("TestName", surname = "TestSurname"),listOf())
val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn)

assertEquals(SentenceOverview(listOf()), response)
assertEquals(expected,response)
verify(personRepository, times(1)).findSummary(PersonGenerator.OVERVIEW.crn)
verify(eventRepository, times(1)).findActiveSentencesByPersonId(personSummary.id)

Expand Down Expand Up @@ -105,9 +104,11 @@ class SentenceServiceTest {
val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn)

val expected = SentenceOverview(
Name("TestName", surname = "TestSurname"),
listOf(
Sentence(
OffenceDetails(
"123457",
Offence("Murder", 1),
LocalDate.now(),
"overview",
Expand Down Expand Up @@ -138,15 +139,4 @@ class SentenceServiceTest {
verifyNoMoreInteractions(courtAppearanceRepository)

}

data class Summary(
override val id: Long,
override val forename: String,
override val secondName: String? = null,
override val thirdName: String? = null,
override val surname: String,
override val crn: String,
override val pnc: String?,
override val dateOfBirth: LocalDate
) : PersonSummaryEntity
}

0 comments on commit 24cb891

Please sign in to comment.