Changed a lot LOL
This commit is contained in:
@@ -30,7 +30,7 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
await SyncronizeIdsAndSkus();
|
||||
//await AddSkus();
|
||||
// await AddVariants();
|
||||
// await AddVariants();
|
||||
|
||||
return new AutomationJobResult
|
||||
{
|
||||
@@ -43,12 +43,13 @@ namespace PartSource.Automation.Jobs
|
||||
/// </summary>
|
||||
private async Task SyncronizeIdsAndSkus()
|
||||
{
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get();
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
|
||||
_partSourceContext.Database.ExecuteSqlCommand("UPDATE ImportData SET ShopifyId = NULL");
|
||||
//_partSourceContext.Database.ExecuteSqlCommand("UPDATE ImportData SET ShopifyId = NULL");
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
|
||||
foreach (Product product in products)
|
||||
{
|
||||
foreach (Variant variant in product.Variants)
|
||||
@@ -62,16 +63,19 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
}
|
||||
|
||||
await _partSourceContext.SaveChangesAsync();
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
await _partSourceContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
Console.WriteLine("Failed to save a batch of products");
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,7 +89,7 @@ namespace PartSource.Automation.Jobs
|
||||
.ToList();
|
||||
|
||||
// items = items.Where(i => i.Title == items.First().Title).ToList();
|
||||
//
|
||||
//
|
||||
foreach (ImportData importData in items)
|
||||
{
|
||||
try
|
||||
@@ -145,12 +149,12 @@ namespace PartSource.Automation.Jobs
|
||||
Title = importData.Title,
|
||||
Vendor = importData.Vendor,
|
||||
Tags = string.Join(",", productTags),
|
||||
Published = true,
|
||||
//ProductType = importData.FINELINE_NM,
|
||||
Images = productImages.ToArray(),
|
||||
//Variants = productVariants.ToArray(),
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedAt = DateTime.Now
|
||||
UpdatedAt = DateTime.Now,
|
||||
PublishedAt = DateTime.Now
|
||||
};
|
||||
|
||||
requestData = await _shopifyClient.Products.Add(requestData);
|
||||
@@ -259,12 +263,12 @@ namespace PartSource.Automation.Jobs
|
||||
Title = items[0].Title,
|
||||
Vendor = items[0].Vendor,
|
||||
Tags = string.Join(",", productTags),
|
||||
Published = true,
|
||||
//ProductType = importData.FINELINE_NM,
|
||||
Images = productImages.ToArray(),
|
||||
//Variants = productVariants.ToArray(),
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedAt = DateTime.Now
|
||||
UpdatedAt = DateTime.Now,
|
||||
PublishedAt = DateTime.Now
|
||||
};
|
||||
|
||||
requestData = await _shopifyClient.Products.Add(requestData);
|
||||
|
||||
@@ -19,6 +19,7 @@ using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using Ratermania.Shopify.Exceptions;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
@@ -40,29 +41,50 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
public async Task<AutomationJobResult> Run()
|
||||
{
|
||||
IList<ImportData> parts = _partSourceContext.ImportData
|
||||
.Where(p => p.UpdatedAt < DateTime.Now.AddDays(-3))
|
||||
.Take(50)
|
||||
.ToList();
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
while (parts != null && parts.Count > 0)
|
||||
try
|
||||
{
|
||||
foreach (ImportData importData in parts)
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO: Logging
|
||||
return new AutomationJobResult
|
||||
{
|
||||
Message = "Failed to get products from Shopify",
|
||||
IsSuccess = false
|
||||
};
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
try
|
||||
{
|
||||
Product product = await _shopifyClient.Products.GetById(importData.ShopifyId.GetValueOrDefault());
|
||||
if (product == null)
|
||||
ImportData importData = await _partSourceContext.ImportData
|
||||
.Where(i => i.ShopifyId == product.Id && i.UpdatedAt <= DateTime.Now.AddDays(-7))
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (importData == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
bool isFitment = false;
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode, 255);
|
||||
|
||||
if (vehicles.Count > 250)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
|
||||
if (vehicleIdFitment.Count > 0 && vehicleIdFitment.Count <= 250)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
@@ -121,11 +143,25 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
|
||||
//Metafield noteTextMetafield = new Metafield
|
||||
//{
|
||||
// Namespace = "Flags",
|
||||
// Key = "IsFitment",
|
||||
// Value = isFitment.ToString(),
|
||||
// ValueType = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
|
||||
//await _shopifyClient.Metafields.Add(noteTextMetafield);
|
||||
|
||||
|
||||
List<string> tags = new List<string>
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber
|
||||
};
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber
|
||||
};
|
||||
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
@@ -143,25 +179,32 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
|
||||
catch (ShopifyClientException ex)
|
||||
{
|
||||
// TODO: Log
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!ex.Message.Contains("response content", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
Console.WriteLine($"{importData.VariantSku}: {ex.Message}");
|
||||
}
|
||||
// TODO: Log
|
||||
}
|
||||
}
|
||||
|
||||
await _partSourceContext.SaveChangesAsync();
|
||||
try
|
||||
{
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
parts = _partSourceContext.ImportData
|
||||
.Where(p => p.UpdatedAt < DateTime.Now.AddDays(-3))
|
||||
.Take(50)
|
||||
.ToList();
|
||||
catch (Exception ex)
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -31,6 +31,10 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
public async Task<AutomationJobResult> Run()
|
||||
{
|
||||
IList<ImportData> importDataz = _partSourceContext.ImportData.ToList();
|
||||
|
||||
return new AutomationJobResult();
|
||||
|
||||
IDictionary<string, object> parameters = new Dictionary<string, object>
|
||||
{
|
||||
{ "limit", 250 }
|
||||
@@ -59,7 +63,7 @@ namespace PartSource.Automation.Jobs
|
||||
continue;
|
||||
}
|
||||
|
||||
await DeletePositionMetafields(product.Id);
|
||||
//await DeletePositionMetafields(product.Id);
|
||||
|
||||
string currentPosition = fitments[0].Position;
|
||||
List<int> vehicleIds = new List<int>();
|
||||
@@ -85,6 +89,8 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
|
||||
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Positioning";
|
||||
|
||||
@@ -169,22 +175,5 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using PartSource.Automation.Jobs.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using PartSource.Automation.Jobs.Interfaces;
|
||||
using PartSource.Automation.Models;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Models;
|
||||
@@ -27,11 +29,13 @@ namespace PartSource.Automation.Jobs
|
||||
public async Task<AutomationJobResult> Run()
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
IEnumerable<PartPrice> prices = null;
|
||||
int updateCount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get();
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
prices = await _partSourceContext.PartPrices.ToListAsync();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
@@ -50,8 +54,9 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
if (product.Variants.Length > 0)
|
||||
{
|
||||
|
||||
Variant variant = product.Variants[0];
|
||||
PartPrice partPrice = _partSourceContext.PartPrices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
|
||||
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
|
||||
|
||||
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
|
||||
{
|
||||
@@ -65,7 +70,8 @@ namespace PartSource.Automation.Jobs
|
||||
product.Variants[0].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
|
||||
product.PublishedAt = partPrice.Active.ToUpperInvariant() == "Y" ? DateTime.Now : default;
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
|
||||
product.PublishedScope = PublishedScope.Global;
|
||||
|
||||
Metafield metafield = new Metafield
|
||||
{
|
||||
@@ -81,29 +87,37 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
await _shopifyClient.Metafields.Add(metafield);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
|
||||
updateCount++;
|
||||
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Console.WriteLine("bad update");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
;
|
||||
Console.WriteLine("failed getting parts");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_partSourceContext.SaveChanges();
|
||||
// _partSourceContext.SaveChanges();
|
||||
|
||||
try
|
||||
{
|
||||
//await _shopifyClient.Products.SaveChanges();
|
||||
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
Console.SetCursorPosition(0, 2);
|
||||
Console.Clear();
|
||||
Console.Write($"Updated: {updateCount} ");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user