Current state, whatever that means
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Models.Jobs;
|
||||
using PartSource.Automation.Services;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
@@ -9,7 +10,9 @@ 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.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
@@ -31,15 +34,14 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
List<UpdatePricingResult> pricingReport = new List<UpdatePricingResult>();
|
||||
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();
|
||||
prices = await _partSourceContext.PartPrices.AsNoTracking().ToListAsync();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
@@ -53,6 +55,8 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
List<UpdatePricingResult> productPricingUpdate = new List<UpdatePricingResult>();
|
||||
|
||||
if (product.Variants.Length > 0)
|
||||
{
|
||||
bool hasUpdate = false;
|
||||
@@ -69,6 +73,15 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
product.Variants[i].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
|
||||
@@ -95,8 +108,8 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
//await _shopifyClient.Metafields.Add(metafield);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
updateCount++;
|
||||
|
||||
pricingReport.AddRange(productPricingUpdate);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
@@ -111,7 +124,7 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
_logger.LogInformation($"Total updated: {updateCount}");
|
||||
_logger.LogInformation($"Total updated: {pricingReport.Count}");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
@@ -121,7 +134,40 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
}
|
||||
|
||||
_emailService.Send("Pricing Update Completed", $"The pricing update has completed. Total updated: {updateCount}");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user