Skip to content

Commit

Permalink
Fix schedule unpublish application forms and add logs upon failure (#266
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dominicwest authored Mar 27, 2024
1 parent ee1b0b0 commit b6e0ad9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,18 @@ public ResponseEntity<Void> deleteApplicationForm(@PathVariable @NotNull Integer
@ApiResponse(responseCode = "404", description = "Application not found with given id",
content = @Content(mediaType = "application/json")), })
@LambdasHeaderValidator
public ResponseEntity<Void> removeApplicationAttachedToGrantAdvert(@PathVariable @NotNull UUID grantAdvertId) {
public ResponseEntity<Void> removeApplicationAttachedToGrantAdvert(@PathVariable @NotNull final UUID grantAdvertId) {
try {

Integer schemeId = grantAdvertService.getAdvertById(grantAdvertId).getScheme().getId();
Optional<ApplicationFormEntity> applicationForm = applicationFormService
.getOptionalApplicationFromSchemeId(schemeId);
final Integer schemeId = grantAdvertService.getSchemeIdFromAdvert(grantAdvertId);
final Optional<ApplicationFormEntity> applicationForm = applicationFormService.getOptionalApplicationFromSchemeId(schemeId);
if (applicationForm.isEmpty()) {
log.info("No application form attached to grant advert with id: " + grantAdvertId + " was found.");
return ResponseEntity.noContent().build();
}

ApplicationFormPatchDTO applicationFormPatchDTO = new ApplicationFormPatchDTO();
final ApplicationFormPatchDTO applicationFormPatchDTO = new ApplicationFormPatchDTO();
applicationFormPatchDTO.setApplicationStatus(ApplicationStatusEnum.REMOVED);
this.applicationFormService.patchApplicationForm(applicationForm.get().getGrantApplicationId(),
applicationFormPatchDTO, true);
applicationFormService.patchApplicationForm(applicationForm.get().getGrantApplicationId(), applicationFormPatchDTO, true);

return ResponseEntity.noContent().build();
}
Expand All @@ -192,10 +189,10 @@ public ResponseEntity<Void> removeApplicationAttachedToGrantAdvert(@PathVariable
catch (UnauthorizedException error) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
catch (ApplicationFormException error) {
catch (Exception error) {
log.error("Error removing application attached to grant advert with id: " + grantAdvertId, error);
return ResponseEntity.internalServerError().build();
}

}

@PatchMapping("/{applicationId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public interface GrantAdvertRepository extends JpaRepository<GrantAdvert, UUID>
@Override
Optional<GrantAdvert> findById(UUID id);

@Query("select g from GrantAdvert g where g.id = ?1")
@EntityGraph(attributePaths = {"scheme"})
Optional<GrantAdvert> findByIdWithScheme(UUID id);

@PreAuthorize("#grantAdminId == authentication.principal.grantAdminId")
Long deleteByIdAndCreatedById(UUID advertId, Integer grantAdminId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ public void patchApplicationForm(Integer applicationId, ApplicationFormPatchDTO
throw new ApplicationFormException("Error occurred when patching application with id of " + applicationId,
e);
}

}

@PreAuthorize("hasRole('SUPER_ADMIN')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ public GrantAdvert getAdvertById(UUID advertId) {
return advert;
}

public Integer getSchemeIdFromAdvert(UUID advertId) {
return grantAdvertRepository.findByIdWithScheme(advertId)
.map(GrantAdvert::getScheme)
.map(SchemeEntity::getId)
.orElseThrow(() -> new NotFoundException("Scheme not found for advert with id " + advertId));
}

public GetGrantAdvertPageResponseDTO getAdvertBuilderPageData(UUID grantAdvertId, String sectionId, String pageId) {

GrantAdvert grantAdvert = getAdvertById(grantAdvertId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ void deleteApplicationForm_AccessDeniedTest() throws Exception {
@Test
void removesApplicationAttachedToGrantAdvert_Successfully() throws Exception {
SchemeEntity scheme = SchemeEntity.builder().id(1).name("scheme").build();
GrantAdvert grantAdvert = GrantAdvert.builder().grantAdvertName("grant-advert").scheme(scheme).build();
when(grantAdvertService.getAdvertById(SAMPLE_ADVERT_ID)).thenReturn(grantAdvert);
when(grantAdvertService.getSchemeIdFromAdvert(SAMPLE_ADVERT_ID)).thenReturn(1);
when(applicationFormService.getOptionalApplicationFromSchemeId(scheme.getId()))
.thenReturn(Optional.of(ApplicationFormEntity.builder().grantApplicationId(1)
.applicationName("application").grantSchemeId(scheme.getId()).build()));
Expand All @@ -310,7 +309,7 @@ void removesApplicationAttachedToGrantAdvert_Successfully() throws Exception {

@Test
void removesApplicationAttachedToGrantAdvert_throwsNotFoundWhenNoAdvertFound() throws Exception {
doThrow(NotFoundException.class).when(grantAdvertService).getAdvertById(SAMPLE_ADVERT_ID);
doThrow(NotFoundException.class).when(grantAdvertService).getSchemeIdFromAdvert(SAMPLE_ADVERT_ID);

this.mockMvc
.perform(delete("/application-forms/lambda/" + SAMPLE_ADVERT_ID + "/application/")
Expand All @@ -321,8 +320,7 @@ void removesApplicationAttachedToGrantAdvert_throwsNotFoundWhenNoAdvertFound() t
@Test
void removesApplicationAttachedToGrantAdvert_throwsApplicationFormExceptionWhenUnableToPatch() throws Exception {
SchemeEntity scheme = SchemeEntity.builder().id(1).name("scheme").build();
GrantAdvert grantAdvert = GrantAdvert.builder().grantAdvertName("grant-advert").scheme(scheme).build();
when(grantAdvertService.getAdvertById(SAMPLE_ADVERT_ID)).thenReturn(grantAdvert);
when(grantAdvertService.getSchemeIdFromAdvert(SAMPLE_ADVERT_ID)).thenReturn(1);
when(applicationFormService.getOptionalApplicationFromSchemeId(scheme.getId()))
.thenReturn(Optional.of(ApplicationFormEntity.builder().grantApplicationId(1)
.applicationName("application").grantSchemeId(scheme.getId()).build()));
Expand Down

0 comments on commit b6e0ad9

Please sign in to comment.