diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/CreateQuestionResponseDto.java b/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/CreateQuestionResponseDto.java index 1e587377..63bd1372 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/CreateQuestionResponseDto.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/CreateQuestionResponseDto.java @@ -14,4 +14,5 @@ public class CreateQuestionResponseDto { private String questionId; private String response; private String[] multiResponse; + private Boolean shouldUpdateSectionStatus; } diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/service/SubmissionService.java b/src/main/java/gov/cabinetoffice/gap/applybackend/service/SubmissionService.java index 65465967..1446b607 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/service/SubmissionService.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/service/SubmissionService.java @@ -122,12 +122,16 @@ public void saveQuestionResponse(final CreateQuestionResponseDto questionRespons if (questionResponse.getResponse() != null) { submissionQuestion.setResponse(questionResponse.getResponse()); - submissionSection.setSectionStatus(SubmissionSectionStatus.IN_PROGRESS); + if (questionResponse.getShouldUpdateSectionStatus()) { + submissionSection.setSectionStatus(SubmissionSectionStatus.IN_PROGRESS); + } } if (questionResponse.getMultiResponse() != null) { submissionQuestion.setMultiResponse(questionResponse.getMultiResponse()); - submissionSection.setSectionStatus(SubmissionSectionStatus.IN_PROGRESS); + if (questionResponse.getShouldUpdateSectionStatus()) { + submissionSection.setSectionStatus(SubmissionSectionStatus.IN_PROGRESS); + } } if (sectionId.equals(ELIGIBILITY)) { diff --git a/src/test/java/gov/cabinetoffice/gap/applybackend/service/SubmissionServiceTest.java b/src/test/java/gov/cabinetoffice/gap/applybackend/service/SubmissionServiceTest.java index aeb6fafb..12ce578f 100644 --- a/src/test/java/gov/cabinetoffice/gap/applybackend/service/SubmissionServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/applybackend/service/SubmissionServiceTest.java @@ -463,12 +463,13 @@ void getQuestionByQuestionId_returnsExpectedQuestion() { @Nested class saveQuestionResponse { @Test - void saveQuestionResponse_SavesResponse() { + void saveQuestionResponse_SavesResponse_WhenShouldSetSectionStatus_IsTrue() { final CreateQuestionResponseDto organisationNameResponse = CreateQuestionResponseDto.builder() .questionId(QUESTION_ID) .submissionId(SUBMISSION_ID) .response("AND Digital") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -508,6 +509,134 @@ void saveQuestionResponse_SavesResponse() { ); } + @Test + void saveQuestionResponse_ResetsSectionStatusToInProgress_IfShouldUpdateSectionStatus_IsTrue() { + + // set the scheme to version 2 + submission.getApplication().getGrantScheme().setVersion(2); + + // remove surplus sections + submission.getDefinition() + .getSections() + .removeIf(section -> section.getSectionStatus() != SubmissionSectionStatus.IN_PROGRESS); + + submission.getDefinition() + .getSections() + .forEach(section -> section.setSectionStatus(SubmissionSectionStatus.COMPLETED)); + + final CreateQuestionResponseDto questionResponse = CreateQuestionResponseDto.builder() + .questionId("APPLICANT_ORG_NAME") + .submissionId(SUBMISSION_ID) + .response("New name") + .shouldUpdateSectionStatus(true) + .build(); + + doReturn(submission) + .when(serviceUnderTest).getSubmissionFromDatabaseBySubmissionId(userId, SUBMISSION_ID); + + final ArgumentCaptor submissionCaptor = ArgumentCaptor.forClass(Submission.class); + + serviceUnderTest.saveQuestionResponse(questionResponse, userId, SUBMISSION_ID, "ESSENTIAL"); + + + verify(submissionRepository).save(submissionCaptor.capture()); + + // organisation details and funding details should be set to in progress + submissionCaptor.getValue() + .getDefinition() + .getSections() + .stream() + .filter(section -> section.getSectionId().equals("ESSENTIAL")) + .findFirst() + .ifPresentOrElse( + capturedSectionResponse -> { + assertThat(capturedSectionResponse.getSectionStatus()).isEqualTo(SubmissionSectionStatus.IN_PROGRESS); + + // Check if the question with the specified questionId and response is present + boolean isQuestionPresent = capturedSectionResponse.getQuestions().stream() + .anyMatch(question -> question.getQuestionId().equals("APPLICANT_ORG_NAME") && question.getResponse().equals("New name")); + + assertThat(isQuestionPresent).isTrue(); + }, + () -> fail("No section with ID 'ESSENTIAL' found") + ); + + submissionCaptor.getValue() + .getDefinition() + .getSections() + .stream() + .filter(section -> section.getSectionId().equals("ELIGIBILITY")) + .findFirst() + .ifPresentOrElse( + capturedSectionResponse -> assertThat(capturedSectionResponse.getSectionStatus()).isEqualTo(SubmissionSectionStatus.COMPLETED), + () -> fail("No section with ID 'ELIGIBILITY' found") + ); + } + + @Test + void saveQuestionResponse_DoesNotResetSectionStatusToInProgress_IfShouldUpdateSectionStatus_IsFalse() { + + // set the scheme to version 2 + submission.getApplication().getGrantScheme().setVersion(2); + + // remove surplus sections + submission.getDefinition() + .getSections() + .removeIf(section -> section.getSectionStatus() != SubmissionSectionStatus.IN_PROGRESS); + + submission.getDefinition() + .getSections() + .forEach(section -> section.setSectionStatus(SubmissionSectionStatus.COMPLETED)); + + final CreateQuestionResponseDto questionResponse = CreateQuestionResponseDto.builder() + .questionId("APPLICANT_ORG_NAME") + .submissionId(SUBMISSION_ID) + .response("New name") + .shouldUpdateSectionStatus(false) + .build(); + + doReturn(submission) + .when(serviceUnderTest).getSubmissionFromDatabaseBySubmissionId(userId, SUBMISSION_ID); + + final ArgumentCaptor submissionCaptor = ArgumentCaptor.forClass(Submission.class); + + serviceUnderTest.saveQuestionResponse(questionResponse, userId, SUBMISSION_ID, "ESSENTIAL"); + + + verify(submissionRepository).save(submissionCaptor.capture()); + + // organisation details and funding details should be set to in progress + submissionCaptor.getValue() + .getDefinition() + .getSections() + .stream() + .filter(section -> section.getSectionId().equals("ESSENTIAL")) + .findFirst() + .ifPresentOrElse( + capturedSectionResponse -> { + assertThat(capturedSectionResponse.getSectionStatus()).isEqualTo(SubmissionSectionStatus.COMPLETED); + + // Check if the question with the specified questionId and response is present + boolean isQuestionPresent = capturedSectionResponse.getQuestions().stream() + .anyMatch(question -> question.getQuestionId().equals("APPLICANT_ORG_NAME") && question.getResponse().equals("New name")); + + assertThat(isQuestionPresent).isTrue(); + }, + () -> fail("No section with ID 'ESSENTIAL' found") + ); + + submissionCaptor.getValue() + .getDefinition() + .getSections() + .stream() + .filter(section -> section.getSectionId().equals("ELIGIBILITY")) + .findFirst() + .ifPresentOrElse( + capturedSectionResponse -> assertThat(capturedSectionResponse.getSectionStatus()).isEqualTo(SubmissionSectionStatus.COMPLETED), + () -> fail("No section with ID 'ELIGIBILITY' found") + ); + } + @Test void saveEligibilityResponseToYesAltersStatus_SavesResponse() { @@ -515,6 +644,7 @@ void saveEligibilityResponseToYesAltersStatus_SavesResponse() { .questionId("ELIGIBILITY") .submissionId(SUBMISSION_ID) .response("Yes") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -594,6 +724,7 @@ void saveQuestionResponse_HandlesV2Schemes_WhenEligibilityResponse_IsYes() { .questionId("ELIGIBILITY") .submissionId(SUBMISSION_ID) .response("Yes") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -687,6 +818,7 @@ void saveQuestionResponse_DoesNotReAssignSectionStatus_IfSectionStatusIsNot_Cann .questionId("ELIGIBILITY") .submissionId(SUBMISSION_ID) .response("Yes") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -753,6 +885,7 @@ void saveEligibilityResponseToNoAltersStatus_SavesResponse() { .questionId("ELIGIBILITY") .submissionId(SUBMISSION_ID) .response("No") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -809,6 +942,7 @@ void saveQuestionResponse_HandlesV2Schemes_WhenEligibilityResponse_IsNo() { .questionId("ELIGIBILITY") .submissionId(SUBMISSION_ID) .response("No") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -862,6 +996,7 @@ void saveQuestionResponse_ThrowsNotFoundException_IfQuestionNotFound() { .questionId("AN-INVALID-QUESTION-ID") .submissionId(SUBMISSION_ID) .response("AND Digital") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission) @@ -877,6 +1012,7 @@ void saveQuestionResponse_ThrowsNotFoundException_IfSectionNotFound() { .questionId("AN-INVALID-QUESTION-ID") .submissionId(SUBMISSION_ID) .response("AND Digital") + .shouldUpdateSectionStatus(true) .build(); doReturn(submission)