diff --git a/NumberSearch.Ops/Controllers/MessagingController.cs b/NumberSearch.Ops/Controllers/MessagingController.cs index af4794dc..c2471c16 100644 --- a/NumberSearch.Ops/Controllers/MessagingController.cs +++ b/NumberSearch.Ops/Controllers/MessagingController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Elfie.Model.Structures; using Microsoft.EntityFrameworkCore; using Models; @@ -39,22 +40,70 @@ public async Task IndexAsync() var stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); var failures = await $"{_baseUrl}message/all/failed?start={DateTime.Now.AddDays(-3).ToShortDateString()}&end={DateTime.Now.AddDays(1).ToShortDateString()}".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); var ownedNumbers = await _context.OwnedPhoneNumbers.ToArrayAsync(); - return View(new MessagingResult { UsageSummary = stats.OrderByDescending(x => x.OutboundSMSCount).ToArray(), FailedMessages = failures.OrderByDescending(x => x.DateReceivedUTC).ToArray(), Owned = ownedNumbers}); + bool refresh = false; + foreach (var number in stats) + { + if (number.RegisteredUpstream is false && number.AsDialed is not "Total") + { + bool checkParse = PhoneNumbersNA.PhoneNumber.TryParse(number.AsDialed, out var phoneNumber); + if (checkParse && phoneNumber is not null && !string.IsNullOrWhiteSpace(phoneNumber.DialedNumber)) + { + _ = await $"{_baseUrl}client?asDialed={phoneNumber.DialedNumber}".WithOAuthBearerToken(_messagingToken).GetStringAsync(); + refresh = true; + } + } + } + + if (refresh) + { + stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); + } + + return View(new MessagingResult { UsageSummary = stats.OrderByDescending(x => x.OutboundSMSCount).ToArray(), FailedMessages = failures.OrderByDescending(x => x.DateReceivedUTC).ToArray(), Owned = ownedNumbers }); } [Authorize] [Route("/Messaging/RefreshStatus")] public async Task RefreshStatusAsync(string dialedNumber) { - var checkParse = PhoneNumbersNA.PhoneNumber.TryParse(dialedNumber, out var phoneNumber); + string message = string.Empty; + string alertType = "alert-success"; + bool checkParse = PhoneNumbersNA.PhoneNumber.TryParse(dialedNumber, out var phoneNumber); + if (checkParse && phoneNumber is not null && !string.IsNullOrWhiteSpace(phoneNumber.DialedNumber)) + { + var refresh = await $"{_baseUrl}client?asDialed={phoneNumber.DialedNumber}".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); + message = $"🔃 Upstream Status {refresh.UpstreamStatusDescription} for {refresh.AsDialed} routed to {refresh.CallbackUrl}"; + } + var stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); + var failures = await $"{_baseUrl}message/all/failed?start={DateTime.Now.AddDays(-3).ToShortDateString()}&end={DateTime.Now.AddDays(1).ToShortDateString()}".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); + var ownedNumbers = await _context.OwnedPhoneNumbers.ToArrayAsync(); + return View("Index", new MessagingResult { UsageSummary = stats.OrderByDescending(x => x.OutboundSMSCount).ToArray(), FailedMessages = failures.OrderByDescending(x => x.DateReceivedUTC).ToArray(), Owned = ownedNumbers, Message = message, AlertType = alertType }); + } + + [Authorize] + [Route("/Messaging/Register")] + public async Task RegisterAsync(string dialedNumber) + { + string message = string.Empty; + string alertType = "alert-success"; + bool checkParse = PhoneNumbersNA.PhoneNumber.TryParse(dialedNumber, out var phoneNumber); if (checkParse && phoneNumber is not null && !string.IsNullOrWhiteSpace(phoneNumber.DialedNumber)) { var refresh = await $"{_baseUrl}client?asDialed={phoneNumber.DialedNumber}".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); + var registrationRequest = new RegistrationRequest + { + DialedNumber = refresh.AsDialed, + CallbackUrl = refresh.CallbackUrl, + ClientSecret = refresh.ClientSecret, + }; + var request = await $"{_baseUrl}client/register".WithOAuthBearerToken(_messagingToken).PostJsonAsync(registrationRequest); + var response = await request.GetJsonAsync(); + message = $"✔️ Reregistration complete! {response.Message}"; } var stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); var failures = await $"{_baseUrl}message/all/failed?start={DateTime.Now.AddDays(-3).ToShortDateString()}&end={DateTime.Now.AddDays(1).ToShortDateString()}".WithOAuthBearerToken(_messagingToken).GetJsonAsync(); var ownedNumbers = await _context.OwnedPhoneNumbers.ToArrayAsync(); - return View("Index", new MessagingResult { UsageSummary = stats.OrderByDescending(x => x.OutboundSMSCount).ToArray(), FailedMessages = failures.OrderByDescending(x => x.DateReceivedUTC).ToArray(), Owned = ownedNumbers }); + return View("Index", new MessagingResult { UsageSummary = stats.OrderByDescending(x => x.OutboundSMSCount).ToArray(), FailedMessages = failures.OrderByDescending(x => x.DateReceivedUTC).ToArray(), Owned = ownedNumbers, Message = message, AlertType = alertType }); } } } diff --git a/NumberSearch.Ops/Models/MessagingResult.cs b/NumberSearch.Ops/Models/MessagingResult.cs index 82214eba..1ec940c6 100644 --- a/NumberSearch.Ops/Models/MessagingResult.cs +++ b/NumberSearch.Ops/Models/MessagingResult.cs @@ -12,5 +12,7 @@ public class MessagingResult public UsageSummary[] UsageSummary { get; set; } = Array.Empty(); public MessageRecord[] FailedMessages { get; set; } = Array.Empty(); public OwnedPhoneNumber[] Owned { get; set; } = Array.Empty(); + public string Message { get; set; } = string.Empty; + public string AlertType { get; set; } = string.Empty; } } diff --git a/NumberSearch.Ops/Views/Messaging/Index.cshtml b/NumberSearch.Ops/Views/Messaging/Index.cshtml index 20d2043b..a5fdcb0a 100644 --- a/NumberSearch.Ops/Views/Messaging/Index.cshtml +++ b/NumberSearch.Ops/Views/Messaging/Index.cshtml @@ -9,12 +9,28 @@

