Skip to content

Commit

Permalink
Merge pull request #2740 from ministryofjustice/CAS-1305-hide-rejecti…
Browse files Browse the repository at this point in the history
…on-reasons-notes-in-PP-timelines

CAS-1305 Hide rejection reasons notes from PP timelines
  • Loading branch information
muhammad-elabdulla authored Dec 23, 2024
2 parents 90f484d + 60e27de commit b0eb355
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class AssessmentReferralHistoryNoteTransformer(
else -> throw RuntimeException("Unsupported ReferralHistoryNote type: ${jpa::class.qualifiedName}")
}

fun transformJpaToApi(jpa: AssessmentReferralHistoryNoteEntity, assessmentEntity: TemporaryAccommodationAssessmentEntity): ReferralHistoryNote = when (jpa) {
fun transformJpaToApi(jpa: AssessmentReferralHistoryNoteEntity, assessmentEntity: TemporaryAccommodationAssessmentEntity, includeUserNotes: Boolean): ReferralHistoryNote = when (jpa) {
is AssessmentReferralHistoryUserNoteEntity -> {
transformJpaToReferralHistoryUserNote(jpa)
}
is AssessmentReferralHistorySystemNoteEntity -> ReferralHistorySystemNote(
id = jpa.id,
createdAt = jpa.createdAt.toInstant(),
message = jpa.message,
messageDetails = getMessageDetails(jpa.type, assessmentEntity),
messageDetails = getMessageDetails(jpa.type, assessmentEntity, includeUserNotes),
createdByUserName = jpa.createdByUser.name,
type = "system",
category = transformSystemNoteTypeToCategory(jpa.type),
Expand All @@ -70,11 +70,15 @@ class AssessmentReferralHistoryNoteTransformer(
)
}

private fun getMessageDetails(systemNoteType: ReferralHistorySystemNoteType, assessmentEntity: TemporaryAccommodationAssessmentEntity): ReferralHistoryNoteMessageDetails? {
private fun getMessageDetails(
systemNoteType: ReferralHistorySystemNoteType,
assessmentEntity: TemporaryAccommodationAssessmentEntity,
includeUserNotes: Boolean,
): ReferralHistoryNoteMessageDetails? {
if (systemNoteType == ReferralHistorySystemNoteType.REJECTED && assessmentEntity.referralRejectionReason != null) {
return ReferralHistoryNoteMessageDetails(
assessmentEntity.referralRejectionReason?.name,
assessmentEntity.referralRejectionReasonDetail,
if (includeUserNotes) assessmentEntity.referralRejectionReasonDetail else null,
assessmentEntity.isWithdrawn,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class AssessmentTransformer(

val notes = assessment.referralHistoryNotes.map {
if (it.id == lastReferralRejectedHistoryNote?.id) {
assessmentReferralHistoryNoteTransformer.transformJpaToApi(it, assessment)
assessmentReferralHistoryNoteTransformer.transformJpaToApi(it, assessment, includeUserNotes)
} else {
assessmentReferralHistoryNoteTransformer.transformJpaToApi(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.TemporaryAcco
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.TemporaryAccommodationAssessmentEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserRole
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.randomStringMultiCaseWithNumbers
import java.time.Instant
import java.time.LocalDate
import java.time.OffsetDateTime
Expand Down Expand Up @@ -100,6 +101,35 @@ class Cas3TimelineTest : IntegrationTestBase() {
}
}

@Test
fun `getTimelineEvents does not return referral rejection detail notes when the user has CAS3_REFERRER ROLE`() {
givenAUser(roles = listOf(UserRole.CAS3_REFERRER)) { userEntity, jwt ->
givenAnOffender { offenderDetails, _ ->

val application = persistApplication(crn = offenderDetails.otherIds.crn, user = userEntity)
val assessment = persistRejectedAssessment(application)

addReferralHistoryNotes(assessment)
addDomainEventTimelineEvents(assessment)

assessment.schemaUpToDate = true

val result = webTestClient.get()
.uri("/cas3/timeline/${assessment.id}")
.header("Authorization", "Bearer $jwt")
.exchange()
.expectBodyList(ReferralHistoryNote::class.java)
.returnResult()
.responseBody!!

assertThat(result.size).isEqualTo(7)
assertThat(result.map { it.type }).doesNotContain("user")
assertThat(result.map { it.type }).containsOnly("system", "domainEvent")
assertThat(result.map { it.messageDetails?.rejectionReasonDetails }).containsOnlyNulls()
}
}
}

private fun persistAssessment(application: TemporaryAccommodationApplicationEntity) =
temporaryAccommodationAssessmentEntityFactory.produceAndPersist {
withAllocatedToUser(application.createdByUser)
Expand All @@ -109,6 +139,24 @@ class Cas3TimelineTest : IntegrationTestBase() {
withAccommodationRequiredFromDate(null)
}

private fun persistRejectedAssessment(application: TemporaryAccommodationApplicationEntity): TemporaryAccommodationAssessmentEntity {
val referralRejectionReason = referralRejectionReasonEntityFactory.produceAndPersist()
val referralRejectionReasonDetail = randomStringMultiCaseWithNumbers(15)

return temporaryAccommodationAssessmentEntityFactory.produceAndPersist {
withAllocatedToUser(application.createdByUser)
withApplication(application)
withAssessmentSchema(persistAssessmentSchema())
withSubmittedAt(OffsetDateTime.now().minusDays(21))
withAllocatedAt(OffsetDateTime.now().minusDays(5))
withReleaseDate(null)
withAccommodationRequiredFromDate(null)
withReferralRejectionReason(referralRejectionReason)
withReferralRejectionReasonDetail(referralRejectionReasonDetail)
withIsWithdrawn(false)
}
}

private fun persistApplication(crn: String, user: UserEntity) =
temporaryAccommodationApplicationEntityFactory.produceAndPersist {
withCrn(crn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class AssessmentReferralHistoryNoteTransformerTest {
.withType(ReferralHistorySystemNoteType.REJECTED)
.produce()

val result = AssessmentReferralHistoryNoteTransformer(objectMapper).transformJpaToApi(note, assessment)
val result = AssessmentReferralHistoryNoteTransformer(objectMapper).transformJpaToApi(note, assessment, true)

assertThat(result is ReferralHistorySystemNote).isTrue
result as ReferralHistorySystemNote
Expand Down Expand Up @@ -194,7 +194,7 @@ class AssessmentReferralHistoryNoteTransformerTest {
.withType(noteType)
.produce()

val result = AssessmentReferralHistoryNoteTransformer(objectMapper).transformJpaToApi(note, assessment)
val result = AssessmentReferralHistoryNoteTransformer(objectMapper).transformJpaToApi(note, assessment, true)

assertThat(result is ReferralHistorySystemNote).isTrue
result as ReferralHistorySystemNote
Expand Down

0 comments on commit b0eb355

Please sign in to comment.