Files
meezi/src/Meezi.API/Services/TerminalRegistryService.cs
T
soroush.asadi 2487f9e30f
CI/CD / CI · API (dotnet build + test) (push) Successful in 1m0s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 42s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m5s
CI/CD / CI · Admin Web (tsc) (push) Successful in 38s
CI/CD / CI · Website (tsc) (push) Successful in 45s
CI/CD / CI · Koja (tsc) (push) Successful in 49s
CI/CD / Deploy · all services (push) Successful in 2m51s
feat(plans): Stage 3b — DB-driven gates for reviews/styling/limits
Make more plan rules read the admin-editable catalog instead of hardcoded values:
- Review reply gated by the `review_reply` feature (Starter+) — 403 if not in plan.
- Custom menu styling gated by `custom_menu_styling` (Starter+): only blocks an
  actual theme change, so a normal settings save re-sending the current theme is fine.
- Menu categories/items limits now read catalog.GetLimitsAsync (Free categories
  editable; message no longer hardcodes a number).
- Terminals limit reads the catalog (enforcement in TerminalRegistryService +
  the displayed max in TerminalsController).

Remaining (small): menu watermark (Free shows it, `watermark_removed` removes it —
needs the public-menu render), report-history (static ReportPlanGate) and AI-3D
routing — these already enforce the correct matrix values, just not yet editable.

86 tests pass; build clean.
2026-06-03 01:40:00 +03:30

98 lines
3.3 KiB
C#

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;
private readonly Meezi.Infrastructure.Services.Platform.IPlatformCatalogService _catalog;
public TerminalRegistryService(
IConnectionMultiplexer redis,
Meezi.Infrastructure.Services.Platform.IPlatformCatalogService catalog)
{
_redis = redis;
_catalog = catalog;
}
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}";
var max = (await _catalog.GetLimitsAsync(tier, cancellationToken)).MaxTerminals;
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());
}
}