2026-05-27 21:33:48 +03:30
|
|
|
using Meezi.Core.Constants;
|
|
|
|
|
using Meezi.Core.Enums;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
|
|
|
|
namespace Meezi.API.Services;
|
|
|
|
|
|
|
|
|
|
public record TerminalInfoDto(string TerminalId, DateTime? LastSeenUtc);
|
|
|
|
|
|
|
|
|
|
public interface ITerminalRegistryService
|
|
|
|
|
{
|
|
|
|
|
Task<(bool Allowed, string? ErrorCode, string? Message)> RegisterAsync(
|
|
|
|
|
string cafeId,
|
|
|
|
|
PlanTier tier,
|
|
|
|
|
string terminalId,
|
|
|
|
|
CancellationToken cancellationToken = default);
|
|
|
|
|
|
|
|
|
|
Task<IReadOnlyList<TerminalInfoDto>> ListAsync(string cafeId, CancellationToken cancellationToken = default);
|
|
|
|
|
|
|
|
|
|
Task RevokeAsync(string cafeId, string terminalId, CancellationToken cancellationToken = default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TerminalRegistryService : ITerminalRegistryService
|
|
|
|
|
{
|
|
|
|
|
private static readonly TimeSpan TerminalTtl = TimeSpan.FromDays(90);
|
|
|
|
|
private readonly IConnectionMultiplexer _redis;
|
2026-06-03 01:40:00 +03:30
|
|
|
private readonly Meezi.Infrastructure.Services.Platform.IPlatformCatalogService _catalog;
|
2026-05-27 21:33:48 +03:30
|
|
|
|
2026-06-03 01:40:00 +03:30
|
|
|
public TerminalRegistryService(
|
|
|
|
|
IConnectionMultiplexer redis,
|
|
|
|
|
Meezi.Infrastructure.Services.Platform.IPlatformCatalogService catalog)
|
|
|
|
|
{
|
|
|
|
|
_redis = redis;
|
|
|
|
|
_catalog = catalog;
|
|
|
|
|
}
|
2026-05-27 21:33:48 +03:30
|
|
|
|
|
|
|
|
public async Task<(bool Allowed, string? ErrorCode, string? Message)> RegisterAsync(
|
|
|
|
|
string cafeId,
|
|
|
|
|
PlanTier tier,
|
|
|
|
|
string terminalId,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(terminalId))
|
|
|
|
|
return (false, "TERMINAL_ID_REQUIRED", "Terminal id is required.");
|
|
|
|
|
|
|
|
|
|
terminalId = terminalId.Trim();
|
|
|
|
|
var db = _redis.GetDatabase();
|
|
|
|
|
var setKey = $"terminals:{cafeId}";
|
2026-06-03 01:40:00 +03:30
|
|
|
var max = (await _catalog.GetLimitsAsync(tier, cancellationToken)).MaxTerminals;
|
2026-05-27 21:33:48 +03:30
|
|
|
|
|
|
|
|
if (max == int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
await db.SetAddAsync(setKey, terminalId);
|
|
|
|
|
await db.KeyExpireAsync(setKey, TerminalTtl);
|
|
|
|
|
return (true, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var members = await db.SetMembersAsync(setKey);
|
|
|
|
|
var known = members.Select(m => m.ToString()).ToHashSet(StringComparer.Ordinal);
|
|
|
|
|
if (known.Contains(terminalId))
|
|
|
|
|
{
|
|
|
|
|
await db.KeyExpireAsync(setKey, TerminalTtl);
|
|
|
|
|
return (true, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (known.Count >= max)
|
|
|
|
|
return (false, "PLAN_LIMIT_REACHED", "Terminal limit reached for your plan. Please upgrade.");
|
|
|
|
|
|
|
|
|
|
await db.SetAddAsync(setKey, terminalId);
|
|
|
|
|
await db.KeyExpireAsync(setKey, TerminalTtl);
|
|
|
|
|
return (true, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<TerminalInfoDto>> ListAsync(
|
|
|
|
|
string cafeId,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var db = _redis.GetDatabase();
|
|
|
|
|
var setKey = $"terminals:{cafeId}";
|
|
|
|
|
var members = await db.SetMembersAsync(setKey);
|
|
|
|
|
return members
|
|
|
|
|
.Select(m => m.ToString())
|
|
|
|
|
.Where(id => !string.IsNullOrEmpty(id))
|
|
|
|
|
.Select(id => new TerminalInfoDto(id!, null))
|
|
|
|
|
.OrderBy(t => t.TerminalId)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task RevokeAsync(
|
|
|
|
|
string cafeId,
|
|
|
|
|
string terminalId,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(terminalId)) return;
|
|
|
|
|
var db = _redis.GetDatabase();
|
|
|
|
|
await db.SetRemoveAsync($"terminals:{cafeId}", terminalId.Trim());
|
|
|
|
|
}
|
|
|
|
|
}
|