Automation and Shopify library updates
This commit is contained in:
@@ -13,161 +13,114 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class UpdatePricing : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdatePricing> _logger;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly EmailService _emailService;
|
||||
public class UpdatePricing : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdatePricing> _logger;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly EmailService _emailService;
|
||||
|
||||
public UpdatePricing(ILogger<UpdatePricing> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient, EmailService emailService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_emailService = emailService;
|
||||
}
|
||||
public UpdatePricing(ILogger<UpdatePricing> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient, EmailService emailService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_emailService = emailService;
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
List<UpdatePricingResult> pricingReport = new List<UpdatePricingResult>();
|
||||
IEnumerable<Product> products = null;
|
||||
IEnumerable<PartPrice> prices = null;
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
List<UpdatePricingResult> pricingReport = new List<UpdatePricingResult>();
|
||||
IEnumerable<Product> products = null;
|
||||
IEnumerable<PartPrice> prices = null;
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
prices = await _partSourceContext.PartPrices.AsNoTracking().ToListAsync();
|
||||
}
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
prices = await _partSourceContext.PartPrices.AsNoTracking().ToListAsync(token);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
|
||||
|
||||
throw;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
List<UpdatePricingResult> productPricingUpdate = new List<UpdatePricingResult>();
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
if (product.Variants.Length > 0)
|
||||
{
|
||||
for (int i = 0; i < product.Variants.Length; i++)
|
||||
{
|
||||
Variant variant = product.Variants[i];
|
||||
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
|
||||
|
||||
if (product.Variants.Length > 0)
|
||||
{
|
||||
bool hasUpdate = false;
|
||||
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0; i < product.Variants.Length; i++)
|
||||
{
|
||||
Variant variant = product.Variants[i];
|
||||
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
|
||||
product.Variants[i].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
|
||||
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
|
||||
product.PublishedScope = PublishedScope.Global;
|
||||
|
||||
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
|
||||
});
|
||||
try
|
||||
{
|
||||
await _shopifyClient.Metafields.Add(new Metafield
|
||||
{
|
||||
Namespace = "Pricing",
|
||||
Key = "CorePrice",
|
||||
Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
|
||||
Type = "single_line_text_field",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
});
|
||||
}
|
||||
|
||||
product.Variants[i].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to update core price metafield for product ID {product.Id}");
|
||||
}
|
||||
}
|
||||
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? DateTime.Now : null;
|
||||
product.PublishedScope = PublishedScope.Global;
|
||||
try
|
||||
{
|
||||
await _shopifyClient.Products.Update(product);
|
||||
}
|
||||
|
||||
//Metafield metafield = new Metafield
|
||||
//{
|
||||
// Namespace = "Pricing",
|
||||
// Key = "CorePrice",
|
||||
// Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
|
||||
// ValueType = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to update pricing for product ID {product.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hasUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasUpdate)
|
||||
{
|
||||
try
|
||||
{
|
||||
//await _shopifyClient.Metafields.Add(metafield);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
pricingReport.AddRange(productPricingUpdate);
|
||||
}
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to update pricing for product ID {product.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_logger.LogInformation($"Total updated: {pricingReport.Count}");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
_logger.LogInformation($"Total updated: {pricingReport.Count}");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
|
||||
_emailService.Send("Pricing Update Completed", $"The pricing update has completed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user