Skip to content

Commit

Permalink
Added linking to Owned Numbers for To and From Numbers in the Failed …
Browse files Browse the repository at this point in the history
…Messages table in support of #383.
  • Loading branch information
uncheckederror committed Aug 14, 2023
1 parent f16efc2 commit 42b47d4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
60 changes: 45 additions & 15 deletions Messaging/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,20 @@
messagebody = message.Message
};
var record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
DateReceivedUTC = DateTime.UtcNow,
From = message?.MSISDN ?? "MSISDN was blank",
To = message?.To ?? "To was blank",
MediaURLs = string.Empty,
MessageSource = MessageSource.Outgoing,
MessageType = message?.MediaURLs.Length > 0 ? MessageType.MMS : MessageType.SMS,
RawRequest = System.Text.Json.JsonSerializer.Serialize(message),
RawResponse = $"MSISDN {message?.MSISDN} could not be parsed as valid NANP (North American Numbering Plan) number.",
};
if (!string.IsNullOrWhiteSpace(message.MSISDN))
{
bool checkFrom = PhoneNumbersNA.PhoneNumber.TryParse(message.MSISDN, out var fromPhoneNumber);
Expand All @@ -721,7 +735,7 @@
{
try
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
Expand Down Expand Up @@ -775,7 +789,7 @@
{
try
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
Expand Down Expand Up @@ -822,6 +836,11 @@
string filename = url.Segments.LastOrDefault() ?? string.Empty;
var data = await fileURL.GetBytesAsync();
multipartContent.Add(new ByteArrayContent(data), "ufiles", filename);
if (filename.Contains(".txt"))
{
record.Content = await url.GetStringAsync();
}
}
// pass them on to the vendor
Expand All @@ -833,10 +852,9 @@
if (sendMessage is not null && sendMessage?.Response?.Text is "OK")
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
DateReceivedUTC = DateTime.UtcNow,
From = message?.MSISDN ?? "MSISDN was blank",
To = message?.To ?? "To was blank",
Expand All @@ -860,10 +878,9 @@
}
else
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
DateReceivedUTC = DateTime.UtcNow,
From = message?.MSISDN ?? "MSISDN was blank",
To = message?.To ?? "To was blank",
Expand Down Expand Up @@ -898,7 +915,7 @@
if (sendMessage is not null && sendMessage?.Response?.Text is "OK")
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
Expand All @@ -925,7 +942,7 @@
}
else
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
Expand All @@ -951,7 +968,7 @@
var error = await ex.GetResponseStringAsync();
Log.Error(error);
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
Expand All @@ -977,7 +994,7 @@
try
{
var record = new MessageRecord
record = new MessageRecord
{
Id = Guid.NewGuid(),
Content = message?.Message ?? string.Empty,
Expand Down Expand Up @@ -1046,6 +1063,7 @@
MessageRecord messageRecord = new MessageRecord
{
RawRequest = incomingRequest,
Content = toForward.Content,
DateReceivedUTC = DateTime.UtcNow,
MessageSource = MessageSource.Incoming,
MessageType = MessageType.MMS,
Expand Down Expand Up @@ -1164,6 +1182,12 @@
};
await fileUtil.UploadAsync(fileRequest);
mediaURLs.Add($"{spacesConfig.ServiceURL}{fileRequest.BucketName}/{fileRequest.Key}");
// For debugging in Ops
if (file.Contains(".txt"))
{
messageRecord.Content = await fileDownloadURL.GetStringAsync();
}
}
}
}
Expand All @@ -1177,14 +1201,15 @@
if (client is not null && client.AsDialed == toRegisteredNumber.DialedNumber)
{
toForward.ClientSecret = client.ClientSecret;
messageRecord.ToForward = System.Text.Json.JsonSerializer.Serialize(toForward);
try
{
toForward.ClientSecret = client.ClientSecret;
var response = await client.CallbackUrl.PostJsonAsync(toForward);
string responseText = await response.GetStringAsync();
Log.Information(responseText);
Log.Information(System.Text.Json.JsonSerializer.Serialize(toForward));
messageRecord.ToForward = System.Text.Json.JsonSerializer.Serialize(toForward);
messageRecord.RawResponse = responseText;
messageRecord.Succeeded = true;
}
Expand Down Expand Up @@ -1274,6 +1299,7 @@
MessageRecord messageRecord = new MessageRecord
{
RawRequest = incomingRequest,
Content = toForward.Content,
DateReceivedUTC = DateTime.UtcNow,
MessageSource = MessageSource.Incoming,
MessageType = MessageType.SMS,
Expand Down Expand Up @@ -1371,14 +1397,16 @@
if (client is not null && client.AsDialed == toRegisteredNumber.DialedNumber)
{
toForward.ClientSecret = client.ClientSecret;
messageRecord.Content = toForward.Content;
messageRecord.ToForward = System.Text.Json.JsonSerializer.Serialize(toForward);
try
{
toForward.ClientSecret = client.ClientSecret;
var response = await client.CallbackUrl.PostJsonAsync(toForward);
string responseText = await response.GetStringAsync();
Log.Information(responseText);
Log.Information(System.Text.Json.JsonSerializer.Serialize(toForward));
messageRecord.ToForward = System.Text.Json.JsonSerializer.Serialize(toForward);
messageRecord.RawResponse = responseText;
messageRecord.Succeeded = true;
}
Expand All @@ -1389,8 +1417,9 @@
Log.Error(System.Text.Json.JsonSerializer.Serialize(client));
Log.Error(System.Text.Json.JsonSerializer.Serialize(toForward));
Log.Error($"Failed to forward message to {toForward.To}");
messageRecord.RawResponse = $"Failed to forward message to {toForward.To} {error}";
messageRecord.RawResponse = $"Failed to forward message to {toForward.To} at {client.CallbackUrl} {error}";
}
try
{
await db.Messages.AddAsync(messageRecord);
Expand All @@ -1410,6 +1439,7 @@
messageRecord.To = toForward.To;
messageRecord.From = toForward.From;
messageRecord.Content = toForward.Content;
messageRecord.ToForward = System.Text.Json.JsonSerializer.Serialize(toForward);
messageRecord.RawResponse = $"{toForward.To} is not registered as a client.";
db.Messages.Add(messageRecord);
Expand Down
5 changes: 3 additions & 2 deletions NumberSearch.Ops/Controllers/MessagingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Flurl.Http;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Models;

Expand Down Expand Up @@ -33,8 +34,8 @@ 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[]>();

return View(new MessagingResult { UsageSummary = stats.OrderByDescending(x => x.OutboundSMSCount).ToArray(), FailedMessages = failures.OrderByDescending(x => x.DateReceivedUTC).ToArray() });
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});
}
}
}
6 changes: 5 additions & 1 deletion NumberSearch.Ops/Models/MessagingResult.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Models;
using AccelerateNetworks.Operations;

