Skip to content

Commit

Permalink
Merge pull request #576 from bounswe/feature/BE-559-recurrent-need-im…
Browse files Browse the repository at this point in the history
…plementation

Feature/be 559 recurrent need implementation
  • Loading branch information
volcaniqueo authored Dec 19, 2023
2 parents 03acf48 + ae9c39f commit 011ac07
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 2 deletions.
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
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
@@ -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
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
Expand Up @@ -12,4 +12,5 @@ public class CreateNeedRequest {
private String categoryTreeId;
private Integer quantity;
private String size;
private Boolean isRecurrent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class UpdateNeedRequest {
private String categoryTreeId;
private Integer quantity;
private String size;
private Boolean isRecurent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Long save(Long userId, CreateNeedRequest createNeedRequest) {
need.setCategoryTreeId(createNeedRequest.getCategoryTreeId());
need.setSize(ESize.valueOf(createNeedRequest.getSize()));
need.setStatus(ENeedStatus.NOT_INVOLVED);
need.setIsRecurrent(createNeedRequest.getIsRecurrent());
return needRepository.save(need).getId();

}
Expand Down Expand Up @@ -105,6 +106,7 @@ public ResponseEntity<String> update (UpdateNeedRequest updateNeedRequest, Long
need.setQuantity(updateNeedRequest.getQuantity());
need.setCategoryTreeId(updateNeedRequest.getCategoryTreeId());
need.setSize(ESize.valueOf(updateNeedRequest.getSize()));
need.setIsRecurrent(updateNeedRequest.getIsRecurent());
needRepository.save(need);
return ResponseEntity.ok("Need updated successfully");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public ResponseEntity<List<TaskResponse>> viewAllTasks(Long userId) {
task.getResources().forEach(resource -> {

resourceResponse.setId(resource.getId())
.setSender(resource.getSender()) // may change to resource.getOwner().getId()
.setSenderId(resource.getSender().getId()) // may change to resource.getOwner().getId()
.setQuantity(resource.getQuantity())
.setGender(resource.getGender())
.setCategoryId(resource.getCategoryTreeId())
Expand Down Expand Up @@ -331,7 +331,7 @@ public ResponseEntity<List<TaskResponse>> viewTaskByFilter(Long assignerId, Long
resources.forEach( resource -> {
ResourceResponse resourceResponse = new ResourceResponse();
resourceResponse.setId(resource.getId())
.setSender(resource.getSender())
.setSenderId(resource.getSender().getId())
.setQuantity(resource.getQuantity())
.setCategoryId(resource.getCategoryTreeId())
.setGender(resource.getGender())
Expand Down

0 comments on commit 011ac07

Please sign in to comment.