Add proposal workflow and voting API
This commit is contained in:
1 parent
9ca23a2c15
commit
1ff4a0a814
17 files changed
+796
-83
No files matched your search
@@ -1,7 +1,7 @@
|
||||
using Helpdesk.Application.Abstractions;
|
||||
using Helpdesk.Application.Common;
|
||||
using Helpdesk.Domain;
|
||||
using Helpdesk.Domain.Issues;
|
||||
using Helpdesk.Domain.Notifications;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Helpdesk.Application.Notifications;
|
||||
@@ -46,13 +46,13 @@ public sealed class NotificationService(IAppDbContext db, ICurrentUser currentUs
|
||||
|
||||
public async Task MarkAllReadAsync(CancellationToken ct) => await MarkUnreadRowsAsync(Inbox(), ct);
|
||||
|
||||
private IQueryable<IssueNotification> Inbox()
|
||||
private IQueryable<Notification> Inbox()
|
||||
{
|
||||
var userId = currentUser.Id;
|
||||
return db.IssueNotifications.Where(n => n.UserId == userId);
|
||||
return db.Notifications.Where(n => n.UserId == userId);
|
||||
}
|
||||
|
||||
private Task<int> MarkUnreadRowsAsync(IQueryable<IssueNotification> query, CancellationToken ct)
|
||||
private Task<int> MarkUnreadRowsAsync(IQueryable<Notification> query, CancellationToken ct)
|
||||
{
|
||||
var now = clock.GetUtcNow().UtcDateTime;
|
||||
return query.Where(n => n.ReadAt == null)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using Helpdesk.Application.Abstractions;
|
||||
using Helpdesk.Domain.Issues;
|
||||
using Helpdesk.Domain.Notifications;
|
||||
using Helpdesk.Domain.Proposals;
|
||||
using Helpdesk.Domain.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Helpdesk.Application.Notifications;
|
||||
|
||||
/// <summary>Připraví notifikace a záznamy outboxu pro uložení spolu s událostí.</summary>
|
||||
public sealed class NotificationWriter(IAppDbContext db, TimeProvider clock)
|
||||
{
|
||||
public Task ForIssueAsync(Issue issue, int actorId, int[] recipientIds, NotificationType type, string title,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var reporterId = issue.ReporterId;
|
||||
var eligibleUsers = db.Users.Where(u =>
|
||||
u.Id == reporterId || u.Role == UserRole.FacilityManager || u.Role == UserRole.Management);
|
||||
return QueueAsync(eligibleUsers, actorId, recipientIds,
|
||||
id => new Notification(issue, id, type, title, issue.Title), ct);
|
||||
}
|
||||
|
||||
public Task ForProposalStatusAsync(Proposal proposal, int actorId, CancellationToken ct) =>
|
||||
QueueAsync(db.Users, actorId, [proposal.AuthorId],
|
||||
id => new Notification(proposal, id, "Stav návrhu se změnil"), ct);
|
||||
|
||||
private async Task QueueAsync(IQueryable<User> eligibleUsers, int actorId, int[] recipientIds,
|
||||
Func<int, Notification> create, CancellationToken ct)
|
||||
{
|
||||
var ids = recipientIds.Where(id => id != actorId).Distinct().ToArray();
|
||||
if (ids.Length == 0)
|
||||
return;
|
||||
|
||||
var recipients = await eligibleUsers.Where(u => u.IsActive && ids.Contains(u.Id))
|
||||
.Select(u => new
|
||||
{
|
||||
u.Id,
|
||||
Email = u.EmailNotificationsEnabled && u.EmailConfirmed,
|
||||
Push = db.PushDevices.Any(device => device.UserId == u.Id),
|
||||
}).ToListAsync(ct);
|
||||
|
||||
var now = clock.GetUtcNow().UtcDateTime;
|
||||
foreach (var recipient in recipients)
|
||||
{
|
||||
var notification = create(recipient.Id);
|
||||
db.Notifications.Add(notification);
|
||||
db.NotificationDeliveries.AddRange(
|
||||
new NotificationDelivery(notification, DeliveryChannel.Email, recipient.Email, now),
|
||||
new NotificationDelivery(notification, DeliveryChannel.Push, recipient.Push, now));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user