Current state, whatever that means
This commit is contained in:
@@ -3,17 +3,22 @@
|
|||||||
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
|
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
|
||||||
"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true"
|
"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Warning"
|
"Default": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"Nexpart": {
|
"Nexpart": {
|
||||||
"ApiKey": "AFA5CC1431CD43DCA663C17BDAD850BB-C8F5BDF9A09C4464A66731CA4427CBB9",
|
"ApiKey": "AFA5CC1431CD43DCA663C17BDAD850BB-C8F5BDF9A09C4464A66731CA4427CBB9",
|
||||||
"ApiSecret": "7AB35766-0379-4DF0-9665-DAEE13822F43",
|
"ApiSecret": "7AB35766-0379-4DF0-9665-DAEE13822F43",
|
||||||
"Url": "http://acespssint.nexpart.com:4001/partselect/1.0/services/PartSelectService.PartSelectHttpSoap11Endpoint/"
|
"Url": "http://acespssint.nexpart.com:4001/partselect/1.0/services/PartSelectService.PartSelectHttpSoap11Endpoint/"
|
||||||
},
|
},
|
||||||
|
//"Shopify": {
|
||||||
|
// "ApiKey": "9a533dad460321c6ce8f30bf5b8691ed",
|
||||||
|
// "ApiSecret": "dc9e28365d9858e544d57ac7af43fee7",
|
||||||
|
// "ShopDomain": "dev-partsource.myshopify.com"
|
||||||
|
//}
|
||||||
"Shopify": {
|
"Shopify": {
|
||||||
"ApiKey": "9a533dad460321c6ce8f30bf5b8691ed",
|
"ApiKey": "9a533dad460321c6ce8f30bf5b8691ed",
|
||||||
"ApiSecret": "dc9e28365d9858e544d57ac7af43fee7",
|
"ApiSecret": "dc9e28365d9858e544d57ac7af43fee7",
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace PartSource.Automation.Jobs
|
|||||||
private readonly ILogger<ExecuteSsisPackages> _logger;
|
private readonly ILogger<ExecuteSsisPackages> _logger;
|
||||||
|
|
||||||
// TODO: set from config
|
// TODO: set from config
|
||||||
private readonly string[] _ssisPackages = { "Parts Availability", "Parts Price" };
|
private readonly string[] _ssisPackages = { "Parts Availability" };
|
||||||
|
|
||||||
public ExecuteSsisPackages(IConfiguration configuration, SsisService ssisService, ILogger<ExecuteSsisPackages> logger)
|
public ExecuteSsisPackages(IConfiguration configuration, SsisService ssisService, ILogger<ExecuteSsisPackages> logger)
|
||||||
{
|
{
|
||||||
|
|||||||
75
PartSource.Automation/Jobs/GetNexpartMenuItems.cs
Normal file
75
PartSource.Automation/Jobs/GetNexpartMenuItems.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using PartSource.Data.Nexpart;
|
||||||
|
using PartSource.Services;
|
||||||
|
using Ratermania.Automation.Interfaces;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Jobs
|
||||||
|
{
|
||||||
|
public class GetNexpartMenuItems : IAutomationJob
|
||||||
|
{
|
||||||
|
private readonly NexpartService _nexpartService;
|
||||||
|
|
||||||
|
public GetNexpartMenuItems(NexpartService nexpartService)
|
||||||
|
{
|
||||||
|
_nexpartService = nexpartService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Run()
|
||||||
|
{
|
||||||
|
IList<string> rows = new List<string>
|
||||||
|
{
|
||||||
|
"\"Level 1\", \"Level 2\", \"Level 3\", \"Menu ID\""
|
||||||
|
};
|
||||||
|
|
||||||
|
MenuNodesLookup menuNodesLookup = new MenuNodesLookup
|
||||||
|
{
|
||||||
|
MenuId = 1,
|
||||||
|
NumberOfLevels = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
MenuNodesLookupResponse categoryResponse = await _nexpartService.SendRequest<MenuNodesLookup, MenuNodesLookupResponse>(menuNodesLookup);
|
||||||
|
|
||||||
|
foreach (MenuNode categoryNode in categoryResponse.ResponseBody.MenuNode)
|
||||||
|
{
|
||||||
|
rows.Add($"\"{categoryNode.Description}\",\"\",\"\",{categoryNode.Id}");
|
||||||
|
|
||||||
|
MenuNodesLookup subgroupLookup = new MenuNodesLookup
|
||||||
|
{
|
||||||
|
MenuId = 1,
|
||||||
|
NumberOfLevels = 1,
|
||||||
|
ParentMenuNodeId = categoryNode.Id
|
||||||
|
};
|
||||||
|
|
||||||
|
MenuNodesLookupResponse subgroupResponse = await _nexpartService.SendRequest<MenuNodesLookup, MenuNodesLookupResponse>(subgroupLookup);
|
||||||
|
|
||||||
|
foreach (MenuNode subgroupNode in subgroupResponse.ResponseBody.MenuNode)
|
||||||
|
{
|
||||||
|
rows.Add($"\"{categoryNode.Description}\",\"{subgroupNode.Description}\",\"\",{subgroupNode.Id}");
|
||||||
|
|
||||||
|
MenuNodesLookup thirdLookup = new MenuNodesLookup
|
||||||
|
{
|
||||||
|
MenuId = 1,
|
||||||
|
NumberOfLevels = 1,
|
||||||
|
ParentMenuNodeId = subgroupNode.Id
|
||||||
|
};
|
||||||
|
|
||||||
|
MenuNodesLookupResponse thirdResponse = await _nexpartService.SendRequest<MenuNodesLookup, MenuNodesLookupResponse>(thirdLookup);
|
||||||
|
|
||||||
|
foreach (MenuNode thirdNode in thirdResponse.ResponseBody.MenuNode)
|
||||||
|
{
|
||||||
|
rows.Add($"\"{categoryNode.Description}\",\"{subgroupNode.Description}\",\"{thirdNode.Description}\",{thirdNode.Id}");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Partsource Menu Items.csv", rows);
|
||||||
|
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
65
PartSource.Automation/Jobs/POC/FixMultipleSeoTables.cs
Normal file
65
PartSource.Automation/Jobs/POC/FixMultipleSeoTables.cs
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
using Ratermania.Automation.Interfaces;
|
||||||
|
using Ratermania.Shopify;
|
||||||
|
using Ratermania.Shopify.Resources;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Jobs.POC
|
||||||
|
{
|
||||||
|
public class FixMultipleSeoTables : IAutomationJob
|
||||||
|
{
|
||||||
|
private readonly ShopifyClient _shopifyClient;
|
||||||
|
|
||||||
|
public FixMultipleSeoTables(ShopifyClient shopifyClient)
|
||||||
|
{
|
||||||
|
_shopifyClient = shopifyClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Run()
|
||||||
|
{
|
||||||
|
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA112-SC137-FL13750_Intake Manifolds" } });
|
||||||
|
|
||||||
|
while (products != null && products.Any())
|
||||||
|
{
|
||||||
|
foreach (Product product in products)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string[] parts = product.BodyHtml.Split("<div id=\"seoData\">");
|
||||||
|
|
||||||
|
if (parts.Length > 2)
|
||||||
|
{
|
||||||
|
string ul = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
|
||||||
|
string seoData = "<div id=\"seoData\">" + parts[1].Substring(0, parts[1].IndexOf("</table>") + "</table>".Length) + "</div>";
|
||||||
|
string vehicleIds = new Regex("<div id=\"vehicleIDs\".*</div>").Match(product.BodyHtml).Value;
|
||||||
|
|
||||||
|
product.BodyHtml = ul + seoData + vehicleIds;
|
||||||
|
|
||||||
|
await _shopifyClient.Products.Update(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Failed to update {product.Id}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine("Did 250");
|
||||||
|
products = await _shopifyClient.Products.GetNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
products = await _shopifyClient.Products.GetPrevious();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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);
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ using System.Data;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -24,6 +25,8 @@ namespace PartSource.Automation.Jobs
|
|||||||
private readonly FtpConfiguration _ftpConfiguration;
|
private readonly FtpConfiguration _ftpConfiguration;
|
||||||
private readonly SeoDataType _seoDataType;
|
private readonly SeoDataType _seoDataType;
|
||||||
|
|
||||||
|
private readonly IDictionary<string, string> _noteDictionary;
|
||||||
|
|
||||||
public ProcessWhiFitment(IConfiguration configuration, ILogger<ProcessWhiFitment> logger, WhiSeoService whiSeoService)
|
public ProcessWhiFitment(IConfiguration configuration, ILogger<ProcessWhiFitment> logger, WhiSeoService whiSeoService)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
@@ -32,48 +35,68 @@ namespace PartSource.Automation.Jobs
|
|||||||
_seoDataType = SeoDataType.Fitment;
|
_seoDataType = SeoDataType.Fitment;
|
||||||
|
|
||||||
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||||
|
|
||||||
|
_noteDictionary = new ConcurrentDictionary<string, string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Run()
|
public async Task Run()
|
||||||
{
|
{
|
||||||
_whiSeoService.Truncate();
|
_whiSeoService.TruncateFitmentTables();
|
||||||
_whiSeoService.GetFiles(_seoDataType);
|
// _whiSeoService.GetFiles(_seoDataType);
|
||||||
|
|
||||||
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||||
|
|
||||||
IEnumerable<IGrouping<string, FileInfo>> fileGroups = directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last());
|
ConcurrentQueue<IGrouping<string, FileInfo>> fileGroups = new ConcurrentQueue<IGrouping<string, FileInfo>>();
|
||||||
|
|
||||||
foreach (IGrouping<string, FileInfo> fileGroup in fileGroups)
|
foreach (IGrouping<string, FileInfo> fileGroup in directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last()))
|
||||||
{
|
{
|
||||||
foreach (FileInfo fileInfo in fileGroup)
|
fileGroups.Enqueue(fileGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fileGroups.Count > 0)
|
||||||
|
{
|
||||||
|
Parallel.For(0, 2, i =>
|
||||||
{
|
{
|
||||||
try
|
bool result = fileGroups.TryDequeue(out IGrouping<string, FileInfo> fileGroup);
|
||||||
|
|
||||||
|
if (!result)
|
||||||
{
|
{
|
||||||
string filename = Decompress(fileInfo);
|
return;
|
||||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
|
||||||
|
|
||||||
DataTable dataTable = GetDataTable(filename);
|
|
||||||
|
|
||||||
_whiSeoService.BulkCopy(_seoDataType, dataTable, tableName);
|
|
||||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
|
||||||
|
|
||||||
File.Delete(filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception ex)
|
foreach (FileInfo fileInfo in fileGroup)
|
||||||
{
|
{
|
||||||
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
try
|
||||||
|
{
|
||||||
|
string filename = Decompress(fileInfo);
|
||||||
|
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||||
|
|
||||||
|
DataTable dataTable = GetDataTable(filename);
|
||||||
|
|
||||||
|
_whiSeoService.BulkCopyFitment(dataTable, tableName);
|
||||||
|
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||||
|
|
||||||
|
File.Delete(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
|
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
|
||||||
_whiSeoService.CreateFitmentTable(fitmentTable);
|
_whiSeoService.CreateFitmentTable(fitmentTable);
|
||||||
|
|
||||||
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
|
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_whiSeoService.CreateFitmentView();
|
_whiSeoService.CreateFitmentView();
|
||||||
|
|
||||||
|
_whiSeoService.SaveNotes(_noteDictionary);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Decompress(FileInfo fileInfo)
|
public string Decompress(FileInfo fileInfo)
|
||||||
@@ -96,7 +119,7 @@ namespace PartSource.Automation.Jobs
|
|||||||
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||||
dataTable.Columns.Add("Position", typeof(string));
|
dataTable.Columns.Add("Position", typeof(string));
|
||||||
dataTable.Columns.Add("NoteText", typeof(string));
|
dataTable.Columns.Add("FitmentNoteHash", typeof(string));
|
||||||
|
|
||||||
using StreamReader reader = new StreamReader(filename);
|
using StreamReader reader = new StreamReader(filename);
|
||||||
string line = reader.ReadLine(); // Burn the header row
|
string line = reader.ReadLine(); // Burn the header row
|
||||||
@@ -112,20 +135,45 @@ namespace PartSource.Automation.Jobs
|
|||||||
}
|
}
|
||||||
|
|
||||||
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
|
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
|
||||||
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9]", string.Empty).Trim();
|
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9\\-]", string.Empty).Trim();
|
||||||
string position = columns[7].Trim();
|
string position = columns[7].Trim();
|
||||||
|
|
||||||
string noteText = columns[4].Trim();
|
string noteText = columns[4].Trim();
|
||||||
|
string noteTextHash = GetMD5Hash(noteText);
|
||||||
|
|
||||||
|
if (!_noteDictionary.ContainsKey(noteTextHash))
|
||||||
|
{
|
||||||
|
_noteDictionary.Add(noteTextHash, noteText);
|
||||||
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(lineCode)
|
if (!string.IsNullOrEmpty(lineCode)
|
||||||
&& !string.IsNullOrEmpty(partNumber)
|
&& !string.IsNullOrEmpty(partNumber)
|
||||||
&& int.TryParse(columns[5], out int baseVehicleId)
|
&& int.TryParse(columns[5], out int baseVehicleId)
|
||||||
&& int.TryParse(columns[6], out int engineConfigId))
|
&& int.TryParse(columns[6], out int engineConfigId))
|
||||||
{
|
{
|
||||||
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteText });
|
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteTextHash });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataTable;
|
return dataTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms", Justification = "Not used for security")]
|
||||||
|
private string GetMD5Hash(string input)
|
||||||
|
{
|
||||||
|
using MD5 md5 = MD5.Create();
|
||||||
|
|
||||||
|
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
||||||
|
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
||||||
|
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
for (int i = 0; i < hashBytes.Length; i++)
|
||||||
|
{
|
||||||
|
stringBuilder.Append(hashBytes[i].ToString("X2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuilder.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
126
PartSource.Automation/Jobs/ProcessWhiVehicles.cs
Normal file
126
PartSource.Automation/Jobs/ProcessWhiVehicles.cs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using PartSource.Automation.Models.Configuration;
|
||||||
|
using PartSource.Automation.Models.Enums;
|
||||||
|
using PartSource.Automation.Services;
|
||||||
|
using Ratermania.Automation.Interfaces;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Jobs
|
||||||
|
{
|
||||||
|
public class ProcessWhiVehicles : IAutomationJob
|
||||||
|
{
|
||||||
|
private readonly ILogger<ProcessWhiVehicles> _logger;
|
||||||
|
private readonly WhiSeoService _whiSeoService;
|
||||||
|
private readonly FtpConfiguration _ftpConfiguration;
|
||||||
|
private readonly SeoDataType _seoDataType;
|
||||||
|
|
||||||
|
public ProcessWhiVehicles(IConfiguration configuration, ILogger<ProcessWhiVehicles> logger, WhiSeoService whiSeoService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_whiSeoService = whiSeoService;
|
||||||
|
|
||||||
|
_seoDataType = SeoDataType.Vehicle;
|
||||||
|
|
||||||
|
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Run()
|
||||||
|
{
|
||||||
|
_whiSeoService.TruncateVehicleTable();
|
||||||
|
_whiSeoService.GetFiles(_seoDataType);
|
||||||
|
|
||||||
|
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||||
|
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||||
|
|
||||||
|
IEnumerable<FileInfo> files = directoryInfo.GetFiles().Where(f => f.Name.StartsWith("seo_aces_vehicle_feed"));
|
||||||
|
|
||||||
|
foreach (FileInfo fileInfo in files)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||||
|
|
||||||
|
DataTable dataTable = GetDataTable(fileInfo.FullName);
|
||||||
|
|
||||||
|
_whiSeoService.BulkCopyVehicle(dataTable, tableName);
|
||||||
|
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||||
|
|
||||||
|
File.Delete(fileInfo.FullName);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_whiSeoService.CreateVehicleTable();
|
||||||
|
|
||||||
|
_logger.LogInformation($"Created vehicle table.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private DataTable GetDataTable(string filename)
|
||||||
|
{
|
||||||
|
using DataTable dataTable = new DataTable();
|
||||||
|
dataTable.Columns.Add("Year", typeof(int));
|
||||||
|
dataTable.Columns.Add("MakeId", typeof(int));
|
||||||
|
dataTable.Columns.Add("MakeName", typeof(string));
|
||||||
|
dataTable.Columns.Add("ModelId", typeof(int));
|
||||||
|
dataTable.Columns.Add("ModelName", typeof(string));
|
||||||
|
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||||
|
dataTable.Columns.Add("EngineDescription", typeof(string));
|
||||||
|
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||||
|
dataTable.Columns.Add("VehicleToEngineConfigId", typeof(int));
|
||||||
|
dataTable.Columns.Add("SubmodelId", typeof(int));
|
||||||
|
dataTable.Columns.Add("SubmodelName", typeof(string));
|
||||||
|
|
||||||
|
using StreamReader reader = new StreamReader(filename);
|
||||||
|
string line = reader.ReadLine(); // Burn the header row
|
||||||
|
|
||||||
|
while (reader.Peek() > 0)
|
||||||
|
{
|
||||||
|
line = reader.ReadLine();
|
||||||
|
|
||||||
|
string[] columns = line.Split("\",\"");
|
||||||
|
for (int i = 0; i < columns.Length; i++)
|
||||||
|
{
|
||||||
|
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
string makeName = columns[4].Trim();
|
||||||
|
string modelName = columns[6].Trim();
|
||||||
|
string submodelName = columns[34].Trim();
|
||||||
|
string engineDescription = columns[51].Trim();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(makeName)
|
||||||
|
&& !string.IsNullOrEmpty(modelName)
|
||||||
|
&& !string.IsNullOrEmpty(submodelName)
|
||||||
|
&& !string.IsNullOrEmpty(engineDescription)
|
||||||
|
&& int.TryParse(columns[0], out int baseVehicleId)
|
||||||
|
&& int.TryParse(columns[2], out int year)
|
||||||
|
&& int.TryParse(columns[3], out int makeId)
|
||||||
|
&& int.TryParse(columns[5], out int modelId)
|
||||||
|
&& int.TryParse(columns[33], out int submodelId)
|
||||||
|
&& int.TryParse(columns[35], out int engineConfigId)
|
||||||
|
&& int.TryParse(columns[36], out int vehicleToEngineConfigId))
|
||||||
|
{
|
||||||
|
dataTable.Rows.Add(new object[] { year, makeId, makeName, modelId, modelName, engineConfigId, engineDescription, baseVehicleId, vehicleToEngineConfigId, submodelId, submodelName });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataTable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -38,14 +39,21 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
public async Task Run()
|
public async Task Run()
|
||||||
{
|
{
|
||||||
IEnumerable<Product> products = null;
|
IList<string> productTypes = new List<string>
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });//, {"product_type", "CA142-SC130-FL13029_Certified Brake Pads" } });
|
"CA108-SC349-FL34907_CV Shafts, New"
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (string type in productTypes)
|
||||||
|
{
|
||||||
|
IEnumerable<Product> products = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA108-SC349-FL34907_CV Shafts, New" } });
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError("Failed to get products from Shopify", ex);
|
_logger.LogError("Failed to get products from Shopify", ex);
|
||||||
throw;
|
throw;
|
||||||
@@ -61,14 +69,25 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
|
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||||
|
|
||||||
if (importData == null)
|
//importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
|
||||||
|
|
||||||
|
//if (importData == null)
|
||||||
|
//{
|
||||||
|
// continue;
|
||||||
|
importData = new ImportData
|
||||||
{
|
{
|
||||||
continue;
|
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;
|
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);
|
||||||
|
|
||||||
@@ -84,6 +103,10 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
if (vehicleIdFitment.Count > 0)
|
if (vehicleIdFitment.Count > 0)
|
||||||
{
|
{
|
||||||
|
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
|
||||||
|
|
||||||
|
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||||
|
|
||||||
isFitment = true;
|
isFitment = true;
|
||||||
|
|
||||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||||
@@ -114,6 +137,28 @@ namespace PartSource.Automation.Jobs
|
|||||||
{
|
{
|
||||||
isFitment = true;
|
isFitment = true;
|
||||||
|
|
||||||
|
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>");
|
||||||
|
|
||||||
|
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||||
|
|
||||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||||
if (json.Length < 100000)
|
if (json.Length < 100000)
|
||||||
{
|
{
|
||||||
@@ -149,12 +194,32 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||||
|
|
||||||
List<string> tags = new List<string>
|
Metafield lineCodeMetafield = new Metafield
|
||||||
{
|
{
|
||||||
importData.LineCode,
|
Namespace = "google",
|
||||||
importData.PartNumber
|
Key = "custom_label_0",
|
||||||
|
Value = importData.LineCode,
|
||||||
|
ValueType = "string",
|
||||||
|
OwnerResource = "product",
|
||||||
|
OwnerId = product.Id
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//await _shopifyClient.Metafields.Add(lineCodeMetafield);
|
||||||
|
|
||||||
|
Metafield partNumberMetafield = new Metafield
|
||||||
|
{
|
||||||
|
Namespace = "google",
|
||||||
|
Key = "custom_label_1",
|
||||||
|
Value = importData.PartNumber,
|
||||||
|
ValueType = "string",
|
||||||
|
OwnerResource = "product",
|
||||||
|
OwnerId = product.Id
|
||||||
|
};
|
||||||
|
|
||||||
|
// await _shopifyClient.Metafields.Add(partNumberMetafield);
|
||||||
|
|
||||||
|
List<string> tags = new List<string>();
|
||||||
|
|
||||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||||
{
|
{
|
||||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||||
@@ -173,8 +238,8 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
tags.Add(zzzIsFitment);
|
tags.Add(zzzIsFitment);
|
||||||
|
|
||||||
product.Tags = string.Join(',', tags);
|
//product.Tags = string.Join(',', tags);
|
||||||
|
product.BodyHtml = bodyHtml;
|
||||||
await _shopifyClient.Products.Update(product);
|
await _shopifyClient.Products.Update(product);
|
||||||
|
|
||||||
importData.IsFitment = isFitment;
|
importData.IsFitment = isFitment;
|
||||||
@@ -205,5 +270,7 @@ namespace PartSource.Automation.Jobs
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,7 +47,14 @@ namespace PartSource.Automation.Jobs
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
|
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||||
|
|
||||||
|
ImportData 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 || importData?.LineCode == "SVG") // Headlights go in front, DUH
|
if (importData == null || importData?.LineCode == "SVG") // Headlights go in front, DUH
|
||||||
{
|
{
|
||||||
@@ -87,40 +94,41 @@ namespace PartSource.Automation.Jobs
|
|||||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||||
|
|
||||||
|
|
||||||
IList<string> notes = fitments.Select(f => f.NoteText)
|
//IList<string> notes = fitments.Select(f => f.NoteText)
|
||||||
.Distinct()
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
IList<object> vehicleNotes = new List<object>();
|
// .Distinct()
|
||||||
|
// .ToList();
|
||||||
|
|
||||||
foreach (string noteText in notes)
|
//IList<object> vehicleNotes = new List<object>();
|
||||||
{
|
|
||||||
vehicleIds = fitments.Where(f => f.NoteText == noteText)
|
|
||||||
.Select(f => new { f.EngineConfigId, f.BaseVehicleId })
|
|
||||||
.SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
|
|
||||||
.Select(v => v.VehicleToEngineConfigId)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
vehicleNotes.Add(new { noteText, vehicleIds });
|
//foreach (string noteText in notes)
|
||||||
}
|
//{
|
||||||
|
// vehicleIds = fitments.Where(f => f.NoteText == noteText)
|
||||||
|
// .Select(f => new { f.EngineConfigId, f.BaseVehicleId })
|
||||||
|
// .SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
|
||||||
|
// .Select(v => v.VehicleToEngineConfigId)
|
||||||
|
// .ToList();
|
||||||
|
|
||||||
string json = JsonConvert.SerializeObject(vehicleNotes);
|
// vehicleNotes.Add(new { noteText, vehicleIds });
|
||||||
if (json.Length >= 100000)
|
//}
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Metafield vehicleMetafield = new Metafield
|
//string json = JsonConvert.SerializeObject(vehicleNotes);
|
||||||
{
|
//if (json.Length >= 100000)
|
||||||
Namespace = "fitment",
|
//{
|
||||||
Key = "note_text",
|
// continue;
|
||||||
Value = json,
|
//}
|
||||||
ValueType = "json_string",
|
|
||||||
OwnerResource = "product",
|
|
||||||
OwnerId = product.Id
|
|
||||||
};
|
|
||||||
|
|
||||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
//Metafield vehicleMetafield = new Metafield
|
||||||
|
//{
|
||||||
|
// Namespace = "fitment",
|
||||||
|
// Key = "note_text",
|
||||||
|
// Value = json,
|
||||||
|
// ValueType = "json_string",
|
||||||
|
// OwnerResource = "product",
|
||||||
|
// OwnerId = product.Id
|
||||||
|
//};
|
||||||
|
|
||||||
|
//await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||||
|
|
||||||
//importData.UpdatedAt = DateTime.Now;
|
//importData.UpdatedAt = DateTime.Now;
|
||||||
//importData.UpdateType = "Positioning";
|
//importData.UpdateType = "Positioning";
|
||||||
@@ -148,7 +156,7 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
private IList<Fitment> GetPositionOrderedFitments(string partNumber, string lineCode)
|
private IList<Fitment> GetPositionOrderedFitments(string partNumber, string lineCode)
|
||||||
{
|
{
|
||||||
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]\\-", string.Empty);
|
||||||
|
|
||||||
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
||||||
.Where(d => d.LineCode == lineCode)
|
.Where(d => d.LineCode == lineCode)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using PartSource.Automation.Models.Jobs;
|
||||||
using PartSource.Automation.Services;
|
using PartSource.Automation.Services;
|
||||||
using PartSource.Data.Contexts;
|
using PartSource.Data.Contexts;
|
||||||
using PartSource.Data.Models;
|
using PartSource.Data.Models;
|
||||||
@@ -9,7 +10,9 @@ using Ratermania.Shopify.Resources;
|
|||||||
using Ratermania.Shopify.Resources.Enums;
|
using Ratermania.Shopify.Resources.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Mail;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PartSource.Automation.Jobs
|
namespace PartSource.Automation.Jobs
|
||||||
@@ -31,15 +34,14 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
public async Task Run()
|
public async Task Run()
|
||||||
{
|
{
|
||||||
|
List<UpdatePricingResult> pricingReport = new List<UpdatePricingResult>();
|
||||||
IEnumerable<Product> products = null;
|
IEnumerable<Product> products = null;
|
||||||
IEnumerable<PartPrice> prices = null;
|
IEnumerable<PartPrice> prices = null;
|
||||||
int updateCount = 0;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||||
|
prices = await _partSourceContext.PartPrices.AsNoTracking().ToListAsync();
|
||||||
prices = await _partSourceContext.PartPrices.ToListAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -53,6 +55,8 @@ namespace PartSource.Automation.Jobs
|
|||||||
{
|
{
|
||||||
foreach (Product product in products)
|
foreach (Product product in products)
|
||||||
{
|
{
|
||||||
|
List<UpdatePricingResult> productPricingUpdate = new List<UpdatePricingResult>();
|
||||||
|
|
||||||
if (product.Variants.Length > 0)
|
if (product.Variants.Length > 0)
|
||||||
{
|
{
|
||||||
bool hasUpdate = false;
|
bool hasUpdate = false;
|
||||||
@@ -69,6 +73,15 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
if (product.Variants[i].Price.ToString("G29") != partPrice.Your_Price.Value.ToString("G29") || product.Variants[i].CompareAtPrice.ToString("G29") != partPrice.Compare_Price.Value.ToString("G29"))
|
if (product.Variants[i].Price.ToString("G29") != partPrice.Your_Price.Value.ToString("G29") || product.Variants[i].CompareAtPrice.ToString("G29") != partPrice.Compare_Price.Value.ToString("G29"))
|
||||||
{
|
{
|
||||||
|
productPricingUpdate.Add(new UpdatePricingResult
|
||||||
|
{
|
||||||
|
Sku = variant.Sku,
|
||||||
|
OldPrice = product.Variants[i].Price,
|
||||||
|
NewPrice = partPrice.Your_Price.Value,
|
||||||
|
OldCompareAt = product.Variants[i].CompareAtPrice,
|
||||||
|
NewCompareAt = partPrice.Compare_Price.Value
|
||||||
|
});
|
||||||
|
|
||||||
product.Variants[i].Price = partPrice.Your_Price.Value;
|
product.Variants[i].Price = partPrice.Your_Price.Value;
|
||||||
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||||
|
|
||||||
@@ -95,8 +108,8 @@ namespace PartSource.Automation.Jobs
|
|||||||
{
|
{
|
||||||
//await _shopifyClient.Metafields.Add(metafield);
|
//await _shopifyClient.Metafields.Add(metafield);
|
||||||
await _shopifyClient.Products.Update(product);
|
await _shopifyClient.Products.Update(product);
|
||||||
|
|
||||||
updateCount++;
|
pricingReport.AddRange(productPricingUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -111,7 +124,7 @@ namespace PartSource.Automation.Jobs
|
|||||||
{
|
{
|
||||||
products = await _shopifyClient.Products.GetNext();
|
products = await _shopifyClient.Products.GetNext();
|
||||||
|
|
||||||
_logger.LogInformation($"Total updated: {updateCount}");
|
_logger.LogInformation($"Total updated: {pricingReport.Count}");
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -121,7 +134,40 @@ namespace PartSource.Automation.Jobs
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_emailService.Send("Pricing Update Completed", $"The pricing update has completed. Total updated: {updateCount}");
|
Attachment attachment = GetPricingReportAttachment(pricingReport);
|
||||||
|
|
||||||
|
_emailService.Send("Pricing Update Completed", $"The pricing update has completed. Total updated: {pricingReport.Count}", attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Attachment GetPricingReportAttachment(IList<UpdatePricingResult> pricingReport)
|
||||||
|
{
|
||||||
|
string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Pricing Reports");
|
||||||
|
string filename = Path.Combine(directory, $"Pricing Update {DateTime.Now.ToString("yyyy-MM-dd")}.csv");
|
||||||
|
|
||||||
|
if (!Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (File.Exists(filename))
|
||||||
|
{
|
||||||
|
File.Delete(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
using FileStream fileStream = File.OpenWrite(filename);
|
||||||
|
using StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
|
||||||
|
|
||||||
|
streamWriter.WriteLine("SKU, Old Price, New Price, Old Compare At, New Compare At");
|
||||||
|
|
||||||
|
foreach (UpdatePricingResult pricingResult in pricingReport)
|
||||||
|
{
|
||||||
|
streamWriter.WriteLine($"{pricingResult.Sku},{pricingResult.OldPrice},{pricingResult.NewPrice},{pricingResult.OldCompareAt},{pricingResult.NewCompareAt}");
|
||||||
|
}
|
||||||
|
|
||||||
|
streamWriter.Close();
|
||||||
|
fileStream.Close();
|
||||||
|
|
||||||
|
return new Attachment(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
19
PartSource.Automation/Models/Jobs/UpdatePricingResult.cs
Normal file
19
PartSource.Automation/Models/Jobs/UpdatePricingResult.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Models.Jobs
|
||||||
|
{
|
||||||
|
public class UpdatePricingResult
|
||||||
|
{
|
||||||
|
public string Sku { get; set; }
|
||||||
|
|
||||||
|
public decimal OldPrice { get; set; }
|
||||||
|
|
||||||
|
public decimal NewPrice { get; set; }
|
||||||
|
|
||||||
|
public decimal OldCompareAt { get; set; }
|
||||||
|
|
||||||
|
public decimal NewCompareAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using PartSource.Automation.Jobs;
|
using PartSource.Automation.Jobs;
|
||||||
|
using PartSource.Automation.Jobs.POC;
|
||||||
using PartSource.Automation.Services;
|
using PartSource.Automation.Services;
|
||||||
using PartSource.Data;
|
using PartSource.Data;
|
||||||
using PartSource.Data.AutoMapper;
|
using PartSource.Data.AutoMapper;
|
||||||
@@ -21,8 +22,7 @@ namespace PartSource.Automation
|
|||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args){
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using IHost host = CreateHostBuilder().Build();
|
using IHost host = CreateHostBuilder().Build();
|
||||||
@@ -55,7 +55,11 @@ namespace PartSource.Automation
|
|||||||
)
|
)
|
||||||
|
|
||||||
.AddDbContext<FitmentContext>(options =>
|
.AddDbContext<FitmentContext>(options =>
|
||||||
options.UseSqlServer(builder.Configuration.GetConnectionString("FitmentDatabase"), opts => opts.EnableRetryOnFailure())
|
options.UseSqlServer(builder.Configuration.GetConnectionString("FitmentDatabase"), opts =>
|
||||||
|
{
|
||||||
|
opts.EnableRetryOnFailure();
|
||||||
|
opts.CommandTimeout(600);
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
.AddShopify(options =>
|
.AddShopify(options =>
|
||||||
@@ -64,33 +68,44 @@ namespace PartSource.Automation
|
|||||||
options.ApiSecret = builder.Configuration["Shopify:ApiSecret"];
|
options.ApiSecret = builder.Configuration["Shopify:ApiSecret"];
|
||||||
options.ApiVersion = "2020-01";
|
options.ApiVersion = "2020-01";
|
||||||
options.ShopDomain = builder.Configuration["Shopify:ShopDomain"];
|
options.ShopDomain = builder.Configuration["Shopify:ShopDomain"];
|
||||||
|
|
||||||
|
//options.ApiKey = "9a533dad460321c6ce8f30bf5b8691ed";
|
||||||
|
//options.ApiSecret = "dc9e28365d9858e544d57ac7af43fee7";
|
||||||
|
//options.ApiVersion = "2020-01";
|
||||||
|
//options.ShopDomain = "dev-partsource.myshopify.com";
|
||||||
})
|
})
|
||||||
|
|
||||||
.AddAutomation(options =>
|
.AddAutomation(options =>
|
||||||
{
|
{
|
||||||
options.HasBaseInterval(new TimeSpan(0, 15, 0))
|
options.HasBaseInterval(new TimeSpan(0, 15, 0))
|
||||||
.HasMaxFailures(5)
|
.HasMaxFailures(1)
|
||||||
//.HasJob<TestJob>(options => options.HasInterval(new TimeSpan(7, 0, 0, 0)));
|
//.HasJob<TestJob>(options => options.HasInterval(new TimeSpan(7, 0, 0, 0)));
|
||||||
//
|
//
|
||||||
//.HasJob<SyncronizeProducts>(options => options.HasInterval(new TimeSpan(24, 0, 0)))
|
//.HasJob<SyncronizeProducts>(options => options.HasInterval(new TimeSpan(24, 0, 0)))
|
||||||
.HasJob<ProcessWhiFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
// .HasJob<ProcessWhiFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0)));
|
||||||
// .HasDependency<SyncronizeProducts>()
|
//.HasJob<ProcessWhiVehicles>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||||
// .HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
//.HasDependency<SyncronizeProducts>()
|
||||||
// .HasDependency<ProcessWhiFitment>()
|
.HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0)));
|
||||||
//.HasDependency<SyncronizeProducts>()
|
//.HasJob<UpdatePositioning>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||||
// .StartsAt(DateTime.Today.AddHours(8))
|
// .HasDependency<UpdateFitment>()
|
||||||
//) ;
|
// .HasDependency<ProcessWhiFitment>()
|
||||||
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
// .HasDependency<SyncronizeProducts>()
|
||||||
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
|
// .StartsAt(DateTime.Today.AddHours(8))
|
||||||
//)
|
//) ;
|
||||||
// .HasJob<ExecuteSsisPackages>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||||
// .StartsAt(DateTime.Today.AddHours(26))
|
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
|
||||||
// )
|
//)
|
||||||
// .HasJob<UpdatePricing>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
//.HasJob<ExecuteSsisPackages>(options =>
|
||||||
// .HasDependency<ExecuteSsisPackages>()
|
// options.HasInterval(new TimeSpan(24, 0, 0))
|
||||||
// .StartsAt(DateTime.Today.AddHours(27)
|
// .StartsAt(DateTime.Today.AddHours(5))
|
||||||
// )
|
// );
|
||||||
);
|
//.StartsAt(DateTime.Today.AddHours(26))
|
||||||
|
//)
|
||||||
|
//.HasJob<UpdatePricing>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||||
|
//.HasDependency<ExecuteSsisPackages>()
|
||||||
|
//.StartsAt(DateTime.Today.AddHours(27)
|
||||||
|
//)
|
||||||
|
//);
|
||||||
//.AddApiServer();
|
//.AddApiServer();
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -98,6 +113,7 @@ namespace PartSource.Automation
|
|||||||
.AddSingleton<SsisService>()
|
.AddSingleton<SsisService>()
|
||||||
.AddSingleton<WhiSeoService>()
|
.AddSingleton<WhiSeoService>()
|
||||||
.AddSingleton<VehicleService>()
|
.AddSingleton<VehicleService>()
|
||||||
|
.AddSingleton<NexpartService>()
|
||||||
|
|
||||||
|
|
||||||
.AddAutoMapper(typeof(PartSourceProfile));
|
.AddAutoMapper(typeof(PartSourceProfile));
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using PartSource.Automation.Models.Configuration;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -19,7 +20,7 @@ namespace PartSource.Automation.Services
|
|||||||
_emailConfiguration = configuration.GetSection("emailConfiguration").Get<EmailConfiguration>();
|
_emailConfiguration = configuration.GetSection("emailConfiguration").Get<EmailConfiguration>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Send(string subject, string body)
|
public void Send(string subject, string body, Attachment attachment = null)
|
||||||
{
|
{
|
||||||
using SmtpClient smtpClient = new SmtpClient
|
using SmtpClient smtpClient = new SmtpClient
|
||||||
{
|
{
|
||||||
@@ -31,15 +32,20 @@ namespace PartSource.Automation.Services
|
|||||||
From = new MailAddress(_emailConfiguration.From),
|
From = new MailAddress(_emailConfiguration.From),
|
||||||
Subject = subject,
|
Subject = subject,
|
||||||
Body = body,
|
Body = body,
|
||||||
IsBodyHtml = true
|
IsBodyHtml = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (attachment != null)
|
||||||
|
{
|
||||||
|
mailMessage.Attachments.Add(attachment);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (string address in _emailConfiguration.To.Split(','))
|
foreach (string address in _emailConfiguration.To.Split(','))
|
||||||
{
|
{
|
||||||
mailMessage.To.Add(address);
|
mailMessage.To.Add(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
// smtpClient.Send(mailMessage);
|
smtpClient.Send(mailMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Send(string to, string subject, string body)
|
public void Send(string to, string subject, string body)
|
||||||
@@ -54,7 +60,7 @@ namespace PartSource.Automation.Services
|
|||||||
From = new MailAddress(_emailConfiguration.From),
|
From = new MailAddress(_emailConfiguration.From),
|
||||||
Subject = subject,
|
Subject = subject,
|
||||||
Body = body,
|
Body = body,
|
||||||
IsBodyHtml = false
|
IsBodyHtml = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (string address in _emailConfiguration.To.Split(','))
|
foreach (string address in _emailConfiguration.To.Split(','))
|
||||||
@@ -62,7 +68,7 @@ namespace PartSource.Automation.Services
|
|||||||
mailMessage.To.Add(to);
|
mailMessage.To.Add(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
// smtpClient.Send(mailMessage);
|
smtpClient.Send(mailMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace PartSource.Automation.Services
|
|||||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
||||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||||
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
||||||
|
|
||||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||||
using Stream responseStream = response.GetResponseStream();
|
using Stream responseStream = response.GetResponseStream();
|
||||||
using FileStream fileStream = new FileStream($"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}", FileMode.Create);
|
using FileStream fileStream = new FileStream($"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}", FileMode.Create);
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ namespace PartSource.Automation.Services
|
|||||||
|
|
||||||
foreach (string file in files)
|
foreach (string file in files)
|
||||||
{
|
{
|
||||||
if (file.EndsWith("csv.gz"))
|
if (file.Contains(".csv"))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -53,7 +53,16 @@ namespace PartSource.Automation.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Truncate()
|
public void TruncateVehicleTable()
|
||||||
|
{
|
||||||
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void TruncateFitmentTables()
|
||||||
{
|
{
|
||||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
@@ -62,11 +71,36 @@ namespace PartSource.Automation.Services
|
|||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void BulkCopy(SeoDataType seoDataType, DataTable dataTable, string tableName)
|
public void SaveNotes(IDictionary<string, string> notes)
|
||||||
|
{
|
||||||
|
using DataTable dataTable = new DataTable();
|
||||||
|
dataTable.Columns.Add("NoteText", typeof(string));
|
||||||
|
dataTable.Columns.Add("Hash", typeof(string));
|
||||||
|
|
||||||
|
foreach (KeyValuePair<string, string> note in notes)
|
||||||
|
{
|
||||||
|
|
||||||
|
dataTable.Rows.Add(new string[] { note.Value, note.Key });
|
||||||
|
}
|
||||||
|
|
||||||
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||||
|
{
|
||||||
|
DestinationTableName = $"FitmentNote",
|
||||||
|
BulkCopyTimeout = 14400
|
||||||
|
};
|
||||||
|
|
||||||
|
bulk.WriteToServer(dataTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BulkCopyFitment(DataTable dataTable, string tableName)
|
||||||
{
|
{
|
||||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
|
string sql = string.Empty;
|
||||||
|
|
||||||
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
|
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
@@ -74,6 +108,25 @@ namespace PartSource.Automation.Services
|
|||||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||||
{
|
{
|
||||||
DestinationTableName = $"FitmentTemp.{tableName}",
|
DestinationTableName = $"FitmentTemp.{tableName}",
|
||||||
|
BulkCopyTimeout = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
bulk.WriteToServer(dataTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BulkCopyVehicle(DataTable dataTable, string tableName)
|
||||||
|
{
|
||||||
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
string sql = string.Empty;
|
||||||
|
|
||||||
|
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||||
|
{
|
||||||
|
DestinationTableName = $"VehicleTemp.{tableName}",
|
||||||
BulkCopyTimeout = 14400
|
BulkCopyTimeout = 14400
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -97,6 +150,21 @@ namespace PartSource.Automation.Services
|
|||||||
|
|
||||||
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
|
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
|
||||||
|
command2.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateVehicleTable()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
|
||||||
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
using SqlCommand command = new SqlCommand($"exec CreateVehicleTable", connection);
|
||||||
|
command.CommandTimeout = 1800;
|
||||||
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
"emailConfiguration": {
|
"emailConfiguration": {
|
||||||
"From": "alerts@ps-automation.eastus2.cloudapp.azure.com",
|
"From": "alerts@ps-automation.eastus2.cloudapp.azure.com",
|
||||||
"To": "tom@soundpress.com,Anas.Bajwa@Partsource.ca,josh@soundpress.com,alex.au@partsource.ca,michael.massara@partsource.ca",
|
"To": "tom@soundpress.com,Anas.Bajwa@Partsource.ca,josh@soundpress.com,alex.au@partsource.ca,michael.massara@partsource.ca",
|
||||||
|
//"To": "tom@tomraterman.com",
|
||||||
"SmtpHost": "localhost"
|
"SmtpHost": "localhost"
|
||||||
},
|
},
|
||||||
"FtpServers": {
|
"FtpServers": {
|
||||||
@@ -34,7 +35,8 @@
|
|||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information",
|
||||||
|
// "Microsoft.EntityFrameworkCore.Database.Command": "Information"
|
||||||
},
|
},
|
||||||
"EventLog": {
|
"EventLog": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
14
PartSource.Data/Dtos/VehicleFitmentDto.cs
Normal file
14
PartSource.Data/Dtos/VehicleFitmentDto.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using PartSource.Data.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PartSource.Data.Dtos
|
||||||
|
{
|
||||||
|
public class VehicleFitmentDto
|
||||||
|
{
|
||||||
|
public Fitment Fitment { get; set; }
|
||||||
|
|
||||||
|
public Vehicle Vehicle { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,6 @@ namespace PartSource.Data.Models
|
|||||||
|
|
||||||
public string Position { get; set; }
|
public string Position { get; set; }
|
||||||
|
|
||||||
public string NoteText { get; set; }
|
public string FitmentNoteHash { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,5 +30,8 @@ namespace PartSource.Data.Models
|
|||||||
|
|
||||||
[Key]
|
[Key]
|
||||||
public int VehicleToEngineConfigId { get; set; }
|
public int VehicleToEngineConfigId { get; set; }
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
public string Position { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,34 +10,42 @@ using System.Xml.Serialization;
|
|||||||
|
|
||||||
namespace PartSource.Services
|
namespace PartSource.Services
|
||||||
{
|
{
|
||||||
public class NexpartService
|
public class NexpartService
|
||||||
{
|
{
|
||||||
public async Task<U> SendRequest<T, U>(T requestContent)
|
public async Task<U> SendRequest<T, U>(T requestContent)
|
||||||
{
|
{
|
||||||
Envelope envelope = new Envelope();
|
Envelope envelope = new Envelope();
|
||||||
envelope.Body.Content = (object)(T)requestContent;
|
envelope.Body.Content = requestContent;
|
||||||
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
|
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
using (TextWriter textWriter = (TextWriter)new StringWriter(sb))
|
|
||||||
serializer.Serialize(textWriter, (object)envelope);
|
|
||||||
U content;
|
|
||||||
using (HttpClient client = new HttpClient())
|
using (TextWriter textWriter = new StringWriter(sb))
|
||||||
{
|
{
|
||||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "QUZBNUNDMTQzMUNENDNEQ0E2NjNDMTdCREFEODUwQkItQzhGNUJERjlBMDlDNDQ2NEE2NjczMUNBNDQyN0NCQjk6N0FCMzU3NjYtMDM3OS00REYwLTk2NjUtREFFRTEzODIyRjQz");
|
serializer.Serialize(textWriter, (object)envelope);
|
||||||
try
|
U content;
|
||||||
{
|
|
||||||
//HttpResponseMessage response = await client.PostAsync(ConfigurationManager.AppSettings["NexpartUrl"], (HttpContent)new StringContent(sb.ToString(), Encoding.UTF8, "text/xml"));
|
string x = textWriter.ToString();
|
||||||
HttpResponseMessage response = await client.PostAsync("http://acespssint.nexpart.com:4001/partselect/1.0/services/PartSelectService.PartSelectHttpSoap11Endpoint/", (HttpContent)new StringContent(sb.ToString(), Encoding.UTF8, "text/xml"));
|
|
||||||
Stream result = await response.Content.ReadAsStreamAsync();
|
using (HttpClient client = new HttpClient())
|
||||||
string str = await response.Content.ReadAsStringAsync();
|
{
|
||||||
content = (U)((Envelope)serializer.Deserialize(result)).Body.Content;
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "QjM4ODAyMzM3QjQxNEM2QTk4M0RFMjM0Mjk4Rjk4M0UtOUIzNUUxNzNBQUYxNEE2QjhCQjI2RjZDOUY2ODk1NDU6MkMzOUVCOTYtRDBBRS00QkVBLTlCMzItMUYyNTA5MDJGQTE0");
|
||||||
}
|
try
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
//HttpResponseMessage response = await client.PostAsync(ConfigurationManager.AppSettings["NexpartUrl"], (HttpContent)new StringContent(sb.ToString(), Encoding.UTF8, "text/xml"));
|
||||||
throw;
|
HttpResponseMessage response = await client.PostAsync("http://acespssprod.nexpart.com:8081/partselect/1.0/services/PartSelectService.PartSelectHttpSoap11Endpoint/", new StringContent(textWriter.ToString(), Encoding.UTF8));
|
||||||
}
|
Stream result = await response.Content.ReadAsStreamAsync();
|
||||||
}
|
string str = await response.Content.ReadAsStringAsync();
|
||||||
return content;
|
content = (U)((Envelope)serializer.Deserialize(result)).Body.Content;
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ namespace PartSource.Services
|
|||||||
|
|
||||||
IList<string> fitmentTags = new List<string>();
|
IList<string> fitmentTags = new List<string>();
|
||||||
|
|
||||||
IList<string> makeModels = vehicles.Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
|
IList<string> makeModels = vehicles.OrderBy(v => v.MakeName).ThenBy(v => v.ModelName).Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
|
||||||
|
|
||||||
foreach (string makeModel in makeModels)
|
foreach (string makeModel in makeModels)
|
||||||
{
|
{
|
||||||
@@ -268,24 +268,55 @@ namespace PartSource.Services
|
|||||||
return fitmentTags;
|
return fitmentTags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IList<string> GetYmmFitmentRange(IList<Vehicle> vehicles)
|
||||||
|
{
|
||||||
|
if (vehicles.Count == 0)
|
||||||
|
{
|
||||||
|
return new string[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
IList<string> fitmentTags = new List<string>();
|
||||||
|
|
||||||
|
IList<string> makeModels = vehicles.Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
|
||||||
|
|
||||||
|
foreach (string makeModel in makeModels)
|
||||||
|
{
|
||||||
|
string make = makeModel.Split(',')[0];
|
||||||
|
string model = makeModel.Split(',')[1];
|
||||||
|
|
||||||
|
int minYear = vehicles
|
||||||
|
.Where(v => v.MakeName == make && v.ModelName == model)
|
||||||
|
.Min(v => v.Year);
|
||||||
|
|
||||||
|
int maxYear = vehicles
|
||||||
|
.Where(v => v.MakeName == make && v.ModelName == model)
|
||||||
|
.Max(v => v.Year);
|
||||||
|
|
||||||
|
string tag = minYear == maxYear
|
||||||
|
? $"{minYear} {make.Trim()} {model.Trim()}"
|
||||||
|
: $"{minYear}-{maxYear} {make.Trim()} {model.Trim()}";
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.WriteLine(tag);
|
||||||
|
|
||||||
|
fitmentTags.Add(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fitmentTags;
|
||||||
|
}
|
||||||
|
|
||||||
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
|
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
|
||||||
{
|
{
|
||||||
return vehicles.Select(v => v.VehicleToEngineConfigId).ToArray();
|
return vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode)
|
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||||
{
|
|
||||||
return GetVehiclesForPart(partNumber, lineCode, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles)
|
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
|
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9\\-]", string.Empty);
|
||||||
|
|
||||||
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
||||||
.Where(d => d.LineCode == lineCode)
|
.Where(d => d.LineCode == lineCode)
|
||||||
@@ -296,7 +327,42 @@ namespace PartSource.Services
|
|||||||
.Join(_fitmentContext.Vehicles,
|
.Join(_fitmentContext.Vehicles,
|
||||||
f => new { f.BaseVehicleId, f.EngineConfigId },
|
f => new { f.BaseVehicleId, f.EngineConfigId },
|
||||||
v => new { v.BaseVehicleId, v.EngineConfigId },
|
v => new { v.BaseVehicleId, v.EngineConfigId },
|
||||||
(f, v) => v);
|
(f, v) => v)
|
||||||
|
.Distinct()
|
||||||
|
.OrderByDescending(x => x.Year);
|
||||||
|
|
||||||
|
if (maxVehicles > 0)
|
||||||
|
{
|
||||||
|
vehicles = vehicles.Take(maxVehicles);
|
||||||
|
}
|
||||||
|
|
||||||
|
return vehicles.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IList<VehicleFitmentDto> GetVehicleFitmentForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9\\-]", string.Empty);
|
||||||
|
|
||||||
|
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
||||||
|
.Where(d => d.LineCode == lineCode)
|
||||||
|
.Select(d => d.WhiCode);
|
||||||
|
|
||||||
|
IQueryable<VehicleFitmentDto> vehicles = _fitmentContext.Fitments
|
||||||
|
.Where(f => f.PartNumber == partNumber && whiCodes.Contains(f.LineCode))
|
||||||
|
.Join(_fitmentContext.Vehicles,
|
||||||
|
f => new { f.BaseVehicleId, f.EngineConfigId },
|
||||||
|
v => new { v.BaseVehicleId, v.EngineConfigId },
|
||||||
|
(f, v) => new VehicleFitmentDto
|
||||||
|
{
|
||||||
|
Fitment = f,
|
||||||
|
Vehicle = v
|
||||||
|
})
|
||||||
|
.Distinct();
|
||||||
|
|
||||||
if (maxVehicles > 0)
|
if (maxVehicles > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user