Implemented remaining business functionality

This commit is contained in:
2026-09-25 19:00:50 +02:00
parent 1efbf9d99f
commit 86e2bad4fe
42 changed files with 1755 additions and 172 deletions
@@ -311,8 +311,8 @@ public sealed class GovernanceCacheService(GovernanceService service, IDistribut
return Result.Ok(cached.FromJson<Tenant[]>());
}));
public async Task<Result<Organisation>> UpdateOrganisationAsync(UpdateOrganisationRequest request, CancellationToken cancellationToken = default) =>
await service.UpdateOrganisationAsync(request, cancellationToken);
public async Task<Result<Organisation>> UpdateOrganisationAsync(UpdateOrganisationRequest request, Guid userId, CancellationToken cancellationToken = default) =>
await service.UpdateOrganisationAsync(request, userId, cancellationToken);
public async Task<Result> UpdatePortfolioAsync(UpdatePortfolioRequest request, Guid userId, CancellationToken cancellationToken = default) =>
await service.UpdatePortfolioAsync(request, userId, cancellationToken);
@@ -10,7 +10,7 @@ using PostFundManagement.Infrastructure.Database;
namespace PostFundManagement.Application.Services.Governance;
public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> contextFactory, HashService hashService,
public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> contextFactory, HashService hashService,
ITenantContextService tenantContext) : IGovernanceService
{
public async Task<Result<Programme[]>> GetProgrammesAsync(string portfolioIdHash, CancellationToken cancellationToken = default) =>
@@ -140,7 +140,7 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
if (await context.Organisations.AnyAsync(o => o.RegistrationNo == registrationNo, cancellationToken))
return Result.Fail(CreateError.Create("Organisation with the same registration number already exists"));
context.Organisations.Add(new Domain.Entities.Organisation
var organisation = context.Organisations.Add(new Domain.Entities.Organisation
{
Name = request.Name.Humanize(LetterCasing.Title).Trim(),
RegistrationNo = registrationNo,
@@ -148,13 +148,18 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
Type = request.Type,
Status = ActivityStatus.Active,
CreatedBy = userId,
CreatedAt = DateTime.UtcNow
CreatedAt = DateTime.UtcNow
});
return Result.OkIf(await context.SaveChangesAsync(cancellationToken) > 0, CreateError.New);
await context.SaveChangesAsync(cancellationToken);
var mapResult = await CreateTenantOrganisationAsync(hashService.HashEncodeLongId(organisation.Entity.Id).Value,
hashService.HashEncodeLongId(tenantContext.TenantId).Value, userId, cancellationToken);
return Result.FailIf(mapResult.IsFailed, CreateError.Create("Failed to create organisation"));
}));
public async Task<Result<Organisation>> UpdateOrganisationAsync(UpdateOrganisationRequest request, CancellationToken cancellationToken = default) =>
public async Task<Result<Organisation>> UpdateOrganisationAsync(UpdateOrganisationRequest request, Guid userId, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result<Organisation>>>)(async () =>
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
@@ -165,9 +170,17 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
.SetProperty(u => u.Name, request.Name.Humanize(LetterCasing.Title).Trim())
.SetProperty(u => u.RegistrationNo, request.RegistrationNo.Clean())
.SetProperty(u => u.Email, request.Email.ToLower(CultureInfo.InvariantCulture).Clean())
.SetProperty(u => u.Type, request.Type), cancellationToken);
.SetProperty(u => u.Type, request.Type)
.SetProperty(u => u.Status, request.Status), cancellationToken);
return Result.OkIf(changedTotal > 0, UpdateError.New);
var tenantOrganisation = await context.TenantOrganisations.AsNoTracking().FirstOrDefaultAsync(f => f.OrganisationId == request.OrganisationId, cancellationToken);
if (tenantOrganisation is null)
return Result.Fail(ValidationError.Create("Failed to find linked tenant organisation"));
var updateResult = await UpdateTenantOrganisationAsync(hashService.HashEncodeLongId(tenantOrganisation.Id).Value, request.Status, userId, cancellationToken);
return Result.OkIf(changedTotal > 0 && updateResult.IsFailed, UpdateError.Create("Failed to update organisation"));
}));
public async Task<Result> CreatePortfolioAsync(CreatePortfolioRequest request, Guid userId, CancellationToken cancellationToken = default) =>
@@ -255,48 +268,171 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
.ToResult(portfolio!.Map());
}));
public Task<Result> CreateTenantAsync(CreateTenantRequest request, Guid userId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public async Task<Result> CreateTenantAsync(CreateTenantRequest request, Guid userId, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result>>)(async () =>
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
public Task<Result<Tenant[]>> GetTenantsAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
var requestedName = request.Name.Humanize(LetterCasing.Title).Trim();
public Task<Result<Tenant>> GetTenantAsync(string tenantIdHash, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
if (await context.Tenants.AnyAsync(t => t.Name == requestedName, cancellationToken))
return Result.Fail(ValidationError.Create("A tenant with an equivalent name exists."));
public Task<Result> UpdateTenantAsync(UpdateTenantRequest request, Guid userId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
context.Tenants.Add(new Domain.Entities.Tenant
{
CreatedAt = DateTime.UtcNow,
CreatedBy = userId,
Name = requestedName,
Status = ActivityStatus.Active
});
public Task<Result> UpdateTenantUserAsync(UpdateTenantUserRequest request, Guid userId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
return Result.OkIf(await context.SaveChangesAsync(cancellationToken) > 0,
CreateError.Create("Failed to create new tenant"));
}));
public Task<Result> CreateTenantOrganisationAsync(string organisationIdHash, string tenantIdHash, Guid userId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public async Task<Result<Tenant[]>> GetTenantsAsync(CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result<Tenant[]>>>)(async () =>
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
public Task<Result> UpdateTenantOrganisationAsync(string tenantOrganisationIdHash, ActivityStatus status, Guid userId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
var tenants = await context.Tenants.AsNoTracking()
.OrderByDescending(o => o.Id)
.ToListAsync(cancellationToken);
public Task<Result<TenantOrganisation[]>> GetTenantOrganisationsAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
return Result.Ok(tenants?.Select(t => t.Map()).ToArray() ?? []);
}));
public Task<Result<TenantOrganisation>> GetTenantOrganisationAsync(string tenantOrganisationIdHash, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public async Task<Result<Tenant>> GetTenantAsync(string tenantIdHash, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result<Tenant>>>)(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 tenant = await context.Tenants.AsNoTracking().FirstOrDefaultAsync(t => t.Id == decodeResult.Value, cancellationToken);
return Result.FailIf(tenant is null, NotFoundError.Create("Failed to retrieve tenants"))
.ToResult(tenant!.Map());
}));
public async Task<Result> UpdateTenantAsync(UpdateTenantRequest request, Guid userId, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result>>)(async () =>
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var requestedName = request.Name.Humanize(LetterCasing.Title).Trim();
if (await context.Tenants.AnyAsync(t => t.Name == requestedName, cancellationToken))
return Result.Fail(ValidationError.Create("A tenant with an equivalent name exists."));
var changedTotal = await context.Tenants
.Where(f => f.Id == request.TenantId)
.ExecuteUpdateAsync(f => f
.SetProperty(u => u.UpdatedAt, DateTime.UtcNow)
.SetProperty(u => u.UpdatedBy, userId)
.SetProperty(u => u.Status, request.Status)
.SetProperty(u => u.Name, requestedName), cancellationToken);
return Result.OkIf(changedTotal > 0, UpdateError.Create("No records updated"));
}));
public async Task<Result> UpdateTenantUserAsync(UpdateTenantUserRequest request, Guid userId, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result>>)(async () =>
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var changedTotal = await context.TenantUsers
.Where(f => f.UserId == request.UserId)
.ExecuteUpdateAsync(f => f.SetProperty(u => u.Status, request.Status), cancellationToken);
return Result.OkIf(changedTotal > 0, UpdateError.Create("No records updated"));
}));
public async Task<Result> CreateTenantOrganisationAsync(string organisationIdHash, string tenantIdHash, Guid userId, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result>>)(async () =>
{
var organisationDecodeResult = hashService.DecodeLongIdHash(organisationIdHash);
if (organisationDecodeResult.IsFailed)
return Result.Fail(IdDecodeError.Create()
.CausedBy(organisationDecodeResult.Errors));
var tenantDecodeResult = hashService.DecodeLongIdHash(tenantIdHash);
if (tenantDecodeResult.IsFailed)
return Result.Fail(IdDecodeError.Create()
.CausedBy(tenantDecodeResult.Errors));
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (await context.TenantOrganisations.AnyAsync(to => to.OrganisationId == organisationDecodeResult.Value && to.TenantId == tenantDecodeResult.Value, cancellationToken))
return Result.Fail(ValidationError.Create("Already exists"));
context.TenantOrganisations.Add(new Domain.Entities.TenantOrganisation
{
CreatedAt = DateTime.UtcNow,
CreatedBy = userId,
OrganisationId = organisationDecodeResult.Value,
TenantId = tenantDecodeResult.ValueOrDefault
});
return Result.OkIf(await context.SaveChangesAsync(cancellationToken) > 0,
CreateError.Create("Failed to create tenant organisation map"));
}));
public async Task<Result> UpdateTenantOrganisationAsync(string tenantOrganisationIdHash, ActivityStatus status, Guid userId, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result>>)(async () =>
{
var decodeResult = hashService.DecodeLongIdHash(tenantOrganisationIdHash);
if (decodeResult.IsFailed)
return Result.Fail(IdDecodeError.Create()
.CausedBy(decodeResult.Errors));
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var changedTotal = await context.TenantOrganisations
.Where(f => f.Id == decodeResult.Value)
.ExecuteUpdateAsync(f => f.SetProperty(u => u.Status, status), cancellationToken);
return Result.OkIf(changedTotal > 0, UpdateError.Create("Failed to update tenant organisation map"));
}));
public async Task<Result<TenantOrganisation[]>> GetTenantOrganisationsAsync(CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result<TenantOrganisation[]>>>)(async () =>
{
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)
.ToListAsync();
return Result.Ok(tenantOrganisations?.Select(i => i.Map()).ToArray() ?? []);
}));
public async Task<Result<TenantOrganisation>> GetTenantOrganisationAsync(string tenantOrganisationIdHash, CancellationToken cancellationToken = default) =>
await Result.Try((Func<Task<Result<TenantOrganisation>>>)(async () =>
{
var decodeResult = hashService.DecodeLongIdHash(tenantOrganisationIdHash);
if (decodeResult.IsFailed)
return Result.Fail(IdDecodeError.Create()
.CausedBy(decodeResult.Errors));
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var tenantOrganisation = await context.TenantOrganisations.AsNoTracking()
.Include(i => i.Organisation)
.Include(i => i.Tenant)
.FirstOrDefaultAsync(f => f.TenantId == tenantContext.TenantId, cancellationToken);
return Result.FailIf(tenantOrganisation is null, NotFoundError.Create("Failed to find tenant organisation map"))
.ToResult(tenantOrganisation!.Map());
}));
}
@@ -3,6 +3,8 @@ using static PostFundManagement.Domain.Extensions.Constants;
namespace PostFundManagement.Application.Services.Governance;
public sealed record CreateTenantRequest([Required][MaxLength(LabelLength)] string Name);
public sealed record UpdateTenantUserRequest([Required] Guid UserId, [Required] ActivityStatus Status);
public sealed record CreateTenantUserRequest([Required] long TenantId, [Required] Guid UserId);
@@ -11,8 +13,6 @@ public sealed record UpdateTenantRequest([Required] long TenantId,
[Required][MaxLength(LabelLength)] string Name,
[Required] ActivityStatus Status);
public sealed record CreateTenantRequest([Required] long TenantId, [Required][MaxLength(LabelLength)] string Name);
public sealed record UpdatePortfolioRequest([Required] long PortfolioId,
[Required][MaxLength(LabelLength)] string Name,
[Required][MaxLength(TextLength)] string Description,
@@ -27,7 +27,8 @@ public sealed record UpdateOrganisationRequest([Required] long OrganisationId,
[Required][MaxLength(LabelLength)] string Name,
[Required][MaxLength(LabelLength)] string RegistrationNo,
[Required][EmailAddress][MaxLength(LabelLength)] string Email,
[Required] OrganisationType Type);
[Required] OrganisationType Type,
[Required] ActivityStatus Status);
public sealed record CreateOrganisationRequest([Required][MaxLength(LabelLength)] string Name,
[Required][MaxLength(LabelLength)] string RegistrationNo,