1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 16:42:50 -05:00

[AC-2567] Billing Performance Improvements (#4143)

* Moved AccountsBilling controller to be owned by Billing

* Added org billing history endpoint

* Updated GetBillingInvoicesAsync to only retrieve paid, open, and uncollectible invoices, and added option to limit results

* Removed invoices and transactions from GetBillingAsync

* Limiting the number of invoices and transactions returned

* Moved Billing models to Billing namespace

* Split billing info and billing history objects

* Removed billing method GetBillingBalanceAndSourceAsync

* Removed unused using

* Cleaned up BillingInfo a bit

* Update migration scripts to use `CREATE OR ALTER` instead of checking for the `OBJECT_ID`

* Applying limit to aggregated invoices after they return from Stripe
This commit is contained in:
Conner Turnbull
2024-06-11 13:55:23 -04:00
committed by GitHub
parent f615858724
commit fc1c488a78
30 changed files with 474 additions and 341 deletions

View File

@ -2,6 +2,7 @@
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Infrastructure.EntityFramework.Models;
using LinqToDB;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@ -15,46 +16,60 @@ public class TransactionRepository : Repository<Core.Entities.Transaction, Trans
public async Task<Core.Entities.Transaction> GetByGatewayIdAsync(GatewayType gatewayType, string gatewayId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Transactions
.FirstOrDefaultAsync(t => (t.GatewayId == gatewayId && t.Gateway == gatewayType));
return Mapper.Map<Core.Entities.Transaction>(results);
}
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var results = await EntityFrameworkQueryableExtensions.FirstOrDefaultAsync(dbContext.Transactions, t => (t.GatewayId == gatewayId && t.Gateway == gatewayType));
return Mapper.Map<Core.Entities.Transaction>(results);
}
public async Task<ICollection<Core.Entities.Transaction>> GetManyByOrganizationIdAsync(Guid organizationId)
public async Task<ICollection<Core.Entities.Transaction>> GetManyByOrganizationIdAsync(Guid organizationId, int? limit = null)
{
using (var scope = ServiceScopeFactory.CreateScope())
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Transactions
.Where(t => t.OrganizationId == organizationId && !t.UserId.HasValue);
if (limit.HasValue)
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Transactions
.Where(t => (t.OrganizationId == organizationId && !t.UserId.HasValue))
.ToListAsync();
return Mapper.Map<List<Core.Entities.Transaction>>(results);
query = query.OrderByDescending(o => o.CreationDate).Take(limit.Value);
}
var results = await EntityFrameworkQueryableExtensions.ToListAsync(query);
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
public async Task<ICollection<Core.Entities.Transaction>> GetManyByUserIdAsync(Guid userId)
public async Task<ICollection<Core.Entities.Transaction>> GetManyByUserIdAsync(Guid userId, int? limit = null)
{
using (var scope = ServiceScopeFactory.CreateScope())
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Transactions
.Where(t => t.UserId == userId);
if (limit.HasValue)
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Transactions
.Where(t => (t.UserId == userId))
.ToListAsync();
return Mapper.Map<List<Core.Entities.Transaction>>(results);
query = query.OrderByDescending(o => o.CreationDate).Take(limit.Value);
}
var results = await EntityFrameworkQueryableExtensions.ToListAsync(query);
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
public async Task<ICollection<Core.Entities.Transaction>> GetManyByProviderIdAsync(Guid providerId)
public async Task<ICollection<Core.Entities.Transaction>> GetManyByProviderIdAsync(Guid providerId, int? limit = null)
{
using var serviceScope = ServiceScopeFactory.CreateScope();
var databaseContext = GetDatabaseContext(serviceScope);
var results = await databaseContext.Transactions
.Where(transaction => transaction.ProviderId == providerId)
.ToListAsync();
var query = databaseContext.Transactions
.Where(transaction => transaction.ProviderId == providerId);
if (limit.HasValue)
{
query = query.Take(limit.Value);
}
var results = await EntityFrameworkQueryableExtensions.ToListAsync(query);
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
}