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

Improved response #56

Merged
merged 9 commits into from
Jun 8, 2024
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ieeervce.gatekeeper.config;

import jakarta.servlet.http.HttpServletResponse;
import org.ieeervce.gatekeeper.entity.User;
import org.ieeervce.gatekeeper.service.UserInfoUserDetailsService;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
Expand Down Expand Up @@ -41,8 +42,10 @@ public PasswordEncoder passwordEncoder() {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

http.formLogin(httpSecurityFormLoginConfigurer -> httpSecurityFormLoginConfigurer.failureForwardUrl("/loginStatus/failed").successForwardUrl("/loginStatus/success"))
.httpBasic(Customizer.withDefaults())

.authorizeHttpRequests(SecurityConfiguration::getCustomizedHttpAuthorization)
.csrf(AbstractHttpConfigurer::disable)
.cors(customizer->customizer.configurationSource(corsConfigurationSource()));
Expand Down Expand Up @@ -82,4 +85,4 @@ public static String getRequesterDetails() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package org.ieeervce.gatekeeper.controller;

import org.ieeervce.gatekeeper.exception.InvalidDataException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.exception.PDFNotConversionException;
import static org.ieeervce.gatekeeper.config.SecurityConfiguration.getRequesterDetails;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.ieeervce.gatekeeper.dto.RequestForm.RequestDTO;
import org.ieeervce.gatekeeper.dto.RequestForm.RequestFormPdfDTO;
import org.ieeervce.gatekeeper.dto.RequestForm.ResponseRequestFormDTO;
import org.ieeervce.gatekeeper.entity.*;

import org.ieeervce.gatekeeper.entity.FinalStatus;
import org.ieeervce.gatekeeper.entity.RequestForm;
import org.ieeervce.gatekeeper.entity.ReviewLog;
import org.ieeervce.gatekeeper.entity.StatusEnum;
import org.ieeervce.gatekeeper.entity.User;
import org.ieeervce.gatekeeper.exception.InvalidDataException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.exception.PDFNotConversionException;
import org.ieeervce.gatekeeper.service.RequestFormService;
import org.ieeervce.gatekeeper.service.ReviewLogService;
import org.ieeervce.gatekeeper.service.RoleService;
Expand All @@ -18,19 +27,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Type;

import java.util.List;

import static org.ieeervce.gatekeeper.config.SecurityConfiguration.getRequesterDetails;

@RestController
@RequestMapping("/requestForm")


public class RequestFormController {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestFormController.class);

Expand All @@ -48,7 +58,8 @@ protected void configure() {
}
};

