diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/LicenceConditionIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/LicenceConditionIntegrationTest.kt new file mode 100644 index 0000000000..ab3a59932b --- /dev/null +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/LicenceConditionIntegrationTest.kt @@ -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() + + 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() + + 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() + + 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) + } +} \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt index 3d13a3b658..78139c6758 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt @@ -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") @@ -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 @@ -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) } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/LicenceCondition.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/LicenceCondition.kt index 37ef0cc519..5d11f58d67 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/LicenceCondition.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/LicenceCondition.kt @@ -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( @@ -18,3 +19,8 @@ data class LicenceConditionNote( val note: String, val hasNotesBeenTruncated: Boolean? = null ) + +data class LicenceConditionNoteDetail( + val personSummary: PersonSummary, + val note: LicenceConditionNote? = null +) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/LicenceCondition.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/LicenceCondition.kt index 08874594a0..c6ca5769a6 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/LicenceCondition.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/LicenceCondition.kt @@ -44,9 +44,12 @@ class LicenceCondition( ) interface LicenceConditionRepository : JpaRepository { + fun findAllByDisposalId(disposalId: Long): List } +fun LicenceConditionRepository.getByLicenceConditionId(id: Long) = findById(id) + @Immutable @Table(name = "r_lic_cond_type_main_cat") @Entity diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionService.kt new file mode 100644 index 0000000000..ebb023ac52 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionService.kt @@ -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? { + + 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 + ) + } + } +} diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt index e5fa4362c9..de79b75ce0 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt @@ -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 @@ -85,40 +84,9 @@ class SentenceService( subCategory?.description, imposedReleasedDate, actualStartDate, - populateLicenceConditionNotes(notes) + toLicenceConditionNote() ) - fun populateLicenceConditionNotes(notes: String?): List? { - 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)