Current state, whatever that means
This commit is contained in:
144
PartSource.Automation/Jobs/POC/UpdateFitmentScratchpad.cs
Normal file
144
PartSource.Automation/Jobs/POC/UpdateFitmentScratchpad.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Automation.Models;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Dtos;
|
||||
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.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class UpdateFitmentScratchpad : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitmentScratchpad> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
|
||||
public UpdateFitmentScratchpad(ILogger<UpdateFitmentScratchpad> 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>
|
||||
{
|
||||
"C172-S231-F23107_(PS) Wipers - TRICO Neoform",
|
||||
"CA172-SC231-FL23106_Wiper Blades - Bosch Icon",
|
||||
"CA172-SC231-FL23107_(PS) Wipers - TRICO Neoform",
|
||||
"CA172-SC231-FL23109_(PS) Wipers - TRICO Tech/Exact Fit",
|
||||
"CA172-SC231-FL23110_Wiper Accessories",
|
||||
"CA172-SC231-FL23114_Wiper Blades - Rear",
|
||||
"CA172-SC231-FL23116_(PS) Wipers - Bosch Insight (Hybrid)",
|
||||
"CA172-SC231-FL23117_(PS) Wipers - Bosch Clear Advantage (Beam)",
|
||||
"CA172-SC231-FL23118_(PS) Wipers - Winter",
|
||||
"CA172-SC231-FL23125_Wiper Blades - Bosch Areotwin"
|
||||
};
|
||||
|
||||
IList<string> csvData = new List<string>
|
||||
{
|
||||
"\"Line Code\", \"Part Number\", \"Year\", \"Make\", \"Model\", \"Position\""
|
||||
};
|
||||
|
||||
foreach (string type in productTypes)
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", type } });
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
string csvRow = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
|
||||
|
||||
IList<VehicleFitmentDto> vehicles = _vehicleService.GetVehicleFitmentForPart(importData.PartNumber, importData.LineCode);
|
||||
|
||||
if (vehicles.Count > 0)
|
||||
{
|
||||
foreach (VehicleFitmentDto vehicle in vehicles)
|
||||
{
|
||||
try
|
||||
{
|
||||
csvData.Add($"\"{importData.LineCode}\", \"{importData.PartNumber}\", \"{vehicle.Vehicle.Year}\", \"{vehicle.Vehicle.MakeName}\", \"{vehicle.Vehicle.ModelName}\", \"{vehicle.Fitment.Position}\"");
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Did an iteration of {type}");
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Wiper Fitment.csv", csvData);
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user