Files
NTBug/backend/src/Helpdesk.Application/Notifications/PushDeviceService.cs
T

27 lines
965 B
C#
Raw Normal View History

2026-11-19 14:02:02 +01:00
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í.");
}
}