Skip to content

Commit

Permalink
Merge pull request #571 from bounswe/feature/BE-397-coordinator-impro…
Browse files Browse the repository at this point in the history
…vements

Improved/Added coordinator functionalities.
  • Loading branch information
alitpc25 authored Dec 19, 2023
2 parents e6d19c2 + 2950b28 commit 751ecd8
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.groupa1.resq.controller;

import com.groupa1.resq.request.CreateActionRequest;
import com.groupa1.resq.request.UpdateActionRequest;
import com.groupa1.resq.response.ActionResponse;
import com.groupa1.resq.service.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,6 +14,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
Expand All @@ -35,5 +37,33 @@ public ResponseEntity<List<ActionResponse>> viewActions(@RequestParam Long taskI
return actionService.viewActions( taskId);
}

@PreAuthorize("hasRole('COORDINATOR')")
@GetMapping("/updateAction")
public ResponseEntity<String> updateAction(@RequestBody UpdateActionRequest updateActionRequest, @RequestParam Long actionId){
return actionService.updateAction(actionId, updateActionRequest);
}

@PreAuthorize("hasRole('COORDINATOR')")
@GetMapping("/deleteAction")
public ResponseEntity<String> deleteAction(@RequestParam Long actionId, @RequestParam Long taskId){
return actionService.deleteAction(actionId, taskId);
}

@PreAuthorize("hasRole('COORDINATOR')")
@GetMapping("/viewActionByFilter")
public ResponseEntity<List<ActionResponse>> viewActionByFilter(@RequestParam(required = false) Long taskId,
@RequestParam(required = false) Long verifierId,
@RequestParam(required = false) Boolean isCompleted,
@RequestParam(required = false) Boolean isVerified,
@RequestParam(required = false) LocalDateTime dueDate){
return actionService.viewActionByFilter(taskId, verifierId, isCompleted, isVerified, dueDate);
}

