Files
amrez-nova-eop-services-api/AMREZ.EOP.Infrastructures/Repositories/UserProfileRepository.cs
Thanakarn Klangkasame 92e614674c Init Git
2025-09-30 11:01:02 +07:00

144 lines
5.3 KiB
C#

using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Abstractions.Storage;
using AMREZ.EOP.Domain.Entities.HumanResources;
using AMREZ.EOP.Infrastructures.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
namespace AMREZ.EOP.Infrastructures.Repositories;
public class UserProfileRepository : IUserProfileRepository
{
private readonly IDbScope _scope;
private readonly ITenantResolver _tenantResolver;
private readonly IHttpContextAccessor _http;
public UserProfileRepository(IDbScope scope, ITenantResolver tenantResolver, IHttpContextAccessor http)
{ _scope = scope; _tenantResolver = tenantResolver; _http = http; }
private Guid TenantId()
{
var http = _http.HttpContext;
var tc = http is not null ? _tenantResolver.Resolve(http) : null;
return Guid.TryParse(tc?.Id, out var g) ? g : Guid.Empty;
}
public async Task<UserProfile?> GetByUserIdAsync(Guid userId, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
var tid = TenantId();
return await db.UserProfiles.FirstOrDefaultAsync(p => p.TenantId == tid && p.UserId == userId, ct);
}
public async Task UpsertAsync(UserProfile profile, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
var tid = TenantId();
var existing = await db.UserProfiles.FirstOrDefaultAsync(p => p.TenantId == tid && p.UserId == profile.UserId, ct);
if (existing is null)
{
profile.TenantId = tid;
await db.UserProfiles.AddAsync(profile, ct);
}
else
{
existing.FirstName = profile.FirstName;
existing.LastName = profile.LastName;
existing.MiddleName = profile.MiddleName;
existing.Nickname = profile.Nickname;
existing.DateOfBirth = profile.DateOfBirth;
existing.Gender = profile.Gender;
}
await db.SaveChangesAsync(ct);
}
public async Task<Employment> AddEmploymentAsync(Employment e, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
e.TenantId = TenantId();
await db.Employments.AddAsync(e, ct);
await db.SaveChangesAsync(ct);
return e;
}
public async Task EndEmploymentAsync(Guid employmentId, DateTime endDate, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
var tid = TenantId();
var e = await db.Employments.FirstOrDefaultAsync(x => x.TenantId == tid && x.Id == employmentId, ct);
if (e is null) return;
e.EndDate = endDate;
await db.SaveChangesAsync(ct);
}
public async Task<EmployeeAddress> AddAddressAsync(EmployeeAddress a, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
a.TenantId = TenantId();
await db.EmployeeAddresses.AddAsync(a, ct);
await db.SaveChangesAsync(ct);
return a;
}
public async Task SetPrimaryAddressAsync(Guid userProfileId, Guid addressId, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
var tid = TenantId();
var all = await db.EmployeeAddresses.Where(x => x.TenantId == tid && x.UserProfileId == userProfileId).ToListAsync(ct);
foreach (var x in all) x.IsPrimary = false;
var target = all.FirstOrDefault(x => x.Id == addressId);
if (target is not null) target.IsPrimary = true;
await db.SaveChangesAsync(ct);
}
public async Task<EmergencyContact> AddEmergencyContactAsync(EmergencyContact c, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
c.TenantId = TenantId();
await db.EmergencyContacts.AddAsync(c, ct);
await db.SaveChangesAsync(ct);
return c;
}
public async Task SetPrimaryEmergencyContactAsync(Guid userProfileId, Guid contactId, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
var tid = TenantId();
var all = await db.EmergencyContacts.Where(x => x.TenantId == tid && x.UserProfileId == userProfileId).ToListAsync(ct);
foreach (var x in all) x.IsPrimary = false;
var target = all.FirstOrDefault(x => x.Id == contactId);
if (target is not null) target.IsPrimary = true;
await db.SaveChangesAsync(ct);
}
public async Task<EmployeeBankAccount> AddBankAccountAsync(EmployeeBankAccount b, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
b.TenantId = TenantId();
await db.EmployeeBankAccounts.AddAsync(b, ct);
await db.SaveChangesAsync(ct);
return b;
}
public async Task SetPrimaryBankAccountAsync(Guid userProfileId, Guid bankAccountId, CancellationToken ct = default)
{
var db = _scope.Get<AppDbContext>();
var tid = TenantId();
var all = await db.EmployeeBankAccounts.Where(x => x.TenantId == tid && x.UserProfileId == userProfileId).ToListAsync(ct);
foreach (var x in all) x.IsPrimary = false;
var target = all.FirstOrDefault(x => x.Id == bankAccountId);
if (target is not null) target.IsPrimary = true;
await db.SaveChangesAsync(ct);
}
}