feat: add resource quotas, PayPal subscriptions and paid disk allowances
Calculate and enforce configurable resource consumption, expose balances in PostIt and document the rule in CGU. Add confirmed monthly PayPal credits and disk quotas of twice the free default for Discovery and five times for Premium, with payment reconciliation, migrations and regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
0728d2a3f7
commit
650e5f5e30
69 changed files with 19111 additions and 203 deletions
|
|
@ -1,96 +1,29 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Abstract.Resources;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.ApiControllers.Administration;
|
||||
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[ResourceUsageExempt]
|
||||
[Route(Constants.APIPrefix + "/" + Constants.ResourceUsagePath)]
|
||||
public sealed class ResourceUsageApiController : Controller
|
||||
public sealed class ResourceUsageApiController(IResourceUsageService usage) : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
public ResourceUsageApiController(ApplicationDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
private static ResourceUsageSummary ToSummary(ResourceUsageRecord record)
|
||||
=> new()
|
||||
{
|
||||
UserId = record.UserId,
|
||||
UserName = record.UserName,
|
||||
RecordedAtUtc = new DateTimeOffset(record.RecordedAtUtc, TimeSpan.Zero),
|
||||
ApiCalls = record.ApiCalls,
|
||||
CpuSeconds = record.CpuSeconds,
|
||||
BandwidthMb = record.BandwidthMb,
|
||||
StorageMb = record.StorageMb,
|
||||
EstimatedCompensation = record.EstimatedCompensation,
|
||||
Currency = record.Currency
|
||||
};
|
||||
|
||||
[HttpGet(Constants.ResourceUsageCurrentUserRoute)]
|
||||
public async Task<ActionResult<ResourceUsageSummary>> GetCurrentUserUsage()
|
||||
public async Task<ActionResult<ResourceUsageSummary>> GetCurrentUserUsage(CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
var userName = User.Identity?.Name ?? Constants.ResourceUsageCurrentUserDisplayName;
|
||||
var record = await _dbContext.ResourceUsageRecords
|
||||
.Where(r => r.UserId == userId)
|
||||
.OrderByDescending(r => r.RecordedAtUtc)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (record is null)
|
||||
{
|
||||
var zeroUsage = new ResourceUsageRecord
|
||||
{
|
||||
UserId = userId,
|
||||
UserName = userName,
|
||||
RecordedAtUtc = DateTime.UtcNow,
|
||||
ApiCalls = 0m,
|
||||
CpuSeconds = 0m,
|
||||
BandwidthMb = 0m,
|
||||
StorageMb = 0m,
|
||||
EstimatedCompensation = 0m,
|
||||
Currency = Constants.ResourceUsageDefaultCurrency,
|
||||
UserCreated = userId,
|
||||
UserModified = userId,
|
||||
DateCreated = DateTime.UtcNow,
|
||||
DateModified = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
_dbContext.ResourceUsageRecords.Add(zeroUsage);
|
||||
await _dbContext.SaveChangesAsync(userId);
|
||||
return Ok(ToSummary(zeroUsage));
|
||||
}
|
||||
|
||||
return Ok(ToSummary(record));
|
||||
if (string.IsNullOrWhiteSpace(userId)) return Unauthorized();
|
||||
return Ok(await usage.GetCurrentAsync(userId,
|
||||
User.Identity?.Name ?? Constants.ResourceUsageCurrentUserDisplayName, cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet(Constants.ResourceUsageAdminRoute)]
|
||||
public async Task<ActionResult<List<ResourceUsageSummary>>> GetAdminOverview()
|
||||
public async Task<ActionResult<List<ResourceUsageSummary>>> GetAdminOverview(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!User.IsInRole(Constants.AdminName))
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
var latestByUser = (await _dbContext.ResourceUsageRecords
|
||||
.OrderByDescending(r => r.RecordedAtUtc)
|
||||
.ToListAsync())
|
||||
.GroupBy(r => r.UserId)
|
||||
.Select(g => g.First())
|
||||
.OrderByDescending(r => r.RecordedAtUtc)
|
||||
.ToList();
|
||||
|
||||
return Ok(latestByUser.Select(ToSummary).ToList());
|
||||
if (!User.IsInRole(Constants.AdminName)) return Forbid();
|
||||
return Ok(await usage.GetOverviewAsync(cancellationToken));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue