59 lines
2.1 KiB
C#
59 lines
2.1 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.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;
|
|
}
|
|
}
|
|
} |