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

[NAYB-151] fix: 라이더 배차 요청 잘못된 로직 수정 #109

Merged
merged 3 commits into from
Sep 16, 2023
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
12 changes: 12 additions & 0 deletions src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.prgrms.nabmart.domain.delivery;

import com.prgrms.nabmart.domain.delivery.exception.AlreadyAssignedDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.InvalidDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.UnauthorizedDeliveryException;
import com.prgrms.nabmart.domain.order.Order;
import com.prgrms.nabmart.domain.user.User;
import com.prgrms.nabmart.global.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -16,6 +19,7 @@
import jakarta.persistence.OneToOne;
import jakarta.persistence.Version;
import java.time.LocalDateTime;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -42,6 +46,7 @@ public class Delivery extends BaseTimeEntity {
@JoinColumn(name = "riderId")
private Rider rider;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private DeliveryStatus deliveryStatus;

Expand Down Expand Up @@ -96,9 +101,16 @@ public void checkAuthority(final Rider rider) {
}

public void assignRider(Rider rider) {
checkAlreadyAssignedToRider();
this.rider = rider;
}

private void checkAlreadyAssignedToRider() {
if (Objects.nonNull(this.rider)) {
throw new AlreadyAssignedDeliveryException("이미 배차 완료된 배달입니다.");
}
}

public void completeDelivery() {
this.arrivedAt = LocalDateTime.now();
this.deliveryStatus = DeliveryStatus.DELIVERED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public interface DeliveryRepository extends JpaRepository<Delivery, Long> {

@Query(value = "select d from Delivery d"
+ " where d.deliveryStatus"
+ " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER")
+ " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER"
+ " and d.rider is null")
Page<Delivery> findWaitingDeliveries(Pageable pageable);

@Query(value = "select d from Delivery d"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ void success() {
int totalElements = 3;
List<Order> orders = createAndSaveOrders(totalElements);
List<Delivery> deliveries = createAndSaveDeliveries(orders);
List<Order> alreadyAssignedOrders = createAndSaveOrders(3);
List<Delivery> alreadyAssignedDeliveries
= createAndSaveDeliveries(alreadyAssignedOrders);
alreadyAssignedDeliveries.forEach(delivery -> delivery.assignRider(rider));
PageRequest pageRequest = PageRequest.of(0, 10);

//when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.prgrms.nabmart.domain.delivery.Delivery;
import com.prgrms.nabmart.domain.delivery.DeliveryStatus;
import com.prgrms.nabmart.domain.delivery.Rider;
import com.prgrms.nabmart.domain.delivery.exception.AlreadyAssignedDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.InvalidDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.NotFoundDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.NotFoundRiderException;
Expand Down Expand Up @@ -401,6 +402,21 @@ void throwExceptionWhenNotFoundDelivery() {
assertThatThrownBy(() -> deliveryService.acceptDelivery(acceptDeliveryCommand))
.isInstanceOf(NotFoundDeliveryException.class);
}

@Test
@DisplayName("예외: 이미 배차 완료된 배달")
void throwExceptionWhenAlreadyAssignedDelivery() {
//given
given(riderRepository.findById(any())).willReturn(Optional.ofNullable(rider));
given(deliveryRepository.findByIdOptimistic(any())).willReturn(
Optional.ofNullable(delivery));
delivery.assignRider(rider);

//when
//then
assertThatThrownBy(() -> deliveryService.acceptDelivery(acceptDeliveryCommand))
.isInstanceOf(AlreadyAssignedDeliveryException.class);
}
}

@Nested
Expand Down