Skip to content

Commit

Permalink
GAP-2433: View last updated by (Applications) (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-tco authored Mar 6, 2024
1 parent 37e589f commit ac32d20
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -232,6 +235,20 @@ public ResponseEntity<GenericErrorDTO> updateApplicationForm(HttpServletRequest

}

@GetMapping("/{applicationId}/lastUpdated/email")
@CheckSchemeOwnership
public ResponseEntity<String> getLastUpdatedEmail(@PathVariable final Integer applicationId) {
final Integer lastUpdatedBy = applicationFormService.getLastUpdatedBy(applicationId);
final Optional<GrantAdmin> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public interface ApplicationFormRepository extends JpaRepository<ApplicationFormEntity, Integer> {

Optional<ApplicationFormEntity> findById(Integer applicationId);

Optional<ApplicationFormNoSections> findByGrantApplicationId(Integer applicationId);
Optional<ApplicationFormEntity> findByGrantSchemeId(Integer grantSchemeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void deleteSectionFromApplication(Integer applicationId, String sectionId
}

ApplicationFormUtils.updateAuditDetailsAfterFormChange(applicationForm, false);

this.applicationFormRepository.save(applicationForm);

}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,6 +99,9 @@ class ApplicationFormControllerTest {
@MockBean
private SchemeService schemeService;

@MockBean
private UserService userService;

@Test
@WithAdminSession
void saveApplicationFormHappyPathTest() throws Exception {
Expand Down Expand Up @@ -441,4 +439,28 @@ void updateApplicationForm_GenericApplicationFormException() throws Exception {
verifyNoInteractions(eventLogService);
}

@Test
void getLastUpdatedEmailHappyPath() throws Exception {
when(userService.getEmailAddressForSub(anyString())).thenReturn("[email protected]");
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("[email protected]"));

}

@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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ac32d20

Please sign in to comment.