Skip to content

Commit

Permalink
tenantIdentityModule serverExecutionBase
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Apr 5, 2024
1 parent 2d577c9 commit 9ae1248
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Modules.Subscription.Features.Infrastructure.EFCore;
using Modules.Subscriptions.IntegrationEvents;
using Shared.Features.Messaging.Command;
using Shared.Features.Messaging.IntegrationEvent;
using Shared.Features.Server;
using Stripe;

Expand All @@ -17,26 +16,16 @@ public class CreateTrialingSubscription : ICommand
public Stripe.Subscription CreatedStripeSubscription { get; set; }
}

public class CreateTrialingSubscriptionCommandHandler : ServerExecutionBase, ICommandHandler<CreateTrialingSubscription>
public class CreateTrialingSubscriptionCommandHandler : ServerExecutionBase<SubscriptionsModule>, ICommandHandler<CreateTrialingSubscription>
{
private readonly SubscriptionsDbContext subscriptionDbContext;
private readonly SubscriptionsConfiguration subscriptionConfiguration;

public CreateTrialingSubscriptionCommandHandler(
SubscriptionsDbContext subscriptionDbContext,
SubscriptionsConfiguration subscriptionConfiguration,
IServiceProvider serviceProvider): base(serviceProvider)
{
this.subscriptionDbContext = subscriptionDbContext;
this.subscriptionConfiguration = subscriptionConfiguration;
}
public CreateTrialingSubscriptionCommandHandler(IServiceProvider serviceProvider): base(serviceProvider) { }

public async Task HandleAsync(CreateTrialingSubscription command, CancellationToken cancellationToken)
{
var subscriptionType = subscriptionConfiguration.Subscriptions.First(s => s.StripePriceId == command.CreatedStripeSubscription.Items.First().Price.Id).Type;
var subscriptionType = module.SubscriptionsConfiguration.Subscriptions.First(s => s.StripePriceId == command.CreatedStripeSubscription.Items.First().Price.Id).Type;

var customer = await new CustomerService().GetAsync(command.CreatedStripeSubscription.CustomerId);
var stripeCustomer = await subscriptionDbContext.StripeCustomers.FirstAsync(sc => sc.StripePortalCustomerId == customer.Id);
var stripeCustomer = await module.SubscriptionsDbContext.StripeCustomers.FirstAsync(sc => sc.StripePortalCustomerId == customer.Id);

var tenantId = new Guid(command.CreatedStripeSubscription.Metadata["TenantId"]);

Expand All @@ -48,9 +37,9 @@ public async Task HandleAsync(CreateTrialingSubscription command, CancellationTo
tenantId,
stripeCustomer);

subscriptionDbContext.StripeSubscriptions.Add(stripeSubscription);
module.SubscriptionsDbContext.StripeSubscriptions.Add(stripeSubscription);

await subscriptionDbContext.SaveChangesAsync();
await module.SubscriptionsDbContext.SaveChangesAsync();

var userSubscriptionUpdatedEvent = new TenantSubscriptionPlanUpdatedIntegrationEvent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Modules.Subscription.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.Command;
using Shared.Features.Server;

Expand All @@ -10,24 +9,17 @@ public class PauseActiveSubscription : ICommand
public Stripe.Subscription Subscription { get; set; }
}

public class PauseActiveSubscriptionCommandHandler : ServerExecutionBase, ICommandHandler<PauseActiveSubscription>
public class PauseActiveSubscriptionCommandHandler : ServerExecutionBase<SubscriptionsModule>, ICommandHandler<PauseActiveSubscription>
{
private readonly SubscriptionsDbContext subscriptionDbContext;

public PauseActiveSubscriptionCommandHandler(
SubscriptionsDbContext subscriptionDbContext,
IServiceProvider serviceProvider) : base(serviceProvider)
{
this.subscriptionDbContext = subscriptionDbContext;
}
public PauseActiveSubscriptionCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task HandleAsync(PauseActiveSubscription command, CancellationToken cancellationToken)
{
var stripeSubscription = await subscriptionDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);
var stripeSubscription = await module.SubscriptionsDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);

stripeSubscription.Status = StripeSubscriptionStatus.Paused;

await subscriptionDbContext.SaveChangesAsync(cancellationToken);
await module.SubscriptionsDbContext.SaveChangesAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Modules.Subscription.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.Command;
using Shared.Features.Server;

Expand All @@ -10,28 +9,21 @@ public class UpdateSubscriptionPeriod : ICommand
public Stripe.Subscription Subscription { get; set; }
}

