Scaffolded UpdatePositioning
This commit is contained in:
136
PartSource.Automation/Jobs/UpdatePositioning.cs
Normal file
136
PartSource.Automation/Jobs/UpdatePositioning.cs
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using PartSource.Automation.Jobs.Interfaces;
|
||||||
|
using PartSource.Automation.Models;
|
||||||
|
using PartSource.Data;
|
||||||
|
using PartSource.Data.Models;
|
||||||
|
using PartSource.Data.Shopify;
|
||||||
|
using PartSource.Services;
|
||||||
|
using PartSource.Services.Integrations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Jobs
|
||||||
|
{
|
||||||
|
public class UpdatePositioning : IAutomationJob
|
||||||
|
{
|
||||||
|
private readonly ShopifyClient _shopifyClient;
|
||||||
|
private readonly PartSourceContext _partSourceContext;
|
||||||
|
private readonly VehicleService _vehicleService;
|
||||||
|
|
||||||
|
public UpdatePositioning(PartSourceContext partSourceContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||||
|
{
|
||||||
|
_partSourceContext = partSourceContext;
|
||||||
|
_shopifyClient = shopifyClient;
|
||||||
|
_vehicleService = vehicleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<AutomationJobResult> Run()
|
||||||
|
{
|
||||||
|
IEnumerable<Product> products = await _shopifyClient.Products.Get();
|
||||||
|
|
||||||
|
while (products != null && products.Any())
|
||||||
|
{
|
||||||
|
foreach (Product product in products)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine(product.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.Write('.');
|
||||||
|
|
||||||
|
products = await _shopifyClient.Products.GetNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
products = await _shopifyClient.Products.GetNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AutomationJobResult
|
||||||
|
{
|
||||||
|
Message = "Fitment updated successfully",
|
||||||
|
IsSuccess = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -109,7 +109,6 @@ namespace PartSource.Services
|
|||||||
return fitmentTags;
|
return fitmentTags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IList<int> GetVehicleIdFitment(IList<VehicleData> vehicles)
|
public IList<int> GetVehicleIdFitment(IList<VehicleData> vehicles)
|
||||||
{
|
{
|
||||||
return vehicles.Select(v => v.VehicleToEngineConfigId).ToArray();
|
return vehicles.Select(v => v.VehicleToEngineConfigId).ToArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user