Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix schedule unpublish application forms and add logs upon failure #266

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -355,7 +355,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
Loading