From 8ef512901fca7fe65690efaa3466aefd82558726 Mon Sep 17 00:00:00 2001 From: Thomas Ryan Date: Fri, 4 Aug 2023 16:31:53 -0700 Subject: [PATCH] Another attemp to fix the usage stats in support of #377. --- Messaging/Program.cs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Messaging/Program.cs b/Messaging/Program.cs index 8d02e14c..2d264e8e 100644 --- a/Messaging/Program.cs +++ b/Messaging/Program.cs @@ -361,11 +361,21 @@ foreach (var reg in registrations) { + var checkAsDialed = PhoneNumbersNA.PhoneNumber.TryParse(reg.AsDialed, out var asDialedNumber); - int inboundMMS = await db.Messages.Where(x => (x.From == reg.AsDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.MMS).CountAsync(); - int outboundMMS = await db.Messages.Where(x => (x.From == reg.AsDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.MMS).CountAsync(); - int inboundSMS = await db.Messages.Where(x => (x.From == reg.AsDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.SMS).CountAsync(); - int outboundSMS = await db.Messages.Where(x => (x.From == reg.AsDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.SMS).CountAsync(); + if (asDialedNumber.Type is not PhoneNumbersNA.NumberType.ShortCode) + { + asDialed = $"1{asDialedNumber.DialedNumber}"; + } + else + { + asDialed = asDialedNumber.DialedNumber; + } + + int inboundMMS = await db.Messages.Where(x => (x.From == asDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.MMS).CountAsync(); + int outboundMMS = await db.Messages.Where(x => (x.From == asDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.MMS).CountAsync(); + int inboundSMS = await db.Messages.Where(x => (x.From == asDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.SMS).CountAsync(); + int outboundSMS = await db.Messages.Where(x => (x.From == asDialed || x.To.Contains(reg.AsDialed)) && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.SMS).CountAsync(); summary.Add(new UsageSummary { AsDialed = reg.AsDialed, InboundMMSCount = inboundMMS, OutboundMMSCount = outboundMMS, InboundSMSCount = inboundSMS, OutboundSMSCount = outboundSMS }); } @@ -399,11 +409,19 @@ if (reg is not null && !string.IsNullOrWhiteSpace(reg.AsDialed)) { - var inboundMMS = await db.Messages.Where(x => x.To == reg.AsDialed && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.MMS).CountAsync(); - var outboundMMS = await db.Messages.Where(x => x.To == reg.AsDialed && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.MMS).CountAsync(); - var inboundSMS = await db.Messages.Where(x => x.To == reg.AsDialed && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.SMS).CountAsync(); - var outboundSMS = await db.Messages.Where(x => x.To == reg.AsDialed && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.SMS).CountAsync(); + if (asDialedNumber.Type is not PhoneNumbersNA.NumberType.ShortCode) + { + asDialed = $"1{asDialedNumber.DialedNumber}"; + } + else + { + asDialed = asDialedNumber.DialedNumber; + } + var inboundMMS = await db.Messages.Where(x => (x.To.Contains(asDialed) || x.From == asDialed) && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.MMS).CountAsync(); + var outboundMMS = await db.Messages.Where(x => (x.To.Contains(asDialed) || x.From == asDialed) && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.MMS).CountAsync(); + var inboundSMS = await db.Messages.Where(x => (x.To.Contains(asDialed) || x.From == asDialed) && x.MessageSource == MessageSource.Incoming && x.MessageType == MessageType.SMS).CountAsync(); + var outboundSMS = await db.Messages.Where(x => (x.To.Contains(asDialed) || x.From == asDialed) && x.MessageSource == MessageSource.Outgoing && x.MessageType == MessageType.SMS).CountAsync(); var summary = new UsageSummary { AsDialed = reg.AsDialed, InboundMMSCount = inboundMMS, OutboundMMSCount = outboundMMS, InboundSMSCount = inboundSMS, OutboundSMSCount = outboundSMS }; return TypedResults.Ok(new UsageSummary[] { summary }); }