From ac631e7daf43b5dfc3e8b6c790393f4d4b4c0cdb Mon Sep 17 00:00:00 2001 From: Piumal Rathnayake Date: Sun, 18 Jul 2021 17:10:12 +0530 Subject: [PATCH] Prevent mentors from editing application in wrong states (#182) --- .../scholarx/controller/ProgramController.java | 2 +- .../sefglobal/scholarx/service/ProgramService.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sefglobal/scholarx/controller/ProgramController.java b/src/main/java/org/sefglobal/scholarx/controller/ProgramController.java index 06a99155..cf8022ff 100644 --- a/src/main/java/org/sefglobal/scholarx/controller/ProgramController.java +++ b/src/main/java/org/sefglobal/scholarx/controller/ProgramController.java @@ -133,7 +133,7 @@ public List editMentorResponses( Authentication authentication, @RequestBody List responses ) - throws ResourceNotFoundException{ + throws ResourceNotFoundException, BadRequestException { Profile profile = (Profile) authentication.getPrincipal(); return programService.editMentorResponses(id, profile.getId(), responses); } diff --git a/src/main/java/org/sefglobal/scholarx/service/ProgramService.java b/src/main/java/org/sefglobal/scholarx/service/ProgramService.java index f7ba0b90..1b2fdd9b 100644 --- a/src/main/java/org/sefglobal/scholarx/service/ProgramService.java +++ b/src/main/java/org/sefglobal/scholarx/service/ProgramService.java @@ -343,11 +343,12 @@ public List getMentorResponses(long mentorId) throws ResourceNot * @param mentorResponses list of {@link MentorResponse}s to be updated * @return the updated list of {@link MentorResponse} * @throws ResourceNotFoundException if a mentor doesn't exist by the given profileId and programId + * @throws BadRequestException is thrown if the {@link Program} is not in the valid {@link ProgramState} */ public List editMentorResponses(long programId, long profileId, List mentorResponses) - throws ResourceNotFoundException { + throws ResourceNotFoundException, BadRequestException { Optional mentor = mentorRepository.findByProfileIdAndProgramId(profileId, programId); if (!mentor.isPresent()) { String msg = "Error, Mentor by profile id: " + profileId + " and " + @@ -356,6 +357,14 @@ public List editMentorResponses(long programId, log.error(msg); throw new ResourceNotFoundException(msg); } + + if (!ProgramState.MENTOR_APPLICATION.equals(mentor.get().getProgram().getState())) { + String msg = "Error, Unable to edit mentor application. " + + "Program with id: " + programId + " is not in the valid state."; + log.error(msg); + throw new BadRequestException(msg); + } + List updatedMentorResponses = new ArrayList<>(); for (MentorResponse response: mentorResponses) { MentorResponse queriedResponse = mentorResponseRepository.getOne(response.getId());