Exemplo n.º 1
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var fraudCheckFailed = evnt.GetBody <FraudCheckFailed>();
            var order            = await _orderStore.GetAsync(fraudCheckFailed.OrderId);

            Trace.TraceInformation("FraudCancelOrderActor - This order must be cancelled: " + order.Id);

            if (order.IsCancelled)
            {
                return(new Event[0]);
            }

            order.IsCancelled = true;
            await _orderStore.UpsertAsync(order);

            Trace.TraceInformation("FraudCancelOrderActor - order cancelled: " + order.Id);

            return(new[]
            {
                new Event(new OrderCancelled()
                {
                    OrderId = order.Id,
                    Reason = "Fraudulent payment"
                })
                {
                    EventType = "OrderCancelled",
                    QueueName = "OrderCancelled"
                }
            });
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var newsFeedPulsed = evnt.GetBody <NewsFeedPulsed>();
            var client         = new HttpClient();
            var stream         = await client.GetStreamAsync(newsFeedPulsed.Url);

            var feedChannel = new FeedChannel(newsFeedPulsed.Url);
            var feed        = SyndicationFeed.Load(XmlReader.Create(stream));
            var offset      = DateTimeOffset.MinValue;

            if (await _channelStore.ExistsAsync(feedChannel.Id))
            {
                feedChannel = await _channelStore.GetAsync(feedChannel.Id);

                offset = feedChannel.LastOffset;
            }
            feedChannel.LastOffset = feed.Items.Max(x => x.PublishDate);
            await _channelStore.UpsertAsync(feedChannel);

            return(feed.Items.OrderByDescending(x => x.PublishDate)
                   .TakeWhile(y => offset < y.PublishDate)
                   .Select(z => new Event(new NewsItemCaptured()
            {
                Item = z.ToFeedItem()
            })));
        }
Exemplo n.º 3
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var orderShipped = evnt.GetBody <OrderShipped>();
            var order        = await _orderStore.GetAsync(orderShipped.OrderId);

            var shipment = await _shipmentStore.GetAsync(orderShipped.ShipmentId);

            var customer = await _customerStore.GetAsync(order.CustomerId);

            _emailer.Send(
                customer.Email,
                string.Format("Hey {0}, your order has been shipped and expected to arrive on {1}",
                              customer.Name, shipment.DeliveryExpectedDate.ToLongDateString()));

            return(new Event[0]);
        }
Exemplo n.º 4
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var paymentFailed = evnt.GetBody <PaymentFailed>();
            var order         = await _orderStore.GetAsync(paymentFailed.OrderId);

            order.IsCancelled = true;
            await _orderStore.UpsertAsync(order);

            Trace.TraceInformation("Order cancelled");

            return(new[]
            {
                new Event(new OrderCancelled()
                {
                    OrderId = order.Id,
                    Reason = "Payment failure"
                })
            });
        }
Exemplo n.º 5
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var authorised = evnt.GetBody <PaymentAuthorised>();
            var order      = await _ordeStore.GetAsync(authorised.OrderId);

            if (order.IsCancelled)
            {
                return(new Event[0]);
            }

            var payment = await _paymentStore.GetAsync(authorised.PaymentId);

            var customer = await _customeStore.GetAsync(order.CustomerId);

            Trace.TraceInformation("FraudCheckActor - checking order for fraud: " + order.Id);

            if (_fraudChecker.IsFradulent(payment, customer))
            {
                Trace.TraceInformation("FraudCheckActor - !! IS fraud: " + order.Id);

                return(new[]
                {
                    new Event(
                        new FraudCheckFailed()
                    {
                        OrderId = order.Id,
                        PaymentId = payment.Id
                    })
                    {
                        EventType = "FraudCheckFailed",
                        QueueName = "FraudCheckFailed"
                    }
                });
            }

            Trace.TraceInformation("FraudCheckActor - order not fraud: " + order.Id);

            return(new Event[0]);
        }
Exemplo n.º 6
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var paymentAuthorised = evnt.GetBody <PaymentAuthorised>();
            var order             = await _orderStore.GetAsync(paymentAuthorised.OrderId);

            Trace.TraceInformation("OrderInventoryActor - ProductRequested");

            var @event = new Event(new OrderItemsNotYetAccountedFor()
            {
                ProductQuantities = order.ProductQuantities,
                OrderId           = order.Id
            })
            {
                QueueName = "OrderItemsNotYetAccountedFor",
                EventType = "OrderItemsNotYetAccountedFor"
            };

            return(new []
            {
                @event
            });
        }
Exemplo n.º 7
0
        /// <summary>
        /// A simplistic implementation. In reality fulfilment can be an external system/domain
        /// Here we publish
        /// </summary>
        /// <param name="evnt"></param>
        /// <returns></returns>
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var orderInventoryCheckCompleted = evnt.GetBody <OrderInventoryCheckCompleted>();
            var order = await _orderStore.GetAsync(orderInventoryCheckCompleted.OrderId);

            if (order.IsCancelled)
            {
                return(new Event[0]);
            }
            else
            {
                await Task.Delay(_random.Next(1000, 5000));

                Trace.TraceInformation("Fulfilment actor - fulfiling : " + order.Id);

                var shipment = new Shipment()
                {
                    OrderId = order.Id,
                    Id      = Guid.NewGuid().ToString("N"),
                    Address = order.ShippingAddress,
                    DeliveryExpectedDate = DateTime.Now.AddDays(_random.Next(1, 5))
                };
                await _shipmentStore.InsertAsync(shipment);

                return(new[]
                {
                    new Event(new OrderShipped()
                    {
                        OrderId = order.Id,
                        ShipmentId = shipment.Id
                    })
                    {
                        QueueName = "OrderShipped",
                        EventType = "OrderShipped"
                    }
                });
            }
        }
Exemplo n.º 8
0
        public async Task <IEnumerable <Event> > ProcessAsync(Event evnt)
        {
            var orderAccepted = evnt.GetBody <OrderAccepted>();
            var order         = await _orderRepo.GetAsync(orderAccepted.OrderId);

            Trace.TraceInformation("PaymentActor received OrderAccepted " + order.Id);

            // if order is cancelled no further processing
            if (order.IsCancelled)
            {
                Trace.TraceInformation("Order was cancelled.");
                return(new Event[0]);
            }

            var payment = new Payment()
            {
                Amount        = order.TotalPrice,
                OrderId       = order.Id,
                PaymentMethod = order.PaymentMethod
            };

            try
            {
                var transactionId = await _paymentGateway.Authorise(payment);

                Trace.TraceInformation("Order transaction authorised: " + transactionId);

                payment.TransactionId = transactionId;
                await _paymentRepo.InsertAsync(payment);

                Trace.TraceInformation("Payment success");

                return(new[]
                {
                    new Event(new PaymentAuthorised()
                    {
                        OrderId = order.Id,
                        PaymentId = payment.Id
                    })
                });
            }
            catch (AggregateException aggregateException)
            {
                var paymentFailureException = aggregateException.InnerExceptions.OfType <PaymentFailureException>()
                                              .FirstOrDefault();
                if (paymentFailureException != null)
                {
                    Trace.TraceInformation("Payment failed");

                    Trace.TraceWarning(paymentFailureException.ToString());
                    return(new[]
                    {
                        new Event(new PaymentFailed()
                        {
                            OrderId = order.Id
                        })
                    });
                }
                else
                {
                    throw;
                }
            }
            catch (PaymentFailureException paymentFailureException)
            {
                Trace.TraceInformation("Payment failed");
                Trace.TraceWarning(paymentFailureException.ToString());
                return(new[]
                {
                    new Event(new PaymentFailed()
                    {
                        OrderId = order.Id
                    })
                });
            }
        }