diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application.Contracts/Orders/UpdateOrderDto.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application.Contracts/Orders/UpdateOrderDto.cs index c907dea9..adf69220 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application.Contracts/Orders/UpdateOrderDto.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application.Contracts/Orders/UpdateOrderDto.cs @@ -1,7 +1,11 @@ -namespace EShopOnAbp.OrderingService.Orders +using System; + +namespace EShopOnAbp.OrderingService.Orders { public class UpdateOrderDto { public int OrderStatusId { get; set; } + public Guid PaymentRequestId { get; set; } + public string PaymentRequestStatus { get; set; } } } \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs index 40911bf2..29bae4f0 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs @@ -55,10 +55,17 @@ public class OrderAppService : ApplicationService, IOrderAppService public async Task UpdateAsync(Guid id, UpdateOrderDto input) { var order = await _orderRepository.GetAsync(id); - order.SetOrder(input.OrderStatusId); - await _orderRepository.UpdateAsync(order); - return CreateOrderDtoMapping(order); + if (input.OrderStatusId == OrderStatus.Shipped.Id) + { + order.SetOrderAsShipped(input.OrderStatusId); + await _orderRepository.UpdateAsync(order); + } + else if (input.OrderStatusId == OrderStatus.Cancelled.Id) + { + await _orderManager.CancelOrderAsync(id, input.PaymentRequestId, input.PaymentRequestStatus); + } + return CreateOrderDtoMapping(order); } public async Task CreateAsync(OrderCreateDto input) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderCancelledEto.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderCancelledEto.cs new file mode 100644 index 00000000..62e221fd --- /dev/null +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain.Shared/Orders/OrderCancelledEto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus; + +namespace EShopOnAbp.OrderingService.Orders; + +[EventName("EShopOnAbp.Order.Cancelled")] +public class OrderCancelledEto : EtoBase +{ + public Guid OrderId { get; set; } + public int OrderNo { get; set; } + public DateTime OrderDate { get; set; } + public BuyerEto Buyer { get; set; } + public List Items { get; set; } = new(); +} \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Order.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Order.cs index 085d6b56..802ec1a9 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Order.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/Order.cs @@ -59,6 +59,15 @@ public class Order : AggregateRoot return this; } + public Order SetOrderCancelled(Guid paymentRequestId, string paymentRequestStatus) + { + PaymentRequestId = paymentRequestId; + PaymentStatus = paymentRequestStatus; + OrderStatus = OrderStatus.Cancelled; + + return this; + } + public Order AddOrderItem(Guid id, Guid productId, string productName, string productCode, decimal unitPrice, decimal discount, string pictureUrl, int units = 1) { @@ -88,14 +97,14 @@ public class Order : AggregateRoot return OrderItems.Sum(o => o.Units * o.UnitPrice); } - public Order SetOrder(int orderStatus) + public Order SetOrderAsShipped(int orderStatus) { if (orderStatus == OrderStatus.Cancelled.Id) { return this; } //TODO no enough to update the object. - _orderStatusId = orderStatus; + _orderStatusId = OrderStatus.Shipped.Id; return this; } } \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs index 60e2c41d..37da005a 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/OrderManager.cs @@ -91,6 +91,30 @@ public class OrderManager : DomainService return await _orderRepository.UpdateAsync(order, autoSave: true); } + public async Task CancelOrderAsync(Guid orderId, Guid paymentRequestId, string paymentRequestStatus) + { + var order = await _orderRepository.GetAsync(orderId); + if (order == null) + { + throw new BusinessException(OrderingServiceErrorCodes.OrderWithIdNotFound) + .WithData("OrderId", orderId); + } + + order.SetOrderCancelled(paymentRequestId, paymentRequestStatus); + + // Publish order cancelled event + await _distributedEventBus.PublishAsync(new OrderCancelledEto + { + OrderId = order.Id, + OrderDate = order.OrderDate, + OrderNo = order.OrderNo, + Buyer = GetBuyerEto(order.Buyer), + Items = GetProductItemEtoList(order.OrderItems) + }); + + return await _orderRepository.UpdateAsync(order, autoSave: true); + } + private BuyerEto GetBuyerEto(Buyer buyer) { return new BuyerEto diff --git a/services/ordering/test/EShopOnAbp.OrderingService.Application.Tests/Orders/OrderApplication_Tests.cs b/services/ordering/test/EShopOnAbp.OrderingService.Application.Tests/Orders/OrderApplication_Tests.cs index 531fae8f..b6543bfe 100644 --- a/services/ordering/test/EShopOnAbp.OrderingService.Application.Tests/Orders/OrderApplication_Tests.cs +++ b/services/ordering/test/EShopOnAbp.OrderingService.Application.Tests/Orders/OrderApplication_Tests.cs @@ -69,9 +69,9 @@ public class OrderApplication_Tests : OrderingServiceApplicationTestBase var cancelledMyOrder = await _orderAppService.UpdateAsync(placedOrder.Id, new UpdateOrderDto() { - OrderStatusId = OrderStatus.Cancelled.Id, + OrderStatusId = OrderStatus.Shipped.Id, }); - //TODO - temp value - it should be Cancelled + //TODO - temp value - it should be Shipped cancelledMyOrder.OrderStatus.ShouldBe(OrderStatus.Placed.ToString()); diff --git a/services/payment/src/EShopOnAbp.PaymentService.Domain/EShopOnAbp.PaymentService.Domain.csproj b/services/payment/src/EShopOnAbp.PaymentService.Domain/EShopOnAbp.PaymentService.Domain.csproj index 36f90232..a1af97d7 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Domain/EShopOnAbp.PaymentService.Domain.csproj +++ b/services/payment/src/EShopOnAbp.PaymentService.Domain/EShopOnAbp.PaymentService.Domain.csproj @@ -12,4 +12,8 @@ + + + + diff --git a/services/payment/src/EShopOnAbp.PaymentService.Domain/EventHandlers/OrderCancelledEventHandler.cs b/services/payment/src/EShopOnAbp.PaymentService.Domain/EventHandlers/OrderCancelledEventHandler.cs new file mode 100644 index 00000000..9f1d781f --- /dev/null +++ b/services/payment/src/EShopOnAbp.PaymentService.Domain/EventHandlers/OrderCancelledEventHandler.cs @@ -0,0 +1,26 @@ +using EShopOnAbp.OrderingService.Orders; +using EShopOnAbp.PaymentService.PaymentRequests; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Uow; + +namespace EShopOnAbp.PaymentService.EventHandlers +{ + public class OrderCancelledEventHandler : IDistributedEventHandler, ITransientDependency + { + private readonly IPaymentRequestRepository _paymentRepository; + + public OrderCancelledEventHandler(IPaymentRequestRepository paymenRepository) + { + _paymentRepository = paymenRepository; + } + + [UnitOfWork] + public async Task HandleEventAsync(OrderCancelledEto eventData) + { + var payment = await _paymentRepository.GetAsync(eventData.Buyer.BuyerId.GetValueOrDefault()); + await _paymentRepository.DeleteAsync(payment); + } + } +}