diff --git a/NumberSearch.DataAccess/InvoiceNinja/Invoice.cs b/NumberSearch.DataAccess/InvoiceNinja/Invoice.cs index 4e593fff..e07d18f1 100644 --- a/NumberSearch.DataAccess/InvoiceNinja/Invoice.cs +++ b/NumberSearch.DataAccess/InvoiceNinja/Invoice.cs @@ -165,6 +165,7 @@ public class InvoiceDatum public string vendor_id { get; set; } = string.Empty; public string status_id { get; set; } = string.Empty; public string design_id { get; set; } = string.Empty; + public string invoice_id { get; set; } = string.Empty; public string recurring_id { get; set; } = string.Empty; public int created_at { get; set; } public int updated_at { get; set; } diff --git a/NumberSearch.DataAccess/Models/Email.cs b/NumberSearch.DataAccess/Models/Email.cs index b25a6b92..f93ae84f 100644 --- a/NumberSearch.DataAccess/Models/Email.cs +++ b/NumberSearch.DataAccess/Models/Email.cs @@ -18,7 +18,7 @@ namespace NumberSearch.DataAccess { public class Email { - public Guid EmailId { get; set; } + public Guid EmailId { get; set; } = Guid.NewGuid(); public Guid OrderId { get; set; } public string PrimaryEmailAddress { get; set; } = string.Empty; public string SalesEmailAddress { get; set; } = string.Empty; @@ -133,6 +133,12 @@ public async Task SendEmailAsync(string username, string password) outboundMessage.Cc.Add(sales); } + if (!string.IsNullOrWhiteSpace(CarbonCopy) && CarbonCopy.Contains("@")) + { + var cc = MailboxAddress.Parse(CarbonCopy); + outboundMessage.Cc.Add(cc); + } + // If there's an attachment send it, if not just send the body. //if (Multipart != null && Multipart.Count > 0) //{ diff --git a/NumberSearch.DataAccess/Models/Order.cs b/NumberSearch.DataAccess/Models/Order.cs index f8021721..a6c8236d 100644 --- a/NumberSearch.DataAccess/Models/Order.cs +++ b/NumberSearch.DataAccess/Models/Order.cs @@ -87,6 +87,21 @@ public static async Task> GetAllAsync(string connectionString return result; } + public static async Task> GetAllQuotesAsync(string connectionString) + { + await using var connection = new NpgsqlConnection(connectionString); + + var result = await connection + .QueryAsync + ("SELECT \"OrderId\", \"FirstName\", \"LastName\", \"Email\", \"Address\", \"Address2\", \"City\", \"State\", \"Zip\", \"DateSubmitted\", \"BusinessName\", \"CustomerNotes\", \"BillingClientId\", \"BillingInvoiceId\", \"Quote\", \"BillingInvoiceReoccuringId\", \"SalesEmail\", \"BackgroundWorkCompleted\", \"Completed\", \"InstallDate\", \"UpfrontInvoiceLink\", \"ReoccuringInvoiceLink\", \"OnsiteInstallation\", \"AddressUnitType\", \"AddressUnitNumber\", \"UnparsedAddress\", \"MergedOrderId\", \"E911ServiceNumber\", \"DateConvertedFromQuote\", \"DateCompleted\", \"ContactPhoneNumber\" " + + "FROM public.\"Orders\" " + + "WHERE \"Quote\" = true " + + "ORDER BY \"DateSubmitted\" DESC") + .ConfigureAwait(false); + + return result; + } + public static async Task> GetByBackGroundworkNotCompletedAsync(string connectionString) { await using var connection = new NpgsqlConnection(connectionString); diff --git a/NumberSearch.Ingest/Orders.cs b/NumberSearch.Ingest/Orders.cs index a7d26c60..9158d540 100644 --- a/NumberSearch.Ingest/Orders.cs +++ b/NumberSearch.Ingest/Orders.cs @@ -1,4 +1,7 @@ -using NumberSearch.DataAccess; +using Flurl.Http; + +using NumberSearch.DataAccess; +using NumberSearch.DataAccess.InvoiceNinja; using Serilog; @@ -248,5 +251,93 @@ public static async Task DailyBriefingEmailAsync(IngestConfiguration appCo return checkSend && checkSave; } + + public async static Task CheckForQuoteConversionsAsync(string postgresql, string invoiceNinjaToken, string emailUsername, string emailPassword) + { + Log.Information($"[Quote Conversion] Looking for quotes that were converted to invoices in the billing system."); + + var orders = await Order.GetAllQuotesAsync(postgresql).ConfigureAwait(false); + + // Don't both checking orders that are from before we upgraded to the current version of invoiceNinja. + foreach (var order in orders.Where(x => x.DateSubmitted > DateTime.Parse("02/01/2023"))) + { + // Get the quotes in invoice ninja and see if they've been converted + try + { + if (!string.IsNullOrWhiteSpace(order.BillingInvoiceId)) + { + var upfront = await Invoice.GetQuoteByIdAsync(order.BillingInvoiceId, invoiceNinjaToken); + + if (upfront is not null && upfront.id == order.BillingInvoiceId && !string.IsNullOrWhiteSpace(upfront.invoice_id)) + { + var convertedInvoice = await Invoice.GetByIdAsync(upfront.invoice_id, invoiceNinjaToken); + + string newUpfrontLink = convertedInvoice.invitations.FirstOrDefault()?.link ?? string.Empty; + + order.BillingInvoiceId = convertedInvoice.id; + order.UpfrontInvoiceLink = string.IsNullOrWhiteSpace(newUpfrontLink) ? order.UpfrontInvoiceLink : newUpfrontLink; + order.Quote = false; + order.DateConvertedFromQuote = DateTime.Now; + + var checkUpdate = await order.PutAsync(postgresql); + string name = string.IsNullOrWhiteSpace(order.BusinessName) ? $"{order.FirstName} {order.LastName}" : order.BusinessName; + var message = new Email + { + SalesEmailAddress = order.SalesEmail ?? "sales@acceleratenetworks.com", + PrimaryEmailAddress = "orders@acceleratenetworks.com", + CarbonCopy = "thomas.ryan@outlook.com", + Subject = $"Quote {upfront.number} has been approved by {name}", + OrderId = order.OrderId, + MessageBody = $@"

Hi Sales Team,

The new invoice can be viewed here. and the order can be edited here, please follow up with the customer to set an install date and collect payment.

Have a great day, hombre! 🤠

" + }; + + // Send the message the email server. + var checkSend = await message.SendEmailAsync(emailUsername,emailPassword).ConfigureAwait(false); + + // If it didn't work try it again. + if (!checkSend) + { + checkSend = await message.SendEmailAsync(emailUsername, emailPassword).ConfigureAwait(false); + } + + // Mark it as sent. + message.DateSent = DateTime.Now; + message.DoNotSend = false; + message.Completed = checkSend; + + // Update the database with the email's new status. + var checkSave = await message.PostAsync(postgresql).ConfigureAwait(false); + + // Log the success or failure of the operation. + if (checkSend && checkSave) + { + Log.Information($"[Quote Conversion] Successfully sent out email {message.EmailId} for order {order.OrderId}."); + } + else + { + Log.Fatal($"[Quote Conversion] Failed to sent out the email {message.EmailId} for order {order.OrderId}."); + } + } + } + + // This is too hard we'll deal with it later + //if (!string.IsNullOrWhiteSpace(order.BillingInvoiceReoccuringId)) + //{ + // var reoccurringQuote = Invoice.GetQuoteByIdAsync(order.BillingInvoiceReoccuringId, _mvcConfiguration.InvoiceNinjaToken); + // var reoccurring = ReccurringInvoice.GetByIdAsync(order.BillingInvoiceReoccuringId, _mvcConfiguration.InvoiceNinjaToken); + //} + } + catch (FlurlHttpException ex) + { + var error = await ex.GetResponseStringAsync(); + Log.Error(error); + } + catch (Exception ex) + { + Log.Error(ex.Message); + Log.Error(ex.StackTrace ?? "No stack trace found."); + } + } + } } } diff --git a/NumberSearch.Ingest/Program.cs b/NumberSearch.Ingest/Program.cs index 70483c07..61111b5e 100644 --- a/NumberSearch.Ingest/Program.cs +++ b/NumberSearch.Ingest/Program.cs @@ -34,7 +34,8 @@ public static async Task Main() SmtpPassword = string.IsNullOrWhiteSpace(config.GetConnectionString("SmtpPassword")) ? throw new Exception("SmtpPassword config key is blank.") : config.GetConnectionString("SmtpPassword") ?? string.Empty, EmailOrders = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailOrders")) ? throw new Exception("EmailOrders config key is blank.") : config.GetConnectionString("EmailOrders") ?? string.Empty, EmailDan = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailDan")) ? throw new Exception("EmailDan config key is blank.") : config.GetConnectionString("EmailDan") ?? string.Empty, - EmailTom = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("EmailTom config key is blank.") : config.GetConnectionString("EmailTom") ?? string.Empty + EmailTom = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("EmailTom config key is blank.") : config.GetConnectionString("EmailTom") ?? string.Empty, + InvoiceNinjaToken = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("InvoiceNinjaToken config key is blank.") : config.GetConnectionString("InvoiceNinjaToken") ?? string.Empty, }; Log.Logger = new LoggerConfiguration() @@ -82,6 +83,7 @@ public static async Task Main() await Provider.VerifyAddToCartAsync(AreaCode.Priority, "Executive", appConfig.Postgresql, appConfig.BulkVSUsername, appConfig.BulkVSPassword, appConfig.PComNetUsername, appConfig.PComNetPassword); await Owned.MatchOwnedNumbersToFusionPBXAsync(appConfig.Postgresql, appConfig.FusionPBXUsername, appConfig.FusionPBXPassword); + await Orders.CheckForQuoteConversionsAsync(appConfig.Postgresql, appConfig.InvoiceNinjaToken, appConfig.SmtpUsername, appConfig.SmtpPassword); } // Daily Ingest diff --git a/NumberSearch.Tests/Ingest.cs b/NumberSearch.Tests/Ingest.cs index 991e6a0f..b6379667 100644 --- a/NumberSearch.Tests/Ingest.cs +++ b/NumberSearch.Tests/Ingest.cs @@ -80,6 +80,7 @@ public FunctionalIngest(ITestOutputHelper output) EmailTom = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("EmailTom config key is blank.") : config.GetConnectionString("EmailTom") ?? string.Empty, FusionPBXUsername = string.IsNullOrWhiteSpace(config.GetConnectionString("FusionPBXUsername")) ? throw new Exception("FusionPBXUsername config key is blank.") : config.GetConnectionString("FusionPBXUsername") ?? string.Empty, FusionPBXPassword = string.IsNullOrWhiteSpace(config.GetConnectionString("FusionPBXPassword")) ? throw new Exception("FusionPBXPassword config key is blank.") : config.GetConnectionString("FusionPBXPassword") ?? string.Empty, + InvoiceNinjaToken = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("InvoiceNinjaToken config key is blank.") : config.GetConnectionString("InvoiceNinjaToken") ?? string.Empty, }; ingestConfiguration = appConfig; } @@ -90,6 +91,12 @@ public FunctionalIngest(ITestOutputHelper output) // await PortRequests.UpdatePortRequestByExternalIdAsync(ingestConfiguration); //} + //[Fact] + //public async Task TestCheckForQuoteConversionsAsync() + //{ + // await Orders.CheckForQuoteConversionsAsync(ingestConfiguration.Postgresql, ingestConfiguration.InvoiceNinjaToken, ingestConfiguration.SmtpUsername, ingestConfiguration.SmtpPassword); + //} + //[Fact] //public async Task TestOwnedNumbersIngestAsync() //{ diff --git a/NumberSearch.Tests/Integration.cs b/NumberSearch.Tests/Integration.cs index 6360401d..20912ddb 100644 --- a/NumberSearch.Tests/Integration.cs +++ b/NumberSearch.Tests/Integration.cs @@ -1523,6 +1523,11 @@ public async Task GetOrderAsync() Assert.False(string.IsNullOrWhiteSpace(result.Email)); Assert.True(result.DateSubmitted > new DateTime(2019, 1, 1)); } + + var quotes = await Order.GetAllQuotesAsync(conn).ConfigureAwait(false); + + Assert.NotNull(quotes); + Assert.NotEmpty(quotes); }