diff --git a/order/src/main/java/com/microservices/order_service/domain/OrderStatus.java b/order/src/main/java/com/microservices/order_service/domain/OrderStatus.java index 3920671b..8812d0f3 100644 --- a/order/src/main/java/com/microservices/order_service/domain/OrderStatus.java +++ b/order/src/main/java/com/microservices/order_service/domain/OrderStatus.java @@ -1,5 +1,5 @@ package com.microservices.order_service.domain; public enum OrderStatus { - CREATED, AVAILABLE, PAID, DELIVERED, CANCELED; + CREATED, AVAILABLE, PAID, CANCELED; } diff --git a/order/src/main/java/com/microservices/order_service/dto/DeliveryStatusMessage.java b/order/src/main/java/com/microservices/order_service/dto/DeliveryStatusMessage.java index bb47570d..5316782b 100644 --- a/order/src/main/java/com/microservices/order_service/dto/DeliveryStatusMessage.java +++ b/order/src/main/java/com/microservices/order_service/dto/DeliveryStatusMessage.java @@ -15,4 +15,5 @@ public class DeliveryStatusMessage { private UUID orderId; private DeliveryStatus status; + private UUID cartId; } diff --git a/order/src/main/java/com/microservices/order_service/kafka/CartConsumer.java b/order/src/main/java/com/microservices/order_service/kafka/CartConsumer.java index 3c564ea4..82b58c09 100644 --- a/order/src/main/java/com/microservices/order_service/kafka/CartConsumer.java +++ b/order/src/main/java/com/microservices/order_service/kafka/CartConsumer.java @@ -3,6 +3,7 @@ import com.microservices.order_service.domain.PaymentStatus; import com.microservices.order_service.dto.*; +import com.microservices.order_service.model.Client; import com.microservices.order_service.model.Item; import com.microservices.order_service.model.Order; import com.microservices.order_service.outbox.OrderEventOutbox; @@ -19,10 +20,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,6 +57,9 @@ public class CartConsumer { @Autowired DeliveryService deliveryService; + @Autowired + ClientRepository clientRepository; + @KafkaListener(topics = "cart-topic", groupId = "cartReceiver", containerFactory = "CartListenerContainerFactory") public void listen(CartDTO cartDTO) { @@ -103,6 +104,10 @@ public void listen(CartDTO cartDTO) { orderEventDTO.setItems(items); orderEventOutbox.setProcessed(false); try{ + Client client = new Client(); + clientRepository.save(client); + + order.setClient(client); orderRepository.save(order); orderEventOutbox.setOrderId(order.getId()); orderEventDTO.setOrderId(order.getId()); @@ -114,9 +119,13 @@ public void listen(CartDTO cartDTO) { availabilityCheckDTO.setOrderId(order.getId()); availabilityCheckDTO.setItems(items); - Map AvailabilityCheckResponse = inventoryService.checkOrderAvailability(availabilityCheckDTO); + Map AvailabilityCheckResponse = new HashMap<>(); + AvailabilityCheckResponse.putIfAbsent("status", "OK"); + + /*AvailabilityCheckResponse= inventoryService.checkOrderAvailability(availabilityCheckDTO); */ // this instruction to verify whether the cart items are available in inventory or not if ("OK".equals(AvailabilityCheckResponse.get("status"))){ orderEventOutboxRepository.save(orderEventOutbox); + } else { OrderStatusOutbox orderStatusOutbox = new OrderStatusOutbox(); @@ -124,7 +133,8 @@ public void listen(CartDTO cartDTO) { orderStatusOutbox.setStatus(CANCELED); orderStatusOutbox.setProcessed(false); orderStatusOutboxRepository.save(orderStatusOutbox); - orderRepository.delete(order); + order.setOrderStatus(CANCELED); + orderRepository.save(order); } diff --git a/order/src/main/java/com/microservices/order_service/kafka/DeliveryEventConsumer.java b/order/src/main/java/com/microservices/order_service/kafka/DeliveryEventConsumer.java index 9bfac42f..649c8842 100644 --- a/order/src/main/java/com/microservices/order_service/kafka/DeliveryEventConsumer.java +++ b/order/src/main/java/com/microservices/order_service/kafka/DeliveryEventConsumer.java @@ -1,9 +1,20 @@ package com.microservices.order_service.kafka; +import com.microservices.order_service.domain.DeliveryStatus; +import com.microservices.order_service.domain.OrderStatus; import com.microservices.order_service.dto.DeliveryStatusMessage; import com.microservices.order_service.events.OrderPaidEvent; +import com.microservices.order_service.model.Order; +import com.microservices.order_service.outbox.OrderPaidOutbox; +import com.microservices.order_service.outbox.OrderStatusOutbox; +import com.microservices.order_service.repository.OrderPaidOutboxRepository; +import com.microservices.order_service.repository.OrderRepository; +import com.microservices.order_service.repository.OrderStatusOutboxRepository; +import com.microservices.order_service.service.OrderService; +import com.microservices.order_service.service.OrderStatusOutboxService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.kafka.annotation.KafkaListener; @@ -14,6 +25,8 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; +import static com.microservices.order_service.domain.OrderStatus.CANCELED; + @Component public class DeliveryEventConsumer { @@ -21,11 +34,15 @@ public class DeliveryEventConsumer { private final RestTemplate restTemplate; - @Value("${delivery.service.url}") - private String deliveryServiceUrl; + @Autowired + private OrderStatusOutboxRepository orderStatusOutboxRepository; + + @Autowired + private OrderRepository orderRepository; + + @Autowired + private OrderService orderService; - @Value("${shipping.service.url}") - private String shippingServiceUrl; @@ -44,6 +61,19 @@ public void consume(@Payload DeliveryStatusMessage deliveryStatusMessage, @Header(KafkaHeaders.OFFSET) long offset) { logger.info("Received deliveryStatusMessage: {}", deliveryStatusMessage.getOrderId()); logger.info("Received deliveryStatusMessage: {}", deliveryStatusMessage.getStatus()); + if(deliveryStatusMessage.getStatus()== DeliveryStatus.CANCELLED || deliveryStatusMessage.getStatus() == DeliveryStatus.RETURNED ){ + OrderStatusOutbox orderStatusOutbox = new OrderStatusOutbox(); + orderStatusOutbox.setOrderId(deliveryStatusMessage.getOrderId()); + orderStatusOutbox.setStatus(CANCELED); + orderStatusOutbox.setProcessed(false); + orderStatusOutboxRepository.save(orderStatusOutbox); + + Order order = orderRepository.findById(deliveryStatusMessage.getOrderId()).get(); + order.setOrderStatus(CANCELED); + + orderService.updateOrder(deliveryStatusMessage.getOrderId(), order); + } + } diff --git a/order/src/main/java/com/microservices/order_service/model/Order.java b/order/src/main/java/com/microservices/order_service/model/Order.java index 4db56455..afec3455 100644 --- a/order/src/main/java/com/microservices/order_service/model/Order.java +++ b/order/src/main/java/com/microservices/order_service/model/Order.java @@ -42,7 +42,7 @@ public class Order { private BigDecimal price; // Price field @ManyToOne - @JoinColumn(name = "idClient", referencedColumnName = "idClient") + @JoinColumn(name = "idClient", referencedColumnName = "idClient", nullable = false) private Client client; // Reference to the Client entity @Column(name = "quantity") diff --git a/order/src/main/java/com/microservices/order_service/service/OrderEventOutboxService.java b/order/src/main/java/com/microservices/order_service/service/OrderEventOutboxService.java index 24ce790b..534e3e33 100644 --- a/order/src/main/java/com/microservices/order_service/service/OrderEventOutboxService.java +++ b/order/src/main/java/com/microservices/order_service/service/OrderEventOutboxService.java @@ -29,7 +29,7 @@ public class OrderEventOutboxService { @Autowired private OrderEventProducer orderEventProducer; - @Scheduled(fixedRate = 60000) + @Scheduled(fixedRate = 30000) public void processOrderEventOutbox() { List events = orderEventOutboxRepository.findAll(); diff --git a/order/src/main/java/com/microservices/order_service/service/OrderPaidOutboxService.java b/order/src/main/java/com/microservices/order_service/service/OrderPaidOutboxService.java index 1bded147..0ca28ce6 100644 --- a/order/src/main/java/com/microservices/order_service/service/OrderPaidOutboxService.java +++ b/order/src/main/java/com/microservices/order_service/service/OrderPaidOutboxService.java @@ -23,7 +23,7 @@ public class OrderPaidOutboxService { @Autowired private OrderPaidEventProducer orderPaidEventProducer; - @Scheduled(fixedRate = 60000) + @Scheduled(fixedRate = 30000) public void processOrderPaidOutbox() { List events = orderPaidOutboxRepository.findAll(); diff --git a/order/src/main/java/com/microservices/order_service/service/OrderStatusOutboxService.java b/order/src/main/java/com/microservices/order_service/service/OrderStatusOutboxService.java index 0045e86f..10c62ccd 100644 --- a/order/src/main/java/com/microservices/order_service/service/OrderStatusOutboxService.java +++ b/order/src/main/java/com/microservices/order_service/service/OrderStatusOutboxService.java @@ -23,7 +23,7 @@ public class OrderStatusOutboxService { @Autowired private OrderStatusUpdateProducer orderStatusUpdateProducer; - @Scheduled(fixedRate = 60000) + @Scheduled(fixedRate = 30000) public void processOrderStatusOutbox() { List events = orderStatusOutboxRepository.findAll();