private async Task UpsertOrderAndShipments(string orderID)
        {
            var orderWorksheet = await _oc.IntegrationEvents.GetWorksheetAsync <HSOrderWorksheet>(OrderDirection.Incoming, orderID);

            var shipments = await _oc.Shipments.ListAllAsync(orderID);

            foreach (var shipment in shipments)
            {
                var shipmentItems = await _oc.Shipments.ListAllItemsAsync(shipment.ID);

                foreach (var shipmentItem in shipmentItems)
                {
                    var cosmosOrderWithShipments = OrderWithShipmentsMapper.Map(orderWorksheet, shipment, shipmentItem);

                    if (cosmosOrderWithShipments.QuantityShipped != 0)
                    {
                        var queryable = _ordersAndShipmentsDataRepo.GetQueryable().Where(orderWithShipments => orderWithShipments.PartitionKey == "PartitionValue");

                        var requestOptions = BuildQueryRequestOptions();

                        var listOptions = BuildOrderWithShipmentsListOptions(orderID, shipmentItem.LineItemID, shipment.ID);

                        CosmosListPage <OrderWithShipments> currentOrderWithShipmentsListPage = await _ordersAndShipmentsDataRepo.GetItemsAsync(queryable, requestOptions, listOptions);

                        var cosmosID = "";
                        if (currentOrderWithShipmentsListPage.Items.Count() == 1)
                        {
                            cosmosID = cosmosOrderWithShipments.id = currentOrderWithShipmentsListPage.Items[0].id;
                        }

                        await _ordersAndShipmentsDataRepo.UpsertItemAsync(cosmosID, cosmosOrderWithShipments);
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task <List <OrderWithShipments> > ShipmentDetail(string templateID, ListArgs <ReportAdHocFilters> args, DecodedToken decodedToken)
        {
            IList <ListFilter> filters = await BuildFilters(templateID, args, decodedToken, "DateSubmitted", "SupplierID");

            CosmosListOptions listOptions = new CosmosListOptions()
            {
                PageSize      = -1,
                Sort          = "OrderID",
                SortDirection = SortDirection.ASC,
                Filters       = filters,
            };

            IQueryable <OrderWithShipments> queryable = _ordersAndShipments.GetQueryable()
                                                        .Where(order =>
                                                               order.PartitionKey == "PartitionValue");

            QueryRequestOptions requestOptions = new QueryRequestOptions
            {
                MaxItemCount   = listOptions.PageSize,
                MaxConcurrency = -1
            };

            CosmosListPage <OrderWithShipments> ordersWithShipmentsDataResponse = await _ordersAndShipments.GetItemsAsync(queryable, requestOptions, listOptions);

            List <OrderWithShipments> orderWithShipmentsData = ordersWithShipmentsDataResponse.Items;

            listOptions.ContinuationToken = ordersWithShipmentsDataResponse.Meta.ContinuationToken;

            while (listOptions.ContinuationToken != null)
            {
                CosmosListPage <OrderWithShipments> responseWithToken = await _ordersAndShipments.GetItemsAsync(queryable, requestOptions, listOptions);

                orderWithShipmentsData.AddRange(responseWithToken.Items);
                listOptions.ContinuationToken = responseWithToken.Meta.ContinuationToken;
            }

            var ordersWithShipments = new List <OrderWithShipments>();

            var supplierFilter = args.Filters.FirstOrDefault(filter => filter.PropertyName == "SupplierID");

            var me = await _oc.Me.GetAsync(decodedToken.AccessToken);

            foreach (OrderWithShipments detailData in orderWithShipmentsData)
            {
                if (supplierFilter == null || supplierFilter.FilterExpression == detailData.SupplierID)
                {
                    if (decodedToken.CommerceRole == CommerceRole.Seller || me.Supplier.ID == detailData.SupplierID)
                    {
                        ordersWithShipments.Add(detailData);
                    }
                }
            }

            return(ordersWithShipments);
        }