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

merging dev into frontend/back_integrate branch #584

Closed
wants to merge 16 commits into from
Closed
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
4 changes: 4 additions & 0 deletions resq/backend/resq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin
<dependency>
<groupId>com.google.firebase</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ResqApplication {

public static void main(String[] args) {
Expand Down
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 @@ -2,13 +2,16 @@

import com.groupa1.resq.entity.Need;
import com.groupa1.resq.entity.Notification;
import com.groupa1.resq.entity.enums.ENotificationEntityType;
import com.groupa1.resq.converter.NotificationConverter;
import com.groupa1.resq.dto.NotificationDto;
import com.groupa1.resq.service.NotificationService;
import com.groupa1.resq.util.NotificationMessages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@CrossOrigin(origins = "*", maxAge = 3600)
Expand All @@ -20,17 +23,31 @@ public class NotificationController {
@Autowired
private NotificationService notificationService;

@Autowired
private NotificationConverter notificationConverter;

@GetMapping("/viewAllNotifications")
@PreAuthorize("hasRole('FACILITATOR') or hasRole('VICTIM') or hasRole('RESPONDER')")
public List<Notification> viewAllNotifications(@RequestParam Long userId) {
public List<NotificationDto> viewAllNotifications(@RequestParam Long userId) {
log.info("Viewing all notifications");
return notificationService.viewAllNotifications(userId);
return notificationService.viewAllNotifications(userId).stream().map(notificationConverter::convertToDto).toList();
}

@GetMapping("/viewNotificationById")
@PreAuthorize("hasRole('FACILITATOR') or hasRole('VICTIM') or hasRole('RESPONDER')")
public Notification viewNotificationById(@RequestParam Long notificationId, @RequestParam Long userId) {
public NotificationDto viewNotificationById(@RequestParam Long notificationId, @RequestParam Long userId) {
log.info("Viewing notification with id: {}, user id: {}", notificationId, userId);
return notificationService.viewNotificationById(userId, notificationId);
return notificationConverter.convertToDto(notificationService.viewNotificationById(userId, notificationId));
}

@PostMapping("/sendNotification")
@PreAuthorize("hasRole('ADMIN')")
public void sendSystemNotification(@RequestParam String title, @RequestParam Long userId,
@RequestParam Long relatedEntityId, @RequestParam ENotificationEntityType notificationType) {
String body = String.format(NotificationMessages.SYSTEM_MESSAGE, userId, relatedEntityId);
log.info("Sending notification with title: {}, body: {}, user id: {}, related entity id: {}, notification type: {}"
, title, body, userId, relatedEntityId, notificationType);
notificationService.sendNotification(title, body, userId, relatedEntityId, notificationType);
}

}
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 @@ -33,6 +33,22 @@ public String requestRole(@RequestParam Long userId, @RequestParam String role)
return "Role successfully inserted to " + user.getName() + ".";
}

@PostMapping("/assignRole")
@PreAuthorize("hasRole('ADMIN') or hasRole('COORDINATOR') or hasRole('FACILITATOR')")
public String assignRole(@RequestParam Long assignerId, @RequestParam Long assigneeId, @RequestParam String role) {
log.info("Requested role: {} requested for user: {}", role, assigneeId);
User user = userService.assignRole(assignerId, assigneeId, role);
return "Role successfully inserted to " + user.getName() + ".";
}

@PostMapping("/removeRole")
@PreAuthorize("hasRole('ADMIN') or hasRole('COORDINATOR') or hasRole('FACILITATOR')")
public String removeRole(@RequestParam Long assignerId, @RequestParam Long assigneeId, @RequestParam String role) {
log.info("Requested role to be removed: {} requested for user: {}", role, assigneeId);
User user = userService.removeRole(assignerId, assigneeId, role);
return "Role successfully removed from " + user.getName() + ".";
}

@GetMapping("/getUserInfo")
@PreAuthorize("hasRole('ADMIN') or hasRole('COORDINATOR') or hasRole('FACILITATOR') or hasRole('RESPONDER') or hasRole('VICTIM')")
public UserDto getUserInfo(@RequestParam Long userId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.groupa1.resq.converter;

import com.groupa1.resq.dto.NotificationDto;
import com.groupa1.resq.entity.Notification;
import org.springframework.stereotype.Service;

@Service
public class NotificationConverter {

public NotificationDto convertToDto(Notification notification) {
NotificationDto notificationDto = new NotificationDto();
notificationDto.setId(notification.getId());
notificationDto.setCreatedAt(notification.getCreatedAt());
notificationDto.setModifiedAt(notification.getModifiedAt());
notificationDto.setNotificationType(notification.getNotificationType().toString());
notificationDto.setUserId(notification.getUser().getId());
notificationDto.setTitle(notification.getTitle());
notificationDto.setBody(notification.getBody());
notificationDto.setRead(notification.isRead());
notificationDto.setRelatedEntityId(notification.getRelatedEntityId());
notificationDto.setNotificationType(notification.getNotificationType().toString());
return notificationDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.groupa1.resq.dto;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class NotificationDto {
private Long id;
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;
private Long userId;
private String title;
private String body;
private boolean isRead;
private Long relatedEntityId;
private String notificationType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ public class Need extends BaseEntity{
@Enumerated(EnumType.STRING)
private ENeedStatus status;

private Boolean isRecurrent;

}
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
@@ -0,0 +1,11 @@
package com.groupa1.resq.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public class EntityNotAllowedException extends RuntimeException {
public EntityNotAllowedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.groupa1.resq.jobs;

import com.groupa1.resq.entity.Need;
import com.groupa1.resq.entity.Request;
import com.groupa1.resq.entity.enums.ENeedStatus;
import com.groupa1.resq.repository.NeedRepository;
import com.groupa1.resq.repository.RequestRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.HashSet;

@Service
@Slf4j
public class RecurrentNeedJob {

@Autowired
private NeedRepository needRepository;

@Autowired
private RequestRepository requestRepository;

// will be open later
//@Scheduled(cron = "0 0 8 * * ?")
public void run() {
// This job will create a request consisting of recurrent needs every day.
HashSet<Request> requests = new HashSet<>();
needRepository.findAllByIsRecurrentTrue().forEach(need -> {
Request request = need.getRequest();
if(!requests.contains(request)) {
Request newRequest = new Request();
newRequest.setNeeds(new HashSet<>());
newRequest.setRequester(request.getRequester());
newRequest.setDescription(request.getDescription());
newRequest.setLongitude(request.getLongitude());
newRequest.setLatitude(request.getLatitude());
newRequest.setUrgency(request.getUrgency());
newRequest.setStatus(request.getStatus());

request.getNeeds().forEach(need1 -> {
if (need1.getIsRecurrent()) {
Need newNeed = new Need();
newNeed.setIsRecurrent(false);
newNeed.setLatitude(need1.getLatitude());
newNeed.setLongitude(need1.getLongitude());
newNeed.setQuantity(need1.getQuantity());
newNeed.setSize(need1.getSize());
newNeed.setDescription(need1.getDescription());
newNeed.setStatus(ENeedStatus.NOT_INVOLVED);
newNeed.setRequester(need1.getRequester());
newNeed.setCategoryTreeId(need1.getCategoryTreeId());
newNeed.setRequest(newRequest);
needRepository.save(newNeed);
newRequest.getNeeds().add(newNeed);
}

});
requests.add(request);
requestRepository.save(newRequest);
}
});
log.info("Recurrent Need Job");
System.out.println("Recurrent Need Job");
}
}
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
Expand Up @@ -20,6 +20,8 @@ public interface NeedRepository extends JpaRepository<Need, Long> {

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

List<Need> findAllByIsRecurrentTrue();


//Haversine formula
@Query(value = "SELECT * FROM NEED WHERE (6371 * acos(cos(radians(?1)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?2)) + sin(radians(?1)) * sin(radians(latitude)))) < ?3", nativeQuery = true)
Expand Down
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
Expand Up @@ -12,4 +12,5 @@ public class CreateNeedRequest {
private String categoryTreeId;
private Integer quantity;
private String size;
private Boolean isRecurrent;
}
Loading
Loading