Skip to content

Commit

Permalink
Work in support of #468.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Jul 20, 2024
1 parent 43c5aca commit 7588c7e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 74 deletions.
98 changes: 90 additions & 8 deletions NumberSearch.Mvc/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
using Microsoft.AspNetCore.Mvc;

using NumberSearch.DataAccess;
using NumberSearch.DataAccess.BulkVS;
using NumberSearch.DataAccess.InvoiceNinja;
using NumberSearch.Mvc.Models;

using Org.BouncyCastle.Bcpg.Sig;

using Serilog;

using System;
Expand Down Expand Up @@ -201,7 +204,7 @@ public async Task<IActionResult> CheckoutAsync()
_ = cart.SetToSession(HttpContext.Session);
}

return View("Order", cart);
return View("Order", new CartResult { Cart = cart });
}

// Show orders that have already been submitted.
Expand Down Expand Up @@ -285,7 +288,7 @@ public async Task<IActionResult> ExistingOrderAsync(Guid Id, bool? AddPortingInf
}
else
{
return View("Order", cart);
return View("Order", new CartResult { Cart = cart });

}
}
Expand Down Expand Up @@ -329,15 +332,17 @@ public async Task<IActionResult> PortingInformationForOrderByIdAsync(Guid Id)
[HttpPost("Cart/Submit")]
[ValidateAntiForgeryToken]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> SubmitAsync(Order order)
public async Task<IActionResult> SubmitAsync(CartResult input)
{
var order = input.Cart.Order;

if (order is not null && !string.IsNullOrWhiteSpace(order.Email))
{
order.DateSubmitted = DateTime.Now;

await HttpContext.Session.LoadAsync().ConfigureAwait(false);
var cart = Cart.GetFromSession(HttpContext.Session);

cart.Order = order;

// This is purely so that we can isolate the state of this call when it fails out.
Log.Information(JsonSerializer.Serialize(cart));

Expand All @@ -354,16 +359,91 @@ public async Task<IActionResult> SubmitAsync(Order order)
}
else
{
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] Email address {order.Email} has an invalid domain: {emailDomain.Host}.");
var message = $"💀 The email server at {emailDomain.Host} didn't have an MX record. Please supply a valid email address.";
return View("Index", new CartResult { Message = message, Cart = cart });
return View("Order", new CartResult { Message = message, Cart = cart });
}
}
catch (Exception ex)
{
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] Email address {order.Email} has an invalid domain: {emailDomain.Host}.");
var message = $"💀 The email server at {emailDomain.Host} didn't have an MX record. Please supply a valid email address.";
return View("Index", new CartResult { Message = message, Cart = cart });
return View("Order", new CartResult { Message = message, Cart = cart });
}

// Validate the install date.
if (order.InstallDate is not null && order.InstallDate < DateTime.Now.AddDays(1))
{
order.InstallDate = null;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] The install date needs to be at least one day in the future.");
var message = $"💀 The install date needs to be at least one day in the future.";
return View("Order", new CartResult { Message = message, Cart = cart });
}

if (order.FirstName == order.BusinessName || order.LastName == order.BusinessName)
{
order.FirstName = string.Empty;
order.LastName = string.Empty;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] Your business name cannot be the same as your last name or first name.");
var message = $"💀 Your business name cannot be the same as your last name or first name.";
return View("Order", new CartResult { Message = message, Cart = cart });
}

if (!string.IsNullOrWhiteSpace(order.AddressUnitNumber) && string.IsNullOrWhiteSpace(order.AddressUnitType))
{
order.AddressUnitNumber = string.Empty;
order.AddressUnitType = string.Empty;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] Please set the Unit Type for the Unit Number you provided.");
var message = $"💀 Please set the Unit Type for the Unit Number you provided.";
return View("Order", new CartResult { Message = message, Cart = cart });
}

if (order.Address == order.AddressUnitNumber)
{
order.AddressUnitNumber = string.Empty;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] The billing address and unit number cannot be the same.");
var message = $"💀 The billing address and unit number cannot be the same.";
return View("Order", new CartResult { Message = message, Cart = cart });
}

var checkParsed = PhoneNumbersNA.PhoneNumber.TryParse(order.ContactPhoneNumber, out var contact);

if (checkParsed is false)
{
order.ContactPhoneNumber = string.Empty;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] The Direct phone number is not a dialable North American phone number.");
var message = $"💀 The Direct phone number is not a dialable North American phone number.";
return View("Order", new CartResult { Message = message, Cart = cart });
}
else
{
try
{
var checkPortable = await ValidatePortability.GetAsync(contact.DialedNumber, _configuration.BulkVSUsername, _configuration.BulkVSPassword);
if (checkPortable is null || checkPortable?.Portable is false)
{
order.ContactPhoneNumber = string.Empty;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] The contact phone number is not a dialable North American phone number.");
var message = $"💀 The contact phone number is not a dialable North American phone number.";
return View("Order", new CartResult { Message = message, Cart = cart });
}
}
catch (Exception ex)
{
order.ContactPhoneNumber = string.Empty;
_ = cart.SetToSession(HttpContext.Session);
Log.Error($"[Checkout] The contact phone number is not a dialable North American phone number.");
var message = $"💀 The contact phone number is not a dialable North American phone number.";
return View("Order", new CartResult { Message = message, Cart = cart });
}
}

if (cart.ProductOrders is null || !cart.ProductOrders.Any())
Expand All @@ -374,9 +454,11 @@ public async Task<IActionResult> SubmitAsync(Order order)
// Reset the session and clear the Cart.
HttpContext.Session.Clear();

return View("Index", new CartResult { Message = "💀 The server disconnected and your Cart was lost. Please try it again now." });
return View("Order", new CartResult { Message = "💀 The server disconnected and your Cart was lost. Please try it again now." });
}

order.DateSubmitted = DateTime.Now;

if (order.OrderId != Guid.Empty)
{
var orderExists = await Order.GetByIdAsync(order.OrderId, _postgresql).ConfigureAwait(false);
Expand Down
4 changes: 3 additions & 1 deletion NumberSearch.Mvc/Models/CartResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NumberSearch.Mvc
using NumberSearch.DataAccess;

namespace NumberSearch.Mvc
{
public class CartResult
{
Expand Down
Loading

0 comments on commit 7588c7e

Please sign in to comment.