diff --git a/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs b/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs new file mode 100644 index 00000000..19b299a6 --- /dev/null +++ b/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs @@ -0,0 +1,109 @@ +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using AyaNova.Util; +using AyaNova.Api.ControllerHelpers; +using AyaNova.Models; + +namespace AyaNova.Biz +{ + + internal class GlobalOpsNotificationSettingsBiz : BizObject + { + + internal GlobalOpsNotificationSettingsBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) + { + ct = dbcontext; + UserId = currentUserId; + UserTranslationId = userTranslationId; + CurrentUserRoles = UserRoles; + BizType = AyaType.Backup; + } + + internal static GlobalOpsNotificationSettingsBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) + { + if (httpContext != null) + return new GlobalOpsNotificationSettingsBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); + else + return new GlobalOpsNotificationSettingsBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); + } + + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// GET + + //Get one + internal async Task GetAsync(bool logTheGetEvent = true) + { + //first try to fetch from db + var ret = await ct.GlobalOpsNotificationSettings.SingleOrDefaultAsync(m => m.Id == 1); + if (logTheGetEvent && ret != null) + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Retrieved), ct); + //expected to exists because it's created on boot if not present + if (ret == null) + throw new System.Exception("GlobalOpsNotificationSettings::GetAsync -> Settings object not found in database!!"); + + return ret; + } + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //UPDATE + // + + //put + internal async Task PutAsync(GlobalOpsNotificationSettings putObject) + { + var dbObject = await ct.GlobalOpsNotificationSettings.FirstOrDefaultAsync(m => m.Id == 1); + if (dbObject == null) + throw new System.Exception("GlobalOpsNotificationSettings::PutAsync -> Settings object not found in database!!"); + + CopyObject.Copy(putObject, dbObject, "Id"); + ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency; + Validate(dbObject); + if (HasErrors) + return null; + + try + { + await ct.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + AddError(ApiErrorCode.CONCURRENCY_CONFLICT); + return null; + } + + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Modified), ct); + //Update the static copy for the server + ServerGlobalOpsSettingsCache.Notify = dbObject; + + return dbObject; + } + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //VALIDATION + // + + //Can save or update? + private void Validate(GlobalOpsNotificationSettings inObj) + { + + //currently nothing to validate + } + + + + + ///////////////////////////////////////////////////////////////////// + + }//eoc + + +}//eons + diff --git a/server/AyaNova/models/AyContext.cs b/server/AyaNova/models/AyContext.cs index d083976b..b13e6324 100644 --- a/server/AyaNova/models/AyContext.cs +++ b/server/AyaNova/models/AyContext.cs @@ -13,6 +13,7 @@ namespace AyaNova.Models public virtual DbSet Widget { get; set; } public virtual DbSet GlobalBizSettings { get; set; } public virtual DbSet GlobalOpsBackupSettings { get; set; } + public virtual DbSet GlobalOpsNotificationSettings { get; set; } public virtual DbSet Event { get; set; } public virtual DbSet SearchDictionary { get; set; } public virtual DbSet SearchKey { get; set; } diff --git a/server/AyaNova/models/GlobalOpsNotificationSettings.cs b/server/AyaNova/models/GlobalOpsNotificationSettings.cs index ab0a1742..055ed55d 100644 --- a/server/AyaNova/models/GlobalOpsNotificationSettings.cs +++ b/server/AyaNova/models/GlobalOpsNotificationSettings.cs @@ -14,10 +14,17 @@ namespace AyaNova.Models public NotifyMailSecurity ConnectionSecurity { get; set; } public int SmtpServerPort { get; set; } public string NotifyFromAddress { get; set; } - + public GlobalOpsNotificationSettings() - { + { Active = true; + Id = 1; + SmtpServerAddress="mail.example.com"; + SmtpAccount="notifydeliverfromaccount@example.com"; + SmtpPassword="examplepassword"; + ConnectionSecurity= NotifyMailSecurity.SSLTLS; + SmtpServerPort=587; + NotifyFromAddress="noreply@example.com"; } } } diff --git a/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs b/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs index 7e761edd..00566f0e 100644 --- a/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs +++ b/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs @@ -16,6 +16,7 @@ namespace AyaNova.Util internal static bool BOOTING { get; set; } internal static GlobalOpsBackupSettings Backup { get; set; } + internal static GlobalOpsNotificationSettings Notify {get;set;} internal static DateTime NextBackup { get; set; } /// /// Populate and / or create the settings @@ -32,6 +33,13 @@ namespace AyaNova.Util } NextBackup = FileUtil.MostRecentAutomatedBackupFileDate(); SetNextBackup(); + + + Notify=ct.GlobalOpsNotificationSettings.FirstOrDefault(z=>z.Id==1); + if(Notify==null) + { + Notify=new GlobalOpsNotificationSettings(); + } } internal static void SetNextBackup()