Current state, whatever that means
This commit is contained in:
145
PartSource.Automation/Jobs/POC/UpdateFitmentHtml.cs
Normal file
145
PartSource.Automation/Jobs/POC/UpdateFitmentHtml.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Automation.Models;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Exceptions;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class UpdateFitmentHtml : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitmentHtml> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
|
||||
public UpdateFitmentHtml(ILogger<UpdateFitmentHtml> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "vendor", "FRAM" } });
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
|
||||
try
|
||||
{
|
||||
importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.VariantSku == product.Variants[0].Sku);
|
||||
|
||||
if (importData == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
product.BodyHtml = importData.BodyHtml;
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine("</table>");
|
||||
|
||||
product.BodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||
}
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
product.BodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
}
|
||||
|
||||
List<string> tags = new List<string>
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber,
|
||||
"zzzIsFitment=true"
|
||||
};
|
||||
|
||||
product.Tags = string.Join(',', tags);
|
||||
|
||||
await _shopifyClient.Products.Update(product);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user