Finalized UpdatePositioning job
This commit is contained in:
@@ -12,36 +12,39 @@ namespace PartSource.Automation.Factories
|
|||||||
{
|
{
|
||||||
public class JobFactory
|
public class JobFactory
|
||||||
{
|
{
|
||||||
public IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
public JobFactory(IServiceProvider serviceProvider)
|
public JobFactory(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_serviceProvider = serviceProvider;
|
_serviceProvider = serviceProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAutomationJob Build(string jobName)
|
public IAutomationJob Build(string jobName)
|
||||||
{
|
{
|
||||||
switch (jobName)
|
switch (jobName)
|
||||||
{
|
{
|
||||||
case nameof(AddProducts):
|
case nameof(AddProducts):
|
||||||
return _serviceProvider.GetService<AddProducts>();
|
return _serviceProvider.GetService<AddProducts>();
|
||||||
|
|
||||||
case nameof(DeleteProducts):
|
case nameof(DeleteProducts):
|
||||||
return _serviceProvider.GetService<DeleteProducts>();
|
return _serviceProvider.GetService<DeleteProducts>();
|
||||||
|
|
||||||
case nameof(TestJob):
|
case nameof(TestJob):
|
||||||
return new TestJob();
|
return new TestJob();
|
||||||
|
|
||||||
case nameof(UpdateFitment):
|
case nameof(UpdateFitment):
|
||||||
return _serviceProvider.GetService<UpdateFitment>();
|
return _serviceProvider.GetService<UpdateFitment>();
|
||||||
|
|
||||||
case nameof(UpdatePricing):
|
case nameof(UpdatePricing):
|
||||||
return _serviceProvider.GetService<UpdatePricing>();
|
return _serviceProvider.GetService<UpdatePricing>();
|
||||||
|
|
||||||
case nameof(ExecuteSsisPackages):
|
case nameof(UpdatePositioning):
|
||||||
return _serviceProvider.GetService<ExecuteSsisPackages>();
|
return _serviceProvider.GetService<UpdatePositioning>();
|
||||||
|
|
||||||
default:
|
case nameof(ExecuteSsisPackages):
|
||||||
|
return _serviceProvider.GetService<ExecuteSsisPackages>();
|
||||||
|
|
||||||
|
default:
|
||||||
throw new Exception($"The job {jobName} could not be found.");
|
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);
|
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
|
||||||
IList<VehicleData> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
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);
|
bool isFitment = false;
|
||||||
if (vehicleIdFitment.Count > 0)
|
|
||||||
|
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||||
|
if (vehicleIdFitment.Count > 0)
|
||||||
|
{
|
||||||
|
isFitment = true;
|
||||||
|
|
||||||
|
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||||
|
if (json.Length >= 100000)
|
||||||
{
|
{
|
||||||
isFitment = true;
|
continue;
|
||||||
|
|
||||||
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);
|
Metafield vehicleMetafield = new Metafield
|
||||||
if (ymmFitment.Count > 0)
|
|
||||||
{
|
{
|
||||||
isFitment = true;
|
Namespace = "fitment",
|
||||||
|
Key = "ids",
|
||||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
Value = json,
|
||||||
if (json.Length >= 100000)
|
ValueType = "json_string",
|
||||||
{
|
|
||||||
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",
|
OwnerResource = "product",
|
||||||
OwnerId = product.Id
|
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
|
catch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ using PartSource.Services;
|
|||||||
using PartSource.Services.Integrations;
|
using PartSource.Services.Integrations;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PartSource.Automation.Jobs
|
namespace PartSource.Automation.Jobs
|
||||||
@@ -38,74 +40,46 @@ namespace PartSource.Automation.Jobs
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
|
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);
|
IList<VehicleData> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||||
|
|
||||||
if (vehicles.Count > 0)
|
if (fitments.Count == 0 || vehicles.Count == 0)
|
||||||
{
|
{
|
||||||
bool isFitment = false;
|
continue;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
catch
|
||||||
{
|
{
|
||||||
@@ -132,5 +106,70 @@ namespace PartSource.Automation.Jobs
|
|||||||
IsSuccess = true
|
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<DeleteProducts>()
|
||||||
.AddSingleton<UpdateFitment>()
|
.AddSingleton<UpdateFitment>()
|
||||||
.AddSingleton<UpdatePricing>()
|
.AddSingleton<UpdatePricing>()
|
||||||
|
.AddSingleton<UpdatePositioning>()
|
||||||
.AddSingleton<ExecuteSsisPackages>()
|
.AddSingleton<ExecuteSsisPackages>()
|
||||||
.AddSingleton<JobFactory>()
|
.AddSingleton<JobFactory>()
|
||||||
.BuildServiceProvider();
|
.BuildServiceProvider();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"profiles": {
|
"profiles": {
|
||||||
"PartSource.Automation": {
|
"PartSource.Automation": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"commandLineArgs": "UpdatePricing",
|
"commandLineArgs": "UpdatePositioning",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"PS_AUTOMATION_ENVIRONMENT": "development"
|
"PS_AUTOMATION_ENVIRONMENT": "development"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
using PartSource.Data;
|
using PartSource.Data;
|
||||||
using PartSource.Data.Dtos;
|
using PartSource.Data.Dtos;
|
||||||
using PartSource.Data.Models;
|
using PartSource.Data.Models;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace PartSource.Services
|
namespace PartSource.Services
|
||||||
{
|
{
|
||||||
|
[Obsolete]
|
||||||
public class PartService
|
public class PartService
|
||||||
{
|
{
|
||||||
private readonly PartSourceContext _context;
|
private readonly PartSourceContext _context;
|
||||||
|
|||||||
Reference in New Issue
Block a user