Skip to content

Commit

Permalink
Continued work in support of #383.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Aug 15, 2023
1 parent b364b03 commit 673c53f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
55 changes: 52 additions & 3 deletions NumberSearch.Ops/Controllers/MessagingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Elfie.Model.Structures;
using Microsoft.EntityFrameworkCore;

using Models;
Expand Down Expand Up @@ -39,22 +40,70 @@ public async Task<IActionResult> IndexAsync()
var stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync<UsageSummary[]>();
var failures = await $"{_baseUrl}message/all/failed?start={DateTime.Now.AddDays(-3).ToShortDateString()}&end={DateTime.Now.AddDays(1).ToShortDateString()}".WithOAuthBearerToken(_messagingToken).GetJsonAsync<MessageRecord[]>();
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<UsageSummary[]>();
}

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<IActionResult> 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<ClientRegistration>();
message = $"🔃 Upstream Status {refresh.UpstreamStatusDescription} for {refresh.AsDialed} routed to {refresh.CallbackUrl}";
}
var stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync<UsageSummary[]>();
var failures = await $"{_baseUrl}message/all/failed?start={DateTime.Now.AddDays(-3).ToShortDateString()}&end={DateTime.Now.AddDays(1).ToShortDateString()}".WithOAuthBearerToken(_messagingToken).GetJsonAsync<MessageRecord[]>();
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<IActionResult> 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<ClientRegistration>();
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<RegistrationResponse>();
message = $"✔️ Reregistration complete! {response.Message}";
}
var stats = await $"{_baseUrl}client/usage".WithOAuthBearerToken(_messagingToken).GetJsonAsync<UsageSummary[]>();
var failures = await $"{_baseUrl}message/all/failed?start={DateTime.Now.AddDays(-3).ToShortDateString()}&end={DateTime.Now.AddDays(1).ToShortDateString()}".WithOAuthBearerToken(_messagingToken).GetJsonAsync<MessageRecord[]>();
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 });
}
}
}
2 changes: 2 additions & 0 deletions NumberSearch.Ops/Models/MessagingResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class MessagingResult
public UsageSummary[] UsageSummary { get; set; } = Array.Empty<UsageSummary>();
public MessageRecord[] FailedMessages { get; set; } = Array.Empty<MessageRecord>();
public OwnedPhoneNumber[] Owned { get; set; } = Array.Empty<OwnedPhoneNumber>();
public string Message { get; set; } = string.Empty;
public string AlertType { get; set; } = string.Empty;
}
}
44 changes: 36 additions & 8 deletions NumberSearch.Ops/Views/Messaging/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
<div class="d-flex justify-content-between align-items-center">
<h1 class="display-4 p-3">✉️ Messaging</h1>
</div>
@if (!string.IsNullOrWhiteSpace(Model?.Message))
{
if (string.IsNullOrWhiteSpace(Model?.AlertType))
{
<div class="alert alert-danger col-12" role="alert">
@Model?.Message
</div>
}
else
{
var alertType = Model?.AlertType ?? "alert-danger";
<div class="alert @alertType col-12" role="alert">
@Model?.Message
</div>
}
}
<h3>Failed Messages in the Last 3 Days</h3>
<div class="table-responsive">
<table class="table table-striped table-borderless table-hover" id="table">
<thead>
<tr>
@* <th>Succeeded?</th> *@
@* <th>Succeeded?</th> *@
<th>Date Received PST</th>
<th>Message Source</th>
<th>Message Type</th>
Expand All @@ -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 ? $"<a href='/Home/OwnedNumbers/{ownedPhoneNumber.DialedNumber}' target='_blank'>{result.To}</a>" : result.To;

<tr>
@*<td>@result.Succeeded</td>*@
<td>@result.DateReceivedUTC.ToLocalTime().ToShortDateString() @result.DateReceivedUTC.ToLocalTime().ToShortTimeString()</td>
@*<td>@result.Succeeded</td>*@
<td data-order="@result.DateReceivedUTC.Ticks">@result.DateReceivedUTC.ToLocalTime().ToShortDateString() @result.DateReceivedUTC.ToLocalTime().ToShortTimeString()</td>
<td>@result.MessageSource</td>
<td>@result.MessageType</td>
<td>@Html.Raw(FromLink)</td>
Expand All @@ -48,7 +64,7 @@
<td>@result.Content</td>
@* <td>@result.MediaURLs</td> *@
@*<td>@result.RawRequest</td>
<td>@result.ToForward</td> *@
<td>@result.ToForward</td> *@
</tr>
}
</tbody>
Expand Down Expand Up @@ -82,9 +98,21 @@
<td>@result.OutboundSMSCount</td>
<td>@result.InboundSMSCount</td>
<td>@result.InboundMMSCount</td>
<td>@result.RegisteredUpstream</td>
<td>@result.UpstreamStatusDescription</td>
<td><a class="btn btn-primary" href="./[email protected]">Refresh Upstream</a></td>
@if (result.AsDialed is not "Total")
{
<td>@result.RegisteredUpstream</td>
<td>@result.UpstreamStatusDescription</td>
<td>
<a class="btn btn-primary" href="./[email protected]">Refresh Upstream</a>
<a class="btn btn-primary" href="./[email protected]">Reregister</a>
</td>
}
else
{
<td></td>
<td></td>
<td>As of @DateTime.Now.ToShortDateString() @DateTime.Now.ToShortTimeString()</td>
}
</tr>
}
</tbody>
Expand Down

0 comments on commit 673c53f

Please sign in to comment.