1cb7a8ca4a
Phase 3 — eliminated ApplicationDbContext from all non-exempt controllers, routing all data access through IUnitOfWork. Added IPlainRepository<T> for the four platform entities (Announcement, BannedIp, DashboardTip, ReleaseNote) that intentionally don't extend BaseEntity and therefore can't use the constrained IRepository<T>. Added permanent-exception comments to the 18 controllers that legitimately retain direct DbContext access (Identity infra, cross-tenant platform ops, bulk streaming exports). Phase 4 — added EnforceDataAccessArchitecture() to Program.cs, a startup gate that reflects over every Controller subclass and throws at boot if any non-exempt controller injects ApplicationDbContext. The app cannot start with a violation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
150 lines
5.6 KiB
C#
150 lines
5.6 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PowderCoating.Core.Interfaces;
|
|
using PowderCoating.Web.Extensions;
|
|
|
|
namespace PowderCoating.Web.Controllers;
|
|
|
|
[Authorize]
|
|
public class InAppNotificationsController : Controller
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ITenantContext _tenant;
|
|
|
|
public InAppNotificationsController(IUnitOfWork unitOfWork, ITenantContext tenant)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_tenant = tenant;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Displays the paginated notification history page (read and unread). SuperAdmins see only platform-level notifications (CompanyId 0) via IgnoreQueryFilters; regular users rely on the global query filter for tenant isolation.
|
|
/// </summary>
|
|
public async Task<IActionResult> Index(int pageNumber = 1, int pageSize = 25)
|
|
{
|
|
pageNumber = Math.Max(1, pageNumber);
|
|
pageSize = pageSize is 10 or 25 or 50 ? pageSize : 25;
|
|
|
|
var all = _tenant.IsPlatformAdmin()
|
|
? (await _unitOfWork.InAppNotifications.FindAsync(
|
|
n => !n.IsDeleted && n.CompanyId == 0, ignoreQueryFilters: true)).ToList()
|
|
: (await _unitOfWork.InAppNotifications.GetAllAsync()).ToList();
|
|
|
|
var totalCount = all.Count;
|
|
|
|
var tz = ViewBag.CompanyTimeZone as string;
|
|
var items = all
|
|
.OrderByDescending(n => n.CreatedAt)
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(n => new
|
|
{
|
|
n.Id,
|
|
n.Title,
|
|
n.Message,
|
|
n.Link,
|
|
n.NotificationType,
|
|
n.IsRead,
|
|
n.ReadAt,
|
|
CreatedAt = n.CreatedAt
|
|
})
|
|
.ToList();
|
|
|
|
ViewBag.TotalCount = totalCount;
|
|
ViewBag.PageNumber = pageNumber;
|
|
ViewBag.PageSize = pageSize;
|
|
ViewBag.TotalPages = (int)Math.Ceiling((double)totalCount / pageSize);
|
|
return View(items);
|
|
}
|
|
|
|
/// <summary>
|
|
/// AJAX endpoint that returns the 20 most recent notifications (read and unread) for the bell dropdown.
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<IActionResult> Recent()
|
|
{
|
|
var all = _tenant.IsPlatformAdmin()
|
|
? (await _unitOfWork.InAppNotifications.FindAsync(
|
|
n => !n.IsDeleted && n.CompanyId == 0, ignoreQueryFilters: true)).ToList()
|
|
: (await _unitOfWork.InAppNotifications.GetAllAsync()).ToList();
|
|
|
|
var tz = ViewBag.CompanyTimeZone as string;
|
|
var items = all
|
|
.OrderByDescending(n => n.CreatedAt)
|
|
.Take(20)
|
|
.Select(n => new { n.Id, n.Title, n.Message, n.Link, n.NotificationType, n.IsRead, n.CreatedAt })
|
|
.ToList();
|
|
|
|
var unreadCount = items.Count(n => !n.IsRead);
|
|
return Json(new { count = unreadCount, items = items.Select(n => new {
|
|
n.Id, n.Title, n.Message, n.Link, n.NotificationType, n.IsRead,
|
|
CreatedAt = n.CreatedAt.Tz(tz).ToString("MMM d, h:mm tt")
|
|
}) });
|
|
}
|
|
|
|
/// <summary>
|
|
/// AJAX endpoint that returns only unread notifications (up to 20) plus an unread count for the bell badge.
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<IActionResult> Unread()
|
|
{
|
|
var items = _tenant.IsPlatformAdmin()
|
|
? (await _unitOfWork.InAppNotifications.FindAsync(
|
|
n => !n.IsDeleted && n.CompanyId == 0 && !n.IsRead, ignoreQueryFilters: true))
|
|
.OrderByDescending(n => n.CreatedAt).Take(20).ToList()
|
|
: (await _unitOfWork.InAppNotifications.FindAsync(n => !n.IsRead))
|
|
.OrderByDescending(n => n.CreatedAt).Take(20).ToList();
|
|
|
|
var tz = ViewBag.CompanyTimeZone as string;
|
|
return Json(new { count = items.Count, items = items.Select(n => new {
|
|
n.Id, n.Title, n.Message, n.Link, n.NotificationType,
|
|
CreatedAt = n.CreatedAt.Tz(tz).ToString("MMM d, h:mm tt")
|
|
}) });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Marks a single notification as read. Tenant isolation is enforced manually for SuperAdmins (CompanyId 0 filter) because IgnoreQueryFilters bypasses the global company filter.
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<IActionResult> MarkRead(int id)
|
|
{
|
|
var notification = _tenant.IsPlatformAdmin()
|
|
? await _unitOfWork.InAppNotifications.FirstOrDefaultAsync(
|
|
n => n.Id == id && n.CompanyId == 0 && !n.IsDeleted, ignoreQueryFilters: true)
|
|
: await _unitOfWork.InAppNotifications.GetByIdAsync(id);
|
|
|
|
if (notification == null) return NotFound();
|
|
|
|
notification.IsRead = true;
|
|
notification.ReadAt = DateTime.UtcNow;
|
|
notification.UpdatedAt = DateTime.UtcNow;
|
|
await _unitOfWork.CompleteAsync();
|
|
|
|
return Json(new { success = true });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Marks every unread notification as read for the current user's scope in a single SaveChanges call.
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<IActionResult> MarkAllRead()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
|
|
var unread = _tenant.IsPlatformAdmin()
|
|
? (await _unitOfWork.InAppNotifications.FindAsync(
|
|
n => !n.IsDeleted && n.CompanyId == 0 && !n.IsRead, ignoreQueryFilters: true)).ToList()
|
|
: (await _unitOfWork.InAppNotifications.FindAsync(n => !n.IsRead)).ToList();
|
|
|
|
foreach (var n in unread)
|
|
{
|
|
n.IsRead = true;
|
|
n.ReadAt = now;
|
|
n.UpdatedAt = now;
|
|
}
|
|
|
|
await _unitOfWork.CompleteAsync();
|
|
return Json(new { success = true, count = unread.Count });
|
|
}
|
|
}
|