Performance: push ORDER BY/TAKE into SQL for hot-path reads

- IInAppNotificationRepository: typed repo with GetPagedAsync, GetRecentAsync, GetUnreadAsync
  — bell dropdown no longer loads all notifications then slices in C#
- Add compound indexes on InAppNotifications(CompanyId, IsDeleted, CreatedAt) and
  (CompanyId, IsDeleted, IsRead); ContactSubmissions(CompanyId, IsDeleted, CreatedAt)
- PlainRepository.GetAllAsync/FindAsync: add AsNoTracking (Announcements, Tips, ReleaseNotes)
- AiUsageReportController: replace GetAllAsync + C# Where with FindAsync (SQL-level filter)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-13 21:34:12 -04:00
parent 54defc158f
commit aeec899cf2
11 changed files with 11628 additions and 62 deletions
@@ -137,7 +137,7 @@ IRepository<ReworkRecord> ReworkRecords { get; }
IPlainRepository<Announcement> Announcements { get; }
IPlainRepository<BannedIp> BannedIps { get; }
IPlainRepository<DashboardTip> DashboardTips { get; }
IRepository<InAppNotification> InAppNotifications { get; }
IInAppNotificationRepository InAppNotifications { get; }
IPlainRepository<ReleaseNote> ReleaseNotes { get; }
// Bug Reports
@@ -0,0 +1,32 @@
using PowderCoating.Core.Entities;
namespace PowderCoating.Core.Interfaces.Repositories;
/// <summary>
/// Typed repository for <see cref="InAppNotification"/> providing DB-level pagination and
/// bounded reads for the bell-dropdown and notification history page. The generic
/// <see cref="IRepository{T}"/> returns materialized lists so ordering/limiting must happen
/// in C#; these methods push ORDER BY, SKIP, and TAKE into SQL where they belong.
/// </summary>
public interface IInAppNotificationRepository : IRepository<InAppNotification>
{
/// <summary>
/// Returns a page of notifications ordered newest-first, plus the total un-paged count.
/// SuperAdmin path filters to CompanyId == 0 (platform notifications only).
/// </summary>
Task<(List<InAppNotification> Items, int TotalCount)> GetPagedAsync(
bool isPlatformAdmin, int pageNumber, int pageSize);
/// <summary>
/// Returns the <paramref name="take"/> most recent notifications (read and unread)
/// for the bell dropdown. SuperAdmin path is scoped to platform notifications.
/// </summary>
Task<List<InAppNotification>> GetRecentAsync(bool isPlatformAdmin, int take = 20);
/// <summary>
/// Returns the <paramref name="take"/> most recent unread notifications plus the full
/// unread count for the bell badge. SuperAdmin path is scoped to platform notifications.
/// </summary>
Task<(List<InAppNotification> Items, int UnreadCount)> GetUnreadAsync(
bool isPlatformAdmin, int take = 20);
}