diff --git a/src/main/java/com/podzilla/warehouse/Events/EventFactory.java b/src/main/java/com/podzilla/warehouse/Events/EventFactory.java index c37602f..dc3df58 100644 --- a/src/main/java/com/podzilla/warehouse/Events/EventFactory.java +++ b/src/main/java/com/podzilla/warehouse/Events/EventFactory.java @@ -93,16 +93,15 @@ public static OrderPackagedEvent createOrderPackagedEvent( public static OrderAssignedToCourierEvent createOrderAssignedToCourierEvent( UUID orderId, UUID courierId, - double totalAmount, + BigDecimal totalAmount, double orderLatitude, double orderLongitude, String signature, ConfirmationType confirmationType) { - BigDecimal totalAmountBigDecimal = BigDecimal.valueOf(totalAmount); return new OrderAssignedToCourierEvent( orderId.toString(), courierId.toString(), - totalAmountBigDecimal, + totalAmount, orderLatitude, orderLongitude, signature, diff --git a/src/main/java/com/podzilla/warehouse/Models/AssignedOrders.java b/src/main/java/com/podzilla/warehouse/Models/AssignedOrders.java index ab6c8bf..148a7fa 100644 --- a/src/main/java/com/podzilla/warehouse/Models/AssignedOrders.java +++ b/src/main/java/com/podzilla/warehouse/Models/AssignedOrders.java @@ -1,12 +1,16 @@ package com.podzilla.warehouse.Models; +import com.podzilla.mq.events.ConfirmationType; +import com.podzilla.mq.events.DeliveryAddress; import jakarta.persistence.*; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; +import java.math.BigDecimal; import java.time.LocalDateTime; +import java.util.List; import java.util.UUID; @Entity @@ -28,18 +32,52 @@ public class AssignedOrders { @CreationTimestamp private LocalDateTime assignedAt; - public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId) { + @Column(nullable = false) + @OneToMany(mappedBy = "packagedOrder") + private List items; + + @Column(nullable = false) + private DeliveryAddress deliveryAddress; + @Column(nullable = false) + private BigDecimal totalAmount; + @Column(nullable = false) + private double orderLatitude; + @Column(nullable = false) + private double orderLongitude; + @Column(nullable = false) + private String signature; + @Column(nullable = false) + private ConfirmationType confirmationType; + + public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId, List items, + DeliveryAddress deliveryAddress, BigDecimal totalAmount, double orderLatitude, + double orderLongitude, String signature, ConfirmationType confirmationType) { this.orderId = orderId; this.assignerId = assignerId; this.courierId = courierId; this.assignedAt = LocalDateTime.now(); + this.items = items; + this.deliveryAddress = deliveryAddress; + this.totalAmount = totalAmount; + this.orderLatitude = orderLatitude; + this.orderLongitude = orderLongitude; + this.signature = signature; + this.confirmationType = confirmationType; } - - - public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId, LocalDateTime assignedAt) { + + public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId, LocalDateTime assignedAt, + List items, DeliveryAddress deliveryAddress, BigDecimal totalAmount, + double orderLatitude, double orderLongitude, String signature, ConfirmationType confirmationType) { this.orderId = orderId; this.assignerId = assignerId; this.courierId = courierId; this.assignedAt = assignedAt; + this.items = items; + this.deliveryAddress = deliveryAddress; + this.totalAmount = totalAmount; + this.orderLatitude = orderLatitude; + this.orderLongitude = orderLongitude; + this.signature = signature; + this.confirmationType = confirmationType; } } diff --git a/src/main/java/com/podzilla/warehouse/Models/PackagedOrders.java b/src/main/java/com/podzilla/warehouse/Models/PackagedOrders.java index d9a56a3..b141b82 100644 --- a/src/main/java/com/podzilla/warehouse/Models/PackagedOrders.java +++ b/src/main/java/com/podzilla/warehouse/Models/PackagedOrders.java @@ -1,12 +1,16 @@ package com.podzilla.warehouse.Models; +import com.podzilla.mq.events.ConfirmationType; +import com.podzilla.mq.events.DeliveryAddress; import jakarta.persistence.*; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; +import java.math.BigDecimal; import java.time.LocalDateTime; +import java.util.List; import java.util.UUID; @Setter @@ -17,8 +21,23 @@ public class PackagedOrders { @Id private UUID orderId; - private UUID packagerId; + @Column(nullable = false) + @OneToMany(mappedBy = "packagedOrder") + private List items; + + @Column(nullable = false) + private DeliveryAddress deliveryAddress; + @Column(nullable = false) + private BigDecimal totalAmount; + @Column(nullable = false) + private double orderLatitude; + @Column(nullable = false) + private double orderLongitude; + @Column(nullable = false) + private String signature; + @Column(nullable = false) + private ConfirmationType confirmationType; @CreationTimestamp private LocalDateTime packagedAt; @@ -32,5 +51,19 @@ public PackagedOrders(UUID orderId, UUID packagerId, LocalDateTime packagedAt) { public PackagedOrders(UUID orderId) { this.orderId = orderId; this.packagedAt = LocalDateTime.now(); + + } + public PackagedOrders(UUID orderId, UUID packagerId, List items, DeliveryAddress deliveryAddress, + BigDecimal totalAmount, double orderLatitude, double orderLongitude, String signature, + ConfirmationType confirmationType) { + this.orderId = orderId; + this.packagerId = packagerId; + this.items = items; + this.deliveryAddress = deliveryAddress; + this.totalAmount = totalAmount; + this.orderLatitude = orderLatitude; + this.orderLongitude = orderLongitude; + this.signature = signature; + this.confirmationType = confirmationType; } } \ No newline at end of file diff --git a/src/main/java/com/podzilla/warehouse/Models/Stock.java b/src/main/java/com/podzilla/warehouse/Models/Stock.java index 19cd040..a3c0331 100644 --- a/src/main/java/com/podzilla/warehouse/Models/Stock.java +++ b/src/main/java/com/podzilla/warehouse/Models/Stock.java @@ -34,7 +34,9 @@ public class Stock { @Column(nullable = false) private String category; - + @ManyToOne + @JoinColumn(name = "order_id", nullable = true) + private PackagedOrders packagedOrder; @CreationTimestamp @Column(updatable = false) private LocalDateTime createdAt; @@ -62,4 +64,5 @@ public Stock(String name, int quantity) { this.quantity = quantity; } + } \ No newline at end of file diff --git a/src/main/java/com/podzilla/warehouse/Repositories/PackagedOrdersRepository.java b/src/main/java/com/podzilla/warehouse/Repositories/PackagedOrdersRepository.java index 1f8934e..412b0bb 100644 --- a/src/main/java/com/podzilla/warehouse/Repositories/PackagedOrdersRepository.java +++ b/src/main/java/com/podzilla/warehouse/Repositories/PackagedOrdersRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import java.util.Optional; import java.util.UUID; @Repository @@ -13,4 +14,5 @@ public interface PackagedOrdersRepository extends JpaRepository findByPackagerIdIsNull(Pageable pageable); Page findByPackagerId(UUID packagerId, Pageable pageable); Page findByOrderId(UUID orderId, Pageable pageable); + Optional findOneByOrderId(UUID orderId); } diff --git a/src/main/java/com/podzilla/warehouse/Services/AssignedOrdersService.java b/src/main/java/com/podzilla/warehouse/Services/AssignedOrdersService.java index 488bda6..44a3d3b 100644 --- a/src/main/java/com/podzilla/warehouse/Services/AssignedOrdersService.java +++ b/src/main/java/com/podzilla/warehouse/Services/AssignedOrdersService.java @@ -5,9 +5,10 @@ import com.podzilla.mq.events.OrderAssignedToCourierEvent; import com.podzilla.warehouse.Events.EventFactory; import com.podzilla.warehouse.Models.AssignedOrders; +import com.podzilla.warehouse.Models.PackagedOrders; import com.podzilla.warehouse.Repositories.AssignedOrdersRepository; +import com.podzilla.warehouse.Repositories.PackagedOrdersRepository; import lombok.RequiredArgsConstructor; -import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; @@ -27,6 +28,7 @@ public class AssignedOrdersService { @Autowired private AssignedOrdersRepository assignedOrdersRepository; + private PackagedOrdersRepository packagedOrdersRepository; @Cacheable(key = "'orderId:' + #orderId") public Optional> findByOrderId(UUID orderId) { @@ -45,13 +47,24 @@ public Optional> findByAssignerIdIsNull() { @CachePut(key = "'orderId:' + #orderId") public AssignedOrders assignOrder(UUID orderId, UUID assignerId, UUID courierId) { - AssignedOrders assignment = new AssignedOrders(orderId, assignerId, courierId); + PackagedOrders packagedOrder = packagedOrdersRepository.findOneByOrderId(orderId).get(); + AssignedOrders assignment = new AssignedOrders(orderId, assignerId, courierId, packagedOrder.getItems(), + packagedOrder.getDeliveryAddress(), packagedOrder.getTotalAmount(), packagedOrder.getOrderLatitude(), + packagedOrder.getOrderLongitude(), packagedOrder.getSignature(), packagedOrder.getConfirmationType()); assignedOrdersRepository.save(assignment); - //TODO: after merge -// OrderAssignedToCourierEvent event = EventFactory.createOrderAssignedToCourierEvent(orderId, courierId); -// -// eventPublisher.publishEvent(EventsConstants.ORDER_ASSIGNED_TO_COURIER, event); + OrderAssignedToCourierEvent event = + EventFactory.createOrderAssignedToCourierEvent( + orderId, + courierId, + assignment.getTotalAmount(), + assignment.getOrderLatitude(), + assignment.getOrderLongitude(), + assignment.getSignature(), + assignment.getConfirmationType() + ); + + eventPublisher.publishEvent(EventsConstants.ORDER_ASSIGNED_TO_COURIER, event); return assignment; }