diff --git a/server/biz/JobsBiz.cs b/server/biz/JobsBiz.cs index cbc5c95..8426845 100644 --- a/server/biz/JobsBiz.cs +++ b/server/biz/JobsBiz.cs @@ -267,6 +267,11 @@ namespace Sockeye.Biz await SockBotProcessVendorNotifications.DoWorkAsync(); if (!KeepOnWorking()) return; + //SOCKBOT - PROCESS PURCHASES INTO LICENSES + await SockBotProcessPurchasesIntoLicenses.DoWorkAsync(); + if (!KeepOnWorking()) return; + + //JOB SWEEPER await CoreJobSweeper.DoWorkAsync(); if (!KeepOnWorking()) return; diff --git a/server/generator/SockBotProcessPurchasesIntoLicenses.cs b/server/generator/SockBotProcessPurchasesIntoLicenses.cs index 016dfc1..cc969be 100644 --- a/server/generator/SockBotProcessPurchasesIntoLicenses.cs +++ b/server/generator/SockBotProcessPurchasesIntoLicenses.cs @@ -20,6 +20,7 @@ namespace Sockeye.Biz { 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 @@ -47,39 +48,46 @@ namespace Sockeye.Biz using (AyContext ct = Sockeye.Util.ServiceProviderProvider.DBContext) { - //get a list of all actionable purchases - var purchaseList = await ct.Purchase + //get a lit 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)) - .OrderBy(z => z.Id) - .ToListAsync(); + .OrderByDescending(z => z.PurchaseDate) + .ToListAsync()).GroupBy(z => (long)z.CustomerId); try { - foreach (var p in purchaseList) + foreach (var purchaseGroup in purchaseList) { - log.LogDebug($"Processing purchase id:{p.Id},purchasedate:{p.PurchaseDate}, custid:{p.CustomerId}"); + //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; + } + log.LogDebug($"Processing purchase id:{purchaseGroup[0].Id},purchasedate:{purchaseGroup.PurchaseDate}, custid:{purchaseGroup.CustomerId}"); //if v7 product group the purchase must be more than 5 minutes old to account for mycommerce trickling in add-on bits to a v7 order //(I went over old sales, mycommerce has at most 2 minutes variance in multiple item order on same day with the exception of unusual situations like where part //of an order was on an invalid credit card or not updated in one case, another was a refund and purchase of alternate item etc) //So in the normal course of things going to assume the best and check further down if something seems missing from last license + - if (string.IsNullOrWhiteSpace(p.VendorData)) + + if (string.IsNullOrWhiteSpace(purchaseGroup.VendorData)) { - var err = $"VendorNotification record {p.Id}-{p.Created} has no vendor data"; + var err = $"VendorNotification record {purchaseGroup.Id}-{purchaseGroup.Created} has no vendor data"; await NotifyEventHelper.AddOpsProblemEvent("SockBotProcessPurchasesIntoLicenses: " + err); log.LogError(err); continue; } //Parse json vendordata - if (await ParseVendorNotificationData(p, ct, log)) + if (await ParseVendorNotificationData(purchaseGroup, ct, log)) { //success, save vendornotification as processed - p.Processed = DateTime.UtcNow; + purchaseGroup.Processed = DateTime.UtcNow; await ct.SaveChangesAsync(); }