public async Task <IPagedResponse <OrderInfo> > GetOrdersAsync(SearchOrdersRequest request) { Logger?.LogDebug("'{0}' has been invoked", nameof(GetOrdersAsync)); var response = new PagedResponse <OrderInfo>(); try { // Get query var query = DbContext .GetOrders(request.OrderStatusID, request.CustomerID, request.EmployeeID, request.ShipperID, request.CurrencyID, request.PaymentMethodID); // Set information for paging response.PageSize = (int)request.PageSize; response.PageNumber = (int)request.PageNumber; response.ItemsCount = await query.CountAsync(); // Retrieve items, set model for response response.Model = await query .Paging((int)request.PageSize, (int)request.PageNumber) .ToListAsync(); response.Message = string.Format("Page {0} of {1}, Total of rows: {2}", response.PageNumber, response.PageCount, response.ItemsCount); Logger?.LogInformation(response.Message); } catch (Exception ex) { response.SetError(Logger, nameof(GetOrdersAsync), ex); } return(response); }
private async Task <SearchOrdersResponse> SearchOrdersAsync(SearchOrdersRequest requestBody, CancellationToken token, Mark mark) { if (token.IsCancellationRequested) { var exceptionDetails = CreateMethodCallInfo(SquareEndPoint.OrdersSearchUrl, mark, additionalInfo: this.AdditionalLogInfo()); var squareException = new SquareException(string.Format("{0}. Search orders request was cancelled", exceptionDetails)); SquareLogger.LogTraceException(squareException); throw squareException; } var response = await base.ThrottleRequest(SquareEndPoint.SearchCatalogUrl, requestBody.ToJson(), mark, (_) => { return(_ordersApi.SearchOrdersAsync(requestBody)); }, token).ConfigureAwait(false); var errors = response.Errors; if (errors != null && errors.Any()) { var methodCallInfo = CreateMethodCallInfo(SquareEndPoint.OrdersSearchUrl, mark, additionalInfo: this.AdditionalLogInfo(), errors: errors.ToJson(), payload: requestBody.ToJson()); var squareException = new SquareException(string.Format("{0}. Search orders returned errors", methodCallInfo)); SquareLogger.LogTraceException(squareException); throw squareException; } return(response); }
public async Task <IActionResult> SearchOrdersAsync([FromBody] SearchOrdersRequest request) { Logger?.LogDebug("{0} has been invoked", nameof(SearchOrdersAsync)); // Get response from business logic var response = await SalesService.GetOrdersAsync(request); // Return as http response return(response.ToHttpResponse()); }
public async Task TestSearchOrdersAsync() { // Arrange var controller = ControllerMocker.GetSalesController(nameof(TestSearchOrdersAsync)); var request = new SearchOrdersRequest(); // Act var response = await controller.SearchOrdersAsync(request) as ObjectResult; var value = response.Value as IPagedResponse <OrderInfo>; // Assert Assert.False(value.DidError); }
public async Task TestSearchOrdersByEmployeeAsync() { // Arrange var controller = ControllerMocker.GetSalesController(nameof(TestSearchOrdersByEmployeeAsync)); var request = new SearchOrdersRequest { EmployeeID = 1 }; // Act var response = await controller.SearchOrdersAsync(request) as ObjectResult; var value = response.Value as IPagedResponse <OrderInfo>; // Assert Assert.False(value.DidError); Assert.True(value.Model.Count(item => item.EmployeeID == request.EmployeeID) == value.Model.Count()); }
public static SearchOrdersRequest CreateSearchOrdersBody(DateTime startDateUtc, DateTime endDateUtc, IEnumerable <SquareLocation> locations, string cursor, int ordersPerPage) { var updatedAtStart = startDateUtc.FromUtcToRFC3339(); var updatedAtEnd = endDateUtc.FromUtcToRFC3339(); var body = new SearchOrdersRequest { ReturnEntries = false, Limit = ordersPerPage, LocationIds = locations.Select(l => l.Id).ToList(), Query = new SearchOrdersQuery { Filter = new SearchOrdersFilter { DateTimeFilter = new SearchOrdersDateTimeFilter { UpdatedAt = new TimeRange { StartAt = updatedAtStart, EndAt = updatedAtEnd } }, StateFilter = new SearchOrdersStateFilter( new List <string> { SquareOrderState.Completed, SquareOrderState.Open, SquareOrderState.Cancelled } ) }, Sort = new SearchOrdersSort("UPDATED_AT", "DESC") } }; if (!string.IsNullOrWhiteSpace(cursor)) { body.Cursor = cursor; } return(body); }
public void GetOrdersByLocation() { var body = new SearchOrdersRequest { ReturnEntries = false, Limit = 10, LocationIds = new List <string> { locationId }, Query = new SearchOrdersQuery { Filter = new SearchOrdersFilter { DateTimeFilter = new SearchOrdersDateTimeFilter { UpdatedAt = new TimeRange { StartAt = "2010-03-03T20:00:00Z", EndAt = "2040-03-03T20:00:00Z" } }, StateFilter = new SearchOrdersStateFilter( new List <string> { "COMPLETED", "OPEN", "CANCELED" } ) }, Sort = new SearchOrdersSort("UPDATED_AT", "DESC") } }; var response = instance.SearchOrdersAsync(body).Result; Assert.IsTrue(response.Errors == null || response.Errors.Count == 0); Assert.AreNotEqual(0, response.Orders.Count); }
public IEnumerable<object> GetPendingOrders([FromBody]SearchOrdersRequest request) { return new[] { new { OrderId = "3", Type = "Delivery", RequestDateTimed = "05/10/18 18:15", CustomerAddress = "23/227 Elizabeth street, Sydney CBD 2000", Total = 43.17 }, new { OrderId = "4", Type = "Delivery", RequestDateTimed = "05/10/18 18:15", CustomerAddress = "23/227 Elizabeth street, Sydney CBD 2000", Total = 43.17 } } .ToList(); }
private async Task <SquareOrdersBatch> GetOrdersWithRelatedDataAsync(SearchOrdersRequest requestBody, CancellationToken token, Mark mark) { var result = await SearchOrdersAsync(requestBody, token, mark).ConfigureAwait(false); if (result != null) { var orders = result.Orders; var cursor = result.Cursor; var ordersWithRelatedData = new List <SquareOrder>(); if (orders != null && orders.Any()) { foreach (var order in orders) { var catalogObjectsIds = (order.LineItems == null) ? new List <string>() : order.LineItems.Where(l => l != null && !string.IsNullOrWhiteSpace(l.CatalogObjectId)).Select(l => l.CatalogObjectId); var catalogObjects = await _itemsService.GetCatalogObjectsByIdsAsync(catalogObjectsIds, token, mark).ConfigureAwait(false); ordersWithRelatedData.Add(order.ToSvOrder(catalogObjects)); } return(new SquareOrdersBatch { Orders = ordersWithRelatedData, Cursor = cursor }); } } return(new SquareOrdersBatch { Orders = new List <SquareOrder>(), Cursor = null }); }
public virtual IHttpActionResult SearchOrders(int start, int maxCount, [FromUri(Name = "")] SearchOrdersRequest request) { Logger.LogGet("GetOrders", Request, new[] { start.ToString(), maxCount.ToString(), $"{request?.OrderShipmentStatus}", $"{request?.ShippingMethodId}" }); if (maxCount < 1 || maxCount > 100) { maxCount = 10; } try { var searchOptions = new OrderSearchOptions { CacheResults = false, StartingRecord = start, RecordsToRetrieve = maxCount, Namespace = "Mediachase.Commerce.Orders" }; var parameters = new OrderSearchParameters(); searchOptions.Classes.Add("PurchaseOrder"); parameters.SqlMetaWhereClause = string.Empty; if (request?.ModifiedFrom.HasValue ?? false) { parameters.SqlMetaWhereClause = $"META.Modified >= '{request.ModifiedFrom.Value:s}'"; } if (request?.OrderShipmentStatus != null && request.ShippingMethodId != null && request.ShippingMethodId != Guid.Empty) { parameters.SqlWhereClause = $"[OrderGroupId] IN (SELECT [OrderGroupId] FROM [Shipment] WHERE [Status] = '{request.OrderShipmentStatus}' AND [ShippingMethodId] = '{request.ShippingMethodId}')"; } else if (request?.OrderShipmentStatus != null) { parameters.SqlWhereClause = $"[OrderGroupId] IN (SELECT [OrderGroupId] FROM [Shipment] WHERE [Status] = '{request.OrderShipmentStatus}')"; } else if (request?.ShippingMethodId != null && request.ShippingMethodId != Guid.Empty) { parameters.SqlWhereClause = $"[OrderGroupId] IN (SELECT [OrderGroupId] FROM [Shipment] WHERE [ShippingMethodId] = '{request.ShippingMethodId}')"; } if (request != null && request.Status?.Length > 0) { if (!string.IsNullOrEmpty(parameters.SqlWhereClause)) { parameters.SqlWhereClause += " AND "; } var statusesParam = string.Join(",", request.Status.Select(x => $"'{x}'")); parameters.SqlWhereClause += $"Status IN ({statusesParam})"; } var orders = OrderContext.Current.FindPurchaseOrders(parameters, searchOptions); return(Ok(orders.Select(x => x.ConvertToPurchaseOrder()).ToArray())); } catch (Exception exception) { Logger.Error(exception.Message, exception); return(InternalServerError(exception)); } }