Merge pull request 'Added budget update' (#100) from finishline into main
Reviewed-on: #100
This commit was merged in pull request #100.
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Governance;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetPorfolioValueEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/governance/portfolios/{portfolioId}/value", async (string portfolioId, IGovernanceService service,
|
||||||
|
CancellationToken cancellationToken = default) =>
|
||||||
|
await service.GetPorfolioValueAsync(portfolioId, cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true, Value: var amount } => Results.Ok(amount),
|
||||||
|
var r when r.HasError<IdDecodeError>() => Results.Forbid(),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Calculates and returns a portfolio's total project's budget value")
|
||||||
|
.RequireAuthorization(Constants.MainScope)
|
||||||
|
.WithName(typeof(GetPorfolioValueEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<decimal>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status400BadRequest)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Governance)
|
||||||
|
.CacheOutput();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Governance;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetProgrammeValueEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/governance/programmes/{programmeId}/value", async (string programmeId, IGovernanceService service,
|
||||||
|
CancellationToken cancellationToken = default) =>
|
||||||
|
await service.GetProgrammeValueAsync(programmeId, cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true, Value: var amount } => Results.Ok(amount),
|
||||||
|
var r when r.HasError<IdDecodeError>() => Results.Forbid(),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Calculates and returns a programme's total project budget value")
|
||||||
|
.RequireAuthorization(Constants.MainScope)
|
||||||
|
.WithName(typeof(GetProgrammeValueEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<decimal>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status400BadRequest)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Governance)
|
||||||
|
.CacheOutput();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Grants;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Grants;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class UpdateBudgetEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapPut("api/grants/budgets", async (UpdateBudgetRequest request, HttpContext context,
|
||||||
|
IGrantsService service, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.UpdateBudgetAsync(request, context.GetUserId(), cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true } => Results.Ok(),
|
||||||
|
var r when r.HasError<UpdateError>() => Results.BadRequest(r.Errors.ToProblems()),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Updates a specific budget")
|
||||||
|
.RequireAuthorization(Constants.MainScope)
|
||||||
|
.WithName(typeof(UpdateBudgetEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status400BadRequest)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Governance)
|
||||||
|
.CacheOutput();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,12 @@ namespace PostFundManagement.Application.Abstractions.Services;
|
|||||||
|
|
||||||
public interface IGovernanceService
|
public interface IGovernanceService
|
||||||
{
|
{
|
||||||
|
[Log]
|
||||||
|
Task<Result<decimal>> GetProgrammeValueAsync(string programmeIdHash, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
[Log]
|
||||||
|
Task<Result<decimal>> GetPorfolioValueAsync(string portfolioIdHash, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
[Log]
|
[Log]
|
||||||
[Audit]
|
[Audit]
|
||||||
Task<Result> UpdateInstrumentAsync(UpdateInstrumentRequest request, Guid userId, CancellationToken cancellationToken = default);
|
Task<Result> UpdateInstrumentAsync(UpdateInstrumentRequest request, Guid userId, CancellationToken cancellationToken = default);
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ namespace PostFundManagement.Application.Abstractions.Services;
|
|||||||
|
|
||||||
public interface IGrantsService
|
public interface IGrantsService
|
||||||
{
|
{
|
||||||
|
[Log]
|
||||||
|
[Audit]
|
||||||
|
Task<Result> UpdateBudgetAsync(UpdateBudgetRequest request, Guid userId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
[Log]
|
[Log]
|
||||||
Task<Result<Contract[]>> GetContractsAsync(Pagination pagination, CancellationToken cancellationToken = default);
|
Task<Result<Contract[]>> GetContractsAsync(Pagination pagination, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
|||||||
@@ -355,4 +355,56 @@ public sealed class GovernanceCacheService(GovernanceService service, IDistribut
|
|||||||
|
|
||||||
return Result.Ok(cached.FromJson<Portfolio[]>());
|
return Result.Ok(cached.FromJson<Portfolio[]>());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
public async Task<Result<decimal>> GetProgrammeValueAsync(string programmeIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<Task<Result<decimal>>>)(async () =>
|
||||||
|
{
|
||||||
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
||||||
|
return await service.GetProgrammeValueAsync(programmeIdHash, cancellationToken);
|
||||||
|
|
||||||
|
var cacheKey = HashService.ToMd5Hash($"getprogrammevalue-{programmeIdHash}").Value;
|
||||||
|
|
||||||
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(cached))
|
||||||
|
{
|
||||||
|
var result = await service.GetProgrammeValueAsync(programmeIdHash, cancellationToken);
|
||||||
|
|
||||||
|
if (result.IsFailed) return result;
|
||||||
|
|
||||||
|
await cache.SetStringAsync(cacheKey, result.Value.ToString(), cacheOptions, cancellationToken);
|
||||||
|
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
decimal.TryParse(cached, CultureInfo.InvariantCulture, out var value);
|
||||||
|
|
||||||
|
return Result.Ok(value);
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async Task<Result<decimal>> GetPorfolioValueAsync(string portfolioIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<Task<Result<decimal>>>)(async () =>
|
||||||
|
{
|
||||||
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
||||||
|
return await service.GetPorfolioValueAsync(portfolioIdHash, cancellationToken);
|
||||||
|
|
||||||
|
var cacheKey = HashService.ToMd5Hash($"getportfoliovalue-{portfolioIdHash}").Value;
|
||||||
|
|
||||||
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(cached))
|
||||||
|
{
|
||||||
|
var result = await service.GetPorfolioValueAsync(portfolioIdHash, cancellationToken);
|
||||||
|
|
||||||
|
if (result.IsFailed) return result;
|
||||||
|
|
||||||
|
await cache.SetStringAsync(cacheKey, result.Value.ToString(), cacheOptions, cancellationToken);
|
||||||
|
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
decimal.TryParse(cached, CultureInfo.InvariantCulture, out var value);
|
||||||
|
|
||||||
|
return Result.Ok(value);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -485,4 +485,46 @@ public sealed class GovernanceService(IDbContextFactory<ApplicationDbContext> co
|
|||||||
|
|
||||||
return Result.Ok(portfolios.Select(p => p.Map()).ToArray());
|
return Result.Ok(portfolios.Select(p => p.Map()).ToArray());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
public async Task<Result<decimal>> GetProgrammeValueAsync(string programmeIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<Task<Result<decimal>>>)(async () =>
|
||||||
|
{
|
||||||
|
var decodeResult = hashService.DecodeLongIdHash(programmeIdHash);
|
||||||
|
|
||||||
|
if (decodeResult.IsFailed)
|
||||||
|
return Result.Fail(IdDecodeError.Create()
|
||||||
|
.CausedBy(decodeResult.Errors));
|
||||||
|
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var value = await context.Budgets.AsNoTracking()
|
||||||
|
.Include(i => i.Project!.Programme)
|
||||||
|
.Where(v => v.Project!.Programme!.Id == decodeResult.Value &&
|
||||||
|
v.Project.Programme.Status == ApprovalStatus.Approved &&
|
||||||
|
v.Status == ApprovalStatus.Approved)
|
||||||
|
.SumAsync(v => v.Amount, cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok(value);
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async Task<Result<decimal>> GetPorfolioValueAsync(string portfolioIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<Task<Result<decimal>>>)(async () =>
|
||||||
|
{
|
||||||
|
var decodeResult = hashService.DecodeLongIdHash(portfolioIdHash);
|
||||||
|
|
||||||
|
if (decodeResult.IsFailed)
|
||||||
|
return Result.Fail(IdDecodeError.Create()
|
||||||
|
.CausedBy(decodeResult.Errors));
|
||||||
|
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var value = await context.Budgets
|
||||||
|
.Include(i => i.Project!.Programme!.Portfolio)
|
||||||
|
.Where(v => v.Project!.Programme!.Portfolio!.Id == decodeResult.Value &&
|
||||||
|
v.Project.Programme.Status == ApprovalStatus.Approved &&
|
||||||
|
v.Status == ApprovalStatus.Approved)
|
||||||
|
.SumAsync(v => v.Amount, cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok(value);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,6 +191,9 @@ public sealed class GrantsCacheService(GrantsService service, IDistributedCache
|
|||||||
return Result.Ok(cached!);
|
return Result.Ok(cached!);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
public async Task<Result> UpdateBudgetAsync(UpdateBudgetRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.UpdateBudgetAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
public async Task<Result> UpdateContractAsync(UpdateContractRequest request, CancellationToken cancellationToken = default) =>
|
public async Task<Result> UpdateContractAsync(UpdateContractRequest request, CancellationToken cancellationToken = default) =>
|
||||||
await service.UpdateContractAsync(request, cancellationToken);
|
await service.UpdateContractAsync(request, cancellationToken);
|
||||||
|
|
||||||
|
|||||||
@@ -167,7 +167,8 @@ public sealed class GrantsService(IDbContextFactory<ApplicationDbContext> contex
|
|||||||
Name = request.Name,
|
Name = request.Name,
|
||||||
Amount = request.Amount,
|
Amount = request.Amount,
|
||||||
CreatedBy = userId,
|
CreatedBy = userId,
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
Status = ApprovalStatus.Pending
|
||||||
};
|
};
|
||||||
|
|
||||||
context.Budgets.Add(budget);
|
context.Budgets.Add(budget);
|
||||||
@@ -273,4 +274,19 @@ public sealed class GrantsService(IDbContextFactory<ApplicationDbContext> contex
|
|||||||
|
|
||||||
return Result.OkIf(changedTotal > 0, UpdateError.Create("Failed to update the contract."));
|
return Result.OkIf(changedTotal > 0, UpdateError.Create("Failed to update the contract."));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
public async Task<Result> UpdateBudgetAsync(UpdateBudgetRequest 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.Budgets
|
||||||
|
.Where(f => f.Id == request.BudgetId)
|
||||||
|
.ExecuteUpdateAsync(f => f
|
||||||
|
.SetProperty(u => u.ApprovedBy, userId)
|
||||||
|
.SetProperty(u => u.UpdatedAt, DateTime.UtcNow)
|
||||||
|
.SetProperty(u => u.Status, request.Status), cancellationToken);
|
||||||
|
|
||||||
|
return Result.OkIf(changedTotal > 0, UpdateError.Create("Failed to update budget"));
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ using static PostFundManagement.Domain.Extensions.Constants;
|
|||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Grants;
|
namespace PostFundManagement.Application.Services.Grants;
|
||||||
|
|
||||||
|
public sealed record UpdateBudgetRequest([Required] long BudgetId, [Required] ApprovalStatus Status);
|
||||||
|
|
||||||
public sealed record UpdateContractRequest([Required] long ContractId, [Required] ContractStatus Status);
|
public sealed record UpdateContractRequest([Required] long ContractId, [Required] ContractStatus Status);
|
||||||
|
|
||||||
public sealed record CreateAwardRequest([Required] long OrganisationId, [Required] long ProgrammeId, [Required] decimal Amount);
|
public sealed record CreateAwardRequest([Required] long OrganisationId, [Required] long ProgrammeId, [Required] decimal Amount);
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ public sealed class BudgetConfiguration : IEntityTypeConfiguration<Budget>
|
|||||||
builder.Property(f => f.UpdatedAt).IsRequired(false);
|
builder.Property(f => f.UpdatedAt).IsRequired(false);
|
||||||
builder.Property(f => f.Name).IsRequired().HasMaxLength(LabelLength);
|
builder.Property(f => f.Name).IsRequired().HasMaxLength(LabelLength);
|
||||||
builder.Property(f => f.Amount).IsRequired().HasPrecision(18, 2);
|
builder.Property(f => f.Amount).IsRequired().HasPrecision(18, 2);
|
||||||
|
builder.Property(f => f.Status).IsRequired().HasConversion<int>().HasDefaultValueSql("1")
|
||||||
|
.HasSentinel(ApprovalStatus.Pending);
|
||||||
|
builder.Property(f => f.Amount).IsRequired().HasPrecision(18, 2);
|
||||||
|
|
||||||
builder.HasOne(f => f.Tenant)
|
builder.HasOne(f => f.Tenant)
|
||||||
.WithMany()
|
.WithMany()
|
||||||
|
|||||||
@@ -107,7 +107,8 @@ public static class Mappers
|
|||||||
CreatedBy = entity.CreatedBy,
|
CreatedBy = entity.CreatedBy,
|
||||||
Name = entity.Name,
|
Name = entity.Name,
|
||||||
ProjectId = entity.ProjectId,
|
ProjectId = entity.ProjectId,
|
||||||
UpdatedAt = entity.UpdatedAt
|
UpdatedAt = entity.UpdatedAt,
|
||||||
|
Status = entity.Status
|
||||||
};
|
};
|
||||||
|
|
||||||
public static ChangeRequest Map(this Entities.ChangeRequest entity) => new()
|
public static ChangeRequest Map(this Entities.ChangeRequest entity) => new()
|
||||||
|
|||||||
@@ -19,4 +19,6 @@ public class Budget
|
|||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
|
|
||||||
public decimal Amount { get; set; }
|
public decimal Amount { get; set; }
|
||||||
|
|
||||||
|
public ApprovalStatus Status { get; set; }
|
||||||
}
|
}
|
||||||
PostFundManagement.Infrastructure/Database/Migrations/20260926124241_AddedstatusToBudget.Designer.cs
Generated
+2454
File diff suppressed because it is too large
Load Diff
+29
@@ -0,0 +1,29 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace PostFundManagement.Infrastructure.Database.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddedstatusToBudget : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<int>(
|
||||||
|
name: "Status",
|
||||||
|
table: "Budgets",
|
||||||
|
type: "integer",
|
||||||
|
nullable: false,
|
||||||
|
defaultValueSql: "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Status",
|
||||||
|
table: "Budgets");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -295,6 +295,11 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
|
|||||||
b.Property<long>("ProjectId")
|
b.Property<long>("ProjectId")
|
||||||
.HasColumnType("bigint");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<int>("Status")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasDefaultValueSql("1");
|
||||||
|
|
||||||
b.Property<long>("TenantId")
|
b.Property<long>("TenantId")
|
||||||
.HasColumnType("bigint");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user