diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java b/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java index 55ba44b..05de291 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java @@ -24,6 +24,9 @@ import java.time.Instant; import java.util.UUID; +import static gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionOrgType.INDIVIDUAL; +import static gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionOrgType.LOCAL_AUTHORITY; + @RequiredArgsConstructor @RestController @RequestMapping("/grant-mandatory-questions") @@ -143,6 +146,13 @@ public ResponseEntity updateMandatoryQuestion(@PathVariable final UUID m grantMandatoryQuestions.setStatus(GrantMandatoryQuestionStatus.COMPLETED); } + if (mandatoryQuestionDto.getOrgType() != null && mandatoryQuestionDto.getOrgType().isPresent() && + (mandatoryQuestionDto.getOrgType().get().equals(LOCAL_AUTHORITY.toString()) + || mandatoryQuestionDto.getOrgType().get().equals(INDIVIDUAL.toString()))) { + grantMandatoryQuestions.setCharityCommissionNumber(null); + grantMandatoryQuestions.setCompaniesHouseNumber(null); + } + grantMandatoryQuestions.setLastUpdatedBy(applicant); grantMandatoryQuestions.setLastUpdated(Instant.now()); diff --git a/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java b/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java index 3004f19..2ed4823 100644 --- a/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java @@ -31,6 +31,7 @@ import java.util.UUID; import static gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionFundingLocation.SCOTLAND; +import static gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionOrgType.*; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -130,7 +131,7 @@ void getGrantMandatoryQuestionsById_ReturnsExpectedMandatoryQuestions() { .postcode("G2 7EZ") .fundingLocation(fundingLocations) .companiesHouseNumber("08761455") - .orgType(GrantMandatoryQuestionOrgType.LIMITED_COMPANY) + .orgType(LIMITED_COMPANY) .build(); final GetGrantMandatoryQuestionDto mandatoryQuestionsDto = GetGrantMandatoryQuestionDto.builder() @@ -178,7 +179,7 @@ void updateMandatoryQuestion_UpdatesExpectedFields_AndSavesChanges() { .postcode("G2 7EZ") .fundingLocation(fundingLocations) .companiesHouseNumber("08761455") - .orgType(GrantMandatoryQuestionOrgType.LIMITED_COMPANY) + .orgType(LIMITED_COMPANY) .build(); when(grantApplicantService.getApplicantById(jwtPayload.getSub())) @@ -291,6 +292,109 @@ void updateMandatoryQuestion_SetsStatusToComplete() { } + + @Test + void updateMandatoryQuestion_setCharityAndCommissionNumberAsNullIfOrgTypeIsLocalAuthority() { + final GrantMandatoryQuestionOrgType localAuthority = LOCAL_AUTHORITY; + final UpdateGrantMandatoryQuestionDto updateDto = UpdateGrantMandatoryQuestionDto.builder() + .orgType(Optional.of("Local authority")) + .build(); + + final GrantMandatoryQuestions mandatoryQuestionsBefore = GrantMandatoryQuestions.builder() + .id(MANDATORY_QUESTION_ID) + .createdBy(applicant) + .grantScheme(scheme) + .name("AND Digital") + .companiesHouseNumber("08761455") + .charityCommissionNumber("123456") + .build(); + + final GrantMandatoryQuestions mandatoryQuestionsAfter = GrantMandatoryQuestions.builder() + .id(MANDATORY_QUESTION_ID) + .createdBy(applicant) + .grantScheme(scheme) + .name("AND Digital") + .orgType(localAuthority) + .companiesHouseNumber(null) + .charityCommissionNumber(null) + .build(); + + when(grantApplicantService.getApplicantById(jwtPayload.getSub())) + .thenReturn(applicant); + + when(grantMandatoryQuestionService.getGrantMandatoryQuestionById(MANDATORY_QUESTION_ID, jwtPayload.getSub())) + .thenReturn(mandatoryQuestionsBefore); + + when(grantMandatoryQuestionMapper.mapUpdateGrantMandatoryQuestionDtoToGrantMandatoryQuestion(updateDto, mandatoryQuestionsBefore)) + .thenReturn(mandatoryQuestionsBefore); + + when(grantMandatoryQuestionService.updateMandatoryQuestion(mandatoryQuestionsBefore, applicant)) + .thenReturn(mandatoryQuestionsAfter); + + when(grantMandatoryQuestionService.generateNextPageUrl("url", MANDATORY_QUESTION_ID, jwtPayload.getSub())) + .thenReturn("nextPageUrl"); + + + final ResponseEntity methodResponse = controllerUnderTest.updateMandatoryQuestion(MANDATORY_QUESTION_ID, updateDto, "url"); + + verify(grantMandatoryQuestionService).addMandatoryQuestionsToSubmissionObject(mandatoryQuestionsBefore); + verify(grantMandatoryQuestionService).updateMandatoryQuestion(mandatoryQuestionsBefore, applicant); + assertThat(methodResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(methodResponse.getBody()).isEqualTo("nextPageUrl"); + + } + + @Test + void updateMandatoryQuestion_setCharityAndCommissionNumberAsNullIfOrgTypeIsIndividual() { + final GrantMandatoryQuestionOrgType individual = INDIVIDUAL; + final UpdateGrantMandatoryQuestionDto updateDto = UpdateGrantMandatoryQuestionDto.builder() + .orgType(Optional.of("I am applying as an individual")) + .build(); + + final GrantMandatoryQuestions mandatoryQuestionsBefore = GrantMandatoryQuestions.builder() + .id(MANDATORY_QUESTION_ID) + .createdBy(applicant) + .grantScheme(scheme) + .name("AND Digital") + .companiesHouseNumber("08761455") + .charityCommissionNumber("123456") + .build(); + + final GrantMandatoryQuestions mandatoryQuestionsAfter = GrantMandatoryQuestions.builder() + .id(MANDATORY_QUESTION_ID) + .createdBy(applicant) + .grantScheme(scheme) + .name("AND Digital") + .orgType(individual) + .companiesHouseNumber(null) + .charityCommissionNumber(null) + .build(); + + when(grantApplicantService.getApplicantById(jwtPayload.getSub())) + .thenReturn(applicant); + + when(grantMandatoryQuestionService.getGrantMandatoryQuestionById(MANDATORY_QUESTION_ID, jwtPayload.getSub())) + .thenReturn(mandatoryQuestionsBefore); + + when(grantMandatoryQuestionMapper.mapUpdateGrantMandatoryQuestionDtoToGrantMandatoryQuestion(updateDto, mandatoryQuestionsBefore)) + .thenReturn(mandatoryQuestionsBefore); + + when(grantMandatoryQuestionService.updateMandatoryQuestion(mandatoryQuestionsBefore, applicant)) + .thenReturn(mandatoryQuestionsAfter); + + when(grantMandatoryQuestionService.generateNextPageUrl("url", MANDATORY_QUESTION_ID, jwtPayload.getSub())) + .thenReturn("nextPageUrl"); + + + final ResponseEntity methodResponse = controllerUnderTest.updateMandatoryQuestion(MANDATORY_QUESTION_ID, updateDto, "url"); + + verify(grantMandatoryQuestionService).addMandatoryQuestionsToSubmissionObject(mandatoryQuestionsBefore); + verify(grantMandatoryQuestionService).updateMandatoryQuestion(mandatoryQuestionsBefore, applicant); + assertThat(methodResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(methodResponse.getBody()).isEqualTo("nextPageUrl"); + + } + @Test void shouldReturnMandatoryQuestionsIfValidSchemeAndUserIdIsGiven() { final GrantMandatoryQuestions mandatoryQuestions = GrantMandatoryQuestions.builder() @@ -303,7 +407,7 @@ void shouldReturnMandatoryQuestionsIfValidSchemeAndUserIdIsGiven() { .city("Glasgow") .postcode("G2 7EZ") .companiesHouseNumber("08761455") - .orgType(GrantMandatoryQuestionOrgType.LIMITED_COMPANY) + .orgType(LIMITED_COMPANY) .build(); final GetGrantMandatoryQuestionDto mandatoryQuestionsDto = GetGrantMandatoryQuestionDto.builder()