This commit is contained in:
Thanakarn Klangkasame
2025-09-30 11:01:02 +07:00
commit 92e614674c
182 changed files with 9596 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
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.AddEmailIdentity;
using AMREZ.EOP.Domain.Shared._Users;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class AddEmailIdentityUseCase : IAddEmailIdentityUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IHttpContextAccessor _http;
public AddEmailIdentityUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_users = users;
_http = http;
}
public async Task<bool> ExecuteAsync(AddEmailIdentityRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var email = request.Email.Trim().ToLowerInvariant();
await _users.AddIdentityAsync(request.UserId, IdentityType.Email, email, request.IsPrimary, ct);
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,47 @@
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.Abstractions.Security;
using AMREZ.EOP.Contracts.DTOs.Authentications.ChangePassword;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class ChangePasswordUseCase : IChangePasswordUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IPasswordHasher _hasher;
private readonly IHttpContextAccessor _http;
public ChangePasswordUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IPasswordHasher h, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _users = users; _hasher = h; _http = http; }
public async Task<bool> ExecuteAsync(ChangePasswordRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var user = await _users.FindByIdAsync(request.UserId, ct);
if (user is null) { await _uow.RollbackAsync(ct); return false; }
if (!_hasher.Verify(request.OldPassword, user.PasswordHash))
{ await _uow.RollbackAsync(ct); return false; }
var newHash = _hasher.Hash(request.NewPassword);
await _users.AddPasswordHistoryAsync(user.Id, user.PasswordHash, ct);
await _users.ChangePasswordAsync(user.Id, newHash, ct);
await _uow.CommitAsync(ct);
return true;
}
catch { await _uow.RollbackAsync(ct); throw; }
}
}

View File

@@ -0,0 +1,36 @@
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.DisableMfa;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class DisableMfaUseCase : IDisableMfaUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IHttpContextAccessor _http;
public DisableMfaUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _users = users; _http = http; }
public async Task<bool> ExecuteAsync(DisableMfaRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _users.DisableMfaFactorAsync(request.FactorId, ct);
await _uow.CommitAsync(ct);
return true;
}
catch { await _uow.RollbackAsync(ct); throw; }
}
}

View File

@@ -0,0 +1,36 @@
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.EnableTotp;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class EnableTotpUseCase : IEnableTotpUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IHttpContextAccessor _http;
public EnableTotpUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _users = users; _http = http; }
public async Task<EnableTotpResponse?> ExecuteAsync(EnableTotpRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var factor = await _users.AddTotpFactorAsync(request.UserId, request.Label, request.Secret, ct);
await _uow.CommitAsync(ct);
return new EnableTotpResponse(factor.Id, request.Label);
}
catch { await _uow.RollbackAsync(ct); throw; }
}
}

View File

@@ -0,0 +1,51 @@
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.Abstractions.Security;
using AMREZ.EOP.Contracts.DTOs.Authentications.Login;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class LoginUseCase : ILoginUseCase
{
private readonly ITenantResolver _tenantResolver;
private readonly IUserRepository _users;
private readonly IPasswordHasher _hasher;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
public LoginUseCase(ITenantResolver r, IUserRepository u, IPasswordHasher h, IHttpContextAccessor http, IUnitOfWork uow)
{ _tenantResolver = r; _users = u; _hasher = h; _http = http; _uow = uow; }
public async Task<LoginResponse?> ExecuteAsync(LoginRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _tenantResolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var email = request.Email.Trim().ToLowerInvariant();
var user = await _users.FindActiveByEmailAsync(email, ct);
if (user is null || !_hasher.Verify(request.Password, user.PasswordHash))
{
await _uow.RollbackAsync(ct);
return null;
}
await _uow.CommitAsync(ct);
// NOTE: ไม่ใช้ DisplayName ใน Entity แล้ว — ส่งกลับเป็นค่าว่าง/ไปดึงจาก HR ฝั่ง API
return new LoginResponse(user.Id, string.Empty, email, tenant.Id);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,45 @@
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;
}
}
}

View File

