From 82adb594ab4baa9ddecd5eaf010af749f3f336da Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 23 Jan 2023 19:47:40 +0000 Subject: [PATCH] --- .../SockBotProcessVendorNotifications.cs | 64 +++++++++---------- server/models/Purchase.cs | 2 +- todo.txt | 22 ++++++- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/server/generator/SockBotProcessVendorNotifications.cs b/server/generator/SockBotProcessVendorNotifications.cs index 88cb29a..cad8950 100644 --- a/server/generator/SockBotProcessVendorNotifications.cs +++ b/server/generator/SockBotProcessVendorNotifications.cs @@ -88,13 +88,20 @@ namespace Sockeye.Biz } - internal static async Task ParseVendorNotificationData(VendorNotification vn, AyContext ct, ILogger log) { try { var jData = JObject.Parse(vn.VendorData); + +#if (DEBUG) + if (vn.VendorData.Contains("coupon")) + { + //trying to find out where the coupon code key is located in the order + System.Diagnostics.Debugger.Break(); + } +#endif //It's a test purchase, no need to process it any further...or is there?? if (jData["order_notification"]["purchase"]["is_test"].Value() == true) { @@ -108,8 +115,6 @@ namespace Sockeye.Biz throw new System.FormatException($"Vendor data unexpected format:{vn.VendorData}"); } - - /////////////////////////////////////////////////////////////////////////////////////////// #region CUSTOMER MAKE OR LOCATED /////////////////////////////////////////////////////////////////////////////////////////// @@ -172,6 +177,9 @@ namespace Sockeye.Biz /////////////////////////////////////////////////////////////////////////////////////////// var salesOrderNumber = jData["order_notification"]["purchase"]["purchase_id"].Value(); +#warning Is this processing the date into UTC properly?? + //https://www.newtonsoft.com/json/help/html/DatesInJSON.htm + var purchaseDate = jData["order_notification"]["purchase"]["purchase_date"].Value(); if (await ct.Purchase.AnyAsync(z => z.SalesOrderNumber == salesOrderNumber)) throw new System.ApplicationException($"Sales order already exists: {salesOrderNumber} will not be processed"); @@ -182,39 +190,29 @@ namespace Sockeye.Biz foreach (JObject jPurchase in jaPurchaseList) { Purchase p = new Purchase(); + p.PurchaseDate = purchaseDate; p.CustomerId = customer.Id; p.VendorId = vn.VendorId; - p.Notes = vn.VendorData;//redundantly keep it p.SalesOrderNumber = salesOrderNumber; - p.Currency = jPurchase["currency"].Value(); - var pId = jPurchase["product_id"].Value(); - var product = await ct.Product.AsNoTracking().FirstOrDefaultAsync(z => z.VendorCode == pId) ?? throw new System.ArgumentOutOfRangeException($"Vendor product code:{pId} was not found in Sockeye Products, record not processed"); + var SalesItemProductVendorCode = jPurchase["product_id"].Value(); + var product = await ct.Product.AsNoTracking().FirstOrDefaultAsync(z => z.VendorCode == SalesItemProductVendorCode) ?? throw new System.ArgumentOutOfRangeException($"Vendor product code:{SalesItemProductVendorCode} was not found in Sockeye Products, record not processed"); p.ProductId = product.Id; p.PGroup = product.PGroup; - p. - - if (p.ProductId == 0) - - switch (p.PGroup) - { - case ProductGroup.Misc: - //not a licensed product, probably misc custom or some fee or etc - p.Processed = true;//flag no further processing is required - break; - case ProductGroup.AyaNova7: - - break; - case ProductGroup.RavenPerpetual: - - break; - case ProductGroup.RavenSubscription: - - break; - default://not set or unrecognized - throw new System.NotSupportedException($"Product group {p.PGroup} not recognized or supported for processing into purchase"); - - } - + p.Currency = jPurchase["accounting"]["currency"].Value(); + p.ProductNet = jPurchase["accounting"]["product_net"].Value(); + p.Discount = jPurchase["accounting"]["discount"].Value(); + p.VendorFee = jPurchase["accounting"]["margin_net"].Value(); + p.Revenue = jPurchase["accounting"]["your_revenue"].Value(); + p.CouponCode = "UNKNOWN WHERE IN VENDOR DATA CONTAINS COUPON CODE"; + p.Quantity = jPurchase["quantity"].Value(); + p.VendorNotificationId = vn.Id; + PurchaseBiz pbiz = PurchaseBiz.GetBiz(ct); + p = await pbiz.CreateAsync(p); + if (p == null) + { + //did not save, throw an error + throw new System.ApplicationException($"Error creating purchase: {pbiz.GetErrorsAsString()} for product item: {SalesItemProductVendorCode} vendor data :{vn.VendorData}"); + } } /////////////////////////////////////////////////////////////////////////////////////////// @@ -222,11 +220,12 @@ namespace Sockeye.Biz } catch (Exception ex) { - var err = $"ParseVendorNotificationData: Purchase record {vn.Id}-{vn.Created} triggered exception, see log"; + var err = $"ParseVendorNotificationData: VendorNotification record {vn.Id}-{vn.Created} triggered exception, see log"; await NotifyEventHelper.AddOpsProblemEvent(err);//notify, this is serious log.LogError(ex, err); return false; } + return true; //successfully processed so vendor notification should be set to processed by caller } private static void UpdateCustomerFromVendorData(JObject jData, Customer c) @@ -1183,6 +1182,7 @@ namespace Sockeye.Biz #endregion sample vendor notifications //order notification json schema on this page down + //actual schema: https://api.shareit.com/xml/2.4/ordernotification.xsd //https://account.mycommerce.com/home/wiki/7479997 //json format //https://account.mycommerce.com/home/wiki/7479805 //overall info diff --git a/server/models/Purchase.cs b/server/models/Purchase.cs index 43704ba..82494b0 100644 --- a/server/models/Purchase.cs +++ b/server/models/Purchase.cs @@ -55,7 +55,7 @@ namespace Sockeye.Models public bool RenewNoticeSent { get; set; } = false; public int Quantity { get; set; } = 1; public long? VendorNotificationId { get; set; } - public bool Processed { get; set; } = false;//indicates it was fully processed and need not be processed by purchase or license generating jobs (all imported data set to processed) + public bool Processed { get; set; } = false;//indicates it was fully processed and need not be processed by license generating jobs (all imported data set to processed) public string Wiki { get; set; } public List Tags { get; set; } //workaround for notification diff --git a/todo.txt b/todo.txt index e1a3d84..3ac5c65 100644 --- a/todo.txt +++ b/todo.txt @@ -12,7 +12,27 @@ TODO: IDEA: if v7 don't process into a license until the newest purchase for the same customer are at least XX minutes old they almost seem to be done manually by mycommerce but I'd say there's no more than 15 minutes max for all to come in !!! maybe look at email history of previous orders and see what the max delay is between order recepts !!!! - + // if (p.ProductId == 0) + + // switch (p.PGroup) + // { + // case ProductGroup.Misc: + // //not a licensed product, probably misc custom or some fee or etc + // p.Processed = true;//flag no further processing is required + // break; + // case ProductGroup.AyaNova7: + + // break; + // case ProductGroup.RavenPerpetual: + + // break; + // case ProductGroup.RavenSubscription: + + // break; + // default://not set or unrecognized + // throw new System.NotSupportedException($"Product group {p.PGroup} not recognized or supported for processing into purchase"); + + // } - JOB: "SOCKBOT" virtual staff member, sockbot - does everything we have to do manually on a delay if it hasn't been done by us yet