using Models;

using System;
using System.Collections.Generic;

namespace NumberSearch.Ops.Models
{
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>();
}
}
29 changes: 20 additions & 9 deletions NumberSearch.Ops/Views/Messaging/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,34 @@
<th>From</th>
<th>To</th>
<th>Raw Response</th>
@* <th>Content</th> *@
<th>Content</th>
@* <th>Media URLs</th> *@
<th>Raw Request</th>
<th>To Forward</th>
@* <th>Raw Request</th>
<th>To Forward</th> *@
</tr>
</thead>
<tbody>
@foreach (var result in Model.FailedMessages)
{
var checkParse = PhoneNumbersNA.PhoneNumber.TryParse(result.From, out var fromPhoneNumber);
var ownedPhoneNumber = Model.Owned.FirstOrDefault(x => x.DialedNumber == fromPhoneNumber.DialedNumber);
string FromLink = checkParse && ownedPhoneNumber is not null ? $"<a href='/Home/OwnedNumbers/{ownedPhoneNumber.DialedNumber}' target='_blank'>{result.From}</a>" : result.From;
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.MessageSource</td>
<td>@result.MessageType</td>
<td>@result.From</td>
<td>@result.To</td>
<td>@Html.Raw(FromLink)</td>
<td>@Html.Raw(ToLink)</td>
<td>@result.RawResponse</td>
@* <td>@result.Content</td> *@
<td>@result.Content</td>
@* <td>@result.MediaURLs</td> *@
<td>@result.RawRequest</td>
<td>@result.ToForward</td>
@* <td>@result.RawRequest</td>
<td>@result.ToForward</td> *@

</tr>
}
Expand All @@ -63,8 +70,12 @@
<tbody>
@foreach (var result in Model.UsageSummary)
{
var checkParse = PhoneNumbersNA.PhoneNumber.TryParse(result.AsDialed, out var phoneNumber);
var ownedPhoneNumber = Model.Owned.FirstOrDefault(x => x.DialedNumber == phoneNumber.DialedNumber);
string AsDialedLink = checkParse && ownedPhoneNumber is not null ? $"<a href='/Home/OwnedNumbers/{ownedPhoneNumber.DialedNumber}' target='_blank'>{result.AsDialed}</a>" : result.AsDialed;

<tr>
<td>@result.AsDialed</td>
<td>@Html.Raw(AsDialedLink)</td>
<td>@result.OutboundMMSCount</td>
<td>@result.OutboundSMSCount</td>
<td>@result.InboundSMSCount</td>
Expand Down

0 comments on commit 42b47d4

Please sign in to comment.