Skip to content

Commit

Permalink
MAN-36 - create common note format function
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Jan 9, 2025
1 parent b25fa04 commit d51e942
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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.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.api.model.sentence.NoteDetail
import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator
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
Expand Down Expand Up @@ -91,7 +91,7 @@ class LicenceConditionIntegrationTest {
LIC_COND_SUB_CAT.description,
LocalDate.now().minusDays(7),
LocalDate.now(),
licenceConditionNote = LicenceConditionNote(
licenceConditionNote = NoteDetail(
1,
"CVL Service",
LocalDate.of(2024, 4, 22),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class SentenceIntegrationTest {
LocalDate.now().minusDays(7),
LocalDate.now(),
listOf(
LicenceConditionNote(
NoteDetail(
0,
"Joe Root",
LocalDate.of(2024, 4, 23),
Expand All @@ -170,7 +170,7 @@ class SentenceIntegrationTest {
""".trimIndent(),
false
),
LicenceConditionNote(
NoteDetail(
1,
"CVL Service",
LocalDate.of(2024, 4, 22),
Expand All @@ -194,7 +194,7 @@ class SentenceIntegrationTest {
LocalDate.now().minusDays(7),
LocalDate.now(),
listOf(
LicenceConditionNote(
NoteDetail(
0,
note = "He shall not contact or associate with Peter Jones without the prior approval of the supervising officer;",
hasNoteBeenTruncated = false
Expand All @@ -208,7 +208,7 @@ class SentenceIntegrationTest {
LocalDate.now().minusDays(7),
LocalDate.now(),
listOf(
LicenceConditionNote(
NoteDetail(
0,
"Tom Brady",
LocalDate.of(2024, 10, 29),
Expand All @@ -217,7 +217,7 @@ class SentenceIntegrationTest {
""".trimIndent(),
false
),
LicenceConditionNote(
NoteDetail(
1,
"Harry Kane",
LocalDate.of(2024, 10, 29),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ data class LicenceCondition(
val subTypeDescription: String? = null,
val imposedReleasedDate: LocalDate,
val actualStartDate: LocalDate? = null,
val licenceConditionNotes: List<LicenceConditionNote>? = null,
val licenceConditionNote: LicenceConditionNote? = null
val licenceConditionNotes: List<NoteDetail>? = null,
val licenceConditionNote: NoteDetail? = null
)

data class LicenceConditionNote(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ data class MinimalOrder(
val startDate: LocalDate,
val endDate: LocalDate? = null,
)

data class NoteDetail(
val id: Int,
val createdBy: String? = null,
val createdByDate: LocalDate? = null,
val note: String,
val hasNoteBeenTruncated: Boolean? = null
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
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.api.model.sentence.MinimalLicenceCondition
import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter
import uk.gov.justice.digital.hmpps.api.model.sentence.*
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

Expand Down Expand Up @@ -53,37 +48,7 @@ fun EntityLicenceCondition.toLicenceConditionSingleNote(noteId: Int, truncateNot
licenceConditionNote = toLicenceConditionNote(truncateNote).elementAtOrNull(noteId)
)

fun EntityLicenceCondition.toLicenceConditionNote(truncateNote: Boolean): List<LicenceConditionNote> {
fun EntityLicenceCondition.toLicenceConditionNote(truncateNote: Boolean): List<NoteDetail> {

return notes?.let {
val splitParam = "---------------------------------------------------------" + System.lineSeparator()
notes.split(splitParam).asReversed().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).removeSuffix(System.lineSeparator()) } ?: 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 -> commentText.length > 1500
else -> null
}
)
}
} ?: listOf()
return formatNote(notes, truncateNote)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 @@ -222,4 +223,38 @@ class SentenceService(
}
return null
}
}

fun formatNote(notes: String?, truncateNote: Boolean): List<NoteDetail> {
return notes?.let {
val splitParam = "---------------------------------------------------------" + System.lineSeparator()
notes.split(splitParam).asReversed().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).removeSuffix(System.lineSeparator()) } ?: note

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


NoteDetail(
index,
userCreatedBy,
dateCreatedBy,
when (truncateNote) {
true -> commentText.removeSuffix(System.lineSeparator()).chunked(1500)[0]
else -> commentText
},
when (truncateNote) {
true -> commentText.length > 1500
else -> null
}
)
}
} ?: listOf()
}

0 comments on commit d51e942

Please sign in to comment.