@PreAuthorize("hasRole('FACILITATOR')")
@GetMapping("/verifyAction")
public ResponseEntity<String> verifyAction(@RequestParam Long actionId, @RequestParam Long verifierId){
return actionService.verifyAction(actionId, verifierId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ public ResponseEntity<List<ResourceDto>> filterByCategory(@RequestParam(required
log.info("Filtering resources by category");
return resourceService.filterResource(latitude, longitude, categoryTreeId, userId);
}

@PreAuthorize("hasRole('COORDINATOR') or hasRole('VICTIM') or hasRole('RESPONDER')")
@GetMapping("/filterByCategoryRectangularScope")
public ResponseEntity<List<ResourceDto>> filterByCategoryRectangularScope(@RequestParam(required = false) String categoryTreeId,
@RequestParam(required = false) BigDecimal longitude1,
@RequestParam(required = false) BigDecimal latitude1,
@RequestParam(required = false) BigDecimal longitude2,
@RequestParam(required = false) BigDecimal latitude2,
@RequestParam(required = false) Long userId) {
log.info("Filtering resources by category, rectangular scope");
return resourceService.filterResourceRectangularScope(latitude1, longitude1, latitude2, longitude2, categoryTreeId, userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.groupa1.resq.controller;

import com.groupa1.resq.entity.enums.EStatus;
import com.groupa1.resq.entity.enums.EUrgency;
import com.groupa1.resq.request.CreateTaskRequest;
import com.groupa1.resq.request.UpdateTaskRequest;
import com.groupa1.resq.response.TaskResponse;
import com.groupa1.resq.service.TaskService;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -44,6 +47,19 @@ public ResponseEntity<List<TaskResponse>> viewAllTasks(@RequestParam Long userId
return taskService.viewAllTasks(userId);
}

@PreAuthorize("hasRole('COORDINATOR')")
@PostMapping("/updateTask")
public ResponseEntity<String> updateTask(@RequestParam Long taskId, @RequestBody UpdateTaskRequest updateTaskRequest) {
return taskService.updateTask(taskId, updateTaskRequest);
}

@PreAuthorize("hasRole('COORDINATOR')")
@PostMapping("/viewTaskByFilter")
public ResponseEntity<List<TaskResponse>> viewTaskByFilter(@RequestParam Long assignerId, @RequestParam Long assigneeId,
@RequestParam EUrgency urgency, @RequestParam EStatus status) {
return taskService.viewTaskByFilter(assignerId, assigneeId, urgency, status);
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Slf4j
public enum EUserRole {
COORDINATOR, FACILITATOR, RESPONDER, VICTIM;
COORDINATOR, FACILITATOR, RESPONDER, VICTIM, ADMIN;

public static EUserRole getEnumByStr(String roleStr) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.groupa1.resq.repository;

import com.groupa1.resq.entity.Action;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ActionRepository extends JpaRepository<Action, Long> {

List<Action> findAll(Specification<Action> specification);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.groupa1.resq.repository;

import com.groupa1.resq.entity.Action;
import com.groupa1.resq.entity.Task;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand All @@ -14,4 +16,6 @@ public interface TaskRepository extends JpaRepository<Task, Long>{
@Query("SELECT t FROM Task t WHERE t.assignee.id = ?1")
Optional<List<Task>> findByAssignee(Long userId);

List<Task> findAll(Specification<Task> specification);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.groupa1.resq.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;


@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UpdateActionRequest {
private Long taskId;
private Long verifierId;
private String description;
private boolean isCompleted;
private boolean isVerified;
private BigDecimal startLatitude;
private BigDecimal startLongitude;

private BigDecimal endLatitude;
private BigDecimal endLongitude;
private LocalDateTime dueDate;



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.groupa1.resq.request;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.groupa1.resq.entity.enums.EGender;
import com.groupa1.resq.entity.enums.EStatus;
import com.groupa1.resq.entity.enums.EUrgency;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class UpdateTaskRequest {
private Long assignerId;
private Long assigneeId;
private String description;
private List<Action> actions;
private List<Resource> resources;
private EUrgency urgency;
private EStatus status;

@Data
public static class Action {
private Long verifierId;
private String description;
private BigDecimal startLatitude;
private BigDecimal startLongitude;
private BigDecimal endLatitude;
private BigDecimal endLongitude;
private boolean isCompleted;
private boolean isVerified;
}

@Data
public static class Resource {
private Long senderId;
private String categoryTreeId;
private EGender gender;
private Integer quantity;
private BigDecimal latitude;
private BigDecimal longitude;


}


}

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import com.fasterxml.jackson.databind.deser.std.NumberDeserializers;
import com.groupa1.resq.entity.Action;
import com.groupa1.resq.entity.Need;
import com.groupa1.resq.entity.Task;
import com.groupa1.resq.entity.User;
import com.groupa1.resq.exception.EntityNotFoundException;
import com.groupa1.resq.repository.ActionRepository;
import com.groupa1.resq.repository.TaskRepository;
import com.groupa1.resq.repository.UserRepository;
import com.groupa1.resq.request.CreateActionRequest;
import com.groupa1.resq.request.UpdateActionRequest;
import com.groupa1.resq.response.ActionResponse;
import com.groupa1.resq.specification.ActionSpecifications;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
Expand Down Expand Up @@ -98,6 +102,112 @@ public ResponseEntity<List<ActionResponse>> viewActions(Long taskId){
}
}

public ResponseEntity<String> updateAction(Long actionId, UpdateActionRequest updateActionRequest) {
Optional<Action> action = actionRepository.findById(actionId);
if (action.isPresent()) {
Action actionEntity = action.get();
User verifier = userRepository.findById(updateActionRequest.getVerifierId()).orElseThrow(()-> new EntityNotFoundException("No user found"));
String description = updateActionRequest.getDescription();
LocalDateTime dueDate = updateActionRequest.getDueDate();
BigDecimal startLatitude = updateActionRequest.getStartLatitude();
BigDecimal startLongitude =updateActionRequest.getStartLongitude();
BigDecimal endLatitude = updateActionRequest.getEndLatitude();
BigDecimal endLongitude = updateActionRequest.getEndLongitude();
boolean isCompleted = updateActionRequest.isCompleted();
boolean isVerified = updateActionRequest.isVerified();
actionEntity.setVerifier(verifier);
actionEntity.setCompleted(isCompleted);
actionEntity.setVerified(isVerified);
actionEntity.setDescription(description);
actionEntity.setDueDate(dueDate);
actionEntity.setStartLatitude(startLatitude);
actionEntity.setStartLongitude(startLongitude);
actionEntity.setEndLatitude(endLatitude);
actionEntity.setEndLongitude(endLongitude);
actionRepository.save(actionEntity);
return ResponseEntity.ok("Action updated successfully!");
} else {
log.error("No action found with id: {}", actionId);
return null;
}
}

public ResponseEntity<String> deleteAction(Long actionId, Long taskId) {
Optional<Action> action = actionRepository.findById(actionId);
if (action.isPresent()) {
Action actionEntity = action.get();
Task task = taskRepository.findById(taskId).orElseThrow(()-> new EntityNotFoundException("No task found"));
task.getActions().remove(actionEntity);
taskRepository.save(task);
actionRepository.deleteById(actionId);
return ResponseEntity.ok("Action deleted successfully!");
} else {
log.error("No action found with id: {}", actionId);
return null;
}
}

public ResponseEntity<List<ActionResponse>> viewActionByFilter(Long taskId, Long verifierId, Boolean isCompleted, Boolean isVerified, LocalDateTime dueDate){

Specification<Action> spec = Specification.where(null);

if (taskId != null) {
spec = spec.and(ActionSpecifications.hasTaskId(taskId));
}
if (verifierId != null) {
spec = spec.and(ActionSpecifications.hasVerifierId(verifierId));
}
if (isCompleted != null) {
spec = spec.and(ActionSpecifications.hasCompleted(isCompleted));
}
if (isVerified != null) {
spec = spec.and(ActionSpecifications.hasVerified(isVerified));
}
if (dueDate != null) {
spec = spec.and(ActionSpecifications.hasDueDate(dueDate));
}

List<ActionResponse> actionResponses = new ArrayList<>();
List<Action> actions = actionRepository.findAll(spec);
actions.forEach(action -> {
ActionResponse actionResponse = new ActionResponse();
actionResponse.setId(action.getId())
.setTaskId(action.getTask().getId())
.setVerifierId(action.getVerifier().getId())
.setDescription(action.getDescription())
.setCompleted(action.isCompleted())
.setStartLatitude(action.getStartLatitude())
.setStartLongitude(action.getStartLongitude())
.setEndLatitude(action.getEndLatitude())
.setEndLongitude(action.getEndLongitude())
.setDueDate(action.getDueDate())
.setCreatedDate(action.getCreatedAt());
actionResponses.add(actionResponse);
});

return ResponseEntity.ok(actionResponses);
}

public ResponseEntity<String> verifyAction(Long actionId, Long verifierId) {
Optional<Action> action = actionRepository.findById(actionId);
if (action.isPresent()) {
Action actionEntity = action.get();
User verifier = userRepository.findById(verifierId).orElseThrow(() -> new EntityNotFoundException("No user found"));
if (actionEntity.getVerifier().getId() != verifier.getId()) {
return ResponseEntity.badRequest().body("User is not the verifier of the action");
} else {
actionEntity.setVerified(true);
actionRepository.save(actionEntity);
return ResponseEntity.ok("Action verified successfully!");
}
} else {
log.error("No action found with id: {}", actionId);
return null;
}
}






Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ public ResponseEntity<List<ResourceDto>> filterResource(BigDecimal latitude, Big
public ResponseEntity<List<ResourceDto>> filterByDistance(BigDecimal latitude, BigDecimal longitude, BigDecimal distance){
return ResponseEntity.ok(resourceRepository.filterByDistance(latitude, longitude, distance).stream().map(resource -> resourceConverter.convertToDto(resource)).toList());
}

public ResponseEntity<List<ResourceDto>> filterResourceRectangularScope(BigDecimal latitude1, BigDecimal longitude1, BigDecimal latitude2, BigDecimal longitude2, String categoryTreeId, Long userId){
Specification<Resource> spec = Specification.where(null);

if (longitude1 != null && latitude1 != null && longitude2 != null && latitude2 != null) {

spec = spec.and(ResourceSpecifications.isWithinRectangleScope(longitude1, longitude2, latitude1, latitude2));

}
if (categoryTreeId != null) {
spec = spec.and(ResourceSpecifications.hasCategoryTreeId(categoryTreeId));
}
if (userId != null) {
spec = spec.and(ResourceSpecifications.hasOwnerId(userId));
}
return ResponseEntity.ok(resourceRepository.findAll(spec).stream().map(resource -> resourceConverter.convertToDto(resource)).toList());

}
}
Loading

0 comments on commit 751ecd8

Please sign in to comment.