Stuff and things

This commit is contained in:
2021-06-29 19:00:13 -04:00
parent 0ff42f148a
commit 962ad3383f
22 changed files with 320 additions and 226 deletions

View File

@@ -1,9 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Ratermania.Automation.Interfaces;
using PartSource.Automation.Models;
using PartSource.Data;
using Microsoft.Extensions.Logging;
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;
@@ -11,8 +11,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Services;
namespace PartSource.Automation.Jobs
{
@@ -46,7 +44,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
throw;
}
@@ -56,35 +54,45 @@ namespace PartSource.Automation.Jobs
{
if (product.Variants.Length > 0)
{
Variant variant = product.Variants[0];
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
bool hasUpdate = false;
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
for (int i = 0; i < product.Variants.Length; i++)
{
continue;
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;
}
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"))
{
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;
//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
//};
hasUpdate = true;
}
}
if (product.Variants[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
if (hasUpdate)
{
product.Variants[0].Price = partPrice.Your_Price.Value;
product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
product.PublishedScope = PublishedScope.Global;
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
};
try
{
await _shopifyClient.Metafields.Add(metafield);
//await _shopifyClient.Metafields.Add(metafield);
await _shopifyClient.Products.Update(product);
updateCount++;
@@ -92,7 +100,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to update pricing for SKU {variant.Sku}");
_logger.LogWarning(ex, $"Failed to update pricing for product ID {product.Id}");
}
}
}
@@ -100,7 +108,7 @@ namespace PartSource.Automation.Jobs
try
{
products = await _shopifyClient.Products.GetNext();
products = await _shopifyClient.Products.GetNext();
_logger.LogInformation($"Total updated: {updateCount}");
}