@@ -0,0 +1,36 @@
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.Logout;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class LogoutUseCase : ILogoutUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IHttpContextAccessor _http;
public LogoutUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _users = users; _http = http; }
public async Task<bool> ExecuteAsync(LogoutRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var n = await _users.RevokeSessionAsync(request.UserId, request.SessionId, ct);
await _uow.CommitAsync(ct);
return n > 0;
}
catch { await _uow.RollbackAsync(ct); throw; }
}
}

View File

@@ -0,0 +1,78 @@
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.Abstractions.Security;
using AMREZ.EOP.Contracts.DTOs.Authentications.Register;
using AMREZ.EOP.Domain.Entities.Authentications;
using AMREZ.EOP.Domain.Shared._Users;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class RegisterUseCase : IRegisterUseCase
{
private readonly ITenantResolver _tenantResolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IPasswordHasher _hasher;
private readonly IHttpContextAccessor _http;
public RegisterUseCase(
ITenantResolver resolver,
IUnitOfWork uow,
IUserRepository users,
IPasswordHasher hasher,
IHttpContextAccessor http)
{
_tenantResolver = resolver; _uow = uow; _users = users; _hasher = hasher; _http = http;
}
public async Task<RegisterResponse?> ExecuteAsync(RegisterRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _tenantResolver.Resolve(http, request);
if (tenant is null) return null;
var emailNorm = request.Email.Trim().ToLowerInvariant();
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
if (await _users.EmailExistsAsync(emailNorm, ct))
{
await _uow.RollbackAsync(ct);
return null;
}
var hash = _hasher.Hash(request.Password);
var user = new User
{
PasswordHash = hash,
IsActive = true,
};
// แนบอัตลักษณ์แบบ Email (เก็บในตารางลูก)
user.Identities.Add(new UserIdentity
{
Type = IdentityType.Email,
Identifier = emailNorm,
IsPrimary = true,
VerifiedAt = null
});
await _users.AddAsync(user, ct);
await _uow.CommitAsync(ct);
// ไม่ส่ง DisplayName (ปล่อยให้ HR/Presentation สร้าง)
return new RegisterResponse(user.Id, string.Empty, emailNorm, tenant.Id);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,37 @@
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.VerifyEmail;
using AMREZ.EOP.Domain.Shared._Users;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Authentications;
public sealed class VerifyEmailUseCase : IVerifyEmailUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserRepository _users;
private readonly IHttpContextAccessor _http;
public VerifyEmailUseCase(ITenantResolver r, IUnitOfWork uow, IUserRepository users, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _users = users; _http = http; }
public async Task<bool> ExecuteAsync(VerifyEmailRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _users.VerifyIdentityAsync(request.UserId, IdentityType.Email, request.Email.Trim().ToLowerInvariant(), DateTimeOffset.UtcNow, ct);
await _uow.CommitAsync(ct);
return true;
}
catch { await _uow.RollbackAsync(ct); throw; }
}
}

View File

@@ -0,0 +1,51 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmergencyContact;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmergencyContactAdd;
using AMREZ.EOP.Domain.Entities.HumanResources;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class AddEmergencyContactUseCase : IAddEmergencyContactUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public AddEmergencyContactUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr, IHttpContextAccessor http)
{ _resolver = r; _uow = uow; _hr = hr; _http = http; }
public async Task<EmergencyContactResponse?> ExecuteAsync(EmergencyContactAddRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var ec = new EmergencyContact
{
UserProfileId = request.UserProfileId,
Name = request.Name,
Relationship = request.Relationship,
Phone = request.Phone,
Email = request.Email,
IsPrimary = request.IsPrimary
};
var added = await _hr.AddEmergencyContactAsync(ec, ct);
if (request.IsPrimary)
await _hr.SetPrimaryEmergencyContactAsync(request.UserProfileId, added.Id, ct);
await _uow.CommitAsync(ct);
return new EmergencyContactResponse(added.Id, added.UserProfileId, added.IsPrimary);
}
catch { await _uow.RollbackAsync(ct); throw; }
}
}

View File

