Enforced approval rights
This commit is contained in:
@@ -206,6 +206,44 @@ public sealed class GrantsService(IDbContextFactory<ApplicationDbContext> contex
|
||||
return Result.OkIf(await context.SaveChangesAsync(cancellationToken) > 0, CreateError.Create("Failed to create award"));
|
||||
}));
|
||||
|
||||
public async Task<Result> UpdateAwardAsync(UpdateAwardRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||
await Result.Try((Func<Task<Result>>)(async () =>
|
||||
{
|
||||
if (request.Amount <= 0)
|
||||
return Result.Fail(ValidationError.Create("Approved amount must be greater than zero."));
|
||||
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var existingAward = await context.Awards.FirstOrDefaultAsync(a => a.Id == request.AwardId, cancellationToken);
|
||||
if (existingAward is null)
|
||||
return Result.Fail(NotFoundError.Create("Award not found"));
|
||||
|
||||
if (existingAward.Status == ApprovalStatus.Approved)
|
||||
return Result.Fail(ValidationError.Create("Approved awards are immutable and cannot be updated."));
|
||||
|
||||
if (!await context.Organisations.AnyAsync(o => o.Id == request.OrganisationId, cancellationToken))
|
||||
return Result.Fail(NotFoundError.Create("Organisation not found"));
|
||||
|
||||
if (!await context.Programmes.AnyAsync(p => p.Id == request.ProgrammeId, cancellationToken))
|
||||
return Result.Fail(NotFoundError.Create("Programme not found"));
|
||||
|
||||
existingAward.OrganisationId = request.OrganisationId;
|
||||
existingAward.ProgrammeId = request.ProgrammeId;
|
||||
existingAward.Amount = request.Amount;
|
||||
if (request.Status.HasValue)
|
||||
{
|
||||
existingAward.Status = request.Status.Value;
|
||||
if (request.Status.Value == ApprovalStatus.Approved)
|
||||
{
|
||||
existingAward.ApprovedBy = userId;
|
||||
}
|
||||
}
|
||||
existingAward.UpdatedBy = userId;
|
||||
existingAward.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
return Result.OkIf(await context.SaveChangesAsync(cancellationToken) > 0, UpdateError.Create("Failed to update award"));
|
||||
}));
|
||||
|
||||
public async Task<Result<string>> GetContractUrlAsync(string contractIdHash, CancellationToken cancellationToken = default) =>
|
||||
await Result.Try((Func<Task<Result<string>>>)(async () =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user