Refactored organisation entities and endpoints

This commit is contained in:
2026-10-04 10:11:28 +02:00
parent 8fed46961a
commit 5fb67df9ac
17 changed files with 2664 additions and 20 deletions
@@ -138,8 +138,14 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
var registrationNo = request.RegistrationNo.Clean();
if(!await context.Users.AnyAsync(u => u.Id == request.OrganisationUser, cancellationToken))
return Result.Fail(NotFoundError.Create("Organisation user not found"));
if(!await context.TenantUsers.AnyAsync(u => u.UserId == request.OrganisationUser && u.TenantId == tenantContext.TenantId, cancellationToken))
return Result.Fail(ValidationError.Create("Organisation user does not belong to the current tenant"));
if (await context.Organisations.AnyAsync(o => o.RegistrationNo == registrationNo, cancellationToken))
return Result.Fail(CreateError.Create("Organisation with the same registration number already exists"));
return Result.Fail(ValidationError.Create("Organisation with the same registration number already exists"));
var organisation = context.Organisations.Add(new Domain.Entities.Organisation
{
@@ -149,9 +155,17 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
Type = request.Type,
Status = ActivityStatus.Active,
CreatedBy = userId,
CreatedAt = DateTime.UtcNow
CreatedAt = DateTime.UtcNow,
UserId = request.OrganisationUser
});
if(!string.IsNullOrWhiteSpace(request.IdNumber))
{
var linkedUser = await context.Users.FirstOrDefaultAsync(u => u.Id == request.OrganisationUser, cancellationToken);
linkedUser!.IdNumber = request.IdNumber.Clean();
}
await context.SaveChangesAsync(cancellationToken);
var mapResult = await CreateTenantOrganisationAsync(hashService.HashEncodeLongId(organisation.Entity.Id).Value,
@@ -165,6 +179,12 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.Users.AnyAsync(u => u.Id == request.OrganisationUser, cancellationToken))
return Result.Fail(ValidationError.Create("Organisation user not found"));
if (!await context.TenantUsers.AnyAsync(u => u.UserId == request.OrganisationUser && u.TenantId == tenantContext.TenantId, cancellationToken))
return Result.Fail(ValidationError.Create("Organisation user does not belong to the current tenant"));
var changedTotal = await context.Organisations
.Where(f => f.Id == request.OrganisationId)
.ExecuteUpdateAsync(f => f
@@ -181,7 +201,16 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
var updateResult = await UpdateTenantOrganisationAsync(hashService.HashEncodeLongId(tenantOrganisation.Id).Value, request.Status, userId, cancellationToken);
return Result.OkIf(changedTotal > 0 && updateResult.IsSuccess, UpdateError.Create("Failed to update organisation"));
if (!string.IsNullOrWhiteSpace(request.IdNumber))
{
var linkedUser = await context.Users.FirstOrDefaultAsync(u => u.Id == request.OrganisationUser, cancellationToken);
linkedUser!.IdNumber = request.IdNumber.Clean();
}
var userUpdated = await context.SaveChangesAsync(cancellationToken) > 0;
return Result.OkIf(changedTotal > 0 && updateResult.IsSuccess && userUpdated, UpdateError.Create("Failed to update organisation"));
}));
public async Task<Result> CreatePortfolioAsync(CreatePortfolioRequest request, Guid userId, CancellationToken cancellationToken = default) =>
@@ -403,15 +432,21 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
return Result.OkIf(changedTotal > 0, UpdateError.Create("Failed to update tenant organisation map"));
}));
public async Task<Result<TenantOrganisation[]>> GetTenantOrganisationsAsync(CancellationToken cancellationToken = default) =>
public async Task<Result<TenantOrganisation[]>> GetTenantOrganisationsAsync(string tenantIdHash, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result<TenantOrganisation[]>>>)(async () =>
{
var decodeResult = hashService.DecodeLongIdHash(tenantIdHash);
if (decodeResult.IsFailed)
return Result.Fail(IdDecodeError.Create()
.CausedBy(decodeResult.Errors));
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var tenantOrganisations = await context.TenantOrganisations.AsNoTracking()
.Include(i => i.Organisation)
.Include(i => i.Tenant)
.Where(f => f.TenantId == tenantContext.TenantId)
.Where(f => f.TenantId == decodeResult.Value)
.ToListAsync();
return Result.Ok(tenantOrganisations?.Select(i => i.Map()).ToArray() ?? []);