/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public async void Handle(AmazonGetOrdersDetails3PartyCommand command) { IDictionary <string, IList <AmazonOrderItemDetail> > orderDetailsByOrderId = await GetOrdersDetailsByOrderId(command); InfoAccumulator info = new InfoAccumulator(); SendReply(info, command, resp => resp.OrderDetailsByOrderId = orderDetailsByOrderId); }
/// <summary> /// Creates the next request. /// </summary> /// <param name="command">The command.</param> /// <param name="nextToken">The next token.</param> /// <returns></returns> private ListOrderItemsByNextTokenRequest CreateNextRequest(AmazonGetOrdersDetails3PartyCommand command, string nextToken) { ListOrderItemsByNextTokenRequest nextRequest = new ListOrderItemsByNextTokenRequest { SellerId = command.SellerId, MWSAuthToken = command.AuthorizationToken, NextToken = nextToken }; return(nextRequest); }
/// <summary> /// Creates the requests. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> private IEnumerable <ListOrderItemsRequest> CreateRequests(AmazonGetOrdersDetails3PartyCommand command) { foreach (string orderId in command.OrdersIds) { yield return(new ListOrderItemsRequest { SellerId = command.SellerId, MWSAuthToken = command.AuthorizationToken, AmazonOrderId = orderId }); } }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public async void Handle(AmazonRegisterCustomerCommand command) { InfoAccumulator info = new InfoAccumulator(); AmazonGetOrders3dPartyCommand request = new AmazonGetOrders3dPartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, MarketplaceId = command.MarketplaceId, DateFrom = DateTime.UtcNow.AddYears(-1) //TODO }; var response = await GetOrdersSendReceive.SendAsync(Config.Address, request); int customerId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid request"); SendReply(info, command, resp => resp.CustomerId = command.CustomerId); return; } int customerMarketPlaceId = 0; //TODO int marketPlaceHistoryId = 0; //TODO AmazonOrder order = new AmazonOrder { Created = DateTime.UtcNow, CustomerMarketPlaceId = customerMarketPlaceId, CustomerMarketPlaceUpdatingHistoryRecordId = marketPlaceHistoryId }; int orderId = (int)AmazonOrdersQueries.SaveOrder(order); if (orderId < 1) { throw new Exception("could not save amazon order"); } bool res = AmazonOrdersQueries.SaveOrdersPayments(response.OrderPayments, orderId); if (!res) { throw new Exception("could not save order/order's payments"); } //Get top N order items IEnumerable <AmazonOrderItem> items = AmazonOrdersQueries.GetTopNOrderItems(customerMarketPlaceId); //Get order details of top N items AmazonGetOrdersDetails3PartyCommand detailsRequestCommand = new AmazonGetOrdersDetails3PartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, OrdersIds = items.Where(o => o.AmazonOrderId.HasValue) .Select(o => o.AmazonOrderId.ToString()) }; AmazonGetOrdersDetails3PartyCommandResponse detailsResponse = await GetOrderDetailsSendReceive.SendAsync(Config.Address, detailsRequestCommand); var skuCollection = detailsResponse.OrderDetailsByOrderId.Values .SelectMany(o => o) .Select(o => o.SellerSKU); //Get categories of top N order items var getProductCategoriesCommand = new AmazonGetProductCategories3dPartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, SellerSKUs = skuCollection }; var productCategoriesResponse = await GetProductCategoriesSendReceive.SendAsync(Config.Address, getProductCategoriesCommand); IDictionary <string, IList <AmazonOrderItemDetail> > orderIdToOrderDetails = detailsResponse.OrderDetailsByOrderId; //save order item details var insertedOrderDetails = AmazonOrdersQueries.OrderDetails.SaveOrderDetails(orderIdToOrderDetails.Values.SelectMany(o => o)); IDictionary <string, IEnumerable <AmazonProductCategory> > skuToCategory = productCategoriesResponse.CategoriesBySku; //upserts categories IDictionary <AmazonProductCategory, int> categoryToId = AmazonOrdersQueries.Categories.UpsertCategories(productCategoriesResponse.CategoriesBySku.Values.SelectMany(o => o)); res = AmazonOrdersQueries.Categories.SaveCategoryOrderDetailsMapping(CreateOrderIdToOrderDetailsIdMappings(insertedOrderDetails, skuToCategory, categoryToId)); if (!res) { throw new Exception("could not save category order details mapping"); } }
/// <summary> /// Gets the orders details by order id. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> private async Task <IDictionary <string, IList <AmazonOrderItemDetail> > > GetOrdersDetailsByOrderId(AmazonGetOrdersDetails3PartyCommand command) { Dictionary <string, IList <AmazonOrderItemDetail> > ordersDetails = new Dictionary <string, IList <AmazonOrderItemDetail> >(); foreach (var request in CreateRequests(command)) { //first request var response = await AmazonService.Orders.ListOrderItems(request); List <AmazonOrderItemDetail> currentOrderDetails = new List <AmazonOrderItemDetail>(); if (response.IsSetListOrderItemsResult()) { //store parsed response data var orderDetails = ParseOrderDetailsResponse(response.ListOrderItemsResult.OrderItems); currentOrderDetails.AddRange(orderDetails); } //tries to get next details requests (for the same order) string nextToken = response.ListOrderItemsResult.NextToken; while (!string.IsNullOrEmpty(nextToken)) { var nextRequest = CreateNextRequest(command, nextToken); //get next response ListOrderItemsByNextTokenResponse nextResponse = await AmazonService.Orders.ListOrderItemsByNextToken(nextRequest); if (nextResponse.IsSetListOrderItemsByNextTokenResult()) { //store parsed response data currentOrderDetails.AddRange(ParseOrderDetailsResponse(nextResponse.ListOrderItemsByNextTokenResult.OrderItems)); nextToken = nextResponse.ListOrderItemsByNextTokenResult.NextToken; } else { nextToken = null; } } //store all order details by AmazonOrderId ordersDetails.Add(request.AmazonOrderId, currentOrderDetails); } return(ordersDetails); }