public override async Task Handle(OfferBecameUnavailableIntegrationEvent @event)
        {
            if (@event.Trigger == UnavailabilityTrigger.Removal)
            {
                _logger.LogWithProps(LogLevel.Information,
                                     $"Ignored {nameof(OfferBecameUnavailableIntegrationEvent)} integration event",
                                     "EventId".ToKvp(@event.Id),
                                     "OfferId".ToKvp(@event.OfferId),
                                     "Trigger".ToKvp(@event.Trigger));

                return;
            }

            _logger.LogWithProps(LogLevel.Information,
                                 $"Handling {nameof(OfferBecameUnavailableIntegrationEvent)} integration event",
                                 "EventId".ToKvp(@event.Id),
                                 "OfferId".ToKvp(@event.OfferId));

            var ordersToCancel = await _orderRepository.GetAllStartedOrdersByOfferId(@event.OfferId);

            foreach (var order in ordersToCancel)
            {
                order.SetCancelled(OrderState.CancelledBySeller);
            }

            await _orderRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync();

            _logger.LogWithProps(LogLevel.Information,
                                 $"Cancelled orders",
                                 "EventId".ToKvp(@event.Id),
                                 "OrderIds".ToKvp(string.Join(",", ordersToCancel.Select(x => x.Id))));

            foreach (var orderToCancel in ordersToCancel)
            {
                var notification = new NotificationIntegrationEvent
                {
                    UserId   = orderToCancel.BuyerId,
                    Code     = NotificationCodes.OfferBecameUnavailable,
                    Message  = "Offer became unavailable",
                    Metadata = new Dictionary <string, string>
                    {
                        { "OrderId", orderToCancel.Id.ToString() },
                        { "SourceOfferId", @event.OfferId.ToString() }
                    }
                };

                await _eventBus.PublishAsync(notification);
            }

            _logger.LogWithProps(LogLevel.Information,
                                 $"Published {ordersToCancel.Count} notifications",
                                 "EventId".ToKvp(@event.Id),
                                 "AffectedOrderIds".ToKvp(string.Join(",", ordersToCancel.Select(x => x.Id))));
        }
Ejemplo n.º 2
0
        public async Task <CreateOrderResponse> CreateOrder(CreateOrderRequest request)
        {
            var orderItems = request.CartItems.Select(item => new OrderItem(
                                                          item.OfferId, item.OfferName, item.Quantity, item.PricePerItem, item.ImageUri)).ToList();
            var order = new Order(request.UserId, request.SellerId, orderItems);

            _orderRepository.Add(order);
            await _orderRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync();

            var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent
            {
                OrderId    = order.Id,
                OrderItems = order.OrderItems.Select(orderItem => new OrderItemDto
                {
                    OfferId  = orderItem.OfferId,
                    Quantity = orderItem.Quantity
                }).ToList()
            };
            await _eventBus.PublishAsync(orderStartedIntegrationEvent);

            _logger.LogInformation($"Integration event {nameof(OrderStartedIntegrationEvent)} published",
                                   "EventId".ToKvp(orderStartedIntegrationEvent.Id),
                                   "OfferIds".ToKvp(string.Join(",", orderStartedIntegrationEvent.OrderItems.Select(x => x.OfferId))));

            var notificationIntegrationEvent = new NotificationIntegrationEvent
            {
                UserId   = order.SellerId,
                Code     = NotificationCodes.OrderStarted,
                Message  = "Order started",
                Metadata = new Dictionary <string, string>
                {
                    { "OrderId", order.Id.ToString() },
                    { "BuyerId", order.BuyerId.ToString() },
                    { "OfferIds", string.Join(",", request.CartItems.Select(x => x.OfferId)) }
                }
            };
            await _eventBus.PublishAsync(notificationIntegrationEvent);

            _logger.LogInformation($"Published {nameof(NotificationIntegrationEvent)}",
                                   "EventId".ToKvp(orderStartedIntegrationEvent.Id),
                                   "PublishedEventId".ToKvp(notificationIntegrationEvent.Id),
                                   "NotifiedUserId".ToKvp(notificationIntegrationEvent.UserId));

            return(new CreateOrderResponse {
                OrderId = order.Id
            });
        }
        public override async Task Handle(OfferBecameUnavailableIntegrationEvent @event)
        {
            _logger.LogWithProps(LogLevel.Information,
                                 $"Handling {nameof(OfferBecameUnavailableIntegrationEvent)} integration event",
                                 "EventId".ToKvp(@event.Id),
                                 "OfferId".ToKvp(@event.OfferId));

            var cartItems = await _cartItemRepository.GetByOfferId(@event.OfferId);

            if (!cartItems.Any())
            {
                return;
            }

            var notifications = new List <NotificationIntegrationEvent>();

            foreach (var cartItem in cartItems)
            {
                var notification = new NotificationIntegrationEvent
                {
                    UserId   = cartItem.CartOwnerId,
                    Code     = NotificationCodes.CartItemBecameUnavailable,
                    Message  = "Offer became unavailable",
                    Metadata = new Dictionary <string, string>
                    {
                        { "OfferId", cartItem.OfferId.ToString() },
                        { "OfferName", cartItem.OfferName },
                        { "CartItemId", cartItem.Id.ToString() }
                    }
                };
                notifications.Add(notification);

                _cartItemRepository.Remove(cartItem);
            }
            _cartItemRepository.RemoveWithOfferId(@event.OfferId);
            await _cartItemRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync();

            foreach (var notification in notifications)
            {
                await _eventBus.PublishAsync(notification);
            }

            _logger.LogWithProps(LogLevel.Information,
                                 $"Published {notifications.Count} {nameof(NotificationIntegrationEvent)}",
                                 "SourceEventId".ToKvp(@event.Id),
                                 "OfferId".ToKvp(@event.OfferId));
        }
Ejemplo n.º 4
0
        public async Task Handle(OrderConfirmedDomainEvent domainEvent, CancellationToken cancellationToken)
        {
            var notification = new NotificationIntegrationEvent
            {
                UserId   = domainEvent.SellerId,
                Code     = NotificationCodes.OrderConfirmed,
                Message  = "Order has been confirmed",
                Metadata = new Dictionary <string, string>
                {
                    { "OrderId", domainEvent.OrderId.ToString() },
                    { "BuyerId", domainEvent.BuyerId.ToString() }
                }
            };

            await _eventBus.PublishAsync(notification);

            _logger.LogWithProps(LogLevel.Information,
                                 $"Published {nameof(NotificationIntegrationEvent)} after order has been confirmed",
                                 "EventId".ToKvp(notification.Id),
                                 "OrderId".ToKvp(domainEvent.OrderId));
        }
Ejemplo n.º 5
0
        public async Task Handle(OrderStatusChangedDomainEvent domainEvent, CancellationToken cancellationToken)
        {
            var notification = new NotificationIntegrationEvent
            {
                UserId   = domainEvent.BuyerId,
                Code     = NotificationCodes.OrderStateChanged,
                Message  = "Order changed state",
                Metadata = new Dictionary <string, string>
                {
                    { "OrderId", domainEvent.OrderId.ToString() },
                    { "PreviousOrderState", domainEvent.PreviousState.Name },
                    { "CurrentOrderState", domainEvent.CurrentState.Name }
                }
            };

            await _eventBus.PublishAsync(notification);

            _logger.LogWithProps(LogLevel.Information,
                                 $"Published {nameof(NotificationIntegrationEvent)} after order status change",
                                 "EventId".ToKvp(notification.Id),
                                 "OrderId".ToKvp(domainEvent.OrderId));
        }