feat/Update_Controllers_to_Allow_for_Premium_Membership
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user