public void CancelOrderThrowIfNoOutput() { CancelOrderOutput mockOutput = null; var mockResponse = CreateJobResponse(mockOutput); var client = new MyRpaClient(GetMockOrchestratorClient(mockResponse), _validSetting); Assert.Throws <ArgumentNullException>(() => client.CancelOrderAsync(new CancelOrderInput()).GetAwaiter().GetResult()); }
public void CancelOrderSuccess() { var mockOutput = new CancelOrderOutput() { ReturnLabelLocation = "randome locaion", }; var mockResponse = CreateJobResponse(mockOutput); var client = new MyRpaClient(GetMockOrchestratorClient(mockResponse), _validSetting); var output = client.CancelOrderAsync(new CancelOrderInput()).GetAwaiter().GetResult(); Assert.Equal(mockOutput.ReturnLabelLocation, output.ReturnLabelLocation); }
public void ProcessCancellations(IUnitOfWork db, string filterTag) { //NOTE: reprocess cancel request every 2 hours var maxLastAttemptDate = _time.GetUtcTime().AddHours(-2); var cancelActions = _actionService.GetUnprocessedByType(db, SystemActionType.UpdateOnMarketCancelOrder, maxLastAttemptDate, null); var actionOutputList = new Dictionary <long, CancelOrderOutput>(); if (!String.IsNullOrEmpty(filterTag)) { cancelActions = cancelActions.Where(a => a.Tag == filterTag).ToList(); } foreach (var action in cancelActions) { try { var data = JsonConvert.DeserializeObject <CancelOrderInput>(action.InputData); var orders = db.Orders.GetAllByCustomerOrderNumber(data.OrderNumber); if (!orders.Any()) { _log.Info(_api.Market + "_" + _api.MarketplaceId + ": Can't find orderId=" + data.OrderNumber); continue; } if (orders[0].Market != (int)_api.Market || (!String.IsNullOrEmpty(_api.MarketplaceId) && orders[0].MarketplaceId != _api.MarketplaceId)) { //_log.Info("skip order=" + data.OrderNumber + ", market=" + order.Market + ", marketplace=" + order.MarketplaceId); continue; } _log.Info("Order to cancel: " + _api.Market + "_" + _api.MarketplaceId + ", actionId=" + action.Id + ", orderId=" + data.OrderNumber + ", itemId=" + data.ItemId); var itemIdList = (data.ItemId ?? "").Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var orderIdList = orders.Select(o => o.Id).ToList(); var allOrderItems = db.OrderItems.GetWithListingInfo().Where(i => i.OrderId.HasValue && orderIdList.Contains(i.OrderId.Value)).ToList(); var orderItemToCancel = itemIdList.Any() ? allOrderItems.Where(i => itemIdList.Contains(i.SourceMarketId)).ToList() : allOrderItems; CallResult <DTOOrder> result; if (orderItemToCancel.Count >= 1) { var orderToCancel = orders.FirstOrDefault(o => o.Id == orderItemToCancel[0].OrderId); result = _api.CancelOrder(orderToCancel.MarketOrderId, orderItemToCancel); } else { result = CallResult <DTOOrder> .Fail("No items to cancel", null); } if (result.IsSuccess) { var output = new CancelOrderOutput() { IsProcessed = true, ResultMessage = "Cancelled by API" }; _actionService.SetResult(db, action.Id, SystemActionStatus.Done, output); db.Commit(); } else { var output = new CancelOrderOutput() { IsProcessed = true, ResultMessage = "Fail: " + result.Message }; _actionService.SetResult(db, action.Id, SystemActionStatus.None, output); } } catch (Exception ex) { if (action.AttemptNumber > 10) { var output = new CancelOrderOutput() { IsProcessed = true, ResultMessage = ex.Message }; _actionService.SetResult(db, action.Id, SystemActionStatus.Fail, output); db.Commit(); } _log.Error("OrderCancellation.Process", ex); } } }
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); }
public void ProcessCancellations(IUnitOfWork db, string tag) { //NOTE: reprocess cancel request every 2 hours var maxLastAttemptDate = _time.GetUtcTime().AddHours(-2); var cancelActions = _actionService.GetUnprocessedByType(db, SystemActionType.UpdateOnMarketCancelOrder, maxLastAttemptDate, tag); var actionOutputList = new Dictionary <long, CancelOrderOutput>(); foreach (var action in cancelActions) { try { var data = JsonConvert.DeserializeObject <CancelOrderInput>(action.InputData); var orders = db.Orders.GetAllByCustomerOrderNumber(data.OrderNumber); if (!orders.Any()) { _log.Info(_api.Market + "_" + _api.MarketplaceId + ": Can't find orderId=" + data.OrderNumber); continue; } if (orders[0].Market != (int)_api.Market || (!String.IsNullOrEmpty(_api.MarketplaceId) && orders[0].MarketplaceId != _api.MarketplaceId)) { //_log.Info("skip order=" + data.OrderNumber + ", market=" + order.Market + ", marketplace=" + order.MarketplaceId); continue; } _log.Info("Order to cancel: " + _api.Market + "_" + _api.MarketplaceId + ", actionId=" + action.Id + ", orderId=" + data.OrderNumber + ", itemId=" + data.ItemId); foreach (var order in orders) { order.OrderStatus = OrderStatusEnumEx.Canceled; } db.Commit(); var output = new CancelOrderOutput() { IsProcessed = true, ResultMessage = "Cancelled by API" }; _actionService.SetResult(db, action.Id, SystemActionStatus.Done, output); db.Commit(); } catch (Exception ex) { if (action.AttemptNumber > 10) { var output = new CancelOrderOutput() { IsProcessed = true, ResultMessage = ex.Message }; _actionService.SetResult(db, action.Id, SystemActionStatus.Fail, output); db.Commit(); } _log.Error("WalmartOrderCancellation.Process", ex); } } }