@@ -0,0 +1,65 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmployeeAddress;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmployeeAddressAdd;
using AMREZ.EOP.Domain.Entities.HumanResources;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class AddEmployeeAddressUseCase : IAddEmployeeAddressUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public AddEmployeeAddressUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<EmployeeAddressResponse?> ExecuteAsync(EmployeeAddressAddRequest request,
CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var addr = new EmployeeAddress
{
UserProfileId = request.UserProfileId,
Type = request.Type,
Line1 = request.Line1,
Line2 = request.Line2,
City = request.City,
State = request.State,
PostalCode = request.PostalCode,
Country = request.Country,
IsPrimary = request.IsPrimary
};
var added = await _hr.AddAddressAsync(addr, ct);
if (request.IsPrimary)
await _hr.SetPrimaryAddressAsync(request.UserProfileId, added.Id, ct);
await _uow.CommitAsync(ct);
return new EmployeeAddressResponse(added.Id, added.UserProfileId, added.IsPrimary);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,63 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmployeeBankAccount;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmployeeBankAccountAdd;
using AMREZ.EOP.Domain.Entities.HumanResources;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class AddEmployeeBankAccountUseCase : IAddEmployeeBankAccountUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public AddEmployeeBankAccountUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<EmployeeBankAccountResponse?> ExecuteAsync(EmployeeBankAccountAddRequest request,
CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var b = new EmployeeBankAccount
{
UserProfileId = request.UserProfileId,
BankName = request.BankName,
AccountNumber = request.AccountNumber,
AccountHolder = request.AccountHolder,
Branch = request.Branch,
Note = request.Note,
IsPrimary = request.IsPrimary
};
var added = await _hr.AddBankAccountAsync(b, ct);
if (request.IsPrimary)
await _hr.SetPrimaryBankAccountAsync(request.UserProfileId, added.Id, ct);
await _uow.CommitAsync(ct);
return new EmployeeBankAccountResponse(added.Id, added.UserProfileId, added.IsPrimary);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,60 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.Employment;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmploymentAdd;
using AMREZ.EOP.Domain.Entities.HumanResources;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class AddEmploymentUseCase : IAddEmploymentUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public AddEmploymentUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<EmploymentResponse?> ExecuteAsync(EmploymentAddRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var e = new Employment
{
UserProfileId = request.UserProfileId,
EmploymentType = request.EmploymentType,
StartDate = request.StartDate,
DepartmentId = request.DepartmentId,
PositionId = request.PositionId,
ManagerUserId = request.ManagerUserId,
WorkEmail = request.WorkEmail,
WorkPhone = request.WorkPhone
};
var added = await _hr.AddEmploymentAsync(e, ct);
await _uow.CommitAsync(ct);
return new EmploymentResponse(added.Id, added.UserProfileId, added.StartDate, added.EndDate);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,46 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.EmploymentEnd;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class EndEmploymentUseCase : IEndEmploymentUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public EndEmploymentUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<bool> ExecuteAsync(EmploymentEndRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _hr.EndEmploymentAsync(request.EmploymentId, request.EndDate, ct);
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,46 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.SetPrimaryAddress;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class SetPrimaryAddressUseCase : ISetPrimaryAddressUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public SetPrimaryAddressUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<bool> ExecuteAsync(SetPrimaryAddressRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _hr.SetPrimaryAddressAsync(request.UserProfileId, request.AddressId, ct);
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,46 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.SetPrimaryBankAccount;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class SetPrimaryBankAccountUseCase : ISetPrimaryBankAccountUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public SetPrimaryBankAccountUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<bool> ExecuteAsync(SetPrimaryBankAccountRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _hr.SetPrimaryBankAccountAsync(request.UserProfileId, request.BankAccountId, ct);
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,46 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.SetPrimaryEmergencyContact;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class SetPrimaryEmergencyContactUseCase : ISetPrimaryEmergencyContactUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public SetPrimaryEmergencyContactUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<bool> ExecuteAsync(SetPrimaryEmergencyContactRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _hr.SetPrimaryEmergencyContactAsync(request.UserProfileId, request.ContactId, ct);
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,59 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.UserProfile;
using AMREZ.EOP.Contracts.DTOs.HumanResources.UserProfileUpsert;
using AMREZ.EOP.Domain.Entities.HumanResources;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class UpsertUserProfileUseCase : IUpsertUserProfileUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public UpsertUserProfileUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<UserProfileResponse?> ExecuteAsync(UserProfileUpsertRequest request,
CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var current = await _hr.GetByUserIdAsync(request.UserId, ct);
var entity = current ?? new UserProfile { UserId = request.UserId };
entity.FirstName = request.FirstName;
entity.LastName = request.LastName;
entity.MiddleName = request.MiddleName;
entity.Nickname = request.Nickname;
entity.DateOfBirth = request.DateOfBirth;
entity.Gender = request.Gender;
await _hr.UpsertAsync(entity, ct);
await _uow.CommitAsync(ct);
return new UserProfileResponse(entity.Id, entity.UserId, entity.FirstName, entity.LastName);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,46 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.AddBaseDomain;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class AddBaseDomainUseCase : IAddBaseDomainUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public AddBaseDomainUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<AddBaseDomainResponse?> ExecuteAsync(AddBaseDomainRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
var baseDomain = (request.BaseDomain ?? string.Empty).Trim().TrimEnd('.').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(baseDomain)) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var ok = await _repo.AddBaseDomainAsync(baseDomain, ct); // repo ดึง target tenant จาก X-Tenant/context
if (!ok) { await _uow.RollbackAsync(ct); return null; }
await _uow.CommitAsync(ct);
return new AddBaseDomainResponse(baseDomain);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,77 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.CreateTenant;
using AMREZ.EOP.Domain.Entities.Tenancy;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class CreateTenantUseCase : ICreateTenantUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
private readonly ITenantProvisioner _provisioner;
public CreateTenantUseCase(
ITenantResolver resolver,
IHttpContextAccessor http,
IUnitOfWork uow,
ITenantRepository repo,
ITenantProvisioner provisioner
)
{
_resolver = resolver;
_http = http;
_uow = uow;
_repo = repo;
_provisioner = provisioner;
}
public async Task<CreateTenantResponse?> ExecuteAsync(CreateTenantRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return null;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
var key = request.TenantKey.Trim().ToLowerInvariant();
if (await _repo.TenantExistsAsync(key, ct))
{
await _uow.RollbackAsync(ct);
return null;
}
var row = new TenantConfig
{
TenantKey = key,
Schema = string.IsNullOrWhiteSpace(request.Schema) ? null : request.Schema!.Trim(),
ConnectionString = string.IsNullOrWhiteSpace(request.ConnectionString) ? null : request.ConnectionString!.Trim(),
Mode = request.Mode,
IsActive = request.IsActive,
UpdatedAtUtc = DateTimeOffset.UtcNow
};
var ok = await _repo.CreateAsync(row, ct);
if (!ok) { await _uow.RollbackAsync(ct); return null; }
await _uow.CommitAsync(ct);
await _provisioner.ProvisionAsync(key, ct);
return new CreateTenantResponse(key);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,43 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class DeleteTenantUseCase : IDeleteTenantUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public DeleteTenantUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<bool> ExecuteAsync(string tenantKey, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var key = (tenantKey ?? string.Empty).Trim().ToLowerInvariant();
if (string.IsNullOrWhiteSpace(key)) return false;
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return false;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var ok = await _repo.DeleteAsync(key, ct);
if (!ok) { await _uow.RollbackAsync(ct); return false; }
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,45 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.ListDomains;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class ListDomainsUseCase : IListDomainsUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public ListDomainsUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<ListDomainsResponse?> ExecuteAsync(ListDomainsRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var key = string.IsNullOrWhiteSpace(request?.TenantKey) ? null : request!.TenantKey!.Trim().ToLowerInvariant();
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var items = (await _repo.ListDomainsAsync(key, ct))
.Select(x => new DomainDto(x.Domain, x.TenantKey, x.IsPlatformBaseDomain, x.IsActive, x.UpdatedAtUtc))
.ToList();
await _uow.CommitAsync(ct);
return new ListDomainsResponse(items);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,43 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.ListTenants;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class ListTenantsUseCase : IListTenantsUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public ListTenantsUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<ListTenantsResponse?> ExecuteAsync(ListTenantsRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var items = (await _repo.ListTenantsAsync(ct))
.Select(x => new TenantDto(x.TenantKey, x.Schema, x.ConnectionString, x.Mode, x.IsActive, x.UpdatedAtUtc))
.ToList();
await _uow.CommitAsync(ct);
return new ListTenantsResponse(items);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.MapDomain;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class MapDomainUseCase : IMapDomainUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public MapDomainUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<MapDomainResponse?> ExecuteAsync(MapDomainRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var key = string.IsNullOrWhiteSpace(request?.TenantKey) ? null : request!.TenantKey!.Trim().ToLowerInvariant();
var domain = (request?.Domain ?? string.Empty).Trim().TrimEnd('.').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(domain)) return null;
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var ok = await _repo.MapDomainAsync(domain, key, ct);
if (!ok) { await _uow.RollbackAsync(ct); return null; }
await _uow.CommitAsync(ct);
return new MapDomainResponse(domain, key);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.RemoveBaseDomain;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class RemoveBaseDomainUseCase : IRemoveBaseDomainUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public RemoveBaseDomainUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<RemoveBaseDomainResponse?> ExecuteAsync(RemoveBaseDomainRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var key = string.IsNullOrWhiteSpace(request?.TenantKey) ? null : request!.TenantKey!.Trim().ToLowerInvariant();
var baseDomain = (request?.BaseDomain ?? string.Empty).Trim().TrimEnd('.').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(baseDomain)) return null;
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var ok = await _repo.RemoveBaseDomainAsync(baseDomain, ct);
if (!ok) { await _uow.RollbackAsync(ct); return null; }
await _uow.CommitAsync(ct);
return new RemoveBaseDomainResponse(baseDomain);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.UnmapDomain;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class UnmapDomainUseCase : IUnmapDomainUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public UnmapDomainUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<UnmapDomainResponse?> ExecuteAsync(UnmapDomainRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var key = string.IsNullOrWhiteSpace(request?.TenantKey) ? null : request!.TenantKey!.Trim().ToLowerInvariant();
var domain = (request?.Domain ?? string.Empty).Trim().TrimEnd('.').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(domain)) return null;
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var ok = await _repo.UnmapDomainAsync(domain, ct);
if (!ok) { await _uow.RollbackAsync(ct); return null; }
await _uow.CommitAsync(ct);
return new UnmapDomainResponse(domain);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}

View File

@@ -0,0 +1,58 @@
using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Tenancy.UpdateTenant;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.Tenancy;
public sealed class UpdateTenantUseCase : IUpdateTenantUseCase
{
private readonly ITenantResolver _resolver;
private readonly IHttpContextAccessor _http;
private readonly IUnitOfWork _uow;
private readonly ITenantRepository _repo;
public UpdateTenantUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
public async Task<UpdateTenantResponse?> ExecuteAsync(UpdateTenantRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var key = (request?.TenantKey ?? string.Empty).Trim().ToLowerInvariant();
if (string.IsNullOrWhiteSpace(key)) return null;
var ctx = _resolver.Resolve(http, hint: null);
if (ctx is null) return null;
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
try
{
var current = await _repo.GetAsync(key, ct);
if (current is null) { await _uow.RollbackAsync(ct); return null; } // 404
if (request!.IfUnmodifiedSince.HasValue && current.UpdatedAtUtc > request.IfUnmodifiedSince.Value)
{ await _uow.RollbackAsync(ct); return null; } // 412
current.Schema = request.Schema is null ? current.Schema : (string.IsNullOrWhiteSpace(request.Schema) ? null : request.Schema.Trim());
current.ConnectionString = request.ConnectionString is null ? current.ConnectionString : (string.IsNullOrWhiteSpace(request.ConnectionString) ? null : request.ConnectionString.Trim());
if (request.Mode.HasValue) current.Mode = request.Mode.Value;
if (request.IsActive.HasValue) current.IsActive = request.IsActive.Value;
current.UpdatedAtUtc = DateTimeOffset.UtcNow;
var ok = await _repo.UpdateAsync(current, request.IfUnmodifiedSince, ct);
if (!ok) { await _uow.RollbackAsync(ct); return null; }
await _uow.CommitAsync(ct);
return new UpdateTenantResponse(current.TenantKey, current.UpdatedAtUtc);
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}