public class UpdateSubscriptionPerioEndCommandHandler : ServerExecutionBase, ICommandHandler<UpdateSubscriptionPeriod>
public class UpdateSubscriptionPerioEndCommandHandler : ServerExecutionBase<SubscriptionsModule>, ICommandHandler<UpdateSubscriptionPeriod>
{
private readonly SubscriptionsDbContext subscriptionDbContext;

public UpdateSubscriptionPerioEndCommandHandler(
SubscriptionsDbContext subscriptionDbContext,
IServiceProvider serviceProvider) : base(serviceProvider)
{
this.subscriptionDbContext = subscriptionDbContext;
}
public UpdateSubscriptionPerioEndCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task HandleAsync(UpdateSubscriptionPeriod command, CancellationToken cancellationToken)
{
var stripeSubscription = await subscriptionDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);
var stripeSubscription = await module.SubscriptionsDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);

if (stripeSubscription.Status != StripeSubscriptionStatus.Active)
{
stripeSubscription.Status = StripeSubscriptionStatus.Active;
}
stripeSubscription.ExpirationDate = command.Subscription.CurrentPeriodEnd;

await subscriptionDbContext.SaveChangesAsync(cancellationToken);
await module.SubscriptionsDbContext.SaveChangesAsync(cancellationToken);
}
}
}
12 changes: 11 additions & 1 deletion Source/Modules/Subscriptions/Features/SubscriptionsModule.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
using Shared.Features.Modules;
using Modules.Subscription.Features.Infrastructure.Configuration;
using Modules.Subscription.Features.Infrastructure.EFCore;
using Shared.Features.Modules;
using System.Reflection;

namespace Modules.Subscriptions.Features
{
public class SubscriptionsModule : IModule
{
public Assembly FeaturesAssembly => typeof(SubscriptionsModule).Assembly;
public SubscriptionsConfiguration SubscriptionsConfiguration { get; set; }
public SubscriptionsDbContext SubscriptionsDbContext { get; set; }

public SubscriptionsModule(SubscriptionsConfiguration configuration, SubscriptionsDbContext dbContext)
{
SubscriptionsConfiguration = configuration;
SubscriptionsDbContext = dbContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.Command;
using Shared.Features.Messaging.Command;
using Shared.Features.Server;
using Shared.Kernel.BuildingBlocks.Auth;
using System.Threading;

Expand All @@ -11,21 +11,17 @@ public class AddUserToTenant : ICommand
public Guid UserId { get; set; }
public TenantRole Role { get; set; }
}
public class AddUserToTenantCommandHandler : ICommandHandler<AddUserToTenant>
public class AddUserToTenantCommandHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<AddUserToTenant>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;
public AddUserToTenantCommandHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public AddUserToTenantCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task HandleAsync(AddUserToTenant command, CancellationToken cancellationToken)
{
var tenant = await tenantIdentityDbContext.GetTenantByIdAsync(command.TenantId);
var tenant = await module.TenantIdentityDbContext.GetTenantByIdAsync(command.TenantId);

tenant.AddUser(command.UserId, command.Role);

await tenantIdentityDbContext.SaveChangesAsync(cancellationToken);
await module.TenantIdentityDbContext.SaveChangesAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Modules.TenantIdentity.Features.DomainFeatures.TenantAggregate.Domain;
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Modules.TenantIdentity.Web.Shared.DTOs.Tenant;
using Shared.Features.Messaging.Command;
using Shared.Features.Server;
using System.Threading;

namespace Modules.TenantIdentity.Features.DomainFeatures.TenantAggregate.Application.Commands
Expand All @@ -12,20 +12,16 @@ public class CreateTenantWithAdmin : ICommand<TenantDTO>
public Guid AdminId { get; set; }
}

public class CreateTenantWithAdminCommandHandler : ICommandHandler<CreateTenantWithAdmin, TenantDTO>
public class CreateTenantWithAdminCommandHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<CreateTenantWithAdmin, TenantDTO>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;
public CreateTenantWithAdminCommandHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public CreateTenantWithAdminCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task<TenantDTO> HandleAsync(CreateTenantWithAdmin createTenant, CancellationToken cancellationToken)
{
var tenant = Tenant.CreateTenantWithAdmin(createTenant.Name, Guid.Empty);

tenantIdentityDbContext.Tenants.Add(tenant);
await tenantIdentityDbContext.SaveChangesAsync(cancellationToken);
module.TenantIdentityDbContext.Tenants.Add(tenant);
await module.TenantIdentityDbContext.SaveChangesAsync(cancellationToken);

return tenant.ToDTO();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.Command;
using Shared.Features.Domain.Exceptions;
using System.Threading;
using Shared.Features.Server;

namespace Modules.TenantIdentity.Features.DomainFeatures.TenantAggregate.Application.Commands
{
Expand All @@ -11,27 +11,22 @@ public class DeleteTenant : ICommand
public Guid TenantId { get; set; }
}

public class DeleteTenantCommandHandler : ICommandHandler<DeleteTenant>
public class DeleteTenantCommandHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<DeleteTenant>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;

public DeleteTenantCommandHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public DeleteTenantCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task HandleAsync(DeleteTenant command, CancellationToken cancellationToken)
{
var tenant = await tenantIdentityDbContext.Tenants.SingleAsync(t => t.TenantId == command.TenantId);
var tenant = await module.TenantIdentityDbContext.Tenants.SingleAsync(t => t.TenantId == command.TenantId);
if (tenant == null)
{
throw new NotFoundException();
}

tenant.ThrowIfUserCantDeleteTenant();

tenantIdentityDbContext.Entry(tenant.Id).State = EntityState.Deleted;
await tenantIdentityDbContext.SaveChangesAsync();
module.TenantIdentityDbContext.Entry(tenant.Id).State = EntityState.Deleted;
await module.TenantIdentityDbContext.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.Command;
using Shared.Features.Messaging.Command;
using Shared.Features.Server;
using System.Threading;

namespace Modules.TenantIdentity.Features.DomainFeatures.TenantAggregate.Application.Commands
Expand All @@ -10,23 +10,18 @@ public class RemoveUserFromTenant : ICommand
public Guid TenantId { get; set; }
}

public class RemoveUserFromTenantommandHandler : ICommandHandler<RemoveUserFromTenant>
public class RemoveUserFromTenantommandHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<RemoveUserFromTenant>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;

public RemoveUserFromTenantommandHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public RemoveUserFromTenantommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) {}

public async Task HandleAsync(RemoveUserFromTenant command, CancellationToken cancellationToken)
{
var tenant = await tenantIdentityDbContext.GetTenantExtendedByIdAsync(command.TenantId);
var tenant = await module.TenantIdentityDbContext.GetTenantExtendedByIdAsync(command.TenantId);

tenant.DeleteTenantMembership(command.UserId);

tenantIdentityDbContext.Remove(tenant);
await tenantIdentityDbContext.SaveChangesAsync();
module.TenantIdentityDbContext.Remove(tenant);
await module.TenantIdentityDbContext.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.Command;
using Shared.Features.Messaging.Command;
using Shared.Features.Server;
using Shared.Kernel.BuildingBlocks.Auth;
using System.Threading;

Expand All @@ -11,20 +11,16 @@ public class UpdateTenantMembership : ICommand
public Guid UserId { get; set; }
public TenantRole Role { get; set; }
}
public class UpdateTenantMembershipCommandHandler : ICommandHandler<UpdateTenantMembership>
public class UpdateTenantMembershipCommandHandler : ServerExecutionBase<TenantIdentityModule>, ICommandHandler<UpdateTenantMembership>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;
public UpdateTenantMembershipCommandHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public UpdateTenantMembershipCommandHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }
public async Task HandleAsync(UpdateTenantMembership command, CancellationToken cancellationToken)
{
var tenant = await tenantIdentityDbContext.GetTenantExtendedByIdAsync(command.TenantId);
var tenant = await module.TenantIdentityDbContext.GetTenantExtendedByIdAsync(command.TenantId);

tenant.ChangeRoleOfTenantMember(command.UserId, command.Role);

await tenantIdentityDbContext.SaveChangesAsync();
await module.TenantIdentityDbContext.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using Microsoft.EntityFrameworkCore;
using Modules.Subscriptions.IntegrationEvents;
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Shared.Features.Messaging.IntegrationEvent;
using Shared.Features.Server;
using System.Threading;

namespace Modules.TenantIdentity.Features.DomainFeatures.TenantAggregate.Application.IntegrationEvents
{
public class TenantSubscriptionUpdatedIntegrationEventHandler : IIntegrationEventHandler<TenantSubscriptionPlanUpdatedIntegrationEvent>
public class TenantSubscriptionUpdatedIntegrationEventHandler : ServerExecutionBase<TenantIdentityModule>, IIntegrationEventHandler<TenantSubscriptionPlanUpdatedIntegrationEvent>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;

public TenantSubscriptionUpdatedIntegrationEventHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public TenantSubscriptionUpdatedIntegrationEventHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task HandleAsync(TenantSubscriptionPlanUpdatedIntegrationEvent integrationEvent, CancellationToken cancellation)
{
var tenant = await tenantIdentityDbContext.Tenants.FirstAsync(tenant => tenant.Id == integrationEvent.TenantId);
var tenant = await module.TenantIdentityDbContext.Tenants.FirstAsync(tenant => tenant.Id == integrationEvent.TenantId);
tenant.SubscriptionPlanType = integrationEvent.SubscriptionPlanType;

await tenantIdentityDbContext.SaveChangesAsync();
await module.TenantIdentityDbContext.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Modules.TenantIdentity.Web.Shared.DTOs.Tenant;
using Shared.Features.Messaging.Query;
using Shared.Features.Server;
using System.Threading;

namespace Modules.TenantIdentity.Features.DomainFeatures.TenantAggregate.Application.Queries
Expand All @@ -10,17 +11,13 @@ public class GetAllTenantMembershipsOfUser : IQuery<List<TenantMembershipDTO>>
{
public Guid UserId { get; set; }
}
public class GetAllTenantMembershipsOfUserQueryHandler : IQueryHandler<GetAllTenantMembershipsOfUser, List<TenantMembershipDTO>>
public class GetAllTenantMembershipsOfUserQueryHandler : ServerExecutionBase<TenantIdentityModule>, IQueryHandler<GetAllTenantMembershipsOfUser, List<TenantMembershipDTO>>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;
public GetAllTenantMembershipsOfUserQueryHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public GetAllTenantMembershipsOfUserQueryHandler(IServiceProvider serviceProvider) : base(serviceProvider) { }

public async Task<List<TenantMembershipDTO>> HandleAsync(GetAllTenantMembershipsOfUser query, CancellationToken cancellation)
{
var tenantMemberships = await tenantIdentityDbContext.TenantMeberships.Where(tm => tm.UserId == query.UserId).ToListAsync();
var tenantMemberships = await module.TenantIdentityDbContext.TenantMeberships.Where(tm => tm.UserId == query.UserId).ToListAsync();
return tenantMemberships.Select(tm => tm.ToDTO()).ToList();
}
}
Expand Down
Loading

0 comments on commit 9ae1248

Please sign in to comment.