Files
bnhtrade/src/bnhtrade.Core/Data/Amazon/FulFillmentInbound/GetShipments.cs
T
2024-04-11 12:26:13 +01:00

111 lines
4.6 KiB
C#

using bnhtrade.Core.Data.Amazon.SellingPartnerAPI;
using FikaAmazonAPI;
using FikaAmazonAPI.AmazonSpApiSDK.Models.FulfillmentInbound;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Data.Amazon.FulFillmentInbound
{
public class GetShipments
{
private AmazonConnection amznConn = new SpApiConnection().Connection;
public GetShipments()
{
}
public List<Model.AmazonFba.ShipmentInfo> GetByDateRange(DateTime utcDateTimeAfter, DateTime utcDateTimeBefore)
{
// At least one of the ShipmentStatusList and ShipmentIdList must be provided
var result = new List<Model.AmazonFba.ShipmentInfo>();
// build the request
var parameter = new FikaAmazonAPI.Parameter.FulFillmentInbound.ParameterGetShipments();
parameter.MarketplaceId = amznConn.GetCurrentMarketplace.ID;
parameter.QueryType = FikaAmazonAPI.Utils.Constants.QueryType.DATE_RANGE;
parameter.LastUpdatedAfter = utcDateTimeAfter;
parameter.LastUpdatedBefore = utcDateTimeBefore;
AddShipmentStatusList(ref parameter);
var apiResult = amznConn.FulFillmentInbound.GetShipments(parameter);
AppendNextToken(ref apiResult);
return TransposeModel(apiResult);
}
public List<Model.AmazonFba.ShipmentInfo> GetByShipmentIdList(List<string> shipmentIdList)
{
var result = new List<Model.AmazonFba.ShipmentInfo>();
// build the request
var parameter = new FikaAmazonAPI.Parameter.FulFillmentInbound.ParameterGetShipments();
parameter.MarketplaceId = amznConn.GetCurrentMarketplace.ID;
parameter.QueryType = FikaAmazonAPI.Utils.Constants.QueryType.SHIPMENT;
parameter.ShipmentIdList = shipmentIdList;
var apiResult = amznConn.FulFillmentInbound.GetShipments(parameter);
AppendNextToken(ref apiResult);
return TransposeModel(apiResult);
}
private void AddShipmentStatusList(ref FikaAmazonAPI.Parameter.FulFillmentInbound.ParameterGetShipments parameters)
{
var list = new List<ShipmentStatus>();
list.Add(ShipmentStatus.CANCELLED);
list.Add(ShipmentStatus.CHECKED_IN);
list.Add(ShipmentStatus.CLOSED);
list.Add(ShipmentStatus.DELETED);
list.Add(ShipmentStatus.DELIVERED);
list.Add(ShipmentStatus.ERROR);
list.Add(ShipmentStatus.IN_TRANSIT);
list.Add(ShipmentStatus.READY_TO_SHIP);
list.Add(ShipmentStatus.RECEIVING);
list.Add(ShipmentStatus.SHIPPED);
list.Add(ShipmentStatus.WORKING);
parameters.ShipmentStatusList = list;
}
// checks for next-token, if true appends additional data to the ShipmentData
private void AppendNextToken(ref FikaAmazonAPI.AmazonSpApiSDK.Models.FulfillmentInbound.GetShipmentsResult shipmentList)
{
while (!string.IsNullOrEmpty(shipmentList.NextToken))
{
// build the request
var parameter = new FikaAmazonAPI.Parameter.FulFillmentInbound.ParameterGetShipments();
parameter.MarketplaceId = amznConn.GetCurrentMarketplace.ID;
parameter.QueryType = FikaAmazonAPI.Utils.Constants.QueryType.NEXT_TOKEN;
parameter.NextToken = shipmentList.NextToken;
var result = amznConn.FulFillmentInbound.GetShipments(parameter);
shipmentList.ShipmentData.AddRange(result.ShipmentData);
shipmentList.NextToken = result.NextToken;
}
}
// transposes return api object data to my model
private List<Model.AmazonFba.ShipmentInfo> TransposeModel(FikaAmazonAPI.AmazonSpApiSDK.Models.FulfillmentInbound.GetShipmentsResult shipmentResult)
{
var returnList = new List<Model.AmazonFba.ShipmentInfo>();
for (var i = 0; i < shipmentResult.ShipmentData.Count; i++)
{
var item = new Model.AmazonFba.ShipmentInfo();
item.FbaShipmentId = shipmentResult.ShipmentData[i].ShipmentId;
item.DestinationFulfillmentCenterId = shipmentResult.ShipmentData[i].DestinationFulfillmentCenterId;
item.ShipmentName = shipmentResult.ShipmentData[i].ShipmentName;
item.ShipmentStatus = shipmentResult.ShipmentData[i].ShipmentStatus.ToString();
returnList.Add(item);
}
return returnList;
}
}
}