Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class SystemConstants {
public static final BigDecimal ADDITIONAL_ITEM_COST = BigDecimal.valueOf(1.00);
public static final BigDecimal DAILY_LATE_FEE = BigDecimal.valueOf(1.00);
public static final int RETURN_DEADLINE_DAYS = 30;
public static final long URGENT_ORDER_THRESHOLD_MINUTES = 15;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -21,14 +22,26 @@ public interface OrderRepository extends JpaRepository<Order, Integer> {
@Query("SELECT o FROM Order o " +
"JOIN o.library l " +
"JOIN l.address a " +
"WHERE (o.status = 'ACCEPTED' AND o.isReturn = false) " +
"OR (o.status = 'PENDING' AND o.isReturn = true) " +
"WHERE " +
"((o.status = 'ACCEPTED' AND o.isReturn = false) " +
"OR (o.status = 'PENDING' AND o.isReturn = true)) " +
"AND (ST_DistanceSphere(ST_MakePoint(a.latitude, a.longitude), " +
"ST_MakePoint(:driverLatitude, :driverLongitude)) <= :maxDistanceInMeters)")
"ST_MakePoint(:driverLatitude, :driverLongitude)) <= :maxDistanceInMeters) " +

"ORDER BY " +
"CASE WHEN (" +
" (o.status = 'ACCEPTED' AND o.isReturn = false AND o.acceptedAt < :olderThanDate) " +
" OR " +
" (o.status = 'PENDING' AND o.isReturn = true AND o.createdAt < :olderThanDate) " +
") THEN 0 ELSE 1 END ASC, " +

"ST_DistanceSphere(ST_MakePoint(a.latitude, a.longitude), " +
"ST_MakePoint(:driverLatitude, :driverLongitude)) ASC")
Page<Order> findOrdersForDriverWithDistance(
@Param("driverLatitude") BigDecimal driverLatitude,
@Param("driverLongitude") BigDecimal driverLongitude,
@Param("maxDistanceInMeters") double maxDistanceInMeters,
@Param("olderThanDate") LocalDateTime olderThanDate,
Pageable pageable);

Page<Order> findByDriverIdAndStatusIn(String driverId, List<OrderStatus> status, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.stream.Collectors;

import static edu.zut.bookrider.config.SystemConstants.SERVICE_FEE_PERCENTAGE;
import static edu.zut.bookrider.config.SystemConstants.URGENT_ORDER_THRESHOLD_MINUTES;
import static java.util.Objects.isNull;

@RequiredArgsConstructor
Expand Down Expand Up @@ -272,10 +273,14 @@ public PageResponseDTO<OrderResponseDTO> getDriverPendingOrdersWithDistance(
@Valid CoordinateDTO location, double maxDistanceInMeters, int page, int size) {

Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
LocalDateTime olderThanDate = LocalDateTime.now().minusMinutes(URGENT_ORDER_THRESHOLD_MINUTES);
Page<Order> pendingOrders = orderRepository.findOrdersForDriverWithDistance(
BigDecimal.valueOf(location.getLatitude()),
BigDecimal.valueOf(location.getLongitude()),
maxDistanceInMeters, pageable);
maxDistanceInMeters,
olderThanDate,
pageable
);

List<OrderResponseDTO> pendingOrderDtos = pendingOrders.getContent().stream().map(order -> {
OrderResponseDTO dto = orderMapper.map(order);
Expand Down