Files
amrez-nova-eop-services-api/AMREZ.EOP.Application/UseCases/Authentications/LogoutAllUseCase.cs
Thanakarn Klangkasame 563a341a99 Add Login Module
2025-10-02 11:18:44 +07:00

40 lines
1.4 KiB
C#

using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Authentications;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Authentications.LogoutAll;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class LogoutAllUseCase : ILogoutAllUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IHttpContextAccessor _http;
public LogoutAllUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _users = users; _http = http; }
public async Task<int> ExecuteAsync(LogoutAllRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return 0;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var n = await _users.RevokeAllSessionsAsync(request.UserId, ct);
await _uow.CommitAsync(ct);
return n;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}