Skip to content

Commit

Permalink
Worked in support of #327.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Aug 10, 2023
1 parent 900e270 commit 7d065e1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
1 change: 1 addition & 0 deletions NumberSearch.DataAccess/InvoiceNinja/Invoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
8 changes: 7 additions & 1 deletion NumberSearch.DataAccess/Models/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,6 +133,12 @@ public async Task<bool> 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)
//{
Expand Down
15 changes: 15 additions & 0 deletions NumberSearch.DataAccess/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public static async Task<IEnumerable<Order>> GetAllAsync(string connectionString
return result;
}

public static async Task<IEnumerable<Order>> GetAllQuotesAsync(string connectionString)
{
await using var connection = new NpgsqlConnection(connectionString);

var result = await connection
.QueryAsync<Order>
("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<IEnumerable<Order>> GetByBackGroundworkNotCompletedAsync(string connectionString)
{
await using var connection = new NpgsqlConnection(connectionString);
Expand Down
93 changes: 92 additions & 1 deletion NumberSearch.Ingest/Orders.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using NumberSearch.DataAccess;
using Flurl.Http;

using NumberSearch.DataAccess;
using NumberSearch.DataAccess.InvoiceNinja;

using Serilog;

Expand Down Expand Up @@ -248,5 +251,93 @@ public static async Task<bool> 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 ?? "[email protected]",
PrimaryEmailAddress = "[email protected]",
CarbonCopy = "[email protected]",
Subject = $"Quote {upfront.number} has been approved by {name}",
OrderId = order.OrderId,
MessageBody = $@"<p>Hi Sales Team,</p><p>The new invoice <a href='{order.UpfrontInvoiceLink}' target='_blank'>can be viewed here.</a> and the order <a href='https://ops.acceleratenetworks.com/Home/Order/{order.OrderId}' target='_blank'>can be edited here</a>, please follow up with the customer to set an install date and collect payment.</p><p>Have a great day, hombre! 🤠</p>"
};

// 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.");
}
}
}
}
}
4 changes: 3 additions & 1 deletion NumberSearch.Ingest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions NumberSearch.Tests/Ingest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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()
//{
Expand Down
5 changes: 5 additions & 0 deletions NumberSearch.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down

0 comments on commit 7d065e1

Please sign in to comment.