Fix NoExtraLayerCharge persistence, appointment reminders, coat notes display, scroll restoration, and invoice Send dead-button
- Appointment reminders: add AppointmentReminderBackgroundService (60s poll), ReminderSentAt dedup stamp, NotifyAppointmentReminderAsync sends both customer email and creator staff email; AppointmentReminderStaff notification type + default template added; DateTime.Now used instead of UtcNow to match locally-stored ScheduledStartTime; ToLocalTime() double-conversion removed - NoExtraLayerCharge not persisted: flag existed on CreateQuoteItemCoatDto and was used by pricing engine but never written to JobItemCoat/QuoteItemCoat entities — every edit reset it to false and re-applied the extra layer charge; added column to both entities (migration AddNoExtraLayerChargeToCoats), both read DTOs, all 3 JobItemAssemblyService overloads, JobItemCoatSeed inner class, and existingItemsData JSON in all 5 wizard views; fixed JS template path that hard-coded noExtraLayerCharge: false - Coat notes not visible: notes were rendered in desktop job details but missing from the wizard item card summary and the mobile card view; both fixed - Scroll position lost on item save: sessionStorage save/restore added to item-wizard.js owner form submit handler; path-keyed so cross-page navigation does not restore stale position; requestAnimationFrame used for reliable mobile scroll restoration - Invoice Send dead button: #sendChannelModal was gated inside @if (isDraft) but the button targeting it fires for Sent/Overdue invoices too when customer has both email and SMS; modal moved outside the Draft guard - InitialCreate migration added for fresh database installs; Baseline migration guarded with IF OBJECT_ID check so it no-ops on fresh DBs; Razor scoping bug fixed in Customers/Index.cshtml Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PowderCoating.Application.Interfaces;
|
||||
using PowderCoating.Infrastructure.Data;
|
||||
|
||||
namespace PowderCoating.Web.BackgroundServices;
|
||||
|
||||
/// <summary>
|
||||
/// Polls every 60 seconds for appointments whose reminder window has opened and dispatches
|
||||
/// an email to the linked customer plus an in-app bell notification to company staff.
|
||||
///
|
||||
/// Deduplication strategy: after selecting candidates the service immediately stamps
|
||||
/// <c>ReminderSentAt</c> on each appointment and saves before calling the notification
|
||||
/// methods. This prevents a second loop iteration from re-sending if notifications are slow
|
||||
/// or the application restarts mid-batch. A 24-hour lookback window caps the query so that
|
||||
/// appointments that slipped through (e.g., server downtime) are silently skipped rather
|
||||
/// than sending a stale reminder.
|
||||
/// </summary>
|
||||
public class AppointmentReminderBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ILogger<AppointmentReminderBackgroundService> _logger;
|
||||
|
||||
private static readonly TimeSpan PollingInterval = TimeSpan.FromMinutes(1);
|
||||
|
||||
/// <summary>
|
||||
/// Appointments whose scheduled start is more than this far in the past are ignored even
|
||||
/// if their reminder was never sent (server was down, etc.). We do not want to blast a
|
||||
/// customer with a "your appointment is in 30 minutes" email hours after it was due.
|
||||
/// </summary>
|
||||
private static readonly TimeSpan MaxLookback = TimeSpan.FromHours(24);
|
||||
|
||||
public AppointmentReminderBackgroundService(
|
||||
IServiceScopeFactory scopeFactory,
|
||||
ILogger<AppointmentReminderBackgroundService> logger)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Long-running loop that wakes every <see cref="PollingInterval"/> (60 s) and calls
|
||||
/// <see cref="RunAsync"/>. Uses <see cref="Task.Delay"/> with the cancellation token so
|
||||
/// the service shuts down promptly when the application stops.
|
||||
/// </summary>
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("AppointmentReminderBackgroundService started.");
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(PollingInterval, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (stoppingToken.IsCancellationRequested) break;
|
||||
|
||||
await RunAsync(stoppingToken);
|
||||
}
|
||||
|
||||
_logger.LogInformation("AppointmentReminderBackgroundService stopped.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One poll iteration: find all appointments whose reminder window has opened, stamp them,
|
||||
/// then dispatch email + in-app notifications. A fresh DI scope is created per poll so that
|
||||
/// the DbContext change tracker is clean each time.
|
||||
/// </summary>
|
||||
private async Task RunAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
var notificationService = scope.ServiceProvider.GetRequiredService<INotificationService>();
|
||||
var inAppService = scope.ServiceProvider.GetRequiredService<IInAppNotificationService>();
|
||||
|
||||
// ScheduledStartTime is stored as server-local time (no UTC conversion on form submit),
|
||||
// so compare against DateTime.Now rather than UtcNow to avoid a 4-hour EDT offset.
|
||||
var now = DateTime.Now;
|
||||
var lookback = now - MaxLookback;
|
||||
|
||||
// Find appointments where:
|
||||
// - Reminder is enabled and has not been sent yet
|
||||
// - The reminder window has opened: ScheduledStartTime - ReminderMinutesBefore <= now
|
||||
// - The appointment hasn't been sitting unprocessed for more than MaxLookback
|
||||
// - The appointment status is not terminal (not cancelled, completed, no-show, etc.)
|
||||
// IgnoreQueryFilters bypasses the tenant filter — no HTTP context in a background service.
|
||||
var candidates = await db.Appointments
|
||||
.IgnoreQueryFilters()
|
||||
.Include(a => a.Customer)
|
||||
.Include(a => a.AppointmentStatus)
|
||||
.Where(a =>
|
||||
!a.IsDeleted &&
|
||||
a.IsReminderEnabled &&
|
||||
a.ReminderSentAt == null &&
|
||||
a.ScheduledStartTime > lookback &&
|
||||
EF.Functions.DateDiffMinute(now, a.ScheduledStartTime) <= a.ReminderMinutesBefore &&
|
||||
!a.AppointmentStatus.IsTerminalStatus)
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (candidates.Count == 0) return;
|
||||
|
||||
_logger.LogInformation(
|
||||
"AppointmentReminderBackgroundService: {Count} appointment reminder(s) to dispatch.",
|
||||
candidates.Count);
|
||||
|
||||
// Stamp ReminderSentAt before sending — prevents a restart from re-sending.
|
||||
var stampedAt = now;
|
||||
foreach (var appt in candidates)
|
||||
appt.ReminderSentAt = stampedAt;
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
// Now send notifications. Failures here don't roll back the stamp because we'd
|
||||
// rather skip one reminder than spam a customer on every restart.
|
||||
foreach (var appt in candidates)
|
||||
{
|
||||
if (ct.IsCancellationRequested) break;
|
||||
|
||||
try
|
||||
{
|
||||
// Email to linked customer (no-ops internally if customer has opted out)
|
||||
await notificationService.NotifyAppointmentReminderAsync(appt);
|
||||
|
||||
// In-app bell notification for company staff
|
||||
var when = appt.IsAllDay
|
||||
? appt.ScheduledStartTime.ToString("MMMM d, yyyy")
|
||||
: appt.ScheduledStartTime.ToString("MMMM d, yyyy 'at' h:mm tt");
|
||||
|
||||
await inAppService.CreateAsync(
|
||||
companyId: appt.CompanyId,
|
||||
title: $"Appointment Reminder: {appt.Title}",
|
||||
message: $"{appt.AppointmentNumber} is scheduled for {when}.",
|
||||
notificationType: "AppointmentReminder",
|
||||
link: $"/Appointments/Details/{appt.Id}",
|
||||
customerId: appt.CustomerId);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Reminder dispatched for appointment {AppointmentNumber} (id {Id}, company {CompanyId}).",
|
||||
appt.AppointmentNumber, appt.Id, appt.CompanyId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"Failed to dispatch reminder for appointment {AppointmentId}.", appt.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "AppointmentReminderBackgroundService poll failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,6 +240,7 @@ builder.Services.AddHostedService<AuditLogRetentionBackgroundService>();
|
||||
builder.Services.AddHostedService<StripeWebhookRetentionBackgroundService>();
|
||||
builder.Services.AddHostedService<SetupWizardReminderBackgroundService>();
|
||||
builder.Services.AddHostedService<RecurringTransactionService>();
|
||||
builder.Services.AddHostedService<AppointmentReminderBackgroundService>();
|
||||
builder.Services.AddScoped<ISubscriptionService, SubscriptionService>();
|
||||
builder.Services.AddScoped<IStripeService, StripeService>();
|
||||
builder.Services.AddScoped<IStripeConnectService, StripeConnectService>();
|
||||
|
||||
@@ -185,12 +185,13 @@
|
||||
<div class="mobile-card-view">
|
||||
@if (!Model.Items.Any())
|
||||
{
|
||||
var isMobileCustomerListFiltered = !string.IsNullOrEmpty(ViewBag.SearchTerm as string);
|
||||
<div class="text-center py-5">
|
||||
<i class="bi bi-inbox" style="font-size: 4rem; color: #d1d5db;"></i>
|
||||
<h5 class="mt-3 text-muted">No customers found</h5>
|
||||
<p class="text-muted mb-4">@(isCustomerListFiltered ? "No customers match your search." : "Get started by adding your first customer.")</p>
|
||||
<p class="text-muted mb-4">@(isMobileCustomerListFiltered ? "No customers match your search." : "Get started by adding your first customer.")</p>
|
||||
<a asp-action="Create" class="btn btn-primary">
|
||||
<i class="bi bi-plus-circle me-2"></i>@(isCustomerListFiltered ? "Add Customer" : "Add Your First Customer")
|
||||
<i class="bi bi-plus-circle me-2"></i>@(isMobileCustomerListFiltered ? "Add Customer" : "Add Your First Customer")
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -869,42 +869,45 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (showSendModal)
|
||||
{
|
||||
<!-- Send Channel Choice Modal (shown when customer has both email + SMS) -->
|
||||
<div class="modal fade" id="sendChannelModal" tabindex="-1" aria-labelledby="sendChannelModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header border-0 pb-0">
|
||||
<h5 class="modal-title" id="sendChannelModalLabel">
|
||||
<i class="bi bi-send text-primary me-2"></i>Send Invoice
|
||||
</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body pt-2">
|
||||
<p class="mb-3">How would you like to send <strong>@Model.InvoiceNumber</strong> to <strong>@Model.CustomerName</strong>?</p>
|
||||
<div class="d-grid gap-2">
|
||||
<button type="button" class="btn btn-outline-primary text-start" onclick="submitSendInvoice(true, false)" data-bs-dismiss="modal">
|
||||
<i class="bi bi-envelope me-2"></i>Email only
|
||||
<small class="d-block text-muted ms-4">PDF attached · @Model.CustomerEmail</small>
|
||||
</button>
|
||||
<button type="button" class="btn btn-outline-primary text-start" onclick="submitSendInvoice(false, true)" data-bs-dismiss="modal">
|
||||
<i class="bi bi-phone me-2"></i>SMS only
|
||||
<small class="d-block text-muted ms-4">View link · @smsPhone</small>
|
||||
</button>
|
||||
<button type="button" class="btn btn-primary text-start" onclick="submitSendInvoice(true, true)" data-bs-dismiss="modal">
|
||||
<i class="bi bi-send me-2"></i>Both Email & SMS
|
||||
<small class="d-block text-muted ms-4">PDF via email + view link via SMS</small>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer border-0 pt-0">
|
||||
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||
}
|
||||
|
||||
@if (showSendModal)
|
||||
{
|
||||
<!-- Send Channel Choice Modal (shown when customer has both email + SMS available).
|
||||
Lives outside the isDraft block so it also renders for Sent/Overdue invoices
|
||||
where the customer's email was added after an SMS-only initial send. -->
|
||||
<div class="modal fade" id="sendChannelModal" tabindex="-1" aria-labelledby="sendChannelModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header border-0 pb-0">
|
||||
<h5 class="modal-title" id="sendChannelModalLabel">
|
||||
<i class="bi bi-send text-primary me-2"></i>Send Invoice
|
||||
</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body pt-2">
|
||||
<p class="mb-3">How would you like to send <strong>@Model.InvoiceNumber</strong> to <strong>@Model.CustomerName</strong>?</p>
|
||||
<div class="d-grid gap-2">
|
||||
<button type="button" class="btn btn-outline-primary text-start" onclick="submitSendInvoice(true, false)" data-bs-dismiss="modal">
|
||||
<i class="bi bi-envelope me-2"></i>Email only
|
||||
<small class="d-block text-muted ms-4">PDF attached · @Model.CustomerEmail</small>
|
||||
</button>
|
||||
<button type="button" class="btn btn-outline-primary text-start" onclick="submitSendInvoice(false, true)" data-bs-dismiss="modal">
|
||||
<i class="bi bi-phone me-2"></i>SMS only
|
||||
<small class="d-block text-muted ms-4">View link · @smsPhone</small>
|
||||
</button>
|
||||
<button type="button" class="btn btn-primary text-start" onclick="submitSendInvoice(true, true)" data-bs-dismiss="modal">
|
||||
<i class="bi bi-send me-2"></i>Both Email & SMS
|
||||
<small class="d-block text-muted ms-4">PDF via email + view link via SMS</small>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer border-0 pt-0">
|
||||
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (canPay)
|
||||
|
||||
@@ -370,7 +370,8 @@
|
||||
transferEfficiency = c.TransferEfficiency,
|
||||
powderCostPerLb = c.PowderCostPerLb,
|
||||
powderToOrder = c.PowderToOrder,
|
||||
notes = c.Notes
|
||||
notes = c.Notes,
|
||||
noExtraLayerCharge = c.NoExtraLayerCharge
|
||||
}),
|
||||
prepServices = item.PrepServices.Select(ps => new {
|
||||
prepServiceId = ps.PrepServiceId,
|
||||
|
||||
@@ -653,7 +653,10 @@
|
||||
<span class="mobile-card-value">
|
||||
@foreach (var coat in item.Coats.OrderBy(c => c.Sequence))
|
||||
{
|
||||
<small class="d-block">@coat.CoatName@if (!string.IsNullOrEmpty(coat.ColorName)) { <text> – @coat.ColorName</text> }</small>
|
||||
<small class="d-block">
|
||||
@coat.CoatName@if (!string.IsNullOrEmpty(coat.ColorName)) { <text> – @coat.ColorName</text> }
|
||||
@if (!string.IsNullOrEmpty(coat.Notes)) { <text><br /><span class="fst-italic text-muted ms-2">@coat.Notes</span></text> }
|
||||
</small>
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -357,7 +357,8 @@
|
||||
transferEfficiency = c.TransferEfficiency,
|
||||
powderCostPerLb = c.PowderCostPerLb,
|
||||
powderToOrder = c.PowderToOrder,
|
||||
notes = c.Notes
|
||||
notes = c.Notes,
|
||||
noExtraLayerCharge = c.NoExtraLayerCharge
|
||||
}),
|
||||
prepServices = item.PrepServices.Select(ps => new {
|
||||
prepServiceId = ps.PrepServiceId,
|
||||
|
||||
@@ -153,7 +153,8 @@
|
||||
transferEfficiency = c.TransferEfficiency,
|
||||
powderCostPerLb = c.PowderCostPerLb,
|
||||
powderToOrder = c.PowderToOrder,
|
||||
notes = c.Notes
|
||||
notes = c.Notes,
|
||||
noExtraLayerCharge = c.NoExtraLayerCharge
|
||||
}),
|
||||
prepServices = item.PrepServices.Select(ps => new {
|
||||
prepServiceId = ps.PrepServiceId,
|
||||
|
||||
@@ -479,7 +479,8 @@
|
||||
transferEfficiency = c.TransferEfficiency,
|
||||
powderCostPerLb = c.PowderCostPerLb,
|
||||
powderToOrder = c.PowderToOrder,
|
||||
notes = c.Notes
|
||||
notes = c.Notes,
|
||||
noExtraLayerCharge = c.NoExtraLayerCharge
|
||||
})
|
||||
})))
|
||||
</script>
|
||||
|
||||
@@ -525,7 +525,8 @@
|
||||
transferEfficiency = c.TransferEfficiency,
|
||||
powderCostPerLb = c.PowderCostPerLb,
|
||||
powderToOrder = c.PowderToOrder,
|
||||
notes = c.Notes
|
||||
notes = c.Notes,
|
||||
noExtraLayerCharge = c.NoExtraLayerCharge
|
||||
})
|
||||
})))
|
||||
</script>
|
||||
|
||||
@@ -82,6 +82,21 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
const ownerForm = hfc?.closest('form');
|
||||
if (ownerForm) {
|
||||
ownerForm.addEventListener('submit', writeHiddenFields, { capture: true });
|
||||
|
||||
// Save scroll position before the form causes a full-page reload so we can
|
||||
// restore it after the server redirects back to this page. Key is path-specific
|
||||
// so navigating away and back doesn't restore a stale position.
|
||||
const scrollKey = 'wizardScrollY:' + location.pathname;
|
||||
ownerForm.addEventListener('submit', () => {
|
||||
sessionStorage.setItem(scrollKey, String(Math.round(window.scrollY)));
|
||||
}, { capture: true });
|
||||
|
||||
// Restore on load — fire after layout is painted so scrollTo lands correctly.
|
||||
const savedY = sessionStorage.getItem(scrollKey);
|
||||
if (savedY !== null) {
|
||||
sessionStorage.removeItem(scrollKey);
|
||||
requestAnimationFrame(() => window.scrollTo({ top: parseInt(savedY, 10), behavior: 'instant' }));
|
||||
}
|
||||
}
|
||||
|
||||
// Close any open powder combobox or catalog lookup dropdown when clicking outside it
|
||||
@@ -2731,8 +2746,9 @@ function buildCardHtml(item, i) {
|
||||
const orderBadge = (!c.inventoryItemId && c.powderToOrder)
|
||||
? ` <span class="badge bg-warning text-dark" style="font-size:.65em;vertical-align:middle;" title="Custom powder — must be purchased before coating"><i class="bi bi-cart me-1"></i>ORDER ${parseFloat(c.powderToOrder).toFixed(2)} lbs</span>`
|
||||
: '';
|
||||
const coatNotes = c.notes ? `<span class="fst-italic ms-1 opacity-75">— ${escHtml(c.notes)}</span>` : '';
|
||||
return `<div style="font-size:.8rem;" class="text-muted mt-1">
|
||||
<i class="bi bi-layers me-1"></i><span class="fw-semibold">${escHtml(c.coatName || 'Coat')}</span>${color ? ` — ${color}${code}` : ''}${orderBadge}
|
||||
<i class="bi bi-layers me-1"></i><span class="fw-semibold">${escHtml(c.coatName || 'Coat')}</span>${color ? ` — ${color}${code}` : ''}${orderBadge}${coatNotes}
|
||||
</div>`;
|
||||
}).join('');
|
||||
|
||||
@@ -3414,7 +3430,7 @@ function loadItemsFromTemplate(templateItems) {
|
||||
coverageSqFtPerLb: c.coverageSqFtPerLb || 30,
|
||||
transferEfficiency: c.transferEfficiency || 65,
|
||||
powderCostPerLb: c.powderCostPerLb || null,
|
||||
noExtraLayerCharge: false
|
||||
noExtraLayerCharge: !!c.noExtraLayerCharge
|
||||
})),
|
||||
prepServices: (ti.prepServices || []).map(p => ({
|
||||
prepServiceId: p.prepServiceId,
|
||||
|
||||
Reference in New Issue
Block a user