Skip to content

Commit

Permalink
TenantMembership explicit TenantId property
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Feb 22, 2024
1 parent 7b27863 commit 02cb06e
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 48 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

namespace Modules.Subscriptions.Features.Aggregates.StripeSubscriptionAggregate.Application.Commands
{
public class CreateTrialingSubscription : ICommand
public class CreateTrialingSubscriptionForTenant : ICommand
{
public Guid UserId { get; set; }
public Guid TenantId { get; set; }
public string StripeCustomerId { get; set; }
public Stripe.Subscription Subscription { get; set; }
public Stripe.Subscription CreatedStripeSubscription { get; set; }
}

public class CreateTrialingSubscriptionCommandHandler : ICommandHandler<CreateTrialingSubscription>
public class CreateTrialingSubscriptionCommandHandler : ICommandHandler<CreateTrialingSubscriptionForTenant>
{
private readonly SubscriptionsDbContext subscriptionDbContext;
private readonly SubscriptionsConfiguration subscriptionConfiguration;
Expand All @@ -32,22 +33,24 @@ public CreateTrialingSubscriptionCommandHandler(
this.integrationEventDispatcher = integrationEventDispatcher;
}

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

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

var stripeSubscription = StripeSubscription.Create(command.Subscription.TrialEnd, subscriptionType, StripeSubscriptionStatus.Trialing, stripeCustomer);
var tenantId = new Guid(command.CreatedStripeSubscription.Metadata["TenantId"]);

var stripeSubscription = StripeSubscription.Create(command.CreatedStripeSubscription.TrialEnd, subscriptionType, StripeSubscriptionStatus.Trialing, tenantId, stripeCustomer);

subscriptionDbContext.StripeSubscriptions.Add(stripeSubscription);

await subscriptionDbContext.SaveChangesAsync();

var userSubscriptionUpdatedEvent = new TenantSubscriptionPlanUpdatedIntegrationEvent
{
TenantId = new Guid(command.Subscription.Metadata["TenantId"]),
TenantId = tenantId,
SubscriptionPlanType = subscriptionType
};

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Shared.Features.CQRS.Command;

namespace Modules.Subscriptions.Features.Aggregates.StripeSubscriptionAggregate.Application.Commands
{
public class UpdateSubscriptionPeriodEnd : ICommand
{
public Stripe.Subscription Subscription { get; set; }
}

public class UpdateSubscriptionPerioEndCommandHandler : ICommandHandler<UpdateSubscriptionPeriodEnd>
{
public Task HandleAsync(UpdateSubscriptionPeriodEnd command, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ public static StripeSubscription Create(
DateTime? expirationDate,
SubscriptionPlanType subscriptionPlanType,
StripeSubscriptionStatus stripeSubscriptionStatus,
Guid tenantId,
StripeCustomer stripeCustomer)
{
return new StripeSubscription()
{
ExpirationDate = expirationDate,
PlanType = subscriptionPlanType,
Status = stripeSubscriptionStatus,
TenantId = tenantId,
StripeCustomer = stripeCustomer
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public async Task<IActionResult> Index()
var subscription = await new SubscriptionService().GetAsync(session.SubscriptionId);
var userId = subscription.Metadata["UserId"];

var createTrialingSubscription = new CreateTrialingSubscription
var createTrialingSubscription = new CreateTrialingSubscriptionForTenant
{
UserId = Guid.Parse(userId),
StripeCustomerId = subscription.CustomerId,
Subscription = subscription
CreatedStripeSubscription = subscription
};

await commandDispatcher.DispatchAsync(createTrialingSubscription);
Expand All @@ -55,7 +55,7 @@ public async Task<IActionResult> Index()

var subscription = await new SubscriptionService().GetAsync(invoice.SubscriptionId);


}
// Sent each billing interval if there is an issue with your customer’s payment method. (Stripe)
else if (stripeEvent.Type == Events.InvoicePaymentFailed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task HandleAsync(UpdateTenantMembership command, CancellationToken
{
var tenant = await tenantIdentityDbContext.GetTenantExtendedByIdAsync(command.TenantId);

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

await tenantIdentityDbContext.SaveChangesAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Shared.Features.Domain;
using Shared.Features.Domain.Exceptions;
using Shared.Kernel.BuildingBlocks.Auth;
using StackExchange.Redis;

namespace Modules.TenantIdentity.Features.Aggregates.TenantAggregate.Domain
{
Expand Down Expand Up @@ -50,7 +49,7 @@ public void AddUser(Guid userId, TenantRole role)
}
}

public void ChangeRoleOfMember(Guid userId, TenantRole newRole)
public void ChangeRoleOfTenantMember(Guid userId, TenantRole newRole)
{
ThrowIfCallerIsNotInRole(TenantRole.Admin);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Modules.TenantIdentity.Features.Aggregates.UserAggregate;
using Shared.Features.Domain;
using Shared.Features.Domain;
using Shared.Kernel.BuildingBlocks.Auth;

namespace Modules.TenantIdentity.Features.Aggregates.TenantAggregate.Domain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ public TenantMembership(Guid userId, TenantRole role)
UserId = userId;
Role = role;
}

public Guid UserId { get; set; }
public Guid TenantId { get; set; }
public Tenant Tenant { get; set; }
public TenantRole Role { get; set; }

public TenantMembershipDTO ToDTO()
{
return new TenantMembershipDTO();
return new TenantMembershipDTO()
{
UserId = UserId,
TenantId = Tenant.Id,
Role = Role
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Modules.TenantIdentity.Features.Aggregates.UserAggregate;

namespace Modules.TenantIdentity.Features.Aggregates.UserAggregate.Infrastructure
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shared.Kernel.BuildingBlocks.Auth.Attributes
namespace Shared.Kernel.BuildingBlocks.Auth.Attributes
{
public class AuthorizationAttribute : Attribute
{
Expand Down
2 changes: 0 additions & 2 deletions Source/Web/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Modules.LandingPages.Web.Server;
using Web.Server.BuildingBlocks;
using Shared.Kernel.BuildingBlocks.Auth;
using Microsoft.AspNetCore.Mvc;
using System;
using Modules.TenantIdentity.Server;
using Shared.Features.Modules;
using Shared.Features;
Expand Down

0 comments on commit 02cb06e

Please sign in to comment.