diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormController.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormController.java index 1f7a010e..5d9b9ed9 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormController.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormController.java @@ -6,6 +6,7 @@ import gov.cabinetoffice.gap.adminbackend.dtos.errors.GenericErrorDTO; import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemeDTO; import gov.cabinetoffice.gap.adminbackend.entities.ApplicationFormEntity; +import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin; import gov.cabinetoffice.gap.adminbackend.enums.ApplicationStatusEnum; import gov.cabinetoffice.gap.adminbackend.enums.EventType; import gov.cabinetoffice.gap.adminbackend.exceptions.ApplicationFormException; @@ -52,6 +53,8 @@ public class ApplicationFormController { private final EventLogService eventLogService; + private final UserService userService; + @PostMapping @ApiResponses(value = { @ApiResponse(responseCode = "201", description = "Application form created successfully.", @@ -232,6 +235,20 @@ public ResponseEntity updateApplicationForm(HttpServletRequest } + @GetMapping("/{applicationId}/lastUpdated/email") + @CheckSchemeOwnership + public ResponseEntity getLastUpdatedEmail(@PathVariable final Integer applicationId) { + final Integer lastUpdatedBy = applicationFormService.getLastUpdatedBy(applicationId); + final Optional grantAdmin = userService.getGrantAdminById(lastUpdatedBy); + if (grantAdmin.isEmpty()) { + return ResponseEntity.notFound().build(); + } + + final String sub = grantAdmin.get().getGapUser().getUserSub(); + final String email = userService.getEmailAddressForSub(sub); + return ResponseEntity.ok(email); + } + private void logApplicationEvent(EventType eventType, String sessionId, String applicationId) { try { diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java index 28b10d6c..6e9232d3 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java @@ -15,7 +15,6 @@ public interface ApplicationFormRepository extends JpaRepository { Optional findById(Integer applicationId); - Optional findByGrantApplicationId(Integer applicationId); Optional findByGrantSchemeId(Integer grantSchemeId); diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java index e4ed6d2a..5807384b 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java @@ -73,7 +73,6 @@ public void deleteSectionFromApplication(Integer applicationId, String sectionId } ApplicationFormUtils.updateAuditDetailsAfterFormChange(applicationForm, false); - this.applicationFormRepository.save(applicationForm); } @@ -128,6 +127,7 @@ public void updateSectionOrder(final Integer applicationId, final String section sections.add(NEW_SECTION_INDEX, section); applicationForm.getDefinition().setSections(sections); + ApplicationFormUtils.updateAuditDetailsAfterFormChange(applicationForm, false); this.applicationFormRepository.save(applicationForm); } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java index 3011f4c7..593e0fd7 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java @@ -24,7 +24,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.math.NumberUtils; -import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Service; @@ -374,6 +373,14 @@ public void updateQuestionOrder(final Integer applicationId, final String sectio questions.add(newSectionIndex, question); applicationForm.getDefinition().setSections(sections); + ApplicationFormUtils.updateAuditDetailsAfterFormChange(applicationForm, false); save(applicationForm); } + + public Integer getLastUpdatedBy(Integer applicationId) { + return this.applicationFormRepository.findById(applicationId) + .orElseThrow(() -> new NotFoundException("Application with id " + applicationId + " does not exist")) + .getLastUpdateBy(); + + } } diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormControllerTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormControllerTest.java index b3c0d077..914f5751 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormControllerTest.java @@ -6,19 +6,14 @@ import gov.cabinetoffice.gap.adminbackend.dtos.application.ApplicationFormsFoundDTO; import gov.cabinetoffice.gap.adminbackend.dtos.errors.GenericErrorDTO; import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemeDTO; -import gov.cabinetoffice.gap.adminbackend.entities.ApplicationFormEntity; -import gov.cabinetoffice.gap.adminbackend.entities.GrantAdvert; -import gov.cabinetoffice.gap.adminbackend.entities.SchemeEntity; +import gov.cabinetoffice.gap.adminbackend.entities.*; import gov.cabinetoffice.gap.adminbackend.enums.ApplicationStatusEnum; import gov.cabinetoffice.gap.adminbackend.exceptions.ApplicationFormException; import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException; import gov.cabinetoffice.gap.adminbackend.mappers.ValidationErrorMapperImpl; import gov.cabinetoffice.gap.adminbackend.repositories.ApplicationFormRepository; import gov.cabinetoffice.gap.adminbackend.security.interceptors.AuthorizationHeaderInterceptor; -import gov.cabinetoffice.gap.adminbackend.services.ApplicationFormService; -import gov.cabinetoffice.gap.adminbackend.services.EventLogService; -import gov.cabinetoffice.gap.adminbackend.services.GrantAdvertService; -import gov.cabinetoffice.gap.adminbackend.services.SchemeService; +import gov.cabinetoffice.gap.adminbackend.services.*; import gov.cabinetoffice.gap.adminbackend.utils.HelperUtils; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -104,6 +99,9 @@ class ApplicationFormControllerTest { @MockBean private SchemeService schemeService; + @MockBean + private UserService userService; + @Test @WithAdminSession void saveApplicationFormHappyPathTest() throws Exception { @@ -441,4 +439,28 @@ void updateApplicationForm_GenericApplicationFormException() throws Exception { verifyNoInteractions(eventLogService); } + @Test + void getLastUpdatedEmailHappyPath() throws Exception { + when(userService.getEmailAddressForSub(anyString())).thenReturn("test@test.gov"); + when(applicationFormRepository.findById(anyInt())).thenReturn(Optional.of(ApplicationFormEntity.builder().lastUpdateBy(1).build())); + when(userService.getGrantAdminById(anyInt())).thenReturn(Optional.of(GrantAdmin.builder().gapUser(GapUser.builder().userSub("sub").build()).build())); + + this.mockMvc.perform(get("/application-forms/1/lastUpdated/email")).andExpect(status().isOk()) + .andExpect(content().string("test@test.gov")); + + } + + @Test + void getLastUpdatedEmailReturnsNotFoundWhenNoApplicationFound() throws Exception { + when(applicationFormRepository.findById(anyInt())).thenReturn(Optional.empty()); + this.mockMvc.perform(get("/application-forms/1/lastUpdated/email")).andExpect(status().isNotFound()); + } + + @Test + void getLastUpdatedEmailReturnsNotFoundWhenNoGrantAdminFound() throws Exception { + when(applicationFormRepository.findById(anyInt())). + thenReturn(Optional.of(ApplicationFormEntity.builder().lastUpdateBy(1).build())); + when(userService.getGrantAdminById(anyInt())).thenReturn(Optional.empty()); + this.mockMvc.perform(get("/application-forms/1/lastUpdated/email")).andExpect(status().isNotFound()); + } } diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormServiceTest.java index 8ba07db7..b9c6ce38 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormServiceTest.java @@ -740,6 +740,27 @@ void patchCreatedByDoesNothingIfAdminIdIsNotFound() { verify(applicationFormRepository, never()).save(any()); } + @Test + void getLastUpdatedByByReturnsLastUpdatedByForApplication() { + ApplicationFormEntity testApplicationFormEntity = randomApplicationFormEntity().lastUpdateBy(2).build(); + Mockito.when(ApplicationFormServiceTest.this.applicationFormRepository.findById(1)) + .thenReturn(Optional.of(testApplicationFormEntity)); + + Integer lastUpdatedBy = ApplicationFormServiceTest.this.applicationFormService.getLastUpdatedBy(1); + + assertThat(lastUpdatedBy).isEqualTo(2); + } + + @Test + void getLastUpdatedByReturnsNFEIfNoApplicationFound() { + Mockito.when(ApplicationFormServiceTest.this.applicationFormRepository.findById(1)) + .thenReturn(Optional.empty()); + + assertThatThrownBy(() -> ApplicationFormServiceTest.this.applicationFormService.getLastUpdatedBy(1)).isInstanceOf(NotFoundException.class) + .hasMessage("Application with id 1 does not exist"); + } + + @Nested class updateQuestionOrder {