✉️ Messaging

+@if (!string.IsNullOrWhiteSpace(Model?.Message)) +{ + if (string.IsNullOrWhiteSpace(Model?.AlertType)) + { + + } + else + { + var alertType = Model?.AlertType ?? "alert-danger"; + + } +}

Failed Messages in the Last 3 Days

- @* *@ + @* *@ @@ -36,10 +52,10 @@ checkParse = PhoneNumbersNA.PhoneNumber.TryParse(result.To, out var toPhoneNumber); ownedPhoneNumber = Model.Owned.FirstOrDefault(x => x.DialedNumber == toPhoneNumber.DialedNumber); string ToLink = checkParse && ownedPhoneNumber is not null ? $"{result.To}" : result.To; - + - @**@ - + @**@ + @@ -48,7 +64,7 @@ @* *@ @* - *@ + *@ } @@ -82,9 +98,21 @@ - - - + @if (result.AsDialed is not "Total") + { + + + + } + else + { + + + + } }
Succeeded?Succeeded?Date Received PST Message Source Message Type
@result.Succeeded@result.DateReceivedUTC.ToLocalTime().ToShortDateString() @result.DateReceivedUTC.ToLocalTime().ToShortTimeString()@result.Succeeded@result.DateReceivedUTC.ToLocalTime().ToShortDateString() @result.DateReceivedUTC.ToLocalTime().ToShortTimeString() @result.MessageSource @result.MessageType @Html.Raw(FromLink)@result.Content@result.MediaURLs@result.RawRequest@result.ToForward@result.ToForward
@result.OutboundSMSCount @result.InboundSMSCount @result.InboundMMSCount@result.RegisteredUpstream@result.UpstreamStatusDescriptionRefresh Upstream@result.RegisteredUpstream@result.UpstreamStatusDescription + Refresh Upstream + Reregister + As of @DateTime.Now.ToShortDateString() @DateTime.Now.ToShortTimeString()