State of OMG-LEGION prior to merge
This commit is contained in:
@@ -20,257 +20,227 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class UpdateFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitment> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
public class UpdateFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitment> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
|
||||
public UpdateFitment(ILogger<UpdateFitment> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
}
|
||||
public UpdateFitment(ILogger<UpdateFitment> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
IList<string> productTypes = new List<string>
|
||||
{
|
||||
"CA108-SC349-FL34907_CV Shafts, New"
|
||||
};
|
||||
public async Task Run()
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
foreach (string type in productTypes)
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA108-SC349-FL34907_CV Shafts, New" } });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
int i = 1;
|
||||
|
||||
int i = 1;
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
// Wiper blades are a separate fitment process.
|
||||
if (product.ProductType.Contains("CA172-SC231"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
ImportData importData = null;
|
||||
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
|
||||
//importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
|
||||
//importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
|
||||
|
||||
//if (importData == null)
|
||||
//{
|
||||
// continue;
|
||||
importData = new ImportData
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
|
||||
PartNumber = product.Title.Split(' ')[0],
|
||||
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
// }
|
||||
//if (importData == null)
|
||||
//{
|
||||
// continue;
|
||||
importData = new ImportData
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
|
||||
PartNumber = product.Title.Split(' ')[0],
|
||||
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
// }
|
||||
|
||||
//importData.PartNumber = product.Title.Split(' ')[0];
|
||||
bool isFitment = false;
|
||||
string bodyHtml = product.BodyHtml[..(product.BodyHtml.IndexOf("</ul>") + "</ul>".Length)];
|
||||
|
||||
bool isFitment = false;
|
||||
string bodyHtml = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
//if (vehicles.Count > 250)
|
||||
//{
|
||||
// vehicles = vehicles.Take(250)
|
||||
// .ToList();
|
||||
if (vehicleIdFitment.Any())
|
||||
{
|
||||
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
|
||||
// _logger.LogInformation($"SKU {importData.VariantSku} fits more than 250 vehicles. Only the first 250 will be used.");
|
||||
//}
|
||||
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
isFitment = true;
|
||||
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
isFitment = true;
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length < 100000)
|
||||
{
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Vehicle ID fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
stringBuilder.AppendLine("</table>");
|
||||
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine("</table>");
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length < 100000)
|
||||
{
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
Metafield lineCodeMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_0",
|
||||
Value = importData.LineCode,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
// await _shopifyClient.Metafields.Add(lineCodeMetafield);
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Year/make/model fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Metafield partNumberMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_1",
|
||||
Value = importData.PartNumber,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
//await _shopifyClient.Metafields.Add(partNumberMetafield);
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
List<string> tags = new List<string>();
|
||||
|
||||
Metafield lineCodeMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_0",
|
||||
Value = importData.LineCode,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
|
||||
//await _shopifyClient.Metafields.Add(lineCodeMetafield);
|
||||
tags.AddRange(ymmFitment);
|
||||
|
||||
Metafield partNumberMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_1",
|
||||
Value = importData.PartNumber,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
if (tags.Count > 249)
|
||||
{
|
||||
tags = tags.Take(249).ToList();
|
||||
}
|
||||
|
||||
// await _shopifyClient.Metafields.Add(partNumberMetafield);
|
||||
string zzzIsFitment = isFitment
|
||||
? "zzzIsFitment=true"
|
||||
: "zzzIsFitment=false";
|
||||
|
||||
List<string> tags = new List<string>();
|
||||
tags.Add(zzzIsFitment);
|
||||
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
product.Tags = string.Join(',', tags);
|
||||
product.BodyHtml = bodyHtml;
|
||||
|
||||
tags.AddRange(ymmFitment);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
if (tags.Count > 249)
|
||||
{
|
||||
tags = tags.Take(249).ToList();
|
||||
}
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
string zzzIsFitment = isFitment
|
||||
? "zzzIsFitment=true"
|
||||
: "zzzIsFitment=false";
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
tags.Add(zzzIsFitment);
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
|
||||
//product.Tags = string.Join(',', tags);
|
||||
product.BodyHtml = bodyHtml;
|
||||
await _shopifyClient.Products.Update(product);
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user