Skip to content

Commit

Permalink
MAN-28 - add new api to get individual note
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Oct 18, 2024
1 parent c2f31fc commit 1f65a94
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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.MockMvcResultHandlers.print
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNote
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNoteDetail
import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.service.toSummary
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 LicenceConditionIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Test
fun `unauthorized status returned`() {
mockMvc
.perform(MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/1/note/1"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized)
}

@Test
fun `licence condition not found`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/1/note/6")
.withToken()
)
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>()

val expected = LicenceConditionNoteDetail(PersonGenerator.OVERVIEW.toSummary())

assertEquals(expected, response)
}

@Test
fun `note not found`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/${LicenceConditionGenerator.LC_WITH_NOTES.id}/note/7")
.withToken()
)
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>()

val expected = LicenceConditionNoteDetail(PersonGenerator.OVERVIEW.toSummary())

assertEquals(expected, response)
}

@Test
fun `get note for licence condition`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/${LicenceConditionGenerator.LC_WITH_NOTES.id}/note/1")
.withToken()
)
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>()

val expected = LicenceConditionNoteDetail(
PersonGenerator.OVERVIEW.toSummary(),
LicenceConditionNote(
1,
"Joe Root",
LocalDate.of(2024, 4, 23),
"""
You must not drink any alcohol until Wednesday 7th August 2024 unless your
probation officer says you can. You will need to wear an electronic tag all the time so
we can check this.
""".trimIndent(),
false
)
)

assertEquals(expected, response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ 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.ContactService
import uk.gov.justice.digital.hmpps.service.OffenceService
import uk.gov.justice.digital.hmpps.service.OrdersService
import uk.gov.justice.digital.hmpps.service.SentenceService
import uk.gov.justice.digital.hmpps.service.*

@RestController
@Tag(name = "Sentence")
Expand All @@ -20,7 +17,8 @@ class SentenceController(
private val sentenceService: SentenceService,
private val ordersService: OrdersService,
private val offenceService: OffenceService,
private val contactService: ContactService
private val contactService: ContactService,
private val licenceConditionService: LicenceConditionService
) {

@GetMapping
Expand All @@ -39,4 +37,11 @@ class SentenceController(
@GetMapping("/contacts")
@Operation(summary = "Display contacts")
fun getContacts(@PathVariable crn: String) = contactService.getContacts(crn)

@GetMapping("/licence-condition/{licenceConditionId}/note/{noteId}")
@Operation(summary = "Get licence condition note")
fun getLicenceConditionNote(
@PathVariable crn: String,
@PathVariable licenceConditionId: Long,
@PathVariable noteId: Int) = licenceConditionService.getLicenceConditionNote(crn, licenceConditionId, noteId)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

import uk.gov.justice.digital.hmpps.api.model.PersonSummary
import java.time.LocalDate

data class LicenceCondition(
Expand All @@ -18,3 +19,8 @@ data class LicenceConditionNote(
val note: String,
val hasNotesBeenTruncated: Boolean? = null
)

data class LicenceConditionNoteDetail(
val personSummary: PersonSummary,
val note: LicenceConditionNote? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ class LicenceCondition(
)

interface LicenceConditionRepository : JpaRepository<LicenceCondition, Long> {

fun findAllByDisposalId(disposalId: Long): List<LicenceCondition>
}

fun LicenceConditionRepository.getByLicenceConditionId(id: Long) = findById(id)

@Immutable
@Table(name = "r_lic_cond_type_main_cat")
@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package uk.gov.justice.digital.hmpps.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNote
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceConditionNoteDetail
import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter
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.LicenceConditionRepository
import java.time.LocalDate
import kotlin.jvm.optionals.getOrNull
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.LicenceCondition as EntityLicenceCondition

@Service
class LicenceConditionService(
private val personRepository: PersonRepository,
private val licenceConditionRepository: LicenceConditionRepository) {

fun getLicenceConditionNote(crn: String, licenceConditionId: Long, noteId: Int): LicenceConditionNoteDetail {
val person = personRepository.getPerson(crn)

val licenceCondition = licenceConditionRepository.findById(licenceConditionId).getOrNull()

return LicenceConditionNoteDetail(
person.toSummary(),
licenceCondition?.toLicenceConditionNote()?.let {
when {
it.size > noteId -> it[noteId]
else -> null
}
}
)

}
}

fun EntityLicenceCondition.toLicenceConditionNote(): List<LicenceConditionNote>? {

return notes?.let {
val splitParam = "---------------------------------------------------------" + System.lineSeparator()
notes.split(splitParam).mapIndexed { index, note ->
val matchResult = Regex(
"^Comment added by (.+?) on (\\d{2}/\\d{2}/\\d{4}) at \\d{2}:\\d{2}"
+ System.lineSeparator()
).find(note)
val commentLine = matchResult?.value
val commentText = commentLine?.let { note.removePrefix(commentLine) } ?: note

val userCreatedBy = matchResult?.groupValues?.get(1)
val dateCreatedBy = matchResult?.groupValues?.get(2)
?.let { LocalDate.parse(it, DeliusDateFormatter) }


LicenceConditionNote(
index,
userCreatedBy,
dateCreatedBy,
commentText.removeSuffix(System.lineSeparator()).chunked(1500)[0],
note.length > 1500
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import uk.gov.justice.digital.hmpps.api.model.overview.Rar
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.api.model.sentence.Offence
import uk.gov.justice.digital.hmpps.api.model.sentence.Requirement
import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.CourtDocumentDetails
import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.DocumentRepository
Expand Down Expand Up @@ -85,40 +84,9 @@ class SentenceService(
subCategory?.description,
imposedReleasedDate,
actualStartDate,
populateLicenceConditionNotes(notes)
toLicenceConditionNote()
)

fun populateLicenceConditionNotes(notes: String?): List<LicenceConditionNote>? {
val noteLength = 1500

notes?.let {
val splitParam = "---------------------------------------------------------" + System.lineSeparator()
return notes.split(splitParam).mapIndexed { index, note ->
val matchResult = Regex(
"^Comment added by (.+?) on (\\d{2}/\\d{2}/\\d{4}) at \\d{2}:\\d{2}"
+ System.lineSeparator()
).find(note)
val commentLine = matchResult?.value
val commentText = commentLine?.let { note.removePrefix(commentLine) } ?: note

val userCreatedBy = matchResult?.groupValues?.get(1)
val dateCreatedBy = matchResult?.groupValues?.get(2)
?.let { LocalDate.parse(it, DeliusDateFormatter) }


LicenceConditionNote(
index,
userCreatedBy,
dateCreatedBy,
commentText.removeSuffix(System.lineSeparator()).chunked(1500)[0],
note.length > noteLength
)
}

}

return null
}

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

0 comments on commit 1f65a94

Please sign in to comment.