public void CloseAmazonListing(AmazonSyncModel syncModel, AmazonListing amazonListing)
        {
            var feedContent = _amazonFeedsApiService.GetSingleProductDeleteFeed(amazonListing);

            var submissionId = _amazonRequestService.SubmitCloseRequest(syncModel, feedContent);

            _amazonRequestService.CheckIfDeleteRequestWasProcessed(syncModel, amazonListing, submissionId);
        }
        public void SubmitSingleProductFeed(AmazonSyncModel model, AmazonListing item)
        {
            var feeds = _amazonFeedsApiService.GetSingleProductMainFeeds(item);

            var submissionIds = _amazonRequestService.SubmitMainFeeds(model, feeds);

            _amazonRequestService.CheckIfRequestWasProcessed(model, item, submissionIds);
        }
        public void CheckIfRequestsWereProcessed(AmazonSyncModel model, AmazonListingGroup item, List<string> submissionIds)
        {
            var uploadSuccess = false;
            var retryCount = 0;

            var feedContent = _amazonFeedsApiService.GetProductsImageFeeds(item);
            while (!uploadSuccess)
            {
                retryCount++;
                if (retryCount == 5)
                {
                    AmazonProgressBarHelper.Update(model.Task, "Error",
                                                  "Request timed out. Please check logs for potential errors and try again later.", 100,
                                                  100);
                    break;
                }

                try
                {
                    AmazonProgressBarHelper.Update(model.Task, "Push", "Checking if request was processed...", 100, 75);
                    if (_amazonFeedsApiService.GetFeedSubmissionList(submissionIds.First()).FeedProcessingStatus ==
                        "_DONE_")
                    {
                        AmazonProgressBarHelper.Update(model.Task, "Push", "Request was processed", 100, 75);
                        foreach (var amazonListing in item.Items)
                        {
                            AmazonProgressBarHelper.Update(model.Task, "Push", "Updating local status of Amazon Listing with SKU:" + amazonListing.SellerSKU, 100, 85);
                            _amazonListingService.UpdateAmazonListingStatusAndAsin(amazonListing, null);
                        }

                        SubmitProductImageFeed(model, feedContent, ref submissionIds);

                        uploadSuccess = true;
                    }
                    else
                    {
                        AmazonProgressBarHelper.Update(model.Task, "Push",
                                                       "Nothing yet, we will wait 2 min. more and try again...", 100, 75);

                        Thread.Sleep(120000);
                    }
                }
                catch (Exception ex)
                {
                    CurrentRequestData.ErrorSignal.Raise(ex);

                    AmazonProgressBarHelper.Update(model.Task, "Push",
                                                   "Amazon Api is busy, we will wait additional 2 min. and try again...", 100,
                                                   75);

                    Thread.Sleep(120000);
                }
            }
        }
        public void CloseAmazonListings(AmazonSyncModel model, AmazonListingGroup amazonListingGroup)
        {
            var feedContent = _amazonFeedsApiService.GetProductsDeleteFeeds(amazonListingGroup);

            var submissionId = _amazonRequestService.SubmitCloseRequest(model, feedContent);

            var isUploaded = false;
            var retryCounter = 0;

            while (!isUploaded)
            {
                retryCounter++;
                retryCounter++;
                if (retryCounter == 3)
                {
                    AmazonProgressBarHelper.Update(model.Task, "Error",
                                                  "Request timed out. Please check logs for potential errors and try again later.", 100,
                                                  100);
                    break;
                }

                try
                {
                    AmazonProgressBarHelper.Update(model.Task, "Push", "Checking if request was processed...", 100, 75);
                    if (_amazonFeedsApiService.GetFeedSubmissionList(submissionId).FeedProcessingStatus == "_DONE_")
                    {
                        AmazonProgressBarHelper.Update(model.Task, "Push", "Request was processed", 100, 90);
                        foreach (var amazonListing in amazonListingGroup.Items)
                        {
                            AmazonProgressBarHelper.Update(model.Task, "Push", "Updating local status of Amazon Listing #"+amazonListing.SellerSKU, 100, 90);
                            _amazonListingService.UpdateAmazonListingStatus(amazonListing);
                        }

                        isUploaded = true;
                    }
                    else
                    {
                        AmazonProgressBarHelper.Update(model.Task, "Push", "Nothing yet, we will wait 2 min. more and try again...", 100, 75);
                        Thread.Sleep(120000);
                    }
                }
                catch (Exception ex)
                {
                    _amazonLogService.Add(AmazonLogType.Listings, AmazonLogStatus.Error, ex, null,
                                          AmazonApiSection.Feeds, null, null,null,null,"Closing Amazon Listings");

                    AmazonProgressBarHelper.Update(model.Task, "Push", "Amazon Api is busy, we will need to wait additional 2 min. and try again", 100, 75);
                    Thread.Sleep(120000);
                }
            }
        }
        public List<string> SubmitMainFeeds(AmazonSyncModel model, List<FileStream> feeds)
        {
            var submissionIds = new List<string>();
            var uploadSuccess = false;
            var retryCount = 0;
            while (!uploadSuccess)
            {
                retryCount++;
                if (retryCount == 3)
                {
                    AmazonProgressBarHelper.Update(model.Task, "Error",
                                                  "Request timed out. Please check logs for potential errors and try again later.", 100,
                                                  100);
                    break;
                }

                try
                {
                    if (feeds.Count >= 1)
                        SubmitProductFeed(model, feeds.First(), ref submissionIds);

                    if (feeds.Count >= 2)
                        SubmitProductPriceFeed(model, feeds[1], ref submissionIds);

                    if (feeds.Count >= 3)
                        SubmitProductInventoryFeed(model, feeds[2], ref submissionIds);

                    uploadSuccess = true;
                }
                catch (Exception ex)
                {
                    CurrentRequestData.ErrorSignal.Raise(ex);

                    Thread.Sleep(120000);
                }
            }
            return submissionIds;
        }
        public void SyncAmazonListings(AmazonSyncModel model)
        {
            AmazonProgressBarHelper.Clean(model.Task);

            var amazonListingGroup = _amazonListingGroupService.Get(model.Id);
            if (amazonListingGroup != null)
            {
                _amazonLogService.Add(AmazonLogType.Listings, AmazonLogStatus.Stage, null,null,AmazonApiSection.Feeds, null, null,null,
                                      amazonListingGroup, "Preparing listings for Amazon");
                AmazonProgressBarHelper.Update(model.Task, "Push", "Preparing listings for Amazon", 100, 0);

                _exportAmazonListingService.SubmitProductFeeds(model,amazonListingGroup);
               
                AmazonProgressBarHelper.Update(model.Task, "Push", "Amazon Listings successfully synced", 100, 100);
            }

            else
            {
                AmazonProgressBarHelper.Update(model.Task, "Push", "No listing to sync", null, null);
            }

            AmazonProgressBarHelper.Update(model.Task, "Completed", "Completed", 100, 100);
        }
        public void CloseAmazonListing(AmazonSyncModel model)
        {
            AmazonProgressBarHelper.Clean(model.Task);

            var amazonListing = _amazonListingService.Get(model.Id);
            if (amazonListing != null)
            {
                _amazonLogService.Add(AmazonLogType.Listings, AmazonLogStatus.Stage, null,null,AmazonApiSection.Feeds, null, null,amazonListing,
                                      null, "Preparing request to close Amazon Listing");
                AmazonProgressBarHelper.Update(model.Task, "Push", "Preparing request to close Amazon Listing", 100, 0);

                _closeAmazonListingService.CloseAmazonListing(model, amazonListing);

                AmazonProgressBarHelper.Update(model.Task, "Push", "Amazon Listing successfully closed",
                                               100, 100);
            }

            else
            {
                AmazonProgressBarHelper.Update(model.Task, "Push", "No listing to close", null, null);
            }

            AmazonProgressBarHelper.Update(model.Task, "Completed", "Completed", 100, 100);
        }
        public void ListingController_CloseManyPOST_ShouldCallSyncAmazonListings()
        {
            var model = new AmazonSyncModel();

            var result = _listingController.CloseMany_POST(model);

            A.CallTo(() => _amazonListingSyncManager.CloseAmazonListings(model)).MustHaveHappened();
        }
        public void ListingController_SyncOne_ShouldCallSyncAmazonListing()
        {
            var model = new AmazonSyncModel();

            var result = _listingController.SyncOne(model);

            A.CallTo(() => _amazonListingSyncManager.SyncAmazonListing(model)).MustHaveHappened();
        }
        private void SubmitProductImageFeed(AmazonSyncModel model, FileStream feedContent, ref List<string> submissionIds)
        {
            if (feedContent == null) return;

            AmazonProgressBarHelper.Update(model.Task, "Push", "Pushing product image", 100, 85);
            var feedResponse = _amazonFeedsApiService.SubmitFeed(AmazonFeedType._POST_PRODUCT_IMAGE_DATA_, feedContent);
            var submissionId = feedResponse.FeedSubmissionId;
            submissionIds.Add(submissionId);
            AmazonProgressBarHelper.Update(model.Task, "Push", "Product image pushed", 100, 100);
        }
        private void SubmitProductInventoryFeed(AmazonSyncModel model, FileStream feedContent, ref List<string> submissionIds)
        {
            if (feedContent == null) return;

            AmazonProgressBarHelper.Update(model.Task, "Push", "Pushing product inventory information", 100, 50);
            var feedResponse = _amazonFeedsApiService.SubmitFeed(AmazonFeedType._POST_INVENTORY_AVAILABILITY_DATA_, feedContent);
            var submissionId = feedResponse.FeedSubmissionId;
            submissionIds.Add(submissionId);
            AmazonProgressBarHelper.Update(model.Task, "Push", "Product inventory information pushed", 100, 75);
        }
        public void CheckIfDeleteRequestWasProcessed(AmazonSyncModel model, AmazonListing amazonListing, string submissionId)
        {
            var uploadSuccess = false;
            var retryCount = 0;

            while (!uploadSuccess)
            {
                retryCount++;
                if (retryCount == 3)
                {
                    AmazonProgressBarHelper.Update(model.Task, "Error",
                                                  "Request timed out. Please check logs for potential errors and try again later.", 100,
                                                  100);
                    break;
                }

                try
                {
                    AmazonProgressBarHelper.Update(model.Task, "Push", "Checking if request was processed...", 100, 75);
                    if (_amazonFeedsApiService.GetFeedSubmissionList(submissionId).FeedProcessingStatus == "_DONE_")
                    {
                        AmazonProgressBarHelper.Update(model.Task, "Push", "Request was processed", 100, 75);
                        AmazonProgressBarHelper.Update(model.Task, "Push",
                                                       "Updating local status of Amazon Listing with SKU:" + amazonListing.SellerSKU,
                                                       100, 85);
                        _amazonListingService.UpdateAmazonListingStatus(amazonListing);

                        uploadSuccess = true;
                    }
                    else
                    {
                        AmazonProgressBarHelper.Update(model.Task, "Push",
                                                       "Nothing yet, we will wait 2 min. more and try again...", 100, 75);
                        Thread.Sleep(120000);
                    }
                }
                catch (Exception ex)
                {
                    _amazonLogService.Add(AmazonLogType.Listings, AmazonLogStatus.Error, ex, null,
                                          AmazonApiSection.Feeds, null, null, amazonListing, null);

                    AmazonProgressBarHelper.Update(model.Task, "Push",
                                                   "Amazon Api is busy, we will need to wait additional 2 min. and try again",
                                                   100, 75);
                    Thread.Sleep(120000);
                }
            }
        }
        public string SubmitCloseRequest(AmazonSyncModel model, FileStream productFeedContent)
        {
            var submissionId = String.Empty;
            var uploadSuccess = false;
            var retryCount = 0;
            while (!uploadSuccess)
            {
                retryCount++;
                if (retryCount == 3)
                {
                    AmazonProgressBarHelper.Update(model.Task, "Error",
                                                  "Request timed out. Please check logs for potential errors and try again later.", 100,
                                                  100);
                    break;
                }

                try
                {
                    AmazonProgressBarHelper.Update(model.Task, "Push", "Pushing request to Amazon", 100, 0);
                    var feedResponse = _amazonFeedsApiService.SubmitFeed(AmazonFeedType._POST_PRODUCT_DATA_, productFeedContent);
                    submissionId = feedResponse.FeedSubmissionId;
                    AmazonProgressBarHelper.Update(model.Task, "Push", "Request pushed to Amazon", 100, 75);

                    uploadSuccess = true;
                }
                catch (Exception ex)
                {
                    _amazonLogService.Add(AmazonLogType.Listings, AmazonLogStatus.Error, ex, null,
                                          AmazonApiSection.Feeds, null, null, null, null, "Error during push of product delete request to Amazon");

                    Thread.Sleep(120000);
                }
            }
            return submissionId;
        }