[CLAUDE] App+Infra+Api+FE: SignalR realtime notifications E2E
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 2m43s

Clean-arch split:
- Application: IRealtimeNotifier (PushToUserAsync, abstraction)
- Api: NotificationHub (/hubs/notifications, [Authorize]) +
  SignalRNotifier impl với IHubContext<NotificationHub>, uses
  Clients.User(userId) (default provider resolves NameIdentifier="sub")
- Infrastructure: NotificationPushInterceptor — SaveChangesInterceptor
  capture Notification entities state=Added trong SavingChanges,
  push qua IRealtimeNotifier trong SavedChanges sau khi commit thành
  công. Zero caller changes — handlers chỉ cần db.Add(Notification).
  Attached vào ApplicationDbContext cùng với AuditingInterceptor.

Auth:
- JWT config thêm OnMessageReceived event: read ?access_token= từ
  query string khi path = /hubs/* (WebSockets không set headers).
- SignalRNotifier singleton (stateless, chỉ delegate IHubContext).

FE (both apps):
- @microsoft/signalr 8.0.7 vào package.json.
- lib/realtime.ts: singleton connection với lazy start + automatic
  reconnect [0,2s,5s,10s,15s] + accessTokenFactory lấy từ localStorage.
- NotificationBell: useEffect subscribe 'notification-created' khi
  isAuthenticated. On push: invalidate query + toast.message. Fallback
  polling giảm từ 30s → 60s (realtime cover gap).
- AuthContext.logout: dynamic import stopConnection() — avoid leaking
  auth'd socket across users.

Result: ERP-grade feel. Contract transition → Drafter nhận toast ngay
trong vòng 100-300ms (same-origin WebSocket), không cần F5 hay polling.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pqhuy1987
2026-04-21 20:56:37 +07:00
parent 2a851caa92
commit ea9ab5e352
14 changed files with 319 additions and 3 deletions

View File

@ -39,13 +39,16 @@ public static class DependencyInjection
services.AddHostedService<SlaExpiryJob>();
services.AddScoped<AuditingInterceptor>();
services.AddScoped<NotificationPushInterceptor>();
services.AddDbContext<ApplicationDbContext>((sp, options) =>
{
var connectionString = configuration.GetConnectionString("Default")
?? throw new InvalidOperationException("Missing ConnectionStrings:Default");
options.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
options.AddInterceptors(sp.GetRequiredService<AuditingInterceptor>());
options.AddInterceptors(
sp.GetRequiredService<AuditingInterceptor>(),
sp.GetRequiredService<NotificationPushInterceptor>());
});
services.AddScoped<IApplicationDbContext>(sp => sp.GetRequiredService<ApplicationDbContext>());

View File

@ -0,0 +1,89 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using SolutionErp.Application.Common.Interfaces;
using SolutionErp.Domain.Notifications;
namespace SolutionErp.Infrastructure.Persistence.Interceptors;
// Auto-pushes newly created Notification entities via IRealtimeNotifier
// AFTER SaveChanges succeeds. Keeps domain handlers unaware of real-time
// concerns — they just db.Notifications.Add(...) and the interceptor fires.
public class NotificationPushInterceptor(
IRealtimeNotifier notifier,
ILogger<NotificationPushInterceptor> logger) : SaveChangesInterceptor
{
// Captured between Saving* and Saved* — interceptor is scoped with DbContext
// so this list is per-unit-of-work, not shared across requests.
private readonly List<Notification> _toPush = new();
public override InterceptionResult<int> SavingChanges(
DbContextEventData eventData, InterceptionResult<int> result)
{
Capture(eventData.Context);
return base.SavingChanges(eventData, result);
}
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
{
Capture(eventData.Context);
return base.SavingChangesAsync(eventData, result, cancellationToken);
}
public override int SavedChanges(SaveChangesCompletedEventData eventData, int result)
{
_ = FlushAsync(CancellationToken.None);
return base.SavedChanges(eventData, result);
}
public override async ValueTask<int> SavedChangesAsync(
SaveChangesCompletedEventData eventData, int result, CancellationToken cancellationToken = default)
{
await FlushAsync(cancellationToken);
return await base.SavedChangesAsync(eventData, result, cancellationToken);
}
private void Capture(DbContext? context)
{
if (context is null) return;
_toPush.Clear();
foreach (var entry in context.ChangeTracker.Entries<Notification>())
{
if (entry.State == EntityState.Added)
_toPush.Add(entry.Entity);
}
}
private async Task FlushAsync(CancellationToken ct)
{
if (_toPush.Count == 0) return;
var batch = _toPush.ToList();
_toPush.Clear();
foreach (var n in batch)
{
try
{
// Same shape as NotificationDto on FE — keeps the client simple
var payload = new
{
id = n.Id,
type = (int)n.Type,
title = n.Title,
description = n.Description,
href = n.Href,
refId = n.RefId,
createdAt = n.CreatedAt,
readAt = (DateTime?)null,
};
await notifier.PushToUserAsync(n.UserId, "notification-created", payload, ct);
}
catch (Exception ex)
{
// Never propagate — real-time push is best-effort.
logger.LogWarning(ex, "Failed to push realtime notification {NotificationId}", n.Id);
}
}
}
}