RequestFormController(RequestFormService requestFormService, ModelMapper modelMapper, UserService userService, RoleService roleService, ReviewLogService reviewLogService) {
RequestFormController(RequestFormService requestFormService, ModelMapper modelMapper, UserService userService,
RoleService roleService, ReviewLogService reviewLogService) {
this.requestFormService = requestFormService;
this.modelMapper = modelMapper;
this.userService = userService;
Expand All @@ -57,30 +68,69 @@ protected void configure() {
this.modelMapper.addMappings(skipReferencedFieldsMap);
this.modelMapper.getConfiguration().setAmbiguityIgnored(true);


}

@GetMapping
public List<RequestDTO> getAll() {
List<RequestForm> requestFormList=requestFormService.list();
String requesterEmail = getRequesterDetails();
Optional<User> optionalUser = userService.getUserByEmail(requesterEmail);

List<RequestForm> requestFormList = new ArrayList<>();

if (optionalUser.isPresent()) {
User user = optionalUser.get();
try {
Integer societyId = user.getSociety().getSocietyId();
requestFormList = requestFormService.findRequestsBySociety(societyId);
} catch (Exception e) {
LOGGER.error("Error fetching requests by society", e);
requestFormList = requestFormService.list();
}
} else {
LOGGER.warn("User not found with email: " + requesterEmail);
requestFormList = requestFormService.list();
}

Type listType = new TypeToken<List<RequestDTO>>() {
}.getType();
return modelMapper.map(requestFormList, listType);

}

@GetMapping("/byRequester")
public List<RequestDTO> getByUser() {
List<RequestForm> requestFormList = requestFormService.getRequestFormByRequester(userService.getUserByEmail(getRequesterDetails()).get());
List<RequestForm> requestFormList = requestFormService
.getRequestFormByRequester(userService.getUserByEmail(getRequesterDetails()).get());
Type listType = new TypeToken<List<RequestDTO>>() {
}.getType();
return modelMapper.map(requestFormList, listType);
}

@GetMapping("/{requestFormId}")
public ResponseRequestFormDTO getOne(@PathVariable Long requestFormId) throws ItemNotFoundException {
Optional<User> optionalUser = userService.getUserByEmail(getRequesterDetails());

return modelMapper.map(requestFormService.findOne(requestFormId), ResponseRequestFormDTO.class);
if (!optionalUser.isPresent()) {
throw new ItemNotFoundException("User not found with email: " + getRequesterDetails());
}

User user = optionalUser.get();
RequestForm requestForm = requestFormService.findOne(requestFormId);

if (requestForm == null) {
throw new ItemNotFoundException("Request form not found with ID: " + requestFormId);
}

ResponseRequestFormDTO responseDTO = modelMapper.map(requestForm, ResponseRequestFormDTO.class);
List<RequestForm> pendingRequests = user.getPendingRequests();

for (RequestForm pendingRequest : pendingRequests) {
if (pendingRequest.getRequestFormId().equals(requestFormId)) {
responseDTO.setActionable(true);
break;
}
}

return responseDTO;
}

@DeleteMapping("/{requestFormId}")
Expand All @@ -89,7 +139,9 @@ public void deleteRequestForm(@PathVariable Long requestFormId) throws ItemNotFo
}

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseRequestFormDTO postRequestForm(@RequestParam("eventTitle") String eventTitle, @RequestParam("isFinance") boolean isFinance, @RequestParam("formPDF") MultipartFile formPDF) throws InvalidDataException, PDFNotConversionException {
public ResponseRequestFormDTO postRequestForm(@RequestParam("eventTitle") String eventTitle,
@RequestParam("isFinance") boolean isFinance, @RequestParam("formPDF") MultipartFile formPDF)
throws InvalidDataException, PDFNotConversionException {
LOGGER.info("in: post request form");
RequestForm requestForm = new RequestForm();
requestForm.setEventTitle(eventTitle);
Expand All @@ -101,10 +153,10 @@ public ResponseRequestFormDTO postRequestForm(@RequestParam("eventTitle") String
User optionalUser = userService.getUserByEmail(getRequesterDetails()).get();
requestForm.setRequester(optionalUser);


requestForm.setRequestHierarchy(roleService.generateHierarchy(optionalUser, isFinance));

userService.setPendingRequests(requestForm, requestForm.getRequestHierarchy(), requestForm.getRequestIndex(), optionalUser);
userService.setPendingRequests(requestForm, requestForm.getRequestHierarchy(),
requestForm.getRequestIndex(), optionalUser);
} catch (Exception e) {
LOGGER.warn("Exception getting user and hierarchy", e);
}
Expand All @@ -115,26 +167,28 @@ public ResponseRequestFormDTO postRequestForm(@RequestParam("eventTitle") String
throw new PDFNotConversionException("Could not store pdf");
}


RequestForm savedRequestForm = requestFormService.save(requestForm);
return modelMapper.map(savedRequestForm, ResponseRequestFormDTO.class);//truncated
return modelMapper.map(savedRequestForm, ResponseRequestFormDTO.class);// truncated
}

@PutMapping("/{requestFormId}")
public ResponseRequestFormDTO editRequestForm(@RequestBody RequestDTO requestDTO, @PathVariable Long requestFormId) throws ItemNotFoundException {
public ResponseRequestFormDTO editRequestForm(@RequestBody RequestDTO requestDTO, @PathVariable Long requestFormId)
throws ItemNotFoundException {
RequestForm editedRequestForm = modelMapper.map(requestDTO, RequestForm.class);

return modelMapper.map(requestFormService.edit(requestFormId, editedRequestForm), ResponseRequestFormDTO.class); //truncated
return modelMapper.map(requestFormService.edit(requestFormId, editedRequestForm), ResponseRequestFormDTO.class); // truncated
}

@PostMapping("/{requestFormId}/approve")
public ResponseRequestFormDTO approveRequest(@PathVariable Long requestFormId, String comment) throws ItemNotFoundException {
public ResponseRequestFormDTO approveRequest(@PathVariable Long requestFormId, String comment)
throws ItemNotFoundException {
User optionalUser = userService.getUserByEmail(getRequesterDetails()).get();
RequestForm requestForm = requestFormService.findOne(requestFormId);
if (requestForm.getStatus() != FinalStatus.PENDING)
return modelMapper.map(requestForm, ResponseRequestFormDTO.class);
int index = requestForm.getRequestIndex();
userService.removePendingRequests(requestForm, requestForm.getRequestHierarchy(), index, optionalUser, StatusEnum.ACCEPTED);
userService.removePendingRequests(requestForm, requestForm.getRequestHierarchy(), index, optionalUser,
StatusEnum.ACCEPTED);
ReviewLog reviewLog = new ReviewLog();
reviewLog.setComments(comment);
reviewLog.setStatus(StatusEnum.ACCEPTED);
Expand All @@ -147,23 +201,27 @@ public ResponseRequestFormDTO approveRequest(@PathVariable Long requestFormId, S
requestForm.setRequestIndex(index + 1);
index++;
if (index < requestForm.getRequestHierarchy().size())
userService.setPendingRequests(requestForm, requestForm.getRequestHierarchy(), index, requestForm.getRequester());
userService.setPendingRequests(requestForm, requestForm.getRequestHierarchy(), index,
requestForm.getRequester());
else {
requestForm.setStatus(FinalStatus.ACCEPTED);
}
//TODO send mails to requester at every step and send mail to the next set of users assigned(update setPendingRequests() method to add this)
return modelMapper.map(requestFormService.save(requestForm), ResponseRequestFormDTO.class);//truncated
// TODO send mails to requester at every step and send mail to the next set of
// users assigned(update setPendingRequests() method to add this)
return modelMapper.map(requestFormService.save(requestForm), ResponseRequestFormDTO.class);// truncated
}

@PostMapping("/{requestFormId}/reject")
public ResponseRequestFormDTO rejectRequest(@PathVariable Long requestFormId, String comment) throws ItemNotFoundException {
public ResponseRequestFormDTO rejectRequest(@PathVariable Long requestFormId, String comment)
throws ItemNotFoundException {

RequestForm requestForm = requestFormService.findOne(requestFormId);
if (requestForm.getStatus() != FinalStatus.PENDING)
return modelMapper.map(requestForm, ResponseRequestFormDTO.class);
User optionalUser = userService.getUserByEmail(getRequesterDetails()).get();
int index = requestForm.getRequestIndex();
userService.removePendingRequests(requestForm, requestForm.getRequestHierarchy(), index, optionalUser, StatusEnum.REJECTED);
userService.removePendingRequests(requestForm, requestForm.getRequestHierarchy(), index, optionalUser,
StatusEnum.REJECTED);
requestForm.setRequestIndex((index + 1));
ReviewLog reviewLog = new ReviewLog();
reviewLog.setComments(comment);
Expand All @@ -173,8 +231,8 @@ public ResponseRequestFormDTO rejectRequest(@PathVariable Long requestFormId, St
reviewLogService.addReview(reviewLog);
requestForm.setStatus(FinalStatus.REJECTED);
requestForm.getReviewLogs().add(reviewLog);
return modelMapper.map(requestFormService.save(requestForm), ResponseRequestFormDTO.class);//truncated
//TODO update requester with email
return modelMapper.map(requestFormService.save(requestForm), ResponseRequestFormDTO.class);// truncated
// TODO update requester with email
}

@GetMapping("/pdf/{requestFormId}")
Expand All @@ -184,6 +242,4 @@ private RequestFormPdfDTO formPdf(@PathVariable Long requestFormId) throws ItemN

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void setRequestIndex(int requestIndex) {

private boolean isFinance;

private boolean actionable;

public boolean isFinance() {
return isFinance;
}
Expand Down Expand Up @@ -127,7 +129,12 @@ public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}

public boolean isActionable() {
return actionable;
}


public void setActionable(boolean actionable) {
this.actionable = actionable;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import org.ieeervce.gatekeeper.entity.RequestForm;
import org.ieeervce.gatekeeper.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface RequestFormRepository extends JpaRepository<RequestForm,Long> {
List<RequestForm> findAllByRequesterOrderByCreatedAtDesc(User user);
List<RequestForm> findAllByOrderByCreatedAtDesc();}

List<RequestForm> findAllByOrderByCreatedAtDesc();

@Query("SELECT rf FROM RequestForm rf WHERE rf.requester.society.societyId = :societyId ORDER BY rf.createdAt DESC")
List<RequestForm> findByRequesterSociety(@Param("societyId") Integer societyId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public RequestForm findOne(Long requestFormId) throws ItemNotFoundException {
return requestFormRepository.findById(requestFormId).orElseThrow(() -> new ItemNotFoundException(ITEM_NOT_FOUND + requestFormId));
}

public List<RequestForm> findRequestsBySociety(Integer societyId)
{
return requestFormRepository.findByRequesterSociety(societyId);
}
public RequestForm add(RequestForm requestForm) {
return requestFormRepository.save(requestForm);
}
Expand Down