From e82c9741b3a7431a060c92b60fee36ea8add3d09 Mon Sep 17 00:00:00 2001 From: Thomas Ryan Date: Thu, 3 Aug 2023 20:38:45 -0700 Subject: [PATCH] Enhanced and endpoint to provide messaging reciepts on a specific dialed number in support of #376. --- Messaging/Program.cs | 69 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/Messaging/Program.cs b/Messaging/Program.cs index 7a1149db..372163ae 100644 --- a/Messaging/Program.cs +++ b/Messaging/Program.cs @@ -25,6 +25,7 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.ServiceModel.Channels; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -507,29 +508,71 @@ }) .RequireAuthorization().WithOpenApi(x => new(x) { Summary = "Register a client for message forwarding.", Description = "Boy I wish I had more to say about this, lmao." }); - app.MapGet("/message/all", async Task, NotFound, BadRequest>> (MessagingContext db) => + app.MapGet("/message/all", async Task, NotFound, BadRequest>> (MessagingContext db, string? asDialed) => { - try + if (string.IsNullOrWhiteSpace(asDialed)) { - var messages = await db.Messages.OrderByDescending(x => x.DateReceivedUTC).Take(10).ToArrayAsync(); - - if (messages is not null && messages.Any()) + try { - return TypedResults.Ok(messages); + var messages = await db.Messages.OrderByDescending(x => x.DateReceivedUTC).Take(100).ToArrayAsync(); + + if (messages is not null && messages.Any()) + { + return TypedResults.Ok(messages); + } + else + { + return TypedResults.NotFound($"No messages have been recorded."); + } } - else + catch (Exception ex) { - return TypedResults.NotFound($"No messages have been recorded."); + Log.Error(ex.Message); + Log.Error(ex.StackTrace ?? "No stacktrace found."); + return TypedResults.BadRequest(ex.Message); } } - catch (Exception ex) + else { - Log.Error(ex.Message); - Log.Error(ex.StackTrace ?? "No stacktrace found."); - return TypedResults.BadRequest(ex.Message); + try + { + bool checkFrom = PhoneNumbersNA.PhoneNumber.TryParse(asDialed, out var fromPhoneNumber); + if (checkFrom && fromPhoneNumber is not null && !string.IsNullOrWhiteSpace(fromPhoneNumber.DialedNumber)) + { + if (fromPhoneNumber.Type is not PhoneNumbersNA.NumberType.ShortCode) + { + asDialed = $"1{fromPhoneNumber.DialedNumber}"; + } + else + { + asDialed = fromPhoneNumber.DialedNumber; + } + + var messages = await db.Messages.Where(x => x.From == asDialed || x.To.Contains(asDialed)).OrderByDescending(x => x.DateReceivedUTC).ToArrayAsync(); + + if (messages is not null && messages.Any()) + { + return TypedResults.Ok(messages); + } + else + { + return TypedResults.NotFound($"No messages have been recorded."); + } + } + else + { + return TypedResults.BadRequest($"asDialed {asDialed} could not be parsed as valid NANP (North American Numbering Plan) number."); + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + Log.Error(ex.StackTrace ?? "No stacktrace found."); + return TypedResults.BadRequest(ex.Message); + } } }) - .RequireAuthorization().WithOpenApi(x => new(x) { Summary = "View all sent and received messages.", Description = "This is intended to help you debug problems with message sending and delievery so you can see if it's this API or the upstream vendor that is causing problems." }); + .RequireAuthorization().WithOpenApi(x => new(x) { Summary = "View all sent and received messages.", Description = "This is intended to help you debug problems with message sending and delivery so you can see if it's this API or the upstream vendor that is causing problems." }); app.MapPost("/message/send", async Task, BadRequest>> ([Microsoft.AspNetCore.Mvc.FromBody] SendMessageRequest message, bool? test, MessagingContext db) => {