Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* PI-2629 PI-2648 PI-2650

---------

Co-authored-by: probation-integration-bot[bot] <177347787+probation-integration-bot[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 72d931d commit b25fa04
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "A000001 was involved in an incident that has a very, very long subject that surpassed the 200 character limit and therefore has been truncated to meet the character limitations for the description in Delius.",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "RE: A000001",
"subject": "A000001 was involved in an incident",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean
import uk.gov.justice.digital.hmpps.data.generator.Data
import uk.gov.justice.digital.hmpps.entity.Contact
import uk.gov.justice.digital.hmpps.entity.ContactRepository
import uk.gov.justice.digital.hmpps.entity.ContactType.Code.EMAIL_TEXT_FROM_OTHER
import uk.gov.justice.digital.hmpps.entity.ContactType.Code.EMAIL
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.messaging.EmailMessage
import uk.gov.justice.digital.hmpps.messaging.Handler
Expand All @@ -41,8 +41,49 @@ internal class IntegrationTest {
verify(telemetryService).notificationReceived(notification)

val contact = verifyContactCreated()
assertThat(contact.type.code, equalTo(EMAIL_TEXT_FROM_OTHER.code))
assertThat(contact.notes, equalTo("Example message\n"))
assertThat(contact.type.code, equalTo(EMAIL.code))
assertThat(contact.description, equalTo("Email - was involved in an incident"))
assertThat(
contact.notes, equalTo(
"""
|This contact was created automatically from a forwarded email sent by [email protected] at 12:34 on 01/01/2020.
|Subject: A000001 was involved in an incident
|
|Example message
|""".trimMargin()
)
)
assertThat(
contact.externalReference,
equalTo("urn:uk:gov:hmpps:justice-email:00000000-0000-0000-0000-000000000000")
)
assertThat(contact.staffId, equalTo(Data.STAFF.id))
assertThat(contact.teamId, equalTo(Data.MANAGER.teamId))
assertThat(contact.providerId, equalTo(Data.MANAGER.providerId))
}

@Test
fun `description is truncated if over 200 chars`() {
val notification = Notification(get<EmailMessage>("successful-long-message"))
handler.handle(notification)
verify(telemetryService).notificationReceived(notification)

val contact = verifyContactCreated()
assertThat(contact.type.code, equalTo(EMAIL.code))
assertThat(
contact.description,
equalTo("Email - was involved in an incident that has a very, very long subject that surpassed the 200 character limit and therefore has been truncated to meet the character limitations for the description i ~")
)
assertThat(
contact.notes, equalTo(
"""
|This contact was created automatically from a forwarded email sent by [email protected] at 12:34 on 01/01/2020.
|Subject: A000001 was involved in an incident that has a very, very long subject that surpassed the 200 character limit and therefore has been truncated to meet the character limitations for the description in Delius.
|
|Example message
|""".trimMargin()
)
)
assertThat(
contact.externalReference,
equalTo("urn:uk:gov:hmpps:justice-email:00000000-0000-0000-0000-000000000000")
Expand Down Expand Up @@ -113,6 +154,9 @@ internal class IntegrationTest {
assertThat(
contact.notes, equalTo(
"""
This contact was created automatically from a forwarded email sent by [email protected] at 12:34 on 01/01/2020.
Subject: A000001 was involved in an incident
Paragraph 1
Paragraph 2 with **bold** text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Contact(
@Lob
val notes: String,

val description: String?,

@Column(name = "probation_area_id")
val providerId: Long,

Expand Down Expand Up @@ -82,7 +84,7 @@ class ContactType(
val code: String
) {
enum class Code(val code: String) {
EMAIL_TEXT_FROM_OTHER("CM3A")
EMAIL("C135")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class Person(

@Column
val softDeleted: Boolean = false,
)
) {
companion object {
const val CRN_REGEX = "[A-Za-z][0-9]{6}"
}
}

interface PersonRepository : JpaRepository<Person, Long> {
fun findByCrn(crn: String): Person?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import uk.gov.justice.digital.hmpps.audit.BusinessInteractionCode.ADD_CONTACT
import uk.gov.justice.digital.hmpps.audit.service.AuditableService
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.converter.NotificationConverter
import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter
import uk.gov.justice.digital.hmpps.entity.*
import uk.gov.justice.digital.hmpps.entity.ContactType.Code.EMAIL_TEXT_FROM_OTHER
import uk.gov.justice.digital.hmpps.entity.ContactType.Code.EMAIL
import uk.gov.justice.digital.hmpps.entity.Person.Companion.CRN_REGEX
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.telemetry.TelemetryMessagingExtensions.notificationReceived
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import java.time.format.DateTimeFormatter

@Component
@Channel("justice-email-and-delius-queue")
Expand Down Expand Up @@ -47,14 +50,21 @@ class Handler(
val person = personRepository.getByCrn(crn)
val manager = personManagerRepository.getManager(person.id)
val staffId = findStaffIdForEmailAddress(emailAddress) ?: manager.staffId
val fullNotes = """
|This contact was created automatically from a forwarded email sent by ${message.fromEmailAddress} ${message.onAt}.
|Subject: ${message.subject}
|
|${htmlToMarkdownConverter.convert(message.bodyContent)}
""".trimMargin()
val contact = contactRepository.save(
Contact(
personId = person.id,
externalReference = "urn:uk:gov:hmpps:justice-email:${message.id}",
type = contactTypeRepository.getByCode(EMAIL_TEXT_FROM_OTHER),
type = contactTypeRepository.getByCode(EMAIL),
date = message.receivedDateTime,
startTime = message.receivedDateTime,
notes = htmlToMarkdownConverter.convert(message.bodyContent),
description = "Email - ${message.subject.replace(CRN_REGEX.toRegex(), "").trim()}".truncated(),
notes = fullNotes,
staffId = staffId,
teamId = manager.teamId,
providerId = manager.providerId,
Expand All @@ -73,7 +83,7 @@ class Handler(
}

private fun EmailMessage.extractCrn(): String {
val crns = Regex("[A-Za-z][0-9]{6}").findAll(subject).map { it.value }.distinct()
val crns = CRN_REGEX.toRegex().findAll(subject).map { it.value }.distinct()
return when (crns.count()) {
1 -> crns.single().uppercase()
0 -> throw IllegalArgumentException("No CRN in message subject")
Expand Down Expand Up @@ -104,4 +114,10 @@ class Handler(
else -> error("Multiple staff records found for $emailAddress")
}
}

private val EmailMessage.onAt: String
get() = "at ${DateTimeFormatter.ofPattern("hh:mm").format(receivedDateTime)} on " +
DeliusDateFormatter.format(receivedDateTime)

private fun String.truncated() = if (length > 200) "${take(198)} ~" else take(200)
}

0 comments on commit b25fa04

Please sign in to comment.