This is it. Don't get scared now. (Converted to Ratermania.Automation)
This commit is contained in:
324
PartSource.Automation/Jobs/Archive/AddAndUpdateProducts.cs
Normal file
324
PartSource.Automation/Jobs/Archive/AddAndUpdateProducts.cs
Normal file
@@ -0,0 +1,324 @@
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using Ratermania.Shopify;
|
||||
//using Ratermania.Shopify.Resources;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class AddAndUpdateProducts// : IAutomationJob
|
||||
// {
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
// public AddAndUpdateProducts(PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
||||
// {
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _shopifyClient = shopifyClient;
|
||||
// }
|
||||
|
||||
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// //throw new Exception("You need to add a ProductVariant resource to the Shopify client");
|
||||
|
||||
// await SyncronizeIdsAndSkus();
|
||||
// //await AddSkus();
|
||||
// // await AddVariants();
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// Ensures syncronization between Shopify IDs and Partsource SKUs
|
||||
// /// </summary>
|
||||
// private async Task SyncronizeIdsAndSkus()
|
||||
// {
|
||||
// IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
|
||||
// //_partSourceContext.Database.ExecuteSqlCommand("UPDATE ImportData SET ShopifyId = NULL");
|
||||
|
||||
// while (products != null && products.Any())
|
||||
// {
|
||||
|
||||
// foreach (Product product in products)
|
||||
// {
|
||||
// foreach (Variant variant in product.Variants)
|
||||
// {
|
||||
// ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == variant.Sku);
|
||||
|
||||
// if (importData != null)
|
||||
// {
|
||||
// importData.ShopifyId = product.Id;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// try
|
||||
// {
|
||||
// await _partSourceContext.SaveChangesAsync();
|
||||
// }
|
||||
|
||||
// catch
|
||||
// {
|
||||
// Console.WriteLine("Failed to save a batch of products");
|
||||
// }
|
||||
|
||||
// finally
|
||||
// {
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public async Task AddSkus()
|
||||
// {
|
||||
// IList<ImportData> items = _partSourceContext.ImportData
|
||||
// .Where(i => i.IsVariant == null && i.ShopifyId == null)
|
||||
// // .OrderBy(i => i.Title)
|
||||
// .ToList();
|
||||
|
||||
// // items = items.Where(i => i.Title == items.First().Title).ToList();
|
||||
// //
|
||||
// foreach (ImportData importData in items)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// // Images
|
||||
// IList<ProductImage> productImages = new List<ProductImage>();
|
||||
|
||||
// if (!string.IsNullOrEmpty(importData.ImageSrc))
|
||||
// {
|
||||
// foreach (string src in importData?.ImageSrc.Split(','))
|
||||
// {
|
||||
// productImages.Add(new ProductImage
|
||||
// {
|
||||
// Src = src,
|
||||
// Alt = importData.ImageAltText
|
||||
// });
|
||||
// }
|
||||
|
||||
// if (productImages.Count > 0)
|
||||
// {
|
||||
// productImages.Add(new ProductImage
|
||||
// {
|
||||
// Src = "https://cdn.shopify.com/s/files/1/2239/4255/files/No_Image_Found.jpg",
|
||||
// Alt = "No Image Found"
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Product Tags
|
||||
// List<string> productTags = new List<string>
|
||||
// {
|
||||
// items[0].LineCode,
|
||||
// items[0].PartNumber,
|
||||
// };
|
||||
|
||||
// //List<ProductVariant> productVariants = new List<ProductVariant>();
|
||||
|
||||
// ////foreach (ImportData itemVariant in items)
|
||||
// ////{
|
||||
// //productVariants.Add(new ProductVariant
|
||||
// //{
|
||||
// // InventoryPolicy = "Deny",
|
||||
// // CompareAtPrice = importData.CompareAt,
|
||||
// // Price = importData.Price,
|
||||
// // Sku = importData.VariantSku,
|
||||
// // Title = importData.VariantTitle ?? importData.Title,
|
||||
// // Option1 = importData.VariantTitle,
|
||||
// // RequiresShipping = false,
|
||||
// //});
|
||||
// //}
|
||||
|
||||
|
||||
|
||||
// Product requestData = new Product
|
||||
// {
|
||||
// BodyHtml = importData.BodyHtml,
|
||||
// Title = importData.Title,
|
||||
// Vendor = importData.Vendor,
|
||||
// Tags = string.Join(",", productTags),
|
||||
// //ProductType = importData.FINELINE_NM,
|
||||
// Images = productImages.ToArray(),
|
||||
// //Variants = productVariants.ToArray(),
|
||||
// CreatedAt = DateTime.Now,
|
||||
// UpdatedAt = DateTime.Now,
|
||||
// PublishedAt = DateTime.Now
|
||||
// };
|
||||
|
||||
// requestData = await _shopifyClient.Products.Add(requestData);
|
||||
|
||||
// if (requestData.Id > 0)
|
||||
// {
|
||||
// //foreach (ImportData variant in items)
|
||||
// //{
|
||||
// // variant.ShopifyId = requestData.Id;
|
||||
// //}
|
||||
|
||||
// importData.ShopifyId = requestData.Id;
|
||||
// _partSourceContext.SaveChanges();
|
||||
|
||||
|
||||
// Console.WriteLine($"{importData.VariantSku}");
|
||||
// }
|
||||
|
||||
// else
|
||||
// {
|
||||
// Console.WriteLine($"SHOPIFY ID WAS 0 - {importData.VariantSku}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"Failed to add SKU {importData.VariantSku}: {ex.StackTrace}");
|
||||
// }
|
||||
|
||||
// //items = _partSourceContext.ImportData
|
||||
// // .Where(i => i.Title == _partSourceContext.ImportData.First(d => d.ShopifyId == null).Title)
|
||||
// // .ToList();
|
||||
// }
|
||||
// }
|
||||
|
||||
// public async Task AddVariants()
|
||||
// {
|
||||
// IList<ImportData> items = _partSourceContext.ImportData
|
||||
// .Where(i => i.IsVariant.Value && i.ShopifyId == null)
|
||||
// .OrderBy(i => i.Title)
|
||||
// .ToList();
|
||||
|
||||
// items = items.Where(i => i.Title == items.First().Title).ToList();
|
||||
|
||||
// while (items != null && items.Count > 0)
|
||||
// {
|
||||
// // Images
|
||||
// IList<ProductImage> productImages = new List<ProductImage>();
|
||||
|
||||
// if (!string.IsNullOrEmpty(items[0].ImageSrc))
|
||||
// {
|
||||
// productImages = items.SelectMany(v =>
|
||||
// {
|
||||
// IList<ProductImage> images = new List<ProductImage>();
|
||||
|
||||
// foreach (string src in v.ImageSrc?.Split(','))
|
||||
// {
|
||||
// images.Add(new ProductImage
|
||||
// {
|
||||
// Src = src,
|
||||
// Alt = v.ImageAltText
|
||||
// });
|
||||
// }
|
||||
|
||||
// return images;
|
||||
// }).ToList();
|
||||
|
||||
// if (productImages.Count > 0)
|
||||
// {
|
||||
// productImages.Add(new ProductImage
|
||||
// {
|
||||
// Src = "https://cdn.shopify.com/s/files/1/2239/4255/files/No_Image_Found.jpg",
|
||||
// Alt = "No Image Found"
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// // Product Tags
|
||||
// List<string> productTags = new List<string>
|
||||
// {
|
||||
// items[0].LineCode,
|
||||
// items[0].PartNumber,
|
||||
// };
|
||||
|
||||
// //List<ProductVariant> productVariants = new List<ProductVariant>();
|
||||
|
||||
// //foreach (ImportData itemVariant in items)
|
||||
// //{
|
||||
// // productVariants.Add(new ProductVariant
|
||||
// // {
|
||||
// // InventoryPolicy = "Deny",
|
||||
// // CompareAtPrice = itemVariant.CompareAt,
|
||||
// // Price = itemVariant.Price,
|
||||
// // Sku = itemVariant.VariantSku,
|
||||
// // Title = itemVariant.VariantTitle,
|
||||
// // Option1 = itemVariant.VariantTitle,
|
||||
// // RequiresShipping = false,
|
||||
// // });
|
||||
// //}
|
||||
|
||||
|
||||
// Product requestData = new Product
|
||||
// {
|
||||
// BodyHtml = items[0].BodyHtml,
|
||||
// Title = items[0].Title,
|
||||
// Vendor = items[0].Vendor,
|
||||
// Tags = string.Join(",", productTags),
|
||||
// //ProductType = importData.FINELINE_NM,
|
||||
// Images = productImages.ToArray(),
|
||||
// //Variants = productVariants.ToArray(),
|
||||
// CreatedAt = DateTime.Now,
|
||||
// UpdatedAt = DateTime.Now,
|
||||
// PublishedAt = DateTime.Now
|
||||
// };
|
||||
|
||||
// requestData = await _shopifyClient.Products.Add(requestData);
|
||||
|
||||
// if (requestData.Id > 0)
|
||||
// {
|
||||
// foreach (ImportData variant in items)
|
||||
// {
|
||||
// variant.ShopifyId = requestData.Id;
|
||||
// }
|
||||
|
||||
// _partSourceContext.SaveChanges();
|
||||
|
||||
// Console.WriteLine($"{items[0].VariantSku}");
|
||||
// }
|
||||
|
||||
// else
|
||||
// {
|
||||
// Console.WriteLine($"SHOPIFY ID WAS 0 - {items[0].VariantSku}");
|
||||
// }
|
||||
|
||||
// ImportData next = _partSourceContext.ImportData.FirstOrDefault(i => i.IsVariant != null && i.ShopifyId == null);
|
||||
|
||||
// if (next != null)
|
||||
// {
|
||||
// items = _partSourceContext.ImportData
|
||||
// .Where(i => i.Title == next.Title)
|
||||
// .ToList();
|
||||
// }
|
||||
|
||||
// else
|
||||
// {
|
||||
// items = null;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// //private void Log(string message)
|
||||
// //{
|
||||
// // try
|
||||
// // {
|
||||
// // using (FileStream fileStream = File.OpenWrite(@"C:\users\tommy\desktop\log.txt"))
|
||||
// // {
|
||||
// // fileStream.Write(Encoding.UTF8.GetBytes(message + "\n"));
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // catch
|
||||
// // {
|
||||
// // // LOL Fix this
|
||||
// // Log(message);
|
||||
// // }
|
||||
// //}
|
||||
//}
|
||||
320
PartSource.Automation/Jobs/Archive/BuildVehicleCache.cs
Normal file
320
PartSource.Automation/Jobs/Archive/BuildVehicleCache.cs
Normal file
@@ -0,0 +1,320 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using PartSource.Automation.Jobs.Interfaces;
|
||||
//using PartSource.Automation.Services;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using PartSource.Data.Nexpart;
|
||||
//using PartSource.Services;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
|
||||
//using BaseVehicle = PartSource.Data.Models.BaseVehicle;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class BuildVehicleCache : IAutomationJob
|
||||
// {
|
||||
// private readonly NexpartService _nexpartService;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
|
||||
// private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
// public BuildVehicleCache(IServiceProvider serviceProvider, PartSourceContext partSourceContext, NexpartService nexpartService)
|
||||
// {
|
||||
// _nexpartService = nexpartService;
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _serviceProvider = serviceProvider;
|
||||
// }
|
||||
|
||||
// public void Run()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// //AddMakes();
|
||||
// //AddModels();
|
||||
|
||||
// // AddEngines();
|
||||
|
||||
// //AddYearMakeModels();
|
||||
|
||||
// AddSubmodels();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// ;
|
||||
// }
|
||||
// }
|
||||
|
||||
// private void AddEngines()
|
||||
// {
|
||||
// IList<BaseVehicle> baseVehicles = _partSourceContext.BaseVehicles.ToList();
|
||||
|
||||
// foreach (BaseVehicle baseVehicle in baseVehicles)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// WHIEngineSearch whiEngineSearch = new WHIEngineSearch
|
||||
// {
|
||||
// VehicleIdentifier = new VehicleIdentifier
|
||||
// {
|
||||
// BaseVehicleId = baseVehicle.Id
|
||||
// }
|
||||
// };
|
||||
|
||||
// WHIEngineSearchResponse response = _nexpartService.SendRequest<WHIEngineSearch, WHIEngineSearchResponse>(whiEngineSearch).Result;
|
||||
|
||||
// foreach (WHIEngine engine in response.ResponseBody.WHIEngine)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
|
||||
// if (!_partSourceContext.Engines.Any(e => e.Id == engine.Id))
|
||||
// {
|
||||
// _partSourceContext.Engines.Add(new Data.Models.Engine
|
||||
// {
|
||||
// Id = engine.Id,
|
||||
// Description = engine.Description
|
||||
// });
|
||||
// }
|
||||
|
||||
|
||||
// _partSourceContext.SaveChanges();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"Failed to add engine { engine.Id } - { ex.Message }");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"Failed to add engines for base vehicle { baseVehicle.Id } - { ex.Message }");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// private void AddSubmodels()
|
||||
// {
|
||||
// int maxBaseVehicleId = _partSourceContext.Submodels.Any()
|
||||
// ? _partSourceContext.Submodels.Max(s => s.BaseVehicleId)
|
||||
// : 0;
|
||||
|
||||
// IList<BaseVehicle> baseVehicles = _partSourceContext.BaseVehicles.AsNoTracking().Where(b => b.Id > maxBaseVehicleId).ToList();
|
||||
|
||||
// foreach (BaseVehicle baseVehicle in baseVehicles)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// SubModelSearch smSearch = new SubModelSearch()
|
||||
// {
|
||||
// MakeId = baseVehicle.VehicleMakeId,
|
||||
// ModelId = baseVehicle.VehicleModelId,
|
||||
// Year = baseVehicle.Year,
|
||||
// RegionId = 2
|
||||
// };
|
||||
// SubModelSearchResponse smResponse = _nexpartService.SendRequest<SubModelSearch, SubModelSearchResponse>(smSearch).Result;
|
||||
|
||||
// SubModel[] subModels = smResponse.ResponseBody?.SubModel;
|
||||
// if (subModels == null)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// foreach (SubModel submodel in subModels)
|
||||
// {
|
||||
// EngineSearch requestContent = new EngineSearch()
|
||||
// {
|
||||
// VehicleIdentifier = new VehicleIdentifier()
|
||||
// {
|
||||
// BaseVehicleId = baseVehicle.Id
|
||||
// },
|
||||
// SubModelId = int.Parse(submodel.Id)
|
||||
// };
|
||||
// EngineSearchResponse response = _nexpartService.SendRequest<EngineSearch, EngineSearchResponse>(requestContent).Result;
|
||||
|
||||
// if (response.ResponseBody == null)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// foreach (Data.Nexpart.Engine engine in response.ResponseBody.Engine)
|
||||
// {
|
||||
// VehicleIdSearch vidSearch = new VehicleIdSearch
|
||||
// {
|
||||
// VehicleIdentifier = new VehicleIdentifier()
|
||||
// {
|
||||
// BaseVehicleId = baseVehicle.Id,
|
||||
// EngineConfigId = engine.Id
|
||||
// },
|
||||
// Criterion = new Criterion[]
|
||||
// {
|
||||
// new Criterion
|
||||
// {
|
||||
// Attribute = "SUB_MODEL",
|
||||
// Id = int.Parse(submodel.Id)
|
||||
// }
|
||||
// },
|
||||
// RegionId = new RegionId
|
||||
// {
|
||||
// Value = 2
|
||||
// },
|
||||
// ResultOption = new ResultOption[]
|
||||
// {
|
||||
// new ResultOption
|
||||
// {
|
||||
// Value = "WHI_ENGINE"
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
|
||||
// VehicleIdSearchResponse vidResponse = _nexpartService.SendRequest<VehicleIdSearch, VehicleIdSearchResponse>(vidSearch).Result;
|
||||
|
||||
// if (vidResponse != null && vidResponse.ResponseBody.VehicleToEngineConfigId > 0)
|
||||
// {
|
||||
// _partSourceContext.Submodels.Add(new Submodel
|
||||
// {
|
||||
// VehicleToEngineConfigId = vidResponse.ResponseBody.VehicleToEngineConfigId,
|
||||
// SubmodelId = int.Parse(submodel.Id),
|
||||
// BaseVehicleId = baseVehicle.Id,
|
||||
// EngineId = vidResponse.ResponseBody.WHIEngineId
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// _partSourceContext.SaveChanges();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"Failed to add BaseVehicleId {baseVehicle.Id}: {ex.Message}");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// private void AddYearMakeModels()
|
||||
// {
|
||||
// BaseVehicleSearch request = new BaseVehicleSearch
|
||||
// {
|
||||
// Years = new Years
|
||||
// {
|
||||
// From = 1950,
|
||||
// To = 2020
|
||||
// },
|
||||
// Region = new[]
|
||||
// {
|
||||
// new Region
|
||||
// {
|
||||
// Id = 2
|
||||
// }
|
||||
// },
|
||||
// VehicleType = new[]
|
||||
// {
|
||||
// new VehicleType
|
||||
// {
|
||||
// Id = 5
|
||||
// },
|
||||
// new VehicleType
|
||||
// {
|
||||
// Id = 6
|
||||
// },
|
||||
// new VehicleType
|
||||
// {
|
||||
// Id = 7
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
|
||||
// BaseVehicleSearchResponse response = _nexpartService.SendRequest<BaseVehicleSearch, BaseVehicleSearchResponse>(request).Result;
|
||||
|
||||
// foreach (Data.Nexpart.BaseVehicle vehicle in response.ResponseBody.BaseVehicle)
|
||||
// {
|
||||
// _partSourceContext.BaseVehicles.Add(new Data.Models.BaseVehicle
|
||||
// {
|
||||
// Id = (int)vehicle.BaseVehicleId,
|
||||
// VehicleMakeId = (int)vehicle.MakeId,
|
||||
// VehicleModelId = (int)vehicle.ModelId,
|
||||
// Year = (int)vehicle.Year
|
||||
// });
|
||||
// }
|
||||
|
||||
// _partSourceContext.SaveChanges();
|
||||
// }
|
||||
|
||||
// private void AddMakes()
|
||||
// {
|
||||
// MakeSearch requestContent = new MakeSearch()
|
||||
// {
|
||||
// VehicleTypeId = new int[] { 5, 6, 7 },
|
||||
// RegionId = new int[] { 2 }
|
||||
// };
|
||||
|
||||
// MakeSearchResponse response = _nexpartService.SendRequest<MakeSearch, MakeSearchResponse>(requestContent).Result;
|
||||
|
||||
// foreach (Make make in response.ResponseBody.Make)
|
||||
// {
|
||||
// _partSourceContext.VehicleMakes.Add(new VehicleMake
|
||||
// {
|
||||
// Id = make.Id,
|
||||
// Name = make.Value
|
||||
// });
|
||||
// }
|
||||
|
||||
// _partSourceContext.SaveChanges();
|
||||
// }
|
||||
|
||||
// private void AddModels()
|
||||
// {
|
||||
// IList<VehicleMake> vehicleMakes = _partSourceContext.VehicleMakes.ToList();
|
||||
// IDictionary<int, VehicleModel> vehicleModels = new Dictionary<int, VehicleModel>();
|
||||
|
||||
// foreach (VehicleMake vehicleMake in vehicleMakes)
|
||||
// {
|
||||
// for (int year = 1950; year <= 2020; year++)
|
||||
// {
|
||||
// ModelSearch requestContent = new ModelSearch()
|
||||
// {
|
||||
// MakeId = vehicleMake.Id,
|
||||
// Year = year,
|
||||
// VehicleTypeId = new int[] { 5, 6, 7 },
|
||||
// RegionId = new int[] { 2 }
|
||||
// };
|
||||
|
||||
// ModelSearchResponse response = _nexpartService.SendRequest<ModelSearch, ModelSearchResponse>(requestContent).Result;
|
||||
|
||||
// if (response.ResponseBody == null || response.ResponseBody.Length == 0)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// foreach (Model model in response.ResponseBody[0].Model)
|
||||
// {
|
||||
// bool result = vehicleModels.TryGetValue(model.Id, out VehicleModel throwaway);
|
||||
|
||||
// if (!result)
|
||||
// {
|
||||
// VehicleModel vehicleModel = new VehicleModel
|
||||
// {
|
||||
// Id = model.Id,
|
||||
// Name = model.Value,
|
||||
// VehicleMakeId = vehicleMake.Id
|
||||
// };
|
||||
|
||||
// vehicleModels.Add(model.Id, vehicleModel);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// _partSourceContext.VehicleModels.AddRange(vehicleModels.Values);
|
||||
// _partSourceContext.SaveChanges();
|
||||
// }
|
||||
|
||||
|
||||
// }
|
||||
//}
|
||||
72
PartSource.Automation/Jobs/Archive/DeleteProducts.cs
Normal file
72
PartSource.Automation/Jobs/Archive/DeleteProducts.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Data;
|
||||
//using Ratermania.Shopify;
|
||||
//using Ratermania.Shopify.Resources;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class DeleteProducts //: IAutomationJob
|
||||
// {
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
|
||||
// public DeleteProducts(ShopifyClient shopifyClient, PartSourceContext partSourceContext)
|
||||
// {
|
||||
// _shopifyClient = shopifyClient;
|
||||
// _partSourceContext = partSourceContext;
|
||||
// }
|
||||
|
||||
// // If this job fails, oh well. Run it again and again until it works, or use the Shopify UI (LOL)
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// Console.WriteLine("This job will delete ALL PRODUCTS from Shopify. If you really want to delete EVERY SINGLE PRODUCT, type 'mechanical keyboard' below.");
|
||||
// string input = Console.ReadLine();
|
||||
|
||||
// if (input != "mechanical keyboard")
|
||||
// {
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
|
||||
// IList<long?> shopifyIds = _partSourceContext.ImportData
|
||||
// .Select(i => i.ShopifyId)
|
||||
// .Distinct()
|
||||
// .ToList();
|
||||
|
||||
// foreach (long? id in shopifyIds)
|
||||
// {
|
||||
// Product product = await _shopifyClient.Products.GetById((long)id);
|
||||
|
||||
// await _shopifyClient.Products.Delete(product);
|
||||
|
||||
// Console.WriteLine(id);
|
||||
// }
|
||||
|
||||
// //IEnumerable<Product> products = await _shopifyClient.Products.Get();
|
||||
|
||||
// //while (products != null)
|
||||
// //{
|
||||
// // foreach (Product product in products)
|
||||
// // {
|
||||
// // bool result = await _shopifyClient.Products.Delete(product);
|
||||
// // }
|
||||
|
||||
// // products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
// // Console.Write('.');
|
||||
// //}
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// Message = "All products deleted. Don't forget to truncate the ImportData table",
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
33
PartSource.Automation/Jobs/Archive/StatusCheck.cs
Normal file
33
PartSource.Automation/Jobs/Archive/StatusCheck.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Automation.Services;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class StatusCheck //: IAutomationJob
|
||||
// {
|
||||
// private readonly string[] phoneNumbers = { "8593609107", "5134008303" };
|
||||
// private readonly EmailService _emailService;
|
||||
|
||||
// public StatusCheck(EmailService emailService)
|
||||
// {
|
||||
// _emailService = emailService;
|
||||
// }
|
||||
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// foreach (string phoneNumber in phoneNumbers)
|
||||
// {
|
||||
// // TODO: One day it won't just be AT&T numbers
|
||||
// string to = $"{phoneNumber}@txt.att.net";
|
||||
|
||||
// _emailService.Send(to, string.Empty, "The Partsource automation server is running. Check the server for more details.");
|
||||
// }
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
246
PartSource.Automation/Jobs/Archive/UpdateFitment - Copy.cs
Normal file
246
PartSource.Automation/Jobs/Archive/UpdateFitment - Copy.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Newtonsoft.Json;
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using PartSource.Services;
|
||||
//using Ratermania.Shopify;
|
||||
//using Ratermania.Shopify.Exceptions;
|
||||
//using Ratermania.Shopify.Resources;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Data;
|
||||
//using System.Linq;
|
||||
//using System.Text.RegularExpressions;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class UpdateFitment //: IAutomationJob
|
||||
// {
|
||||
// private readonly IServiceProvider _serviceProvider;
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
// private readonly NexpartService _nexpartService;
|
||||
|
||||
// private readonly VehicleService _vehicleService;
|
||||
|
||||
// public UpdateFitment(IServiceProvider serviceProvider, PartSourceContext partSourceContext, ShopifyClient shopifyClient, NexpartService nexpartService, VehicleService vehicleService)
|
||||
// {
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _shopifyClient = shopifyClient;
|
||||
// _vehicleService = vehicleService;
|
||||
// }
|
||||
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// IEnumerable<Product> products = null;
|
||||
|
||||
// try
|
||||
// {
|
||||
// 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
|
||||
// {
|
||||
// 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, 255);
|
||||
|
||||
// if (vehicles.Count > 250)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
// if (vehicleIdFitment.Count > 0 && vehicleIdFitment.Count <= 250)
|
||||
// {
|
||||
// isFitment = true;
|
||||
|
||||
// string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Metafield vehicleMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "ids",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
// }
|
||||
|
||||
// IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
// if (ymmFitment.Count > 0)
|
||||
// {
|
||||
// isFitment = true;
|
||||
|
||||
// string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Metafield ymmMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "seo",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
// }
|
||||
|
||||
// Metafield isFitmentMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "Flags",
|
||||
// Key = "IsFitment",
|
||||
// Value = isFitment.ToString(),
|
||||
// ValueType = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// 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
|
||||
// };
|
||||
|
||||
// for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
// {
|
||||
// tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
// }
|
||||
|
||||
// tags.AddRange(ymmFitment);
|
||||
|
||||
// if (tags.Count > 250)
|
||||
// {
|
||||
// tags = tags.Take(250).ToList();
|
||||
// }
|
||||
|
||||
// product.Tags = string.Join(',', tags);
|
||||
|
||||
// await _shopifyClient.Products.Update(product);
|
||||
|
||||
// importData.IsFitment = isFitment;
|
||||
// importData.UpdatedAt = DateTime.Now;
|
||||
// importData.UpdateType = "Fitment";
|
||||
// }
|
||||
|
||||
// catch (ShopifyClientException ex)
|
||||
// {
|
||||
// // TODO: Log
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// // TODO: Log
|
||||
// }
|
||||
// }
|
||||
|
||||
// try
|
||||
// {
|
||||
// _partSourceContext.SaveChanges();
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
|
||||
// private async Task DeleteFitmentMetafields(long shopifyId)
|
||||
// {
|
||||
// IDictionary<string, object> parameters = new Dictionary<string, object>
|
||||
// {
|
||||
// { "metafield[owner_id]", shopifyId},
|
||||
// { "metafield[owner_resource]", "product" },
|
||||
// { "namespace", "fitment" },
|
||||
// };
|
||||
|
||||
// IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(parameters);
|
||||
|
||||
// foreach (Metafield metafield in metafields)
|
||||
// {
|
||||
// await _shopifyClient.Metafields.Delete(metafield);
|
||||
// }
|
||||
// }
|
||||
|
||||
// public IList<Vehicle> GetVehicles(string partNumber, string lineCode)
|
||||
// {
|
||||
// partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
// //string sql = $"select distinct BaseVehicleId, EngineConfigId from dbo.Fitment where ManufacturerCode in (select WhiCode from DcfMapping where PartSourceCode='{lineCode}') and (partNumber = '{partNumber}' or partNumber = '{partNumber.Replace("-", string.Empty)}')";
|
||||
// string sql = $"with FitmentIds (BaseVehicleId, EngineConfigId) as (select distinct BaseVehicleId, EngineConfigId from dbo.Fitment where LineCode in (select WhiCode from DcfMapping where LineCode='{lineCode}') and PartNumber = '{partNumber}') select v.* from VehicleData v join FitmentIds f on v.BaseVehicleId = f.BaseVehicleId and v.EngineConfigId = f.EngineConfigId;";
|
||||
|
||||
//#pragma warning disable EF1000 // Possible SQL injection vulnerability.
|
||||
// IList<Vehicle> vehicles = _partSourceContext.Vehicles.FromSql(sql).ToList();
|
||||
//#pragma warning restore EF1000 // Possible SQL injection vulnerability.
|
||||
|
||||
// return vehicles;
|
||||
// }
|
||||
// private void Update()
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
200
PartSource.Automation/Jobs/Archive/UpdateFitment.cs
Normal file
200
PartSource.Automation/Jobs/Archive/UpdateFitment.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Newtonsoft.Json;
|
||||
//using PartSource.Automation.Jobs.Interfaces;
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Automation.Services;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using PartSource.Data.Nexpart;
|
||||
//using PartSource.Services;
|
||||
//using PartSource.Services.Integrations;
|
||||
//using System;
|
||||
//using System.Collections.Concurrent;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Data;
|
||||
//using System.Data.SqlClient;
|
||||
//using System.IO;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Text.RegularExpressions;
|
||||
//using System.Threading.Tasks;
|
||||
//using Ratermania.Shopify.Entities;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class UpdateFitment : IAutomationJob
|
||||
// {
|
||||
// private readonly IServiceProvider _serviceProvider;
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
// private readonly NexpartService _nexpartService;
|
||||
|
||||
// private readonly VehicleService _vehicleService;
|
||||
|
||||
// public UpdateFitment(IServiceProvider serviceProvider, PartSourceContext partSourceContext, ShopifyClient shopifyClient, NexpartService nexpartService, VehicleService vehicleService)
|
||||
// {
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _shopifyClient = shopifyClient;
|
||||
// _vehicleService = vehicleService;
|
||||
// }
|
||||
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// IEnumerable<Product> products = await _shopifyClient.Products.Get();
|
||||
|
||||
// int i = 0;
|
||||
|
||||
// while (products != null && products.Any())
|
||||
// {
|
||||
// foreach (Product product in products)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku && i.UpdatedAt == null);
|
||||
// IList<VehicleData> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
|
||||
// if (vehicles == null || vehicles.Count == 0)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// await DeleteFitmentMetafields(product.Id);
|
||||
|
||||
// bool isFitment = false;
|
||||
|
||||
// IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
// if (vehicleIdFitment.Count > 0)
|
||||
// {
|
||||
// isFitment = true;
|
||||
|
||||
// string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Metafield vehicleMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "ids",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
// }
|
||||
|
||||
// IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
// if (ymmFitment.Count > 0)
|
||||
// {
|
||||
// isFitment = true;
|
||||
|
||||
// string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Metafield ymmMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "seo",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
// }
|
||||
|
||||
// Metafield isFitmentMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "Flags",
|
||||
// Key = "IsFitment",
|
||||
// Value = isFitment.ToString(),
|
||||
// ValueType = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
// importData.UpdatedAt = DateTime.Now;
|
||||
// importData.UpdateType = "Fitment";
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"{product.Id}: {ex.Message}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// try
|
||||
// {
|
||||
// i++;
|
||||
// Console.WriteLine(i);
|
||||
|
||||
// await _partSourceContext.SaveChangesAsync();
|
||||
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// i++;
|
||||
// Console.WriteLine(i);
|
||||
|
||||
// Console.WriteLine($"Retrying request: {ex.Message}");
|
||||
|
||||
// await _partSourceContext.SaveChangesAsync();
|
||||
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
|
||||
// private async Task DeleteFitmentMetafields(long shopifyId)
|
||||
// {
|
||||
// IDictionary<string, object> parameters = new Dictionary<string, object>
|
||||
// {
|
||||
// { "metafield[owner_id]", shopifyId},
|
||||
// { "metafield[owner_resource]", "product" },
|
||||
// { "namespace", "fitment" },
|
||||
// };
|
||||
|
||||
// IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(parameters);
|
||||
|
||||
// foreach (Metafield metafield in metafields)
|
||||
// {
|
||||
// await _shopifyClient.Metafields.Delete(metafield);
|
||||
// }
|
||||
// }
|
||||
|
||||
// public IList<VehicleData> GetVehicles(string partNumber, string lineCode)
|
||||
// {
|
||||
// partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
// //string sql = $"select distinct BaseVehicleId, EngineConfigId from dbo.Fitment where ManufacturerCode in (select WhiCode from DcfMapping where PartSourceCode='{lineCode}') and (partNumber = '{partNumber}' or partNumber = '{partNumber.Replace("-", string.Empty)}')";
|
||||
// string sql = $"with FitmentIds (BaseVehicleId, EngineConfigId) as (select distinct BaseVehicleId, EngineConfigId from dbo.Fitment where LineCode in (select WhiCode from DcfMapping where LineCode='{lineCode}') and PartNumber = '{partNumber}') select v.* from VehicleData v join FitmentIds f on v.BaseVehicleId = f.BaseVehicleId and v.EngineConfigId = f.EngineConfigId;";
|
||||
|
||||
//#pragma warning disable EF1000 // Possible SQL injection vulnerability.
|
||||
// IList<VehicleData> vehicles = _partSourceContext.VehicleData.FromSql(sql).ToList();
|
||||
//#pragma warning restore EF1000 // Possible SQL injection vulnerability.
|
||||
|
||||
// return vehicles;
|
||||
// }
|
||||
// private void Update()
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
209
PartSource.Automation/Jobs/Archive/UpdatePositioning.cs
Normal file
209
PartSource.Automation/Jobs/Archive/UpdatePositioning.cs
Normal file
@@ -0,0 +1,209 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Newtonsoft.Json;
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using PartSource.Services;
|
||||
//using Ratermania.Shopify;
|
||||
//using Ratermania.Shopify.Resources;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Diagnostics.CodeAnalysis;
|
||||
//using System.Linq;
|
||||
//using System.Text.RegularExpressions;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class UpdatePositioning// : IAutomationJob
|
||||
// {
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
// private readonly VehicleService _vehicleService;
|
||||
|
||||
// public UpdatePositioning(PartSourceContext partSourceContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
// {
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _shopifyClient = shopifyClient;
|
||||
// _vehicleService = vehicleService;
|
||||
// }
|
||||
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// IDictionary<string, object> parameters = new Dictionary<string, object>
|
||||
// {
|
||||
// { "limit", 250 }
|
||||
// };
|
||||
|
||||
// IEnumerable<Product> products = await _shopifyClient.Products.Get(parameters);
|
||||
|
||||
// while (products != null && products.Any())
|
||||
// {
|
||||
// foreach (Product product in products)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
|
||||
|
||||
// if (importData == null || importData?.LineCode == "SVG") // Headlights go in front, DUH
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// IList<Fitment> fitments = GetPositionOrderedFitments(importData?.PartNumber, importData?.LineCode);
|
||||
// IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
|
||||
// if (fitments.Count == 0 || vehicles.Count == 0)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// //await DeletePositionMetafields(product.Id);
|
||||
|
||||
// //string currentPosition = fitments[0].Position;
|
||||
// //List<int> vehicleIds = new List<int>();
|
||||
|
||||
// //foreach (Fitment fitment in fitments)
|
||||
// //{
|
||||
// // if (fitment.Position != currentPosition)
|
||||
// // {
|
||||
// // await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
// // currentPosition = fitment.Position;
|
||||
// // vehicleIds = new List<int>();
|
||||
// // }
|
||||
|
||||
// // // We don't need to DCF map because these are both sourced from WHI
|
||||
// // IList<int> fitmentVehicleIds = vehicles.Where(v => v.BaseVehicleId == fitment.BaseVehicleId && v.EngineConfigId == fitment.EngineConfigId)
|
||||
// // .Select(v => v.VehicleToEngineConfigId)
|
||||
// // .Distinct()
|
||||
// // .ToList();
|
||||
|
||||
// // vehicleIds.AddRange(fitmentVehicleIds);
|
||||
// //}
|
||||
|
||||
// //await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
|
||||
// IList<string> notes = fitments.Select(f => f.NoteText)
|
||||
// .Distinct()
|
||||
// .ToList();
|
||||
|
||||
// IList<object> vehicleNotes = new List<object>();
|
||||
|
||||
// foreach (string noteText in notes)
|
||||
// {
|
||||
// IList<int> vehicleIds = fitments.Where(f => f.NoteText == noteText)
|
||||
// .Select(f => new { f.EngineConfigId, f.BaseVehicleId })
|
||||
// .SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
|
||||
// .Select(v => v.VehicleToEngineConfigId)
|
||||
// .ToList();
|
||||
|
||||
|
||||
// vehicleNotes.Add(new { noteText, vehicleIds });
|
||||
// }
|
||||
|
||||
// string json = JsonConvert.SerializeObject(vehicleNotes);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Metafield vehicleMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "note_text",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
|
||||
// //importData.UpdatedAt = DateTime.Now;
|
||||
// //importData.UpdateType = "Positioning";
|
||||
// ;
|
||||
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"{product.Id}: {ex.Message}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// try
|
||||
// {
|
||||
// Console.Write('.');
|
||||
// // await _partSourceContext.SaveChangesAsync();
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"Retrying: {ex.Message}");
|
||||
|
||||
// products = await _shopifyClient.Products.GetPrevious();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// Message = "Positioning updated successfully",
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
|
||||
// private IList<Fitment> GetPositionOrderedFitments(string partNumber, string lineCode)
|
||||
// {
|
||||
// partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
// IQueryable<string> whiCodes = _partSourceContext.DcfMappings
|
||||
// .Where(d => d.LineCode == lineCode)
|
||||
// .Select(d => d.WhiCode);
|
||||
|
||||
// return _partSourceContext.Fitments
|
||||
// .Where(f => f.PartNumber == partNumber && whiCodes.Contains(f.LineCode) && !string.IsNullOrEmpty(f.Position))
|
||||
// .OrderBy(f => f.Position)
|
||||
// .ToList();
|
||||
|
||||
// }
|
||||
|
||||
// [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "It's a Shopify metafield key")]
|
||||
// private async Task SavePositionMetafield(Product product, IList<int> vehicleIds, string position)
|
||||
// {
|
||||
// if (vehicleIds.Count == 0)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// string json = JsonConvert.SerializeObject(vehicleIds);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// // TODO: Logging
|
||||
// return;
|
||||
// }
|
||||
|
||||
// string key = position.ToLowerInvariant().Replace(" ", "_");
|
||||
// if (key.Length > 20)
|
||||
// {
|
||||
// key = key.Substring(0, 20);
|
||||
// }
|
||||
|
||||
// Metafield vehicleMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "position",
|
||||
// Key = key,
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
|
||||
// System.Diagnostics.Debug.WriteLine(json);
|
||||
|
||||
// await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
113
PartSource.Automation/Jobs/Archive/UpdatePricing.cs
Normal file
113
PartSource.Automation/Jobs/Archive/UpdatePricing.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Microsoft.Extensions.Logging.Abstractions;
|
||||
//using Ratermania.Automation.Interfaces;
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using Ratermania.Shopify;
|
||||
//using Ratermania.Shopify.Resources;
|
||||
//using Ratermania.Shopify.Resources.Enums;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
//using Microsoft.Extensions.Logging;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class UpdatePricing : IAutomationJob
|
||||
// {
|
||||
// private readonly ILogger<UpdatePricing> _logger;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
// public UpdatePricing(ILogger<UpdatePricing> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
||||
// {
|
||||
// _logger = logger;
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _shopifyClient = shopifyClient;
|
||||
// }
|
||||
|
||||
// public async Task Run()
|
||||
// {
|
||||
// IEnumerable<Product> products = null;
|
||||
// IEnumerable<PartPrice> prices = null;
|
||||
// int updateCount = 0;
|
||||
|
||||
// try
|
||||
// {
|
||||
// products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
// prices = await _partSourceContext.PartPrices.ToListAsync();
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// _logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
|
||||
|
||||
// throw;
|
||||
// }
|
||||
|
||||
// while (products != null && products.Any())
|
||||
// {
|
||||
// foreach (Product product in products)
|
||||
// {
|
||||
// if (product.Variants.Length > 0)
|
||||
// {
|
||||
// Variant variant = product.Variants[0];
|
||||
// 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[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
|
||||
// {
|
||||
// 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.Products.Update(product);
|
||||
|
||||
// updateCount++;
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// _logger.LogWarning(ex, $"Failed to update pricing for SKU {variant.Sku}");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// try
|
||||
// {
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
// _logger.LogInformation($"Total updated: {updateCount}");
|
||||
// }
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// _logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
// products = await _shopifyClient.Products.GetPrevious();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user