65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
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;
|
|
}
|
|
}
|
|
} |