120 lines
4.6 KiB
C#
120 lines
4.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using PartSource.Automation.Models.Jobs;
|
|
using PartSource.Automation.Services;
|
|
using PartSource.Data.Contexts;
|
|
using PartSource.Data.Models;
|
|
using Ratermania.Automation.Interfaces;
|
|
using Ratermania.Shopify;
|
|
using Ratermania.Shopify.Resources;
|
|
using Ratermania.Shopify.Resources.Enums;
|
|
using System;
|
|
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 UpdatePricing(ILogger<UpdatePricing> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient, EmailService emailService)
|
|
{
|
|
_logger = logger;
|
|
_partSourceContext = partSourceContext;
|
|
_shopifyClient = shopifyClient;
|
|
_emailService = emailService;
|
|
}
|
|
|
|
public async Task Run(CancellationToken token, params string[] arguments)
|
|
{
|
|
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();
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
|
|
|
|
throw;
|
|
}
|
|
|
|
int count = 0;
|
|
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 (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
product.Variants[i].Price = partPrice.Your_Price.Value;
|
|
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
|
|
|
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
|
|
product.PublishedScope = PublishedScope.Global;
|
|
|
|
try
|
|
{
|
|
|
|
await _shopifyClient.Metafields.Add(new Metafield
|
|
{
|
|
Namespace = "Pricing",
|
|
Key = "CorePrice",
|
|
Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
|
|
Type = "string",
|
|
OwnerResource = "product",
|
|
OwnerId = product.Id
|
|
});
|
|
|
|
await _shopifyClient.Products.Update(product);
|
|
|
|
_logger.LogInformation("Updated product id {productId}", product.Id);
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to update pricing for product ID {productId}", product.Id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
count += products.Count();
|
|
products = await _shopifyClient.Products.GetNext();
|
|
_logger.LogInformation($"Total updated: {count}");
|
|
}
|
|
|
|
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.");
|
|
}
|
|
}
|
|
}
|
|
} |