diff --git a/projects/justice-email-and-delius/src/dev/resources/messages/successful-long-message.json b/projects/justice-email-and-delius/src/dev/resources/messages/successful-long-message.json new file mode 100644 index 000000000..c9c2be9a3 --- /dev/null +++ b/projects/justice-email-and-delius/src/dev/resources/messages/successful-long-message.json @@ -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": "example@justice.gov.uk", + "receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]" +} \ No newline at end of file diff --git a/projects/justice-email-and-delius/src/dev/resources/messages/successful-message.json b/projects/justice-email-and-delius/src/dev/resources/messages/successful-message.json index dc96ce335..8209bce0d 100644 --- a/projects/justice-email-and-delius/src/dev/resources/messages/successful-message.json +++ b/projects/justice-email-and-delius/src/dev/resources/messages/successful-message.json @@ -1,6 +1,6 @@ { "id": "00000000-0000-0000-0000-000000000000", - "subject": "RE: A000001", + "subject": "A000001 was involved in an incident", "bodyContent": "Example message", "fromEmailAddress": "example@justice.gov.uk", "receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]" diff --git a/projects/justice-email-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/justice-email-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index 161b2e216..ae2014436 100644 --- a/projects/justice-email-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/justice-email-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -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 @@ -41,8 +41,37 @@ 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.notes, equalTo(""" + |This contact was created automatically from a forwarded email sent by example@justice.gov.uk 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("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 in ~")) + assertThat(contact.notes, equalTo(""" + |This contact was created automatically from a forwarded email sent by example@justice.gov.uk 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") @@ -113,6 +142,9 @@ internal class IntegrationTest { assertThat( contact.notes, equalTo( """ + This contact was created automatically from a forwarded email sent by example@justice.gov.uk at 12:34 on 01/01/2020. + Subject: A000001 was involved in an incident + Paragraph 1 Paragraph 2 with **bold** text diff --git a/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Contact.kt b/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Contact.kt index 41085d168..60f108607 100644 --- a/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Contact.kt +++ b/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Contact.kt @@ -33,6 +33,8 @@ class Contact( @Lob val notes: String, + val description: String?, + @Column(name = "probation_area_id") val providerId: Long, @@ -82,7 +84,7 @@ class ContactType( val code: String ) { enum class Code(val code: String) { - EMAIL_TEXT_FROM_OTHER("CM3A") + EMAIL("C135") } } diff --git a/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt b/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt index 264186b85..9a768aab1 100644 --- a/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt +++ b/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt @@ -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 { fun findByCrn(crn: String): Person? diff --git a/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt b/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt index 7ccfd7cc9..9a452de73 100644 --- a/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt +++ b/projects/justice-email-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Handler.kt @@ -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") @@ -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, @@ -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") @@ -104,4 +114,11 @@ 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(max: Int = 200) = + substring(0, length.coerceAtMost(max)) + if (length > max) "~" else "" } \ No newline at end of file