Whatever this is
This commit is contained in:
89
PartSource.Automation/Jobs/PartsSync.cs
Normal file
89
PartSource.Automation/Jobs/PartsSync.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
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 PartsSync : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdatePricing> _logger;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
public PartsSync(ILogger<UpdatePricing> logger, FitmentContext fitmentContext, ShopifyClient shopifyClient)
|
||||
{
|
||||
_logger = logger;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
|
||||
foreach (Product product in products)
|
||||
{
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
Part part = new Part
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0")?.Value ?? string.Empty,
|
||||
PartNumber = metafields.FirstOrDefault(m => m.Key == "custom_label_1")?.Value ?? string.Empty,
|
||||
Sku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
|
||||
// part.PartNumber = part.PartNumber.Replace("-", string.Empty);
|
||||
|
||||
if (
|
||||
string.IsNullOrEmpty(part.LineCode)
|
||||
|| string.IsNullOrEmpty(part.PartNumber)
|
||||
|| int.TryParse(part.LineCode, out _)) //If the line code is numeric, it cannot have fitment data associated with it.
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Part? existing = await _fitmentContext.Parts.FirstOrDefaultAsync(p => p.Sku == part.Sku);
|
||||
if (existing == null)
|
||||
{
|
||||
await _fitmentContext.Parts.AddAsync(part);
|
||||
await _fitmentContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogInformation(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogInformation(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user