From 68e181a085ccfba5e205b4dd911848d1c85e1f84 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 13 Apr 2023 22:39:07 +0000 Subject: [PATCH] --- server/biz/BizObjectExistsInDatabase.cs | 2 + server/biz/BizObjectFactory.cs | 2 + server/biz/BizRoles.cs | 16 + server/biz/FormFieldReference.cs | 17 +- server/biz/SockType.cs | 4 +- server/biz/SubscriptionBiz.cs | 441 ++++++++++++++++++++++++ server/models/AyContext.cs | 4 + server/models/Subscription.cs | 36 ++ server/models/SubscriptionItem.cs | 30 ++ server/sockeye.csproj | 4 +- server/util/SockeyeVersion.cs | 2 +- todo.txt | 20 +- 12 files changed, 572 insertions(+), 6 deletions(-) create mode 100644 server/biz/SubscriptionBiz.cs create mode 100644 server/models/Subscription.cs create mode 100644 server/models/SubscriptionItem.cs diff --git a/server/biz/BizObjectExistsInDatabase.cs b/server/biz/BizObjectExistsInDatabase.cs index e05a593..7ba9901 100644 --- a/server/biz/BizObjectExistsInDatabase.cs +++ b/server/biz/BizObjectExistsInDatabase.cs @@ -65,6 +65,8 @@ namespace Sockeye.Biz return await ct.Product.AnyAsync(z => z.Id == id); case SockType.GZCase: return await ct.GZCase.AnyAsync(z => z.Id == id); + case SockType.Subscription: + return await ct.Subscription.AnyAsync(z => z.Id == id); case SockType.CustomerNotifySubscription: diff --git a/server/biz/BizObjectFactory.cs b/server/biz/BizObjectFactory.cs index 881fd6c..edb3c44 100644 --- a/server/biz/BizObjectFactory.cs +++ b/server/biz/BizObjectFactory.cs @@ -67,6 +67,8 @@ namespace Sockeye.Biz return new ProductBiz(ct, userId, translationId, roles); case SockType.GZCase: return new GZCaseBiz(ct, userId, translationId, roles); + case SockType.Subscription: + return new SubscriptionBiz(ct, userId, translationId, roles); default: throw new System.NotSupportedException($"Sockeye.BLL.BizObjectFactory::GetBizObject type {sockType.ToString()} is not supported"); diff --git a/server/biz/BizRoles.cs b/server/biz/BizRoles.cs index 23abea7..92b72bf 100644 --- a/server/biz/BizRoles.cs +++ b/server/biz/BizRoles.cs @@ -650,6 +650,22 @@ namespace Sockeye.Biz }); + //////////////////////////////////////////////////////////// + //SUBSCRIPTION + // + roles.Add(SockType.Subscription, new BizRoleSet() + { + Change = AuthorizationRoles.BizAdmin + | AuthorizationRoles.Service + | AuthorizationRoles.Sales + | AuthorizationRoles.Accounting, + ReadFullRecord = AuthorizationRoles.BizAdminRestricted + | AuthorizationRoles.ServiceRestricted + | AuthorizationRoles.Tech + | AuthorizationRoles.SalesRestricted + , + Select = AuthorizationRoles.All + }); //////////////////////////////////////////////////////////////////// #endregion all roles init diff --git a/server/biz/FormFieldReference.cs b/server/biz/FormFieldReference.cs index 93b318d..99851b1 100644 --- a/server/biz/FormFieldReference.cs +++ b/server/biz/FormFieldReference.cs @@ -391,7 +391,7 @@ namespace Sockeye.Biz } #endregion - + #region SubscriptionServer { List l = new List(); @@ -426,6 +426,21 @@ namespace Sockeye.Biz } #endregion + #region Subscription + { + List l = new List(); + + + l.Add(new FormField { TKey = "Tags", FieldKey = "Tags" }); + l.Add(new FormField { TKey = "Wiki", FieldKey = "Wiki" }); + l.Add(new FormField { TKey = "Attachments", FieldKey = "Attachments", Requireable = false }); + + + _formFields.Add(SockType.Subscription.ToString(), l); + } + #endregion + + #region TrialLicenseRequest { List l = new List(); diff --git a/server/biz/SockType.cs b/server/biz/SockType.cs index f47d5b0..2d82b0f 100644 --- a/server/biz/SockType.cs +++ b/server/biz/SockType.cs @@ -77,7 +77,9 @@ namespace Sockeye.Biz [CoreBizObject, ReportableBizObject] GZCase = 98, [CoreBizObject, ReportableBizObject] - VendorNotification = 99 + VendorNotification = 99, + [CoreBizObject, ReportableBizObject] + Subscription = 100 diff --git a/server/biz/SubscriptionBiz.cs b/server/biz/SubscriptionBiz.cs new file mode 100644 index 0000000..fe6de37 --- /dev/null +++ b/server/biz/SubscriptionBiz.cs @@ -0,0 +1,441 @@ +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using System.Linq; +using Sockeye.Util; +using Sockeye.Api.ControllerHelpers; +using Sockeye.Models; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace Sockeye.Biz +{ + internal class SubscriptionBiz : BizObject, IJobObject, ISearchAbleObject, IReportAbleObject, IExportAbleObject, INotifiableObject + { + internal SubscriptionBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) + { + ct = dbcontext; + UserId = currentUserId; + UserTranslationId = userTranslationId; + CurrentUserRoles = UserRoles; + BizType = SockType.Subscription; + } + + internal static SubscriptionBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) + { + if (httpContext != null) + return new SubscriptionBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); + else + return new SubscriptionBiz(ct, 1, ServerBootConfig.SOCKEYE_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdmin); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + //EXISTS + internal async Task ExistsAsync(long id) + { + return await ct.Subscription.AnyAsync(z => z.Id == id); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + //CREATE + // + internal async Task CreateAsync(Subscription newObject) + { + await ValidateAsync(newObject, null); + if (HasErrors) + return null; + else + { + newObject.Tags = TagBiz.NormalizeTags(newObject.Tags); + await ct.Subscription.AddAsync(newObject); + await ct.SaveChangesAsync(); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, SockEvent.Created), ct); + await SearchIndexAsync(newObject, true); + await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null); + await HandlePotentialNotificationEvent(SockEvent.Created, newObject); + return newObject; + } + } + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //GET + // + internal async Task GetAsync(long id, bool logTheGetEvent = true) + { + var ret = await ct.Subscription.AsNoTracking().SingleOrDefaultAsync(z => z.Id == id); + if (logTheGetEvent && ret != null) + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, SockEvent.Retrieved), ct); + return ret; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + //UPDATE + // + internal async Task PutAsync(Subscription putObject) + { + var dbObject = await GetAsync(putObject.Id, false); + if (dbObject == null) + { + AddError(ApiErrorCode.NOT_FOUND, "id"); + return null; + } + if (dbObject.Concurrency != putObject.Concurrency) + { + AddError(ApiErrorCode.CONCURRENCY_CONFLICT); + return null; + } + + putObject.Tags = TagBiz.NormalizeTags(putObject.Tags); + await ValidateAsync(putObject, dbObject); + if (HasErrors) return null; + ct.Replace(dbObject, putObject); + try + { + await ct.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!await ExistsAsync(putObject.Id)) + AddError(ApiErrorCode.NOT_FOUND); + else + AddError(ApiErrorCode.CONCURRENCY_CONFLICT); + return null; + } + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, putObject.Id, BizType, SockEvent.Modified), ct); + await SearchIndexAsync(putObject, false); + await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, putObject.Tags, dbObject.Tags); + await HandlePotentialNotificationEvent(SockEvent.Modified, putObject, dbObject); + return putObject; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + //DELETE + // + internal async Task DeleteAsync(long id) + { + using (var transaction = await ct.Database.BeginTransactionAsync()) + { + + Subscription dbObject = await GetAsync(id, false); + if (dbObject == null) + { + AddError(ApiErrorCode.NOT_FOUND); + return false; + } + await ValidateCanDeleteAsync(dbObject); + if (HasErrors) + return false; + + + + + { + var IDList = await ct.Review.AsNoTracking().Where(x => x.SockType == SockType.Subscription && x.ObjectId == id).Select(x => x.Id).ToListAsync(); + if (IDList.Count() > 0) + { + ReviewBiz b = new ReviewBiz(ct, UserId, UserTranslationId, CurrentUserRoles); + foreach (long ItemId in IDList) + if (!await b.DeleteAsync(ItemId, transaction)) + { + AddError(ApiErrorCode.CHILD_OBJECT_ERROR, null, $"Review [{ItemId}]: {b.GetErrorsAsString()}"); + return false; + } + } + } + + ct.Subscription.Remove(dbObject); + await ct.SaveChangesAsync(); + + //Log event + await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct); + await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType, ct); + await TagBiz.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags); + await FileUtil.DeleteAttachmentsForObjectAsync(BizType, dbObject.Id, ct); + await transaction.CommitAsync(); + await HandlePotentialNotificationEvent(SockEvent.Deleted, dbObject); + + return true; + } + } + + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //SEARCH + // + private async Task SearchIndexAsync(Subscription obj, bool isNew) + { + var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); + DigestSearchText(obj, SearchParams); + if (isNew) + await Search.ProcessNewObjectKeywordsAsync(SearchParams); + else + await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); + } + + public async Task GetSearchResultSummary(long id, SockType specificType) + { + var obj = await GetAsync(id, false); + var SearchParams = new Search.SearchIndexProcessObjectParameters(); + DigestSearchText(obj, SearchParams); + return SearchParams; + } + + public void DigestSearchText(Subscription obj, Search.SearchIndexProcessObjectParameters searchParams) + { + if (obj != null) + searchParams.AddText(obj.Name) + .AddText(obj.Tags) + .AddText(obj.Notes); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + //VALIDATION + // + + private async Task ValidateAsync(Subscription proposedObj, Subscription currentObj) + { + await Task.CompletedTask; + // bool isNew = currentObj == null; + + + + } + + + private async Task ValidateCanDeleteAsync(Subscription inObj) + { + + await Task.CompletedTask; + } + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //REPORTING + // + public async Task GetReportData(DataListSelectedRequest dataListSelectedRequest, Guid jobId) + { + var idList = dataListSelectedRequest.SelectedRowIds; + JArray ReportData = new JArray(); + while (idList.Any()) + { + var batch = idList.Take(IReportAbleObject.REPORT_DATA_BATCH_SIZE); + idList = idList.Skip(IReportAbleObject.REPORT_DATA_BATCH_SIZE).ToArray(); + + //query for this batch, comes back in db natural order unfortunately + var batchResults = await ct.Subscription.AsNoTracking().Where(z => batch.Contains(z.Id)).ToArrayAsync(); + + //order the results back into original + //What is happening here: + //for performance the query is batching a bunch at once by fetching a block of items from the sql server + //however it's returning in db order which is often not the order the id list is in + //so it needs to be sorted back into the same order as the ide list + //This would not be necessary if just fetching each one at a time individually (like in workorder get report data) + + var orderedList = from id in batch join z in batchResults on id equals z.Id select z; + batchResults = null; + + foreach (Subscription w in orderedList) + { + if (!ReportRenderManager.KeepGoing(jobId)) return null; + await PopulateVizFields(w); + var jo = JObject.FromObject(w); + if (!JsonUtil.JTokenIsNullOrEmpty(jo["CustomFields"])) + jo["CustomFields"] = JObject.Parse((string)jo["CustomFields"]); + ReportData.Add(jo); + } + orderedList = null; + } + vc.Clear(); + return ReportData; + } + private VizCache vc = new VizCache(); + + + //populate viz fields from provided object + private async Task PopulateVizFields(Subscription o) + { + if (!vc.Has("customer", o.CustomerId)) + { + vc.Add(await ct.Customer.AsNoTracking().Where(x => x.Id == o.CustomerId).Select(x => x.Name).FirstOrDefaultAsync(), "customer", o.CustomerId); + } + o.CustomerViz = vc.Get("customer", o.CustomerId); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // IMPORT EXPORT + // + + public async Task GetExportData(DataListSelectedRequest dataListSelectedRequest, Guid jobId) + { + return await GetReportData(dataListSelectedRequest, jobId); + } + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //JOB / OPERATIONS + // + public async Task HandleJobAsync(OpsJob job) + { + //Hand off the particular job to the corresponding processing code + //NOTE: If this code throws an exception the caller (JobsBiz::ProcessJobsAsync) will automatically set the job to failed and log the exeption so + //basically any error condition during job processing should throw up an exception if it can't be handled + switch (job.JobType) + { + case JobType.BatchCoreObjectOperation: + await ProcessBatchJobAsync(job); + break; + default: + throw new System.ArgumentOutOfRangeException($"SubscriptionBiz.HandleJob-> Invalid job type{job.JobType.ToString()}"); + } + } + + + + private async Task ProcessBatchJobAsync(OpsJob job) + { + await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running); + await JobsBiz.LogJobAsync(job.GId, $"LT:StartJob {job.SubType}"); + List idList = new List(); + long FailedObjectCount = 0; + JObject jobData = JObject.Parse(job.JobInfo); + if (jobData.ContainsKey("idList")) + idList = ((JArray)jobData["idList"]).ToObject>(); + else + idList = await ct.Subscription.AsNoTracking().Select(z => z.Id).ToListAsync(); + bool SaveIt = false; + + //--------------------------------- + //case 4192 + TimeSpan ProgressAndCancelCheckSpan = new TimeSpan(0, 0, ServerBootConfig.JOB_PROGRESS_UPDATE_AND_CANCEL_CHECK_SECONDS); + DateTime LastProgressCheck = DateTime.UtcNow.Subtract(new TimeSpan(1, 1, 1, 1, 1)); + var TotalRecords = idList.LongCount(); + long CurrentRecord = -1; + //--------------------------------- + + foreach (long id in idList) + { + try + { + //-------------------------------- + //case 4192 + //Update progress / cancel requested? + CurrentRecord++; + if (DateUtil.IsAfterDuration(LastProgressCheck, ProgressAndCancelCheckSpan)) + { + await JobsBiz.UpdateJobProgressAsync(job.GId, $"{CurrentRecord}/{TotalRecords}"); + if (await JobsBiz.GetJobStatusAsync(job.GId) == JobStatus.CancelRequested) + break; + LastProgressCheck = DateTime.UtcNow; + } + //--------------------------------- + + SaveIt = false; + ClearErrors(); + Subscription o = null; + //save a fetch if it's a delete + if (job.SubType != JobSubType.Delete) + o = await GetAsync(id, false); + switch (job.SubType) + { + case JobSubType.TagAddAny: + case JobSubType.TagAdd: + case JobSubType.TagRemoveAny: + case JobSubType.TagRemove: + case JobSubType.TagReplaceAny: + case JobSubType.TagReplace: + SaveIt = TagBiz.ProcessBatchTagOperation(o.Tags, (string)jobData["tag"], jobData.ContainsKey("toTag") ? (string)jobData["toTag"] : null, job.SubType); + break; + case JobSubType.Delete: + if (!await DeleteAsync(id)) + { + await JobsBiz.LogJobAsync(job.GId, $"LT:Errors {GetErrorsAsString()} id {id}"); + FailedObjectCount++; + } + break; + default: + throw new System.ArgumentOutOfRangeException($"ProcessBatchJobAsync -> Invalid job Subtype{job.SubType}"); + } + if (SaveIt) + { + o = await PutAsync(o); + if (o == null) + { + await JobsBiz.LogJobAsync(job.GId, $"LT:Errors {GetErrorsAsString()} id {id}"); + FailedObjectCount++; + } + } + + //delay so we're not tying up all the resources in a tight loop + await Task.Delay(Sockeye.Util.ServerBootConfig.JOB_OBJECT_HANDLE_BATCH_JOB_LOOP_DELAY); + } + catch (Exception ex) + { + await JobsBiz.LogJobAsync(job.GId, $"LT:Errors id({id})"); + await JobsBiz.LogJobAsync(job.GId, ExceptionUtil.ExtractAllExceptionMessages(ex)); + } + } + + //--------------------------------- + //case 4192 + await JobsBiz.UpdateJobProgressAsync(job.GId, $"{++CurrentRecord}/{TotalRecords}"); + //--------------------------------- + + await JobsBiz.LogJobAsync(job.GId, $"LT:BatchJob {job.SubType} {idList.Count}{(FailedObjectCount > 0 ? " - LT:Failed " + FailedObjectCount : "")}"); + await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed); + } + + + + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + // NOTIFICATION PROCESSING + // + public async Task HandlePotentialNotificationEvent(SockEvent ayaEvent, ICoreBizObjectModel proposedObj, ICoreBizObjectModel currentObj = null) + { + if (ServerBootConfig.MIGRATING) return; + ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger(); + + log.LogDebug($"HandlePotentialNotificationEvent processing: [SockType:{this.BizType}, AyaEvent:{ayaEvent}]"); + + bool isNew = currentObj == null; + + //STANDARD EVENTS FOR ALL OBJECTS + await NotifyEventHelper.ProcessStandardObjectEvents(ayaEvent, proposedObj, ct); + + //SPECIFIC EVENTS FOR THIS OBJECT + Subscription o = (Subscription)proposedObj; + + //## DELETED EVENTS + //any event added below needs to be removed, so + //just blanket remove any event for this object of eventtype that would be added below here + //do it regardless any time there's an update and then + //let this code below handle the refreshing addition that could have changes + //await NotifyEventHelper.ClearPriorEventsForObject(ct, SockType.Subscription, o.Id, NotifyEventType.ContractExpiring); + + + //## CREATED / MODIFIED EVENTS + if (ayaEvent == SockEvent.Created || ayaEvent == SockEvent.Modified) + { + + + + } + + }//end of process notifications + + + ///////////////////////////////////////////////////////////////////// + + }//eoc + + +}//eons + diff --git a/server/models/AyContext.cs b/server/models/AyContext.cs index fb88e6e..0732f95 100644 --- a/server/models/AyContext.cs +++ b/server/models/AyContext.cs @@ -65,6 +65,10 @@ namespace Sockeye.Models public virtual DbSet GZCase { get; set; } public virtual DbSet SubscriptionServer { get; set; } + + public virtual DbSet Subscription { get; set; } + public virtual DbSet SubscriptionItem { get; set; } + //Note: had to add this constructor to work with the code in startup.cs that gets the connection string from the appsettings.json file //and commented out the above on configuring public AyContext(DbContextOptions options) : base(options) diff --git a/server/models/Subscription.cs b/server/models/Subscription.cs new file mode 100644 index 0000000..07ddd59 --- /dev/null +++ b/server/models/Subscription.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using Sockeye.Biz; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Newtonsoft.Json; + +namespace Sockeye.Models +{ + //https://stackoverflow.com/questions/46517584/how-to-add-a-parent-record-with-its-children-records-in-ef-core#46615455 + + public class Subscription : ICoreBizObjectModel + { + public long Id { get; set; } + public uint Concurrency { get; set; } + + [Required] + public string Name { get; set; } + public bool Active { get; set; } +[Required] + public ProductGroup PGroup { get; set; } + public long? CustomerId { get; set; } + [NotMapped] + public string CustomerViz { get; set; } + + public string Notes { get; set; } + public List Tags { get; set; } + + public List Items { get; set; } = new List(); + + [NotMapped, JsonIgnore] + public SockType SType { get => SockType.Subscription; } + + }//eoc + +}//eons diff --git a/server/models/SubscriptionItem.cs b/server/models/SubscriptionItem.cs new file mode 100644 index 0000000..d1f2346 --- /dev/null +++ b/server/models/SubscriptionItem.cs @@ -0,0 +1,30 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Newtonsoft.Json; + +namespace Sockeye.Models +{ + //https://stackoverflow.com/questions/46517584/how-to-add-a-parent-record-with-its-children-records-in-ef-core#46615455 + public class SubscriptionItem + { + public long Id { get; set; } + public uint Concurrency { get; set; } + + [Required] + public long SubscriptionId { get; set; } + [Required] + public long ProductId { get; set; } + [NotMapped] + public string ProductViz { get; set; } + [Required] + public DateTime ExpireDate { get; set; } + [Required] + public int Quantity { get; set; } = 1; + + [JsonIgnore] + public Subscription Subscription { get; set; } + + }//eoc + +}//eons \ No newline at end of file diff --git a/server/sockeye.csproj b/server/sockeye.csproj index fae5840..62937e6 100644 --- a/server/sockeye.csproj +++ b/server/sockeye.csproj @@ -4,8 +4,8 @@ true - 8.0.10 - 8.0.10.0 + 8.0.11 + 8.0.11.0 sockeye.ico bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml 1591 diff --git a/server/util/SockeyeVersion.cs b/server/util/SockeyeVersion.cs index e90db64..234fd63 100644 --- a/server/util/SockeyeVersion.cs +++ b/server/util/SockeyeVersion.cs @@ -5,7 +5,7 @@ namespace Sockeye.Util /// internal static class SockeyeVersion { - public const string VersionString = "8.0.10"; + public const string VersionString = "8.0.11"; public const string FullNameAndVersion = "Sockeye server " + VersionString; public const string CurrentApiVersion="v8"; }//eoc diff --git a/todo.txt b/todo.txt index 0bbb35c..d985051 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,24 @@ TODO: -sub server state checking and notification wtf? + +- Subscriptions need to be a first class root object and drive other stuff, it's not good enough to have licenses and purchases + Make subscriptions fully realized and populate and update. + - Need to be able to run an revenue projected report based on timeframe + - need to be able to start stop and archive subscriptions and use them to make licenses from multiple selected + - Subscription "group"?? (was "site" :) + + Subscription object + - customer id + - active + - product group + subscription item + - product + - quantity + - expires + - active + + + - Subscribe to following notifications: Ops problem event should notify me immediately