Exemple #1
0
        protected override DocumentInfo ComposeDocument(IUnitOfWork db,
                                                        long companyId,
                                                        MarketType market,
                                                        string marketplaceId,
                                                        IList <string> asinList)
        {
            //NOTE: reprocess cancel request every 2 hours
            var maxLastAttemptDate = Time.GetUtcTime().AddHours(-2);

            _cancelActions    = _actionService.GetUnprocessedByType(db, SystemActionType.UpdateOnMarketCancelOrder, maxLastAttemptDate, null);
            _actionOutputList = new Dictionary <long, CancelOrderOutput>();

            if (_cancelActions.Any())
            {
                var orderMessages = new List <XmlElement>();
                var index         = 0;
                var merchant      = db.Companies.Get(companyId).AmazonFeedMerchantIdentifier;
                foreach (var action in _cancelActions)
                {
                    var data  = JsonConvert.DeserializeObject <CancelOrderInput>(action.InputData);
                    var order = db.Orders.GetByOrderNumber(data.OrderNumber);
                    if (order == null)
                    {
                        Log.Info(marketplaceId + ": Can't find orderId=" + data.OrderNumber);
                        if (action.CreateDate < Time.GetAppNowTime().AddHours(-21)) //NOTE: Take care about 2 hours
                        {
                            var output = new CancelOrderOutput()
                            {
                                IsProcessed   = false,
                                ResultMessage = "Mark as cancelled w/o order by timeout"
                            };
                            _actionService.SetResult(db, action.Id, SystemActionStatus.Done, output);
                            db.Commit();

                            Log.Info("Mark as cancelled w/o order by timeout");
                        }
                        continue;
                    }

                    if (order.Market != (int)market ||
                        (!String.IsNullOrEmpty(marketplaceId) &&
                         order.MarketplaceId != marketplaceId))
                    {
                        //Log.Info("skip order=" + data.OrderNumber + ", market=" + order.Market + ", marketplace=" + order.MarketplaceId);
                        continue;
                    }

                    Log.Info(marketplaceId + ": add order " + index + ", actionId=" + action.Id + ", orderId=" + data.OrderNumber);

                    if (order.SourceOrderStatus == OrderStatusEnumEx.Canceled)
                    {
                        var output = new CancelOrderOutput()
                        {
                            IsProcessed   = false,
                            ResultMessage = "Already cancelled"
                        };
                        _actionService.SetResult(db, action.Id, SystemActionStatus.Done, output);
                        db.Commit();
                        continue;
                    }

                    index++;

                    var items = db.OrderItems.GetByOrderIdAsDto(data.OrderNumber)
                                //Remove canceled items with 0 price
                                .Where(m => m.ItemPrice > 0 || m.QuantityOrdered > 0).ToList();

                    OrderHelper.PrepareSourceItemOrderId(items);
                    items = OrderHelper.GroupBySourceItemOrderId(items);

                    orderMessages.Add(FeedHelper.ComposeOrderAcknowledgementMessage(index,
                                                                                    data.OrderNumber,
                                                                                    items));

                    _actionOutputList.Add(new KeyValuePair <long, CancelOrderOutput>(action.Id, new CancelOrderOutput()
                    {
                        Identifier = index
                    }));
                }

                if (orderMessages.Any())
                {
                    Log.Info(marketplaceId + ": Compose feed");
                    var document = FeedHelper.ComposeFeed(orderMessages, merchant, Type.ToString());
                    return(new DocumentInfo
                    {
                        XmlDocument = document,
                        NodesCount = index
                    });
                }
            }
            return(null);
        }