Initial commit
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PowderCoating.Core.Interfaces;
|
||||
using PowderCoating.Infrastructure.Data;
|
||||
using PowderCoating.Web.Extensions;
|
||||
|
||||
namespace PowderCoating.Web.Controllers;
|
||||
|
||||
[Authorize]
|
||||
public class InAppNotificationsController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly ITenantContext _tenant;
|
||||
|
||||
public InAppNotificationsController(ApplicationDbContext db, ITenantContext tenant)
|
||||
{
|
||||
_db = db;
|
||||
_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;
|
||||
|
||||
IQueryable<PowderCoating.Core.Entities.InAppNotification> query;
|
||||
|
||||
if (_tenant.IsPlatformAdmin())
|
||||
{
|
||||
query = _db.InAppNotifications
|
||||
.IgnoreQueryFilters()
|
||||
.Where(n => !n.IsDeleted && n.CompanyId == 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = _db.InAppNotifications.AsQueryable();
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.AsNoTracking()
|
||||
.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
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
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. The response includes a count of unread items so the badge can be updated without a separate round-trip.
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Recent()
|
||||
{
|
||||
IQueryable<PowderCoating.Core.Entities.InAppNotification> query;
|
||||
|
||||
if (_tenant.IsPlatformAdmin())
|
||||
{
|
||||
query = _db.InAppNotifications
|
||||
.IgnoreQueryFilters()
|
||||
.Where(n => !n.IsDeleted && n.CompanyId == 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = _db.InAppNotifications.AsQueryable();
|
||||
}
|
||||
|
||||
var tz = ViewBag.CompanyTimeZone as string;
|
||||
var items = await query
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Take(20)
|
||||
.Select(n => new { n.Id, n.Title, n.Message, n.Link, n.NotificationType, n.IsRead, n.CreatedAt })
|
||||
.ToListAsync();
|
||||
|
||||
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. Used by the initial page load poll; after that the bell relies on Recent to show history.
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Unread()
|
||||
{
|
||||
IQueryable<PowderCoating.Core.Entities.InAppNotification> query;
|
||||
|
||||
if (_tenant.IsPlatformAdmin())
|
||||
{
|
||||
// SuperAdmins see only platform-level notifications (CompanyId = 0)
|
||||
query = _db.InAppNotifications
|
||||
.IgnoreQueryFilters()
|
||||
.Where(n => !n.IsDeleted && n.CompanyId == 0 && !n.IsRead);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Regular users see their company's notifications (global filter handles tenant isolation)
|
||||
query = _db.InAppNotifications.Where(n => !n.IsRead);
|
||||
}
|
||||
|
||||
var tz = ViewBag.CompanyTimeZone as string;
|
||||
var items = await query
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Take(20)
|
||||
.Select(n => new { n.Id, n.Title, n.Message, n.Link, n.NotificationType, n.CreatedAt })
|
||||
.ToListAsync();
|
||||
|
||||
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 and records the timestamp. 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 _db.InAppNotifications.IgnoreQueryFilters().FirstOrDefaultAsync(n => n.Id == id && n.CompanyId == 0 && !n.IsDeleted)
|
||||
: await _db.InAppNotifications.FirstOrDefaultAsync(n => n.Id == id);
|
||||
|
||||
if (notification == null) return NotFound();
|
||||
|
||||
notification.IsRead = true;
|
||||
notification.ReadAt = DateTime.UtcNow;
|
||||
notification.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return Json(new { success = true });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks every unread notification as read for the current user's scope in a single SaveChanges call for efficiency. Returns the count of items marked so the UI can update the badge without refetching.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> MarkAllRead()
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
List<PowderCoating.Core.Entities.InAppNotification> unread;
|
||||
if (_tenant.IsPlatformAdmin())
|
||||
{
|
||||
unread = await _db.InAppNotifications.IgnoreQueryFilters()
|
||||
.Where(n => !n.IsDeleted && n.CompanyId == 0 && !n.IsRead)
|
||||
.ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
unread = await _db.InAppNotifications.Where(n => !n.IsRead).ToListAsync();
|
||||
}
|
||||
|
||||
foreach (var n in unread)
|
||||
{
|
||||
n.IsRead = true;
|
||||
n.ReadAt = now;
|
||||
n.UpdatedAt = now;
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
return Json(new { success = true, count = unread.Count });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user