From 93b9b39ff0582c5ce011b0af5b0d357a828fe3be Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 23 Oct 2024 10:20:51 +0100 Subject: [PATCH] Clear staff & prisoner involvements and reset to draft when changing type --- .../hmpps/incidentreporting/jpa/Report.kt | 17 +++++-- .../jpa/ReportRepositoryTest.kt | 7 ++- .../resource/ReportResourceTest.kt | 48 ++++++++++++++++--- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/Report.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/Report.kt index 4cafbfc4..552b72eb 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/Report.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/Report.kt @@ -142,15 +142,22 @@ class Report( fun popLastQuestion(): Question? = questions.removeLastOrNull() fun changeType(newType: Type, changedAt: LocalDateTime, changedBy: String): Report { - copyToHistory(changedAt, changedBy) - questions.clear() - type = newType + if (type != newType) { + copyToHistory(changedAt, changedBy) + questions.clear() + type = newType + changeStatus(Status.DRAFT, changedAt, changedBy) + prisonersInvolved.clear() + staffInvolved.clear() + } return this } fun changeStatus(newStatus: Status, changedAt: LocalDateTime, changedBy: String): Report { - status = newStatus - addStatusHistory(newStatus, changedAt, changedBy) + if (status != newStatus) { + status = newStatus + addStatusHistory(newStatus, changedAt, changedBy) + } return this } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/ReportRepositoryTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/ReportRepositoryTest.kt index 48a6984e..0b038d0b 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/ReportRepositoryTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/jpa/ReportRepositoryTest.kt @@ -248,6 +248,7 @@ class ReportRepositoryTest : IntegrationTestBase() { ), ) report.addStatusHistory(Status.DRAFT, now, "user5") + report.addStatusHistory(Status.AWAITING_ANALYSIS, now, "user1") report.addStaffInvolved(StaffRole.FIRST_ON_SCENE, "user1") report.addPrisonerInvolved("A1234AA", PrisonerRole.VICTIM) @@ -301,7 +302,7 @@ class ReportRepositoryTest : IntegrationTestBase() { report = reportRepository.findOneEagerlyByReportReference(report.reportReference) ?: throw EntityNotFoundException() - assertThat(report.status).isEqualTo(Status.AWAITING_ANALYSIS) + assertThat(report.status).isEqualTo(Status.DRAFT) assertThat(report.type).isEqualTo(Type.ASSAULT) assertThat(report.getQuestions()).hasSize(1) assertThat(report.getQuestions()[0].code).isEqualTo("SOME_QUESTION") @@ -310,7 +311,9 @@ class ReportRepositoryTest : IntegrationTestBase() { assertThat(report.history[2].questions).hasSize(3) assertThat(report.history[2].questions[1].getResponses()).hasSize(2) assertThat(report.history[2].questions[1].getResponses()[1].response).isEqualTo("OTHER") - assertThat(report.historyOfStatuses).hasSize(1) + assertThat(report.historyOfStatuses).hasSize(3) assertThat(report.historyOfStatuses[0].status).isEqualTo(Status.DRAFT) + assertThat(report.historyOfStatuses[1].status).isEqualTo(Status.AWAITING_ANALYSIS) + assertThat(report.historyOfStatuses[2].status).isEqualTo(Status.DRAFT) } } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/resource/ReportResourceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/resource/ReportResourceTest.kt index 8a9fea74..93a320e1 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/resource/ReportResourceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/incidentreporting/resource/ReportResourceTest.kt @@ -1697,6 +1697,8 @@ class ReportResourceTest : SqsIntegrationTestBase() { status = Status.AWAITING_ANALYSIS, generateQuestions = 2, generateResponses = 2, + generateStaffInvolvement = 1, + generatePrisonerInvolvement = 1, generateHistory = 0, ), ) @@ -1720,8 +1722,8 @@ class ReportResourceTest : SqsIntegrationTestBase() { "description": "A new incident created in the new service of type Finds", "reportedBy": "USER1", "reportedAt": "2023-12-05T12:31:56", - "status": "AWAITING_ANALYSIS", - "nomisStatus": "AWAN", + "status": "DRAFT", + "nomisStatus": null, "assignedTo": "USER1", "createdAt": "2023-12-05T12:31:56", "modifiedAt": "2023-12-05T12:34:56", @@ -1779,7 +1781,23 @@ class ReportResourceTest : SqsIntegrationTestBase() { ] } ], - "questions": [] + "questions": [], + "historyOfStatuses": [ + { + "status": "AWAITING_ANALYSIS", + "nomisStatus": "AWAN", + "changedAt": "2023-12-05T12:31:56", + "changedBy": "USER1" + }, + { + "status": "DRAFT", + "nomisStatus": null, + "changedAt": "2023-12-05T12:34:56", + "changedBy": "request-user" + } + ], + "staffInvolved": [], + "prisonersInvolved": [] } """, false, @@ -1802,6 +1820,8 @@ class ReportResourceTest : SqsIntegrationTestBase() { status = Status.AWAITING_ANALYSIS, generateQuestions = 1, generateResponses = 1, + generateStaffInvolvement = 1, + generatePrisonerInvolvement = 1, generateHistory = 1, ), ) @@ -1825,8 +1845,8 @@ class ReportResourceTest : SqsIntegrationTestBase() { "description": "A new incident created in the new service of type Finds", "reportedBy": "USER1", "reportedAt": "2023-12-05T12:31:56", - "status": "AWAITING_ANALYSIS", - "nomisStatus": "AWAN", + "status": "DRAFT", + "nomisStatus": null, "assignedTo": "USER1", "createdAt": "2023-12-05T12:31:56", "modifiedAt": "2023-12-05T12:34:56", @@ -1878,7 +1898,23 @@ class ReportResourceTest : SqsIntegrationTestBase() { ] } ], - "questions": [] + "questions": [], + "historyOfStatuses": [ + { + "status": "AWAITING_ANALYSIS", + "nomisStatus": "AWAN", + "changedAt": "2023-12-05T12:31:56", + "changedBy": "USER1" + }, + { + "status": "DRAFT", + "nomisStatus": null, + "changedAt": "2023-12-05T12:34:56", + "changedBy": "request-user" + } + ], + "staffInvolved": [], + "prisonersInvolved": [] } """, false,