feat/Update_Controllers_to_Allow_for_Premium_Membership

This commit is contained in:
cesnimda
2026-08-03 09:17:28 +02:00
parent de937d25dc
commit c3f4a57195
187 changed files with 26062 additions and 991 deletions
+17 -3
View File
@@ -33,10 +33,14 @@ public sealed class AiUsageController : ControllerBase
var roles = await _users.GetRolesAsync(user);
var entitlements = AccountPlans.ForRoles(roles);
var monthStart = new DateTimeOffset(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0, TimeSpan.Zero);
var interactions = _db.AiInteractions.Where(x => x.OwnerUserId == user.Id);
var currentMonth = _db.Database.IsSqlite()
? Sum((await interactions.ToListAsync(cancellationToken)).Where(x => x.CreatedAtUtc >= monthStart))
: await SumAsync(interactions.Where(x => x.CreatedAtUtc >= monthStart), cancellationToken);
return Ok(new UsageDto(
await SumAsync(_db.AiInteractions.Where(x => x.OwnerUserId == user.Id && x.CreatedAtUtc >= monthStart), cancellationToken),
await SumAsync(_db.AiInteractions.Where(x => x.OwnerUserId == user.Id), cancellationToken),
entitlements.AdvancedAi ? "premium" : "free",
currentMonth,
await SumAsync(interactions, cancellationToken),
AccountPlans.Name(entitlements),
entitlements.MonthlyAiCalls,
entitlements.MonthlyAiTokens,
await _db.Attachments.Where(x => x.JobApplication.OwnerUserId == user.Id).SumAsync(x => (long?)x.FileSize, cancellationToken) ?? 0,
@@ -52,4 +56,14 @@ public sealed class AiUsageController : ControllerBase
group.Sum(x => (long)x.EstimatedTokenCount))).FirstOrDefaultAsync(cancellationToken);
return totals ?? new UsagePeriodDto(0, 0, 0, 0);
}
private static UsagePeriodDto Sum(IEnumerable<AiInteraction> interactions)
{
var rows = interactions.ToList();
return new UsagePeriodDto(
rows.Count,
rows.Sum(x => (long)x.InputCharacterCount),
rows.Sum(x => (long)x.OutputCharacterCount),
rows.Sum(x => (long)x.EstimatedTokenCount));
}
}