feat: ship agent-first mission control v0.2.57
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Nexus.Api.Data;
|
||||
using Nexus.Api.Models;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Nexus.Api.Services;
|
||||
|
||||
@@ -17,6 +18,7 @@ public sealed class NotificationService(NexusDbContext db, ILiveUpdateService li
|
||||
TaskId = taskId
|
||||
};
|
||||
db.Notifications.Add(notification);
|
||||
db.OutboxEvents.Add(CreateNotificationEvent("notification.created", notification));
|
||||
await db.SaveChangesAsync(ct);
|
||||
await PublishSnapshotAsync(notification.ForUser, ct);
|
||||
return notification;
|
||||
@@ -36,23 +38,57 @@ public sealed class NotificationService(NexusDbContext db, ILiveUpdateService li
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<bool> MarkAsReadAsync(Guid id, CancellationToken ct = default)
|
||||
public async Task<NotificationReadResult> MarkAsReadAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
var notification = await db.Notifications.FindAsync([id], ct);
|
||||
if (notification is null) return false;
|
||||
if (notification is null) return new NotificationReadResult(null, false);
|
||||
|
||||
notification.IsRead = true;
|
||||
await db.SaveChangesAsync(ct);
|
||||
var changed = !notification.IsRead;
|
||||
if (changed)
|
||||
{
|
||||
notification.IsRead = true;
|
||||
db.OutboxEvents.Add(CreateNotificationEvent("notification.updated", notification));
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
await PublishSnapshotAsync(notification.ForUser, ct);
|
||||
return true;
|
||||
return new NotificationReadResult(notification, changed);
|
||||
}
|
||||
|
||||
public async Task<int> MarkAllAsReadAsync(string forUser, CancellationToken ct = default)
|
||||
{
|
||||
var normalizedUser = forUser.ToLowerInvariant();
|
||||
var count = await db.Notifications
|
||||
.Where(n => n.ForUser == normalizedUser && !n.IsRead)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(n => n.IsRead, true), ct);
|
||||
int count;
|
||||
if (db.Database.IsRelational())
|
||||
{
|
||||
await using var transaction = await db.Database.BeginTransactionAsync(ct);
|
||||
count = await db.Notifications
|
||||
.Where(n => n.ForUser == normalizedUser && !n.IsRead)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(n => n.IsRead, true), ct);
|
||||
if (count > 0)
|
||||
{
|
||||
db.OutboxEvents.Add(CreateNotificationCollectionEvent(
|
||||
"notification.read_all",
|
||||
count));
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
await transaction.CommitAsync(ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
var unread = await db.Notifications
|
||||
.Where(n => n.ForUser == normalizedUser && !n.IsRead)
|
||||
.ToListAsync(ct);
|
||||
foreach (var notification in unread)
|
||||
notification.IsRead = true;
|
||||
count = unread.Count;
|
||||
if (count > 0)
|
||||
{
|
||||
db.OutboxEvents.Add(CreateNotificationCollectionEvent(
|
||||
"notification.read_all",
|
||||
count));
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
await PublishSnapshotAsync(normalizedUser, ct);
|
||||
return count;
|
||||
}
|
||||
@@ -83,4 +119,33 @@ public sealed class NotificationService(NexusDbContext db, ILiveUpdateService li
|
||||
private static NotificationDto MapToDto(Notification n) => new(
|
||||
n.Id, n.Type, n.Title, n.Message,
|
||||
n.ForUser, n.TaskId, n.IsRead, n.CreatedAt);
|
||||
|
||||
private static OutboxEvent CreateNotificationEvent(
|
||||
string type,
|
||||
Notification notification)
|
||||
=> new()
|
||||
{
|
||||
Type = type,
|
||||
AggregateType = "notification",
|
||||
AggregateId = notification.Id.ToString(),
|
||||
AggregateRevision = notification.IsRead ? 1 : 0,
|
||||
PayloadJson = JsonSerializer.Serialize(new
|
||||
{
|
||||
notificationType = notification.Type,
|
||||
notification.TaskId,
|
||||
notification.IsRead
|
||||
})
|
||||
};
|
||||
|
||||
private static OutboxEvent CreateNotificationCollectionEvent(
|
||||
string type,
|
||||
int affectedCount)
|
||||
=> new()
|
||||
{
|
||||
Type = type,
|
||||
AggregateType = "notification",
|
||||
AggregateId = "*",
|
||||
AggregateRevision = 0,
|
||||
PayloadJson = JsonSerializer.Serialize(new { affectedCount })
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user