Changed a lot LOL
This commit is contained in:
@@ -19,6 +19,7 @@ using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using Ratermania.Shopify.Exceptions;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
@@ -40,29 +41,50 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
public async Task<AutomationJobResult> Run()
|
||||
{
|
||||
IList<ImportData> parts = _partSourceContext.ImportData
|
||||
.Where(p => p.UpdatedAt < DateTime.Now.AddDays(-3))
|
||||
.Take(50)
|
||||
.ToList();
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
while (parts != null && parts.Count > 0)
|
||||
try
|
||||
{
|
||||
foreach (ImportData importData in parts)
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO: Logging
|
||||
return new AutomationJobResult
|
||||
{
|
||||
Message = "Failed to get products from Shopify",
|
||||
IsSuccess = false
|
||||
};
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
try
|
||||
{
|
||||
Product product = await _shopifyClient.Products.GetById(importData.ShopifyId.GetValueOrDefault());
|
||||
if (product == null)
|
||||
ImportData importData = await _partSourceContext.ImportData
|
||||
.Where(i => i.ShopifyId == product.Id && i.UpdatedAt <= DateTime.Now.AddDays(-7))
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (importData == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
bool isFitment = false;
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode, 255);
|
||||
|
||||
if (vehicles.Count > 250)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
|
||||
if (vehicleIdFitment.Count > 0 && vehicleIdFitment.Count <= 250)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
@@ -121,11 +143,25 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
|
||||
//Metafield noteTextMetafield = new Metafield
|
||||
//{
|
||||
// Namespace = "Flags",
|
||||
// Key = "IsFitment",
|
||||
// Value = isFitment.ToString(),
|
||||
// ValueType = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
|
||||
//await _shopifyClient.Metafields.Add(noteTextMetafield);
|
||||
|
||||
|
||||
List<string> tags = new List<string>
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber
|
||||
};
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber
|
||||
};
|
||||
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
@@ -143,25 +179,32 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
|
||||
catch (ShopifyClientException ex)
|
||||
{
|
||||
// TODO: Log
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!ex.Message.Contains("response content", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
Console.WriteLine($"{importData.VariantSku}: {ex.Message}");
|
||||
}
|
||||
// TODO: Log
|
||||
}
|
||||
}
|
||||
|
||||
await _partSourceContext.SaveChangesAsync();
|
||||
try
|
||||
{
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
parts = _partSourceContext.ImportData
|
||||
.Where(p => p.UpdatedAt < DateTime.Now.AddDays(-3))
|
||||
.Take(50)
|
||||
.ToList();
|
||||
catch (Exception ex)
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user