From 9dda0623baccf4b326f686e86e4eb533f3301ce9 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 22 Jul 2020 21:23:34 +0000 Subject: [PATCH] --- .../AyaNova/Controllers/EnumListController.cs | 91 ++++++++++--------- server/AyaNova/generator/CoreJobNotify.cs | 28 +++++- .../models/GlobalOpsNotificationSettings.cs | 13 ++- 3 files changed, 89 insertions(+), 43 deletions(-) diff --git a/server/AyaNova/Controllers/EnumListController.cs b/server/AyaNova/Controllers/EnumListController.cs index 0afa5cff..221944e7 100644 --- a/server/AyaNova/Controllers/EnumListController.cs +++ b/server/AyaNova/Controllers/EnumListController.cs @@ -8,6 +8,7 @@ using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using AyaNova.Util; +using System.Threading.Tasks; namespace AyaNova.Api.Controllers @@ -48,17 +49,54 @@ namespace AyaNova.Api.Controllers /// The key name of the enumerated type /// List [HttpGet("list/{enumkey}")] - public ActionResult GetList([FromRoute] string enumkey) + public async Task GetList([FromRoute] string enumkey) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); long TranslationId = UserTranslationIdFromContext.Id(HttpContext.Items); + + var ret = await GetEnumList(enumkey, TranslationId); + + return Ok(ApiOkResponse.Response(ret)); + } + + + + + /// + /// Get all possible enumerated values list key names + /// + /// List of AyaNova enumerated type list key names that can be fetched from the GetList Route + [HttpGet("listkeys")] + public ActionResult GetTypesList() + { + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + + List> ret = new List>(); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(UserType).ToString()), "AyaNova user account types")); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(AuthorizationRoles).ToString()), "AyaNova user account role types")); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(AyaType).ToString()), "All AyaNova object types")); + ret.Add(new KeyValuePair("Core", "All Core AyaNova business object types")); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(UiFieldDataType).ToString()), "Types of data used in AyaNova for display and formatting UI purposes")); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(NotifyEventType).ToString()), "Notification event types")); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(NotifyDeliveryMethod).ToString()), "Notification delivery methods")); + ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(NotifyMailSecurity).ToString()), "Notification SMTP mail server security method")); + + return Ok(ApiOkResponse.Response(ret)); + } + + + + public static async Task> GetEnumList(string enumKey, long translationId) + { + List TranslationKeysToFetch = new List(); List ReturnList = new List(); - var keyNameInLowerCase = enumkey.ToLowerInvariant(); + var keyNameInLowerCase = enumKey.ToLowerInvariant(); if (keyNameInLowerCase == StringUtil.TrimTypeName(typeof(UiFieldDataType).ToString()).ToLowerInvariant()) @@ -83,7 +121,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add(t.ToString()); } } - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); foreach (AyaType t in values) { @@ -112,7 +150,7 @@ namespace AyaNova.Api.Controllers foreach (AyaType t in values) TranslationKeysToFetch.Add(t.ToString()); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); foreach (AyaType t in values) { @@ -138,7 +176,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add("UserTypeHeadOffice"); TranslationKeysToFetch.Add("UserTypeServiceContractor"); TranslationKeysToFetch.Add("UserTypeUtility"); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); ReturnList.Add(new NameIdItem() { Name = LT["UserTypeAdministrator"], Id = (long)UserType.Administrator }); ReturnList.Add(new NameIdItem() { Name = LT["UserTypeService"], Id = (long)UserType.Service }); @@ -165,7 +203,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add("AdminEraseDatabase"); TranslationKeysToFetch.Add("EventResetSerial"); TranslationKeysToFetch.Add("EventUtilityFileDownload"); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); ReturnList.Add(new NameIdItem() { Name = LT["EventDeleted"], Id = (long)AyaEvent.Deleted }); ReturnList.Add(new NameIdItem() { Name = LT["EventCreated"], Id = (long)AyaEvent.Created }); @@ -205,7 +243,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add("AuthorizationRoleSalesLimited"); TranslationKeysToFetch.Add("AuthorizationRoleSalesFull"); // TranslationKeysToFetch.Add("AuthorizationRoleAll"); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); // ReturnList.Add(new NameIdItem() { Name = LT["AuthorizationRoleNoRole"], Id = (long)AuthorizationRoles.NoRole }); ReturnList.Add(new NameIdItem() { Name = LT["AuthorizationRoleBizAdminLimited"], Id = (long)AuthorizationRoles.BizAdminLimited }); @@ -259,7 +297,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add("NotifyEventUnitMeterReadingMultipleExceeded"); TranslationKeysToFetch.Add("NotifyEventServerOperationsProblem"); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); ReturnList.Add(new NameIdItem() { Name = LT["NotifyEventObjectDeleted"], Id = (long)NotifyEventType.ObjectDeleted }); ReturnList.Add(new NameIdItem() { Name = LT["NotifyEventObjectCreated"], Id = (long)NotifyEventType.ObjectCreated }); @@ -296,7 +334,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add("NotifyDeliveryMethodApp"); TranslationKeysToFetch.Add("NotifyDeliveryMethodSMTP"); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); ReturnList.Add(new NameIdItem() { Name = LT["NotifyDeliveryMethodApp"], Id = (long)NotifyDeliveryMethod.App }); ReturnList.Add(new NameIdItem() { Name = LT["NotifyDeliveryMethodSMTP"], Id = (long)NotifyDeliveryMethod.SMTP }); @@ -307,7 +345,7 @@ namespace AyaNova.Api.Controllers TranslationKeysToFetch.Add("NotifyMailSecuritySSLTLS"); TranslationKeysToFetch.Add("NotifyMailSecurityStartTls"); - var LT = TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, TranslationId).Result; + var LT = await TranslationBiz.GetSubsetStaticAsync(TranslationKeysToFetch, translationId); ReturnList.Add(new NameIdItem() { Name = LT["NotifyMailSecurityNone"], Id = (long)NotifyMailSecurity.None }); ReturnList.Add(new NameIdItem() { Name = LT["NotifyMailSecuritySSLTLS"], Id = (long)NotifyMailSecurity.SSLTLS }); @@ -315,42 +353,13 @@ namespace AyaNova.Api.Controllers } else { - ReturnList.Add(new NameIdItem() { Name = $"Unknown enum type list key value {enumkey}", Id = (long)UserType.Administrator }); + ReturnList.Add(new NameIdItem() { Name = $"Unknown enum type list key value {enumKey}", Id = (long)UserType.Administrator }); } + return ReturnList; - - return Ok(ApiOkResponse.Response(ReturnList)); } - - - /// - /// Get all possible enumerated values list key names - /// - /// List of AyaNova enumerated type list key names that can be fetched from the GetList Route - [HttpGet("listkeys")] - public ActionResult GetTypesList() - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - List> ret = new List>(); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(UserType).ToString()), "AyaNova user account types")); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(AuthorizationRoles).ToString()), "AyaNova user account role types")); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(AyaType).ToString()), "All AyaNova object types")); - ret.Add(new KeyValuePair("Core", "All Core AyaNova business object types")); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(UiFieldDataType).ToString()), "Types of data used in AyaNova for display and formatting UI purposes")); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(NotifyEventType).ToString()), "Notification event types")); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(NotifyDeliveryMethod).ToString()), "Notification delivery methods")); - ret.Add(new KeyValuePair(StringUtil.TrimTypeName(typeof(NotifyMailSecurity).ToString()), "Notification SMTP mail server security method")); - - return Ok(ApiOkResponse.Response(ret)); - } - - - - }//eoc }//ens \ No newline at end of file diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 63bf9c44..d94c86dc 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -33,6 +34,10 @@ namespace AyaNova.Biz private static TimeSpan DELETE_AFTER_AGE = new TimeSpan(90, 0, 0, 0); private static TimeSpan RUN_EVERY_INTERVAL = new TimeSpan(0, 2, 0);//no more frequently than once every 2 minutes #endif + + //temporary list to hold translations as required during delivery + private static Dictionary> _transCache = new Dictionary>(); + //////////////////////////////////////////////////////////////////////////////////////////////// // DoSweep // @@ -150,6 +155,14 @@ namespace AyaNova.Biz } + //cache any translations required for email notification + private static async Task CacheNotifyEventTypeTranslations(long translationId){ + if(_transCache.ContainsKey(translationId)){ + return; + } + _transCache.Add(translationId,AyaNova.Api.Controllers.EnumListController.GetEnumList("NotifyEventType",translationId)); + } + private static async Task DeliverInApp(NotifyEvent ne, AyContext ct) { log.LogTrace($"DeliverInApp deliving notify event: {ne}"); @@ -158,10 +171,23 @@ namespace AyaNova.Biz await ct.SaveChangesAsync(); } - private static async Task DeliverSMTP(NotifyEvent ne, AyContext ct) + private static async Task DeliverSMTP(NotifyEvent ne, string toAddress, AyContext ct) { log.LogTrace($"DeliverSMTP deliving notify event: {ne}"); + var subject = $"Notification: {ne.EventType.ToString()}"; + + IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; + try + { + await m.SendEmailAsync(toAddress, subject, "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify); + return "ok"; + } + catch (Exception ex) + { + return ExceptionUtil.ExtractAllExceptionMessages(ex); + } + //todo: //Open question: what to do with failed deliveries? //we dont' want them piling up but we don't want to just dump them do we? //it should be only mail ones that fail, not app ones, there's no way for an app delivery to fail as it's just put in a table diff --git a/server/AyaNova/models/GlobalOpsNotificationSettings.cs b/server/AyaNova/models/GlobalOpsNotificationSettings.cs index 793d9bc9..1a79562e 100644 --- a/server/AyaNova/models/GlobalOpsNotificationSettings.cs +++ b/server/AyaNova/models/GlobalOpsNotificationSettings.cs @@ -19,12 +19,23 @@ namespace AyaNova.Models { SmtpDeliveryActive = true; Id = 1; - SmtpServerAddress="mail.example.com"; + +#if (DEBUG) + SmtpServerAddress = "mail.ayanova.com"; + SmtpAccount = "support@ayanova.com"; + SmtpPassword = "e527b6c5a00c27bb61ca694b3de0ee178cbe3f1541a772774762ed48e9caf5ce"; + ConnectionSecurity = NotifyMailSecurity.StartTls; + SmtpServerPort = 465; + NotifyFromAddress = "support@ayanova.com"; +#else + SmtpServerAddress="mail.example.com"; SmtpAccount="notifydeliverfromaccount@example.com"; SmtpPassword="examplepassword"; ConnectionSecurity= NotifyMailSecurity.SSLTLS; SmtpServerPort=587; NotifyFromAddress="noreply@example.com"; +#endif + } } }