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

[AC-1900] Update Vault DB to support provider billing (#3875)

* Add Gateway columns to Provider table

* Add ProviderId column to Transaction table

* Create ProviderPlan table

* Matt's feedback

* Rui's feedback

* Fixed Gateway parameter on Provider
This commit is contained in:
Alex Morask
2024-03-21 11:15:49 -04:00
committed by GitHub
parent 43ee5a24ec
commit 9f7e05869e
44 changed files with 9139 additions and 45 deletions

View File

@ -0,0 +1,21 @@
using Bit.Infrastructure.EntityFramework.Billing.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Bit.Infrastructure.EntityFramework.Billing.Configurations;
public class ProviderPlanEntityTypeConfiguration : IEntityTypeConfiguration<ProviderPlan>
{
public void Configure(EntityTypeBuilder<ProviderPlan> builder)
{
builder
.Property(t => t.Id)
.ValueGeneratedNever();
builder
.HasIndex(providerPlan => new { providerPlan.Id, providerPlan.PlanType })
.IsUnique();
builder.ToTable(nameof(ProviderPlan));
}
}

View File

@ -0,0 +1,17 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
namespace Bit.Infrastructure.EntityFramework.Billing.Models;
public class ProviderPlan : Core.Billing.Entities.ProviderPlan
{
public virtual Provider Provider { get; set; }
}
public class ProviderPlanMapperProfile : Profile
{
public ProviderPlanMapperProfile()
{
CreateMap<Core.Billing.Entities.ProviderPlan, ProviderPlan>().ReverseMap();
}
}

View File

@ -0,0 +1,29 @@
using AutoMapper;
using Bit.Core.Billing.Entities;
using Bit.Core.Billing.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using EFProviderPlan = Bit.Infrastructure.EntityFramework.Billing.Models.ProviderPlan;
namespace Bit.Infrastructure.EntityFramework.Billing.Repositories;
public class ProviderPlanRepository(
IMapper mapper,
IServiceScopeFactory serviceScopeFactory)
: Repository<ProviderPlan, EFProviderPlan, Guid>(
serviceScopeFactory,
mapper,
context => context.ProviderPlans), IProviderPlanRepository
{
public async Task<ProviderPlan> GetByProviderId(Guid providerId)
{
using var serviceScope = ServiceScopeFactory.CreateScope();
var databaseContext = GetDatabaseContext(serviceScope);
var query =
from providerPlan in databaseContext.ProviderPlans
where providerPlan.ProviderId == providerId
select providerPlan;
return await query.FirstOrDefaultAsync();
}
}

View File

@ -1,5 +1,6 @@
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Auth.Repositories;
using Bit.Core.Billing.Repositories;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.SecretsManager.Repositories;
@ -7,6 +8,7 @@ using Bit.Core.Tools.Repositories;
using Bit.Core.Vault.Repositories;
using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
using Bit.Infrastructure.EntityFramework.Auth.Repositories;
using Bit.Infrastructure.EntityFramework.Billing.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.SecretsManager.Repositories;
using Bit.Infrastructure.EntityFramework.Tools.Repositories;
@ -85,6 +87,7 @@ public static class EntityFrameworkServiceCollectionExtensions
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<IOrganizationDomainRepository, OrganizationDomainRepository>();
services.AddSingleton<IWebAuthnCredentialRepository, WebAuthnCredentialRepository>();
services.AddSingleton<IProviderPlanRepository, ProviderPlanRepository>();
if (selfHosted)
{

View File

@ -1,5 +1,6 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
namespace Bit.Infrastructure.EntityFramework.Models;
@ -7,6 +8,7 @@ public class Transaction : Core.Entities.Transaction
{
public virtual Organization Organization { get; set; }
public virtual User User { get; set; }
public virtual Provider Provider { get; set; }
}
public class TransactionMapperProfile : Profile

View File

@ -2,6 +2,7 @@
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
using Bit.Infrastructure.EntityFramework.Auth.Models;
using Bit.Infrastructure.EntityFramework.Billing.Models;
using Bit.Infrastructure.EntityFramework.Converters;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.SecretsManager.Models;
@ -65,6 +66,7 @@ public class DatabaseContext : DbContext
public DbSet<AuthRequest> AuthRequests { get; set; }
public DbSet<OrganizationDomain> OrganizationDomains { get; set; }
public DbSet<WebAuthnCredential> WebAuthnCredentials { get; set; }
public DbSet<ProviderPlan> ProviderPlans { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{

View File

@ -47,4 +47,14 @@ public class TransactionRepository : Repository<Core.Entities.Transaction, Trans
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
}
public async Task<ICollection<Core.Entities.Transaction>> GetManyByProviderIdAsync(Guid providerId)
{
using var serviceScope = ServiceScopeFactory.CreateScope();
var databaseContext = GetDatabaseContext(serviceScope);
var results = await databaseContext.Transactions
.Where(transaction => transaction.ProviderId == providerId)
.ToListAsync();
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
}