Skip to content

Commit

Permalink
GAP-2537:add logic to handle application with no adverts
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lor-cab committed Apr 17, 2024
1 parent 58aaba2 commit e6c1ca8
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@AllArgsConstructor
@Builder
public class SchemeMandatoryQuestionApplicationFormInfosDto {
private boolean hasAdvertPublished;
private boolean hasInternalApplication;
private boolean hasPublishedInternalApplication;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gov.cabinetoffice.gap.applybackend.exception;

public class AdvertNotPublishedException extends RuntimeException {
public AdvertNotPublishedException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class GrantAdvertService {
private final GrantMandatoryQuestionService grantMandatoryQuestionService;
private final GrantApplicantService grantApplicantService;

public String getExternalSubmissionUrl(GrantAdvert advert) {
public String getApplyToUrl(GrantAdvert advert) {
return advert.getResponse().getSections().stream()
.filter(section -> section.getId().equals("howToApply"))
.flatMap(section -> section.getPages().stream())
Expand All @@ -60,7 +60,7 @@ public GetGrantAdvertDto generateGetGrantAdvertDto(GrantAdvert advert, GetGrantM
return GetGrantAdvertDto.builder()
.id(advert.getId())
.version(advert.getVersion())
.externalSubmissionUrl(getExternalSubmissionUrl(advert))
.externalSubmissionUrl(getApplyToUrl(advert))
.isInternal(isInternal)
.grantApplicationId(grantApplicationId)
.grantSchemeId(advert.getScheme().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public boolean doesSchemeHaveAPublishedApplication(final GrantScheme grantScheme
return grantApplicationRepository.findByGrantSchemeAndApplicationStatus(grantScheme, GrantApplicationStatus.PUBLISHED).isPresent();
}

public boolean doesSchemeHaveAnApplication(final GrantScheme grantScheme) {
return grantApplicationRepository.findByGrantScheme(grantScheme).isPresent();
}

public Integer getGrantApplicationId(final GrantScheme grantScheme) {
final Optional<GrantApplication> grantApplication = grantApplicationRepository.findByGrantScheme(grantScheme);
return grantApplication.map(GrantApplication::getId).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantMandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.dto.api.UpdateGrantMandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionStatus;
import gov.cabinetoffice.gap.applybackend.exception.AdvertNotPublishedException;
import gov.cabinetoffice.gap.applybackend.exception.NotFoundException;
import gov.cabinetoffice.gap.applybackend.mapper.GrantMandatoryQuestionMapper;
import gov.cabinetoffice.gap.applybackend.model.*;
import gov.cabinetoffice.gap.applybackend.service.GrantAdvertService;
Expand Down Expand Up @@ -54,29 +57,26 @@ public ResponseEntity<GetGrantMandatoryQuestionDto> createMandatoryQuestion(@Req
final JwtPayload jwtPayload = (JwtPayload) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
final GrantApplicant applicant = grantApplicantService.getApplicantById(jwtPayload.getSub());

log.info("Getting scheme with id {}", schemeId);
final GrantScheme scheme = grantSchemeService.getSchemeById(schemeId);
log.info("Scheme with id {} found", schemeId);
boolean isForInternalApplication = true;

log.info("Getting Advert associated to scheme with id {}", schemeId);
final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(schemeId.toString());
log.info("Advert with id {} found for scheme with id {}", advert.getId(), schemeId);
try {
final String webpageUrl = getApplyingUrlFromAdvert(scheme);
isForInternalApplication = webpageUrl.contains(environmentProperties.getFrontEndUri());
log.debug("Advert is pointing to an internal application form : {}", isForInternalApplication);

final String webpageUrl = grantAdvertService.getExternalSubmissionUrl(advert);

log.info("Checking that the advert has link to internal or external application");
final boolean webPageUrlIsForInternalApplications = webpageUrl.contains(environmentProperties.getFrontEndUri());
log.info("Advert is pointing to an internal application form : {}", webPageUrlIsForInternalApplications);
} catch (AdvertNotPublishedException | NotFoundException e) {
log.debug(e.getMessage());
}

final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant, webPageUrlIsForInternalApplications);
final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant, isForInternalApplication);
log.info("Mandatory question with ID {} has been created.", grantMandatoryQuestions.getId());

final GetGrantMandatoryQuestionDto getGrantMandatoryQuestionDto = grantMandatoryQuestionMapper.mapGrantMandatoryQuestionToGetGrantMandatoryQuestionDTO(grantMandatoryQuestions);

return ResponseEntity.ok(getGrantMandatoryQuestionDto);
}


@GetMapping("/{mandatoryQuestionId}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Grant Mandatory found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = GrantMandatoryQuestions.class))),
Expand Down Expand Up @@ -193,4 +193,18 @@ public ResponseEntity<String> getApplicationStatusByMandatoryQuestionId(@PathVar

return ResponseEntity.ok(grantApplication.getApplicationStatus().name());
}

private String getApplyingUrlFromAdvert(GrantScheme scheme) {
log.debug("Getting Advert associated to scheme with id {}", scheme.getId());
final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(scheme.getId().toString());
log.debug("Advert with id {} found for scheme with id {}", advert.getId(), scheme.getId());

final boolean isAdvertPublished = advert.getStatus().equals(GrantAdvertStatus.PUBLISHED);
if (!isAdvertPublished) {
throw new AdvertNotPublishedException("Advert with id " + advert.getId() + " is not published");
}

final String webpageUrl = grantAdvertService.getApplyToUrl(advert);
return webpageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import gov.cabinetoffice.gap.applybackend.config.properties.EnvironmentProperties;
import gov.cabinetoffice.gap.applybackend.dto.api.*;
import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.applybackend.exception.AdvertNotPublishedException;
import gov.cabinetoffice.gap.applybackend.exception.NotFoundException;
import gov.cabinetoffice.gap.applybackend.mapper.GrantSchemeMapper;
import gov.cabinetoffice.gap.applybackend.model.GrantAdvert;
import gov.cabinetoffice.gap.applybackend.model.GrantScheme;
Expand Down Expand Up @@ -67,36 +69,52 @@ public ResponseEntity<GetGrantSchemeWithApplicationAndAdverts> getGrantSchemeByI
@ApiResponse(responseCode = "404", description = "No Grant Scheme found",
content = @Content(mediaType = "application/json")),
})
public ResponseEntity<SchemeMandatoryQuestionApplicationFormInfosDto> schemeHasInternalApplication(@PathVariable Integer grantSchemeId){
log.debug("Checking if scheme with id {} has internal application form and if it is published",grantSchemeId);
public ResponseEntity<SchemeMandatoryQuestionApplicationFormInfosDto> schemeHasInternalApplication(@PathVariable Integer grantSchemeId) {
log.debug("Checking if scheme with id {} has internal application form and if it is published", grantSchemeId);

final GrantScheme grantScheme = grantSchemeService.getSchemeById(grantSchemeId);
log.debug("Scheme with id {} found. Getting associated advert", grantSchemeId);

log.debug("Getting Advert associated to scheme with id {}", grantSchemeId);
final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(grantSchemeId.toString());
log.debug("Advert with id {} found for scheme with id {}", advert.getId(), grantSchemeId);

final String webpageUrl = grantAdvertService.getExternalSubmissionUrl(advert);

final boolean webPageUrlIsForInternalApplications = webpageUrl.contains(environmentProperties.getFrontEndUri());
log.debug("Advert is pointing to an internal application form : {}", webPageUrlIsForInternalApplications);
final boolean hasSchemeAnApplication = grantApplicationService.doesSchemeHaveAnApplication(grantScheme);
final boolean hasPublishedApplication = grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme);
log.debug("Application form associated to scheme with Id {} has PUBLISHED status: {}", grantScheme.getId(), hasPublishedApplication);

final SchemeMandatoryQuestionApplicationFormInfosDto dto = SchemeMandatoryQuestionApplicationFormInfosDto.builder()
.hasInternalApplication(webPageUrlIsForInternalApplications)
.hasPublishedInternalApplication(false)
.hasAdvertPublished(false)
.hasInternalApplication(hasSchemeAnApplication)
.hasPublishedInternalApplication(hasPublishedApplication)
.build();

if (webPageUrlIsForInternalApplications) {
final boolean hasPublishedApplication = grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme);
log.debug("Application form associated to advert with Id {} has PUBLISHED status: {}", advert.getId(), hasPublishedApplication);
dto.setHasPublishedInternalApplication(hasPublishedApplication);
}
checkIfAdvertExistAndHasApplyingUrlPointingToInternalApplication(grantSchemeId, dto);

log.debug("Scheme with ID {} is for internal application: {} and has a published application form : {}",
grantSchemeId, dto.isHasInternalApplication(), dto.isHasPublishedInternalApplication());

return ResponseEntity.ok(dto);
}


private void checkIfAdvertExistAndHasApplyingUrlPointingToInternalApplication(Integer grantSchemeId, SchemeMandatoryQuestionApplicationFormInfosDto dto) {
try {
log.debug("Getting Advert associated to scheme with id {}", grantSchemeId);
final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(grantSchemeId.toString());
log.debug("Advert with id {} found for scheme with id {}", advert.getId(), grantSchemeId);

final boolean isAdvertPublished = advert.getStatus().equals(GrantAdvertStatus.PUBLISHED);
if (!isAdvertPublished) {
throw new AdvertNotPublishedException("Advert with id " + advert.getId() + " is not published");
}
dto.setHasAdvertPublished(true);

final String webpageUrl = grantAdvertService.getApplyToUrl(advert);

final boolean webPageUrlIsForInternalApplications = webpageUrl.contains(environmentProperties.getFrontEndUri());
log.debug("Advert is pointing to an internal application form : {}", webPageUrlIsForInternalApplications);
dto.setHasInternalApplication(webPageUrlIsForInternalApplications);

} catch (AdvertNotPublishedException | NotFoundException e) {
log.debug(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void getExternalSubmissionUrl() {
.response(response)
.build();

final String methodResponse = grantAdvertService.getExternalSubmissionUrl(advert);
final String methodResponse = grantAdvertService.getApplyToUrl(advert);

assertThat(methodResponse).isEqualTo("responseUrl");
}
Expand All @@ -91,7 +91,7 @@ void getExternalSubmissionUrl_returnEmptyString() {
.response(response)
.build();

final String methodResponse = grantAdvertService.getExternalSubmissionUrl(advert);
final String methodResponse = grantAdvertService.getApplyToUrl(advert);

assertThat(methodResponse).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void isGrantApplicationPublished__False() {
}

@Test
void doesSchemeHaveApplication__True() {
void doesSchemeHavePublishedApplication__True() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();
final GrantApplication application = GrantApplication.builder()
.grantScheme(scheme)
Expand All @@ -113,7 +113,7 @@ void doesSchemeHaveApplication__True() {
}

@Test
void doesSchemeHaveApplication__False() {
void doesSchemeHavePublishedApplication__False() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();

when(grantApplicationRepository.findByGrantSchemeAndApplicationStatus(scheme, GrantApplicationStatus.PUBLISHED)).thenReturn(Optional.empty());
Expand All @@ -124,6 +124,33 @@ void doesSchemeHaveApplication__False() {
assertFalse(response);
}

@Test
void doesSchemeHaveApplication__True() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();
final GrantApplication application = GrantApplication.builder()
.grantScheme(scheme)
.build();

when(grantApplicationRepository.findByGrantScheme(scheme))
.thenReturn(Optional.of(application));

final boolean response = serviceUnderTest.doesSchemeHaveAnApplication(scheme);

verify(grantApplicationRepository).findByGrantScheme(scheme);
assertTrue(response);
}

@Test
void doesSchemeHaveApplication__False() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();

when(grantApplicationRepository.findByGrantScheme(scheme)).thenReturn(Optional.empty());

final boolean response = serviceUnderTest.doesSchemeHaveAnApplication(scheme);

verify(grantApplicationRepository).findByGrantScheme(scheme);
assertFalse(response);
}
@Test
void getGrantApplicationId__returnsId() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantMandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.dto.api.UpdateGrantMandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.applybackend.enums.GrantApplicationStatus;
import gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionFundingLocation;
import gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionOrgType;
Expand Down Expand Up @@ -40,6 +41,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -109,13 +111,13 @@ void createMandatoryQuestion_CreatesMandatoryQuestionEntry_AndReturnsItsID() {
final GetGrantMandatoryQuestionDto emptyMandatoryQuestionsDto = GetGrantMandatoryQuestionDto.builder()
.id(MANDATORY_QUESTION_ID)
.build();
final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(scheme).build();
final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(scheme).status(GrantAdvertStatus.PUBLISHED).build();
final String webPageUrl = "http://localhost:3000/apply/applicant/internalUrl";
final String frontEndUrl = "http://localhost:3000/apply/applicant";


when(grantAdvertService.getAdvertBySchemeId(schemeId.toString())).thenReturn(grantAdvert);
when(grantAdvertService.getExternalSubmissionUrl(grantAdvert)).thenReturn(webPageUrl);
when(grantAdvertService.getApplyToUrl(grantAdvert)).thenReturn(webPageUrl);
when(environmentProperties.getFrontEndUri()).thenReturn(frontEndUrl);

when(grantApplicantService.getApplicantById(applicantUserId))
Expand All @@ -138,6 +140,40 @@ void createMandatoryQuestion_CreatesMandatoryQuestionEntry_AndReturnsItsID() {

}

@Test
void createMandatoryQuestion_AdvertIsNotPublished_CreatesMandatoryQuestionEntry_AndReturnsItsID() {

final GrantMandatoryQuestions emptyMandatoryQuestions = GrantMandatoryQuestions.builder()
.id(MANDATORY_QUESTION_ID)
.build();
final GetGrantMandatoryQuestionDto emptyMandatoryQuestionsDto = GetGrantMandatoryQuestionDto.builder()
.id(MANDATORY_QUESTION_ID)
.build();
final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(scheme).status(GrantAdvertStatus.UNPUBLISHED).build();

when(grantAdvertService.getAdvertBySchemeId(schemeId.toString())).thenReturn(grantAdvert);

when(grantApplicantService.getApplicantById(applicantUserId))
.thenReturn(applicant);

when(grantSchemeService.getSchemeById(schemeId))
.thenReturn(scheme);

when(grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant, true))
.thenReturn(emptyMandatoryQuestions);

when(grantMandatoryQuestionMapper.mapGrantMandatoryQuestionToGetGrantMandatoryQuestionDTO(emptyMandatoryQuestions))
.thenReturn(emptyMandatoryQuestionsDto);

final ResponseEntity<GetGrantMandatoryQuestionDto> methodResponse = controllerUnderTest.createMandatoryQuestion(schemeId);

verify(grantMandatoryQuestionService).createMandatoryQuestion(scheme, applicant, true);
verify(grantAdvertService, never()).getApplyToUrl(grantAdvert);
assertThat(methodResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(methodResponse.getBody()).isEqualTo(emptyMandatoryQuestionsDto);

}

@Test
void getGrantMandatoryQuestionsById_ReturnsExpectedMandatoryQuestions() {
final GrantMandatoryQuestionFundingLocation fundingLocation = SCOTLAND;
Expand Down
Loading

0 comments on commit e6c1ca8

Please sign in to comment.