Add queued email and Firebase push delivery

This commit is contained in:
GonnaHetzMe committed 2026-11-19 14:02:02 +01:00
1 parent c96eb155ae
commit 599237dd2c
22 files changed
+497 -8

No files matched your search

@@ -0,0 +1,53 @@
using Helpdesk.Application.Abstractions;
using Helpdesk.Application.Common;
using Helpdesk.Domain;
using Helpdesk.Domain.Notifications;
using Microsoft.EntityFrameworkCore;
namespace Helpdesk.Application.Notifications;
public sealed record NotificationDeliveryDto(
int Id, int NotificationId, string Channel, string Status, int AttemptCount,
string? LastError, DateTime? NextAttemptAt, DateTime? SentAt);
public sealed class NotificationDeliveryService(IAppDbContext db, TimeProvider clock)
{
public async Task<PagedResult<NotificationDeliveryDto>> ListAsync(string status, int page, int pageSize, CancellationToken ct)
{
if (!EnumCode.TryParse<DeliveryStatus>(status, out var deliveryStatus))
throw new BadRequestException("Neplatný stav doručení.");
page = Math.Max(1, page);
pageSize = Math.Clamp(pageSize, 1, 100);
var query = db.NotificationDeliveries.AsNoTracking().Where(d => d.Status == deliveryStatus);
var count = await query.CountAsync(ct);
var offset = (long)(page - 1) * pageSize;
if (offset >= count)
return new PagedResult<NotificationDeliveryDto>([], count, page, pageSize);
var rows = await query.OrderByDescending(d => d.Id).Skip((int)offset).Take(pageSize).ToListAsync(ct);
var items = rows.Select(d => new NotificationDeliveryDto(d.Id, d.NotificationId, d.Channel.ToCode(),
d.Status.ToCode(), d.AttemptCount, d.LastError, d.NextAttemptAt, d.SentAt)).ToArray();
return new PagedResult<NotificationDeliveryDto>(items, count, page, pageSize);
}
public async Task RetryAsync(int id, CancellationToken ct)
{
var now = clock.GetUtcNow().UtcDateTime;
var delivery = db.NotificationDeliveries.Where(d => d.Id == id);
var updated = await delivery.Where(d => d.Status == DeliveryStatus.Failed)
.ExecuteUpdateAsync(setters => setters
.SetProperty(d => d.Status, DeliveryStatus.Pending)
.SetProperty(d => d.AttemptCount, 0)
.SetProperty(d => d.NextAttemptAt, now)
.SetProperty(d => d.LastError, (string?)null)
.SetProperty(d => d.SentAt, (DateTime?)null), ct);
if (updated == 1)
return;
if (!await delivery.AnyAsync(ct))
throw new NotFoundException("Doručení neexistuje.");
throw new ConflictException("Zopakovat lze pouze neúspěšné doručení.");
}
}
@@ -0,0 +1,27 @@
using Helpdesk.Application.Abstractions;
using Helpdesk.Application.Common;
using Microsoft.EntityFrameworkCore;
namespace Helpdesk.Application.Notifications;
public sealed class PushDeviceService(IAppDbContext db, ICurrentUser currentUser)
{
public Task RegisterAsync(string token, CancellationToken ct)
{
ValidateToken(token);
return db.RegisterPushDeviceAsync(currentUser.Id, token, ct);
}
public async Task UnregisterAsync(string token, CancellationToken ct)
{
ValidateToken(token);
var userId = currentUser.Id;
await db.PushDevices.Where(d => d.UserId == userId && d.FcmToken == token).ExecuteDeleteAsync(ct);
}
private static void ValidateToken(string? token)
{
if (string.IsNullOrEmpty(token) || token.Length > 512 || token.Any(c => char.IsWhiteSpace(c) || char.IsControl(c)))
throw new BadRequestException("Neplatný registrační token zařízení.");
}
}