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.
Man 28 sentence license condition long notes (#4326)
* MAN-28 - add id attribute to lc note * MAN-28 - do not include lc notes if there are not any * MAN-28 - add id to LicenceCondition data class * MAN-28 - add new api to get individual note * MAN-28 - update api so that single note call is not truncated * MAN-28 - update api so that empty list is returned where no lc note is returned from db * MAN-28 - update api return spec * MAN-28 - update api * Formatting changes --------- Co-authored-by: probation-integration-bot[bot] <177347787+probation-integration-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
919536f
commit 4ac4ff5
Showing
8 changed files
with
231 additions
and
48 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
107 changes: 107 additions & 0 deletions
107
...rc/integrationTest/kotlin/uk/gov/justice/digital/hmpps/LicenceConditionIntegrationTest.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,107 @@ | ||
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.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 | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceCondition | ||
import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator.LC_WITH_NOTES | ||
import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator.LIC_COND_MAIN_CAT | ||
import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator.LIC_COND_SUB_CAT | ||
|
||
@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() | ||
) | ||
.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/${LC_WITH_NOTES.id}/note/7") | ||
.withToken() | ||
) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>() | ||
|
||
val expected = LicenceConditionNoteDetail( | ||
PersonGenerator.OVERVIEW.toSummary(), | ||
LicenceCondition( | ||
LC_WITH_NOTES.id, | ||
LIC_COND_MAIN_CAT.description, | ||
LIC_COND_SUB_CAT.description, | ||
LocalDate.now().minusDays(7), | ||
LocalDate.now() | ||
) | ||
) | ||
|
||
assertEquals(expected, response) | ||
} | ||
|
||
@Test | ||
fun `get note for licence condition`() { | ||
val response = mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/licence-condition/${LC_WITH_NOTES.id}/note/0") | ||
.withToken() | ||
) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andReturn().response.contentAsJson<LicenceConditionNoteDetail>() | ||
|
||
val expected = LicenceConditionNoteDetail( | ||
PersonGenerator.OVERVIEW.toSummary(), | ||
LicenceCondition( | ||
LC_WITH_NOTES.id, | ||
LIC_COND_MAIN_CAT.description, | ||
LIC_COND_SUB_CAT.description, | ||
LocalDate.now().minusDays(7), | ||
LocalDate.now(), | ||
note = LicenceConditionNote( | ||
0, | ||
"CVL Service", | ||
LocalDate.of(2024, 4, 22), | ||
""" | ||
${LicenceConditionGenerator.LONG_NOTE} | ||
""".trimIndent() + System.lineSeparator() | ||
) | ||
) | ||
) | ||
|
||
assertEquals(expected, response) | ||
} | ||
} |
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
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
11 changes: 10 additions & 1 deletion
11
...elius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/LicenceCondition.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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
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( | ||
val id: Long, | ||
val mainDescription: String, | ||
val subTypeDescription: String? = null, | ||
val imposedReleasedDate: LocalDate, | ||
val actualStartDate: LocalDate? = null, | ||
val notes: List<LicenceConditionNote> = listOf() | ||
val notes: List<LicenceConditionNote>? = null, | ||
val note: LicenceConditionNote? = null | ||
) | ||
|
||
data class LicenceConditionNote( | ||
val id: Int, | ||
val createdBy: String? = null, | ||
val createdByDate: LocalDate? = null, | ||
val note: String, | ||
val hasNotesBeenTruncated: Boolean? = null | ||
) | ||
|
||
data class LicenceConditionNoteDetail( | ||
val personSummary: PersonSummary, | ||
val licenceCondition: LicenceCondition? = null | ||
) |
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
85 changes: 85 additions & 0 deletions
85
...nd-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionService.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,85 @@ | ||
package uk.gov.justice.digital.hmpps.service | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.api.model.sentence.LicenceCondition | ||
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?.toLicenceConditionSingleNote(noteId, false) | ||
) | ||
} | ||
} | ||
|
||
fun EntityLicenceCondition.toLicenceCondition() = | ||
LicenceCondition( | ||
id, | ||
mainCategory.description, | ||
subCategory?.description, | ||
imposedReleasedDate, | ||
actualStartDate, | ||
toLicenceConditionNote(true) | ||
) | ||
|
||
fun EntityLicenceCondition.toLicenceConditionSingleNote(noteId: Int, truncateNote: Boolean) = | ||
LicenceCondition( | ||
id, | ||
mainCategory.description, | ||
subCategory?.description, | ||
imposedReleasedDate, | ||
actualStartDate, | ||
note = toLicenceConditionNote(truncateNote).elementAtOrNull(noteId) | ||
) | ||
|
||
fun EntityLicenceCondition.toLicenceConditionNote(truncateNote: Boolean): 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, | ||
when (truncateNote) { | ||
true -> commentText.removeSuffix(System.lineSeparator()).chunked(1500)[0] | ||
else -> commentText | ||
}, | ||
when (truncateNote) { | ||
true -> note.length > 1500 | ||
else -> null | ||
} | ||
) | ||
} | ||
} ?: listOf() | ||
} |
Oops, something went wrong.