Finalized UpdatePositioning job
This commit is contained in:
@@ -12,36 +12,39 @@ namespace PartSource.Automation.Factories
|
||||
{
|
||||
public class JobFactory
|
||||
{
|
||||
public IServiceProvider _serviceProvider;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public JobFactory(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
public JobFactory(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public IAutomationJob Build(string jobName)
|
||||
{
|
||||
switch (jobName)
|
||||
{
|
||||
case nameof(AddProducts):
|
||||
return _serviceProvider.GetService<AddProducts>();
|
||||
case nameof(AddProducts):
|
||||
return _serviceProvider.GetService<AddProducts>();
|
||||
|
||||
case nameof(DeleteProducts):
|
||||
return _serviceProvider.GetService<DeleteProducts>();
|
||||
case nameof(DeleteProducts):
|
||||
return _serviceProvider.GetService<DeleteProducts>();
|
||||
|
||||
case nameof(TestJob):
|
||||
return new TestJob();
|
||||
case nameof(TestJob):
|
||||
return new TestJob();
|
||||
|
||||
case nameof(UpdateFitment):
|
||||
return _serviceProvider.GetService<UpdateFitment>();
|
||||
case nameof(UpdateFitment):
|
||||
return _serviceProvider.GetService<UpdateFitment>();
|
||||
|
||||
case nameof(UpdatePricing):
|
||||
case nameof(UpdatePricing):
|
||||
return _serviceProvider.GetService<UpdatePricing>();
|
||||
|
||||
case nameof(ExecuteSsisPackages):
|
||||
return _serviceProvider.GetService<ExecuteSsisPackages>();
|
||||
case nameof(UpdatePositioning):
|
||||
return _serviceProvider.GetService<UpdatePositioning>();
|
||||
|
||||
default:
|
||||
case nameof(ExecuteSsisPackages):
|
||||
return _serviceProvider.GetService<ExecuteSsisPackages>();
|
||||
|
||||
default:
|
||||
throw new Exception($"The job {jobName} could not be found.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,72 +53,73 @@ namespace PartSource.Automation.Jobs
|
||||
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
|
||||
IList<VehicleData> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
|
||||
if (vehicles.Count > 0)
|
||||
if (vehicles.Count == 0)
|
||||
{
|
||||
bool isFitment = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
bool isFitment = false;
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -8,8 +8,10 @@ using PartSource.Services;
|
||||
using PartSource.Services.Integrations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
@@ -38,74 +40,46 @@ namespace PartSource.Automation.Jobs
|
||||
try
|
||||
{
|
||||
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
|
||||
|
||||
if (importData?.LineCode == "SVG") // Headlights go in front, DUH
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<Fitment> fitments = GetPositionOrderedFitments(importData?.PartNumber, importData?.LineCode);
|
||||
IList<VehicleData> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
|
||||
if (vehicles.Count > 0)
|
||||
if (fitments.Count == 0 || vehicles.Count == 0)
|
||||
{
|
||||
bool isFitment = false;
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
await DeletePositionMetafields(product.Id);
|
||||
|
||||
string currentPosition = fitments[0].Position;
|
||||
List<int> vehicleIds = new List<int>();
|
||||
|
||||
foreach (Fitment fitment in fitments)
|
||||
{
|
||||
if (fitment.Position != currentPosition)
|
||||
{
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
currentPosition = fitment.Position;
|
||||
vehicleIds = new List<int>();
|
||||
}
|
||||
|
||||
// We don't need to DCF map because these are both sourced from WHI
|
||||
IList<int> fitmentVehicleIds = vehicles.Where(v => v.BaseVehicleId == fitment.BaseVehicleId && v.EngineConfigId == fitment.EngineConfigId)
|
||||
.Select(v => v.VehicleToEngineConfigId)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
vehicleIds.AddRange(fitmentVehicleIds);
|
||||
}
|
||||
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
@@ -132,5 +106,70 @@ namespace PartSource.Automation.Jobs
|
||||
IsSuccess = true
|
||||
};
|
||||
}
|
||||
|
||||
private IList<Fitment> GetPositionOrderedFitments(string partNumber, string lineCode)
|
||||
{
|
||||
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
IQueryable<string> whiCodes = _partSourceContext.DcfMappings
|
||||
.Where(d => d.LineCode == lineCode)
|
||||
.Select(d => d.WhiCode);
|
||||
|
||||
return _partSourceContext.Fitments
|
||||
.Where(f => f.PartNumber == partNumber && whiCodes.Contains(f.LineCode) && !string.IsNullOrEmpty(f.Position))
|
||||
.OrderBy(f => f.Position)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "It's a Shopify metafield key")]
|
||||
private async Task SavePositionMetafield(Product product, IList<int> vehicleIds, string position)
|
||||
{
|
||||
if (vehicleIds.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIds);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
// TODO: Logging
|
||||
return;
|
||||
}
|
||||
|
||||
string key = position.ToLowerInvariant().Replace(" ", "_");
|
||||
if (key.Length > 20)
|
||||
{
|
||||
key = key.Substring(0, 20);
|
||||
}
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "position",
|
||||
Key = key,
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
private async Task DeletePositionMetafields(long shopifyId)
|
||||
{
|
||||
IDictionary<string, object> parameters = new Dictionary<string, object>
|
||||
{
|
||||
{ "metafield[owner_id]", shopifyId},
|
||||
{ "metafield[owner_resource]", "product" },
|
||||
{ "namespace", "position" },
|
||||
};
|
||||
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(parameters);
|
||||
|
||||
foreach (Metafield metafield in metafields)
|
||||
{
|
||||
await _shopifyClient.Metafields.Delete(metafield);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,7 @@ namespace PartSource.Automation
|
||||
.AddSingleton<DeleteProducts>()
|
||||
.AddSingleton<UpdateFitment>()
|
||||
.AddSingleton<UpdatePricing>()
|
||||
.AddSingleton<UpdatePositioning>()
|
||||
.AddSingleton<ExecuteSsisPackages>()
|
||||
.AddSingleton<JobFactory>()
|
||||
.BuildServiceProvider();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"profiles": {
|
||||
"PartSource.Automation": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "UpdatePricing",
|
||||
"commandLineArgs": "UpdatePositioning",
|
||||
"environmentVariables": {
|
||||
"PS_AUTOMATION_ENVIRONMENT": "development"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user