Add Login Module
This commit is contained in:
@@ -2,6 +2,7 @@ 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.Domain.Entities.Tenancy;
|
||||
using AMREZ.EOP.Infrastructures.Data;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -19,22 +20,33 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
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;
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
var tc = _tenantResolver.Resolve(http) ?? throw new InvalidOperationException("No tenant");
|
||||
_scope.EnsureForTenant(tc);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(tc.Id) && Guid.TryParse(tc.Id, out var g) && g != Guid.Empty)
|
||||
return g;
|
||||
|
||||
var key = (http.Items.TryGetValue("TargetTenantKey", out var v) ? v as string : null) ?? tc.TenantKey;
|
||||
if (string.IsNullOrWhiteSpace(key)) throw new InvalidOperationException("Tenant key missing");
|
||||
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var cfg = db.Set<TenantConfig>().AsNoTracking().FirstOrDefault(x => x.TenantKey == key);
|
||||
if (cfg is null) throw new InvalidOperationException($"Tenant '{key}' not found");
|
||||
return cfg.TenantId;
|
||||
}
|
||||
|
||||
public async Task<UserProfile?> GetByUserIdAsync(Guid userId, CancellationToken ct = default)
|
||||
{
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
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 db = _scope.Get<AppDbContext>();
|
||||
|
||||
var existing = await db.UserProfiles.FirstOrDefaultAsync(p => p.TenantId == tid && p.UserId == profile.UserId, ct);
|
||||
if (existing is null)
|
||||
@@ -56,8 +68,9 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task<Employment> AddEmploymentAsync(Employment e, CancellationToken ct = default)
|
||||
{
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
e.TenantId = TenantId();
|
||||
e.TenantId = tid;
|
||||
await db.Employments.AddAsync(e, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return e;
|
||||
@@ -65,8 +78,8 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task EndEmploymentAsync(Guid employmentId, DateTime endDate, CancellationToken ct = default)
|
||||
{
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var e = await db.Employments.FirstOrDefaultAsync(x => x.TenantId == tid && x.Id == employmentId, ct);
|
||||
if (e is null) return;
|
||||
e.EndDate = endDate;
|
||||
@@ -75,8 +88,9 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task<EmployeeAddress> AddAddressAsync(EmployeeAddress a, CancellationToken ct = default)
|
||||
{
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
a.TenantId = TenantId();
|
||||
a.TenantId = tid;
|
||||
await db.EmployeeAddresses.AddAsync(a, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return a;
|
||||
@@ -84,8 +98,8 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task SetPrimaryAddressAsync(Guid userProfileId, Guid addressId, CancellationToken ct = default)
|
||||
{
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
|
||||
var all = await db.EmployeeAddresses.Where(x => x.TenantId == tid && x.UserProfileId == userProfileId).ToListAsync(ct);
|
||||
foreach (var x in all) x.IsPrimary = false;
|
||||
@@ -98,8 +112,9 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task<EmergencyContact> AddEmergencyContactAsync(EmergencyContact c, CancellationToken ct = default)
|
||||
{
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
c.TenantId = TenantId();
|
||||
c.TenantId = tid;
|
||||
await db.EmergencyContacts.AddAsync(c, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return c;
|
||||
@@ -107,8 +122,8 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task SetPrimaryEmergencyContactAsync(Guid userProfileId, Guid contactId, CancellationToken ct = default)
|
||||
{
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
|
||||
var all = await db.EmergencyContacts.Where(x => x.TenantId == tid && x.UserProfileId == userProfileId).ToListAsync(ct);
|
||||
foreach (var x in all) x.IsPrimary = false;
|
||||
@@ -121,8 +136,9 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task<EmployeeBankAccount> AddBankAccountAsync(EmployeeBankAccount b, CancellationToken ct = default)
|
||||
{
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
b.TenantId = TenantId();
|
||||
b.TenantId = tid;
|
||||
await db.EmployeeBankAccounts.AddAsync(b, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return b;
|
||||
@@ -130,8 +146,8 @@ public class UserProfileRepository : IUserProfileRepository
|
||||
|
||||
public async Task SetPrimaryBankAccountAsync(Guid userProfileId, Guid bankAccountId, CancellationToken ct = default)
|
||||
{
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
var tid = TenantId();
|
||||
var db = _scope.Get<AppDbContext>();
|
||||
|
||||
var all = await db.EmployeeBankAccounts.Where(x => x.TenantId == tid && x.UserProfileId == userProfileId).ToListAsync(ct);
|
||||
foreach (var x in all) x.IsPrimary = false;
|
||||
|
||||
Reference in New Issue
Block a user