From da3a0a0ee3f76c029770d91f74d1ad5dc772acb4 Mon Sep 17 00:00:00 2001 From: Marcus Aspin Date: Thu, 19 Dec 2024 16:56:41 +0000 Subject: [PATCH] PI-2708 Add review contact notes to review notes Also add note about risk level change --- .../justice/digital/hmpps/IntegrationTest.kt | 16 +++--- .../delius/person/entity/Registration.kt | 2 +- .../digital/hmpps/service/RiskService.kt | 51 +++++++++++-------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index d28d71a75..2cb98bff0 100644 --- a/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/assessment-summary-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -343,6 +343,7 @@ internal class IntegrationTest { assertThat( review.contact.notes?.trim(), equalTo( """ + The OASys assessment of Review on 07/12/2023 identified the Risk to children to be H. Type: Safeguarding - Risk to children Next Review Date: ${DeliusDateFormatter.format(LocalDate.now().plusMonths(6))} """.trimIndent() @@ -387,14 +388,13 @@ internal class IntegrationTest { registrationRepository.findByPersonIdAndTypeCode(person.id, RiskType.CHILDREN.code).single() assertThat(riskToChildren.level?.code, equalTo(RiskLevel.H.code)) assertThat(riskToChildren.reviews, hasSize(2)) - assertThat( - riskToChildren.reviews[1].contact.notes?.trim(), equalTo( - """ - Type: Safeguarding - Risk to children - Next Review Date: 14/12/2023 - """.trimIndent() - ) - ) + val expectedReviewNotes = """ + The OASys assessment of Review on 07/12/2023 identified the Risk to children to be H. + Type: Safeguarding - Risk to children + Next Review Date: 14/12/2023 + """.trimIndent() + assertThat(riskToChildren.reviews[1].notes?.trim(), equalTo(expectedReviewNotes)) + assertThat(riskToChildren.reviews[1].contact.notes?.trim(), equalTo(expectedReviewNotes)) assertThat(domainEvents.ofType(RiskType.CHILDREN), hasSize(0)) val riskToPrisoner = diff --git a/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt b/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt index 41e231519..79397ee40 100644 --- a/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt +++ b/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Registration.kt @@ -75,7 +75,7 @@ class Registration( private set fun withReview(contact: Contact): Registration { - reviews += RegistrationReview(personId, this, contact, nextReviewDate, null, teamId, staffId) + reviews += RegistrationReview(personId, this, contact, nextReviewDate, null, teamId, staffId, contact.notes) return this } diff --git a/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt b/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt index 7f7d72c6c..3bb025493 100644 --- a/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt +++ b/projects/assessment-summary-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/RiskService.kt @@ -73,24 +73,27 @@ class RiskService( .partition { it.level != null && it.level.code == riskLevel.code } val events = registrationsToRemove.map { person.removeRegistration(it) }.toMutableList() + val type = registerTypeRepository.getByCode(riskType.code) + val level = referenceDataRepository.registerLevel(riskLevel.code) + val existingLevel = RiskLevel.maxByCode(registrationsToRemove.mapNotNull { it.level?.code }) + val assessmentNote = + "The OASys assessment of ${summary.furtherInformation.pOAssessmentDesc} on ${summary.dateCompleted.toDeliusDate()} identified the ${type.description} ${ + when { + existingLevel == null -> "to be" + existingLevel.ordinal < riskLevel.ordinal -> "to have increased to" + existingLevel.ordinal > riskLevel.ordinal -> "to have decreased to" + else -> "to have remained" + } + } ${level.description}." + // Add registration with the identified level if it doesn't already exist if (matchingRegistrations.isEmpty()) { - val type = registerTypeRepository.getByCode(riskType.code) - val level = referenceDataRepository.registerLevel(riskLevel.code) val roshSummary = ordsClient.getRoshSummary(summary.assessmentPk)?.assessments?.singleOrNull() - - val existingLevel = RiskLevel.maxByCode(registrationsToRemove.mapNotNull { it.level?.code }) val notes = """ - |The OASys assessment of ${summary.furtherInformation.pOAssessmentDesc} on ${summary.dateCompleted.toDeliusDate()} identified the ${type.description} ${ - when { - existingLevel == null || existingLevel.ordinal == riskLevel.ordinal -> "to be" - existingLevel.ordinal < riskLevel.ordinal -> "to have increased to" - else -> "to have decreased to" - } - } ${level.description}. - |${roshSummary?.whoAtRisk?.let { "\n|*R10.1 Who is at risk*\n|$it" }} - |${roshSummary?.natureOfRisk?.let { "\n|*R10.2 What is the nature of the risk*\n|$it" }} - """.trimMargin() + |$assessmentNote + |${roshSummary?.whoAtRisk?.let { "\n|*R10.1 Who is at risk*\n|$it" }} + |${roshSummary?.natureOfRisk?.let { "\n|*R10.2 What is the nature of the risk*\n|$it" }} + """.trimMargin() events += registrations.addRegistration(person, type, level, notes).regEvent(person.crn) // Registrations in the type's duplicate group should also be removed @@ -104,8 +107,9 @@ class RiskService( } // Always add a review, regardless of whether the register level has changed - registrationRepository.findByPersonIdAndTypeCode(person.id, riskType.code).singleOrNull() - ?.let { it.withReview(it.reviewContact(person)) } + registrationRepository.findByPersonIdAndTypeCode(person.id, riskType.code).singleOrNull()?.let { + it.withReview(it.reviewContact(person, "$assessmentNote\n${it.notes()}")) + } return@flatMap events } @@ -144,16 +148,19 @@ class RiskService( return registration } - private fun Person.removeRegistration(it: Registration, notes: String? = null): HmppsDomainEvent { - val contact = contactService.createContact(ContactDetail(DEREGISTRATION, notes = notes ?: it.notes()), this) - it.deregister(contact) - return it.deRegEvent(crn) + private fun Person.removeRegistration( + registration: Registration, + notes: String = registration.notes() + ): HmppsDomainEvent { + val contact = contactService.createContact(ContactDetail(DEREGISTRATION, notes = notes), this) + registration.deregister(contact) + return registration.deRegEvent(crn) } - private fun Registration.reviewContact(person: Person) = contactService.createContact( + private fun Registration.reviewContact(person: Person, notes: String = notes()) = contactService.createContact( ContactDetail( ContactType.Code.REGISTRATION_REVIEW, - notes = notes(), + notes = notes, contactType = type.reviewContactType ), person