Files
sockeye/server/generator/SockBotProcessPurchasesIntoLicenses.cs
2023-01-30 20:24:36 +00:00

115 lines
4.5 KiB
C#

using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Sockeye.Models;
using Sockeye.Util;
namespace Sockeye.Biz
{
/// <summary>
/// Process purchases into licenses
///
/// </summary>
internal static class SockBotProcessPurchasesIntoLicenses
{
private static ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger("SockBotProcessPurchasesIntoLicenses");
private static DateTime lastSweep = DateTime.MinValue;
private static TimeSpan PROCESS_V7_AGE = new TimeSpan(0, 5, 0);//Don't process a v7 order until at least 5 minutes old as it doesn't always come in at once
#if (DEBUG)
private static TimeSpan PROCESS_EVERY_INTERVAL = new TimeSpan(0, 0, 30);//every 30 seconds during development
#else
private static TimeSpan PROCESS_EVERY_INTERVAL = new TimeSpan(0, 5, 10);//every 5 minutes
#endif
////////////////////////////////////////////////////////////////////////////////////////////////
// DoSweep
//
public static async Task DoWorkAsync()
{
//This will get triggered roughly every minute, but we don't want to check that frequently
if (DateTime.UtcNow - lastSweep < PROCESS_EVERY_INTERVAL)
return;
if (ServerBootConfig.MIGRATING) return;//don't do this during migration (migration is one time only so can remove after up and running)
log.LogDebug("Process purchases into licenses starting");
await ProcessPurchasesIntoLicenses();
lastSweep = DateTime.UtcNow;
}
private static async Task ProcessPurchasesIntoLicenses()
{
using (AyContext ct = Sockeye.Util.ServiceProviderProvider.DBContext)
{
//get a list of all actionable purchases grouped by customer id
var purchaseList = (await ct.Purchase
.Where(z => z.Processed == false
&& z.LicenseId == null
&& z.CustomerId != null
&& (z.PGroup == ProductGroup.AyaNova7 || z.PGroup == ProductGroup.RavenPerpetual || z.PGroup == ProductGroup.RavenSubscription))
.OrderByDescending(z => z.PurchaseDate)
.ToListAsync()).GroupBy(z => (long)z.CustomerId);
try
{
foreach (var purchaseGroup in purchaseList)
{
//vet the group if v7 to be sure no item is less than 5 minutes old to ensure multiple add-on's have all arrived from mycommerce
foreach(var purchase in purchaseGroup){
if (purchase.PGroup == ProductGroup.AyaNova7 && DateTime.UtcNow - purchase.PurchaseDate < PROCESS_V7_AGE)
continue;
}
// LEFT OFF HERE, the stuff below here is old stuff rom copied code so wipe adn repurpose as required
//was about to encode
//if v7 license then lookup last license for same pgroup for same customer, if none then consider it a new license fresh
//if there is one and it's not entirely expired then duplicate and fixup from purchases in this group
//IF raven license and have what is likely a recognizable dbid that ends in an = symbol then just make the new license and cancel the last one if that's necessary
// log.LogDebug($"Processing purchase id:{purchaseGroup[0].Id},purchasedate:{purchaseGroup.PurchaseDate}, custid:{purchaseGroup.CustomerId}");
}
}
catch (Exception ex)
{
var err = "SockBotProcessPurchasesIntoLicenses error running job";
//serious issue requires immediate notification
await NotifyEventHelper.AddOpsProblemEvent(err, ex);
log.LogError(ex, err);
}
}
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons