diff --git a/server/AyaNova/Controllers/GlobalOpsNotificationSettingsController.cs b/server/AyaNova/Controllers/GlobalOpsNotificationSettingsController.cs new file mode 100644 index 00000000..84fe71a5 --- /dev/null +++ b/server/AyaNova/Controllers/GlobalOpsNotificationSettingsController.cs @@ -0,0 +1,134 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authorization; +using Microsoft.EntityFrameworkCore; +using AyaNova.Models; +using AyaNova.Api.ControllerHelpers; +using AyaNova.Biz; +using System.Threading.Tasks; + +namespace AyaNova.Api.Controllers +{ + + [ApiController] + [ApiVersion("8.0")] + [Route("api/v{version:apiVersion}/global-ops-notification-setting")] + [Produces("application/json")] + [Authorize] + public class GlobalOpsNotificationSettingsController : ControllerBase + { + private readonly AyContext ct; + private readonly ILogger log; + private readonly ApiServerState serverState; + + + + /// + /// ctor + /// + /// + /// + /// + public GlobalOpsNotificationSettingsController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) + { + ct = dbcontext; + log = logger; + serverState = apiServerState; + } + + /// + /// Get GlobalOpsNotificationSettings + /// + /// Global ops Notification settings object + [HttpGet] + public async Task GetGlobalOpsNotificationSettings() + { + if (serverState.IsClosed) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + + //Instantiate the business object handler + GlobalOpsNotificationSettingsBiz biz = GlobalOpsNotificationSettingsBiz.GetBiz(ct, HttpContext); + + if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) + return StatusCode(403, new ApiNotAuthorizedResponse()); + + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + + var o = await biz.GetAsync(); + if (o == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + + return Ok(ApiOkResponse.Response(o)); + } + + + /// + /// PUT Global ops Notification settings + /// + /// + /// New concurrency token + [HttpPut] + public async Task ReplaceGlobalOpsNotificationSettings([FromBody] GlobalOpsNotificationSettings updatedObject) + { + if (serverState.IsClosed) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + GlobalOpsNotificationSettingsBiz biz = GlobalOpsNotificationSettingsBiz.GetBiz(ct, HttpContext); + if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) + return StatusCode(403, new ApiNotAuthorizedResponse()); + var o = await biz.PutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token + if (o == null) + return StatusCode(409, new ApiErrorResponse(biz.Errors)); + return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency })); + } + + + /// + /// Test SMTP mail delivery + /// + /// + /// Status result + [HttpPost("test-smtp-settings/{toAddress}")] + [Authorize] + public async Task PostTestSmtpDelivery([FromRoute] string toAddress) + { + if (serverState.IsClosed) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.OpsNotificationSettings))//technically maybe this could be wider open, but for now keeping as locked down + return StatusCode(403, new ApiNotAuthorizedResponse()); + var res = await CoreJobNotify.TestSMTPDelivery(toAddress); + return Ok(ApiOkResponse.Response(res)); + } + + + + // /// + // /// Get Client app relevant GlobalOpsNotificationSettings + // /// + // /// Global ops Notification settings object + // [HttpGet("client")] + // public async Task GetClientGlobalOpsNotificationSettings() + // { + // //NOTE: currently this looks like a dupe of get above and it is + // //but it's kept here in case want to return a subset of the settings only to client users + // //where some internal server only stuff might not be desired to send to user + + // if (serverState.IsClosed) + // return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + // GlobalOpsNotificationSettingsBiz biz = GlobalOpsNotificationSettingsBiz.GetBiz(ct, HttpContext); + // //this route is available to Ops role user only + // if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) + // return StatusCode(403, new ApiNotAuthorizedResponse()); + // if (!ModelState.IsValid) + // return BadRequest(new ApiErrorResponse(ModelState)); + // var o = await biz.GetAsync(); + // if (o == null) + // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + // return Ok(ApiOkResponse.Response(o)); + // } + + }//eoc +}//ens \ No newline at end of file diff --git a/server/AyaNova/biz/AyaType.cs b/server/AyaNova/biz/AyaType.cs index 3f858bfa..85f4904a 100644 --- a/server/AyaNova/biz/AyaType.cs +++ b/server/AyaNova/biz/AyaType.cs @@ -111,7 +111,8 @@ namespace AyaNova.Biz Reminder = 52, UnitMeterReading = 53, CustomerServiceRequest = 54, - ServiceBank = 55 + ServiceBank = 55, + OpsNotificationSettings = 56 diff --git a/server/AyaNova/biz/BizRoles.cs b/server/AyaNova/biz/BizRoles.cs index 108ff68d..bbe9d572 100644 --- a/server/AyaNova/biz/BizRoles.cs +++ b/server/AyaNova/biz/BizRoles.cs @@ -449,6 +449,16 @@ namespace AyaNova.Biz ReadFullRecord = AuthorizationRoles.OpsAdminLimited | AuthorizationRoles.BizAdminFull | AuthorizationRoles.BizAdminLimited }); + //////////////////////////////////////////////////////////// + //OPERATIONS / Notification settings + //Only opsfull can change operations + //ops and biz admin can view operations + roles.Add(AyaType.OpsNotificationSettings, new BizRoleSet() + { + Change = AuthorizationRoles.OpsAdminFull, + ReadFullRecord = AuthorizationRoles.OpsAdminLimited | AuthorizationRoles.BizAdminFull | AuthorizationRoles.BizAdminLimited + }); + //////////////////////////////////////////////////////////// //SERVERMETRICS diff --git a/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs b/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs index 19b299a6..1eb6648f 100644 --- a/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs +++ b/server/AyaNova/biz/GlobalOpsNotificationSettingsBiz.cs @@ -17,7 +17,7 @@ namespace AyaNova.Biz UserId = currentUserId; UserTranslationId = userTranslationId; CurrentUserRoles = UserRoles; - BizType = AyaType.Backup; + BizType = AyaType.OpsNotificationSettings; } internal static GlobalOpsNotificationSettingsBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 3f4ccf74..402b3071 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -166,6 +166,14 @@ namespace AyaNova.Biz //If it's not possible to notify the person via in app of the failed smtp then perhaps it notifies OPS personnel and biz admin personnel } + //Called from ops notification settings to test smtp setup by delivering to address of choosing + public static async Task TestSMTPDelivery(string toAddress) + { + //DO TEST DELIVERY HERE USING EXACT SAME SETTINGS AS FOR DeliverSMTP above + return "ok"; + + } + /////////////////////////////////////////////////////////////////////