feat(api): .NET 10 multi-tenant REST API
Full backend implementation: - Multi-tenant cafe/restaurant management (menus, orders, tables, staff) - POS order flow with ZarinPal and Snappfood payment integration - OTP authentication via Kavenegar SMS - QR digital menu with public discover/finder endpoints - Customer loyalty, coupons, CRM - PostgreSQL via EF Core, Redis for caching/sessions - Background jobs, webhook handlers - Full migration history Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
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;
|
||||
|
||||
public TerminalRegistryService(IConnectionMultiplexer redis) => _redis = redis;
|
||||
|
||||
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 = PlanLimits.MaxTerminals(tier);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user