private async Task <BatchProcessResult> ProcessShipments(DocumentImportResult importResult, VerifiedUserContext userContext) { BatchProcessResult processResult = new BatchProcessResult(); if (importResult == null) { return(null); } if (importResult.Valid?.Count < 0) { return(null); } foreach (Misc.Shipment shipment in importResult.Valid) { try { bool isSuccessful = await ProcessShipment(shipment, processResult, userContext); } catch (Exception ex) { BatchProcessFailure failureDto = new BatchProcessFailure(); failureDto.Error = ex.Message; failureDto.Shipment = shipment; processResult.ProcessFailureList.Add(failureDto); } } processResult.InvalidRowFailureList.AddRange(importResult.Invalid); processResult.Meta = new BatchProcessSummary() { ProcessFailureListCount = processResult.ProcessFailureList.Count(), DocumentFailureListCount = processResult.InvalidRowFailureList.Count(), SuccessfulCount = processResult.SuccessfulList.Count(), TotalCount = importResult.Meta.TotalCount }; return(processResult); }
private async Task <bool> ProcessShipment(Misc.Shipment shipment, BatchProcessResult result, VerifiedUserContext userContext) { PartialShipment newShipment = null; Shipment ocShipment; try { if (shipment == null) { throw new Exception("Shipment cannot be null"); } Order ocOrder = await GetOutgoingOrder(shipment); LineItem lineItem = await GetOutgoingLineItem(shipment); //Don't continue if attempting to ship more items than what's in the order. ValidateShipmentAmount(shipment, lineItem, ocOrder); ShipmentItem newShipmentItem = new ShipmentItem() { OrderID = shipment.OrderID, LineItemID = lineItem.ID, QuantityShipped = Convert.ToInt32(shipment.QuantityShipped), UnitPrice = Convert.ToDecimal(shipment.Cost) }; ocShipment = await GetShipmentByTrackingNumber(shipment, userContext?.AccessToken); //If a user included a ShipmentID in the spreadsheet, find that shipment and patch it with the information on that row if (ocShipment != null) { newShipment = PatchShipment(ocShipment, shipment); } if (newShipment != null) { Shipment processedShipment = await _oc.Shipments.PatchAsync(newShipment.ID, newShipment, userContext?.AccessToken); //Before updating shipment item, must post the shipment line item comment to the order line item due to OC bug. await PatchPartialLineItemComment(shipment, newShipment.ID); await PatchLineItemStatus(shipment.OrderID, newShipmentItem, userContext); //POST a shipment item, passing it a Shipment ID parameter, and a request body of Order ID, Line Item ID, and Quantity Shipped await _oc.Shipments.SaveItemAsync(newShipment.ID, newShipmentItem, accessToken : userContext?.AccessToken); //Re-patch the shipment adding the date shipped now due to oc bug var repatchedShipment = PatchShipment(ocShipment, shipment); await _oc.Shipments.PatchAsync(newShipment.ID, repatchedShipment); result.SuccessfulList.Add(processedShipment); } if (lineItem?.ID == null) { //Before updating shipment item, must post the shipment line item comment to the order line item due to OC bug. await PatchPartialLineItemComment(shipment, newShipment.ID); //Create new lineItem await _oc.Shipments.SaveItemAsync(shipment.ShipmentID, newShipmentItem); } return(true); } catch (OrderCloudException ex) { result.ProcessFailureList.Add(CreateBatchProcessFailureItem(shipment, ex)); return(false); } catch (Exception ex) { result.ProcessFailureList.Add(new BatchProcessFailure() { Error = ex.Message, Shipment = shipment }